import { datetime , DateUnits } from '../../../libs/datetime' ; export class CalendarTableCell { readonly label : string | null = null ; readonly date : Date | null = null ; constructor ( date ? ) { if ( date ) { this . date = datetime . copy ( date ) ; this . label = datetime . shortFormat ( this . date ) ; } } isBlank ( ) { return this . date === null ; } isToday ( ) { if ( this . isBlank ( ) ) { return false ; } return datetime . isSameDay ( this . date , datetime . today ( ) ) ; } } export class CalendarTableRow { cells : CalendarTableCell [ ] = [ ] ; constructor ( index , beforeBlankCellCount , afterBlankCellCount ) { this . addBlankCell ( beforeBlankCellCount ) ; for ( let i = 0 ; i < 7 - ( beforeBlankCellCount + afterBlankCellCount ) ; i ++ ) { this . cells . push ( new CalendarTableCell ( index ) ) ; datetime . add ( index , DateUnits . DAY , 1 ) ; } this . addBlankCell ( afterBlankCellCount ) ; } private addBlankCell ( count ) { for ( let i = 0 ; i < count ; i ++ ) { this . cells . push ( new CalendarTableCell ( ) ) ; } } } export class CalendarTable { rows : CalendarTableRow [ ] = [ ] ; private _year : number ; private _month : number ; get currentMonth ( ) : Date { return new Date ( this . _year , this . _month ) ; } render ( year , month ) { this . rows = [ ] ; this . _year = year ; this . _month = month ; const firstDate = datetime . getFirstDateOfMonth ( year , month ) ; const dayOfFirstDate = firstDate . getDay ( ) ; const maxDays = datetime . getLastDateOfMonth ( year , month ) . getDate ( ) ; const firstRowCellsCount = 7 - dayOfFirstDate ; const middleRowsCount = Math . floor ( ( maxDays - firstRowCellsCount ) / 7 ) ; const lastRowCellsCount = maxDays - ( firstRowCellsCount + ( middleRowsCount * 7 ) ) ; const indexDate = firstDate ; let weekRow ; weekRow = new CalendarTableRow ( indexDate , dayOfFirstDate , 0 ) ; this . rows . push ( weekRow ) ; for ( let i = 0 ; i < middleRowsCount ; i += 1 ) { weekRow = new CalendarTableRow ( indexDate , 0 , 0 ) ; this . rows . push ( weekRow ) ; } if ( lastRowCellsCount > 0 ) { weekRow = new CalendarTableRow ( indexDate , 0 , 7 - lastRowCellsCount ) ; this . rows . push ( weekRow ) ; } } renderThisMonth ( ) { const now = new Date ( ) ; this . render ( now . getFullYear ( ) , now . getMonth ( ) ) ; } }