/* * @Author: 疯狂秀才(Lucas Huang) * @Date: 2019-08-15 17:31:08 * @LastEditors: 疯狂秀才(Lucas Huang) * @LastEditTime: 2019-09-04 15:01:15 * @QQ: 1055818239 * @Version: v0.0.1 */ import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges, ViewEncapsulation } from '@angular/core'; import { UtilService } from '../../services/public-api'; import { IMyCalendarMonth, IMyOptions, IMyDate, IMyDateRange, IMyMonthRow } from '../../interfaces/public-api'; import { KeyCode } from '../../enums/public-api'; import { OPTS, MONTHS } from '../../constants/constants'; @Component({ selector: 'lib-month-view', templateUrl: './month-view.component.html', providers: [UtilService], encapsulation: ViewEncapsulation.None }) export class MonthViewComponent implements OnChanges { @Input() opts: IMyOptions; @Input() months: Array = []; @Input() selectedDate: IMyDate; @Input() selectedDateRange: IMyDateRange; @Output() monthCellClicked: EventEmitter = new EventEmitter(); @Output() monthCellKeyDown: EventEmitter = new EventEmitter(); @Output() onMouseEnter: EventEmitter = new EventEmitter(); @Output() onMouseLeave: EventEmitter = new EventEmitter(); constructor(private utilService: UtilService) { } ngOnChanges(changes: SimpleChanges): void { if (changes.hasOwnProperty(OPTS)) { this.opts = changes[OPTS].currentValue; } if (changes.hasOwnProperty(MONTHS)) { this.months = changes[MONTHS].currentValue; } } onMonthCellClicked(event: any, cell: IMyCalendarMonth): void { event.stopPropagation(); if (cell.disabled) { return; } this.monthCellClicked.emit(cell); } onMonthCellKeyDown(event: KeyboardEvent, cell: IMyCalendarMonth) { const keyCode: number = this.utilService.getKeyCodeFromEvent(event); if (keyCode !== KeyCode.tab) { event.preventDefault(); if (keyCode === KeyCode.enter || keyCode === KeyCode.space) { this.onMonthCellClicked(event, cell); } else if (this.opts.moveFocusByArrowKeys) { this.monthCellKeyDown.emit(cell); } } } onMonthCellMouseEnter(cell: any): void { if ( this.utilService.isInitializedDate(this.selectedDateRange.begin) && !this.utilService.isInitializedDate(this.selectedDateRange.end) ) { for (let i = 0; i < this.months.length; i++) { let row = this.months[i].row; for (let j = 0; j < row.length; j++) { let month = row[j]; month.range = (this.utilService.isDateSameOrEarlier( this.selectedDateRange.begin, month.monthObj ) && this.utilService.isDateSameOrEarlier( month.monthObj, cell.monthObj )) || (this.utilService.isDateSameOrEarlier( month.monthObj, this.selectedDateRange.begin ) && this.utilService.isDateSameOrEarlier( cell.monthObj, month.monthObj )); } } this.onMouseEnter.emit(cell); } } onMonthCellMouseLeave(): void { for (let i = 0; i < this.months.length; i++) { let row = this.months[i].row; for (let j = 0; j < row.length; j++) { let month = row[j]; month.range = false; } } this.onMouseLeave.emit(); } isMonthInRange(date: IMyDate): boolean { return this.utilService.isDateInRange(date, this.selectedDateRange); } isMonthSame(date: IMyDate): boolean { return this.utilService.isDateSame( { year: this.selectedDate.year, month: this.selectedDate.month }, date ); } isMonthRangeBeginOrEndSame(date: IMyDate): boolean { return this.utilService.isDateRangeBeginOrEndSame( this.selectedDateRange, date ); } }