igniteui-angular-sovn
Version:
Ignite UI for Angular is a dependency-free Angular toolkit for building modern web apps
280 lines (251 loc) • 7.28 kB
text/typescript
import {
Component,
HostListener,
ViewChild,
HostBinding,
Input,
ElementRef
} from '@angular/core';
import { NgIf, NgStyle } from '@angular/common';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { trigger, transition, useAnimation } from '@angular/animations';
import { fadeIn, scaleInCenter, slideInLeft, slideInRight } from '../../animations/main';
import { IgxMonthsViewComponent } from '../months-view/months-view.component';
import { IgxMonthPickerBaseDirective } from './month-picker-base';
import { IgxYearsViewComponent } from '../years-view/years-view.component';
import { IgxDaysViewComponent } from '../days-view/days-view.component';
import { IgxIconComponent } from '../../icon/icon.component';
import { IgxCalendarView, ScrollMonth } from '../calendar';
let NEXT_ID = 0;
export class IgxMonthPickerComponent extends IgxMonthPickerBaseDirective {
/**
* Sets/gets the `id` of the month picker.
* If not set, the `id` will have value `"igx-month-picker-0"`.
*/
public id = `igx-month-picker-${NEXT_ID++}`;
/**
* The default css class applied to the component.
*
* @hidden
*/
public styleClass = true;
/**
* @hidden
*/
public monthsView: IgxMonthsViewComponent;
/**
* @hidden
*/
public dacadeView: IgxYearsViewComponent;
/**
* @hidden
*/
public daysView: IgxDaysViewComponent;
/**
* @hidden
*/
public yearsBtn: ElementRef;
/**
* @hidden
*/
public yearAction = '';
/**
* @hidden
*/
public previousYear(event?: KeyboardEvent) {
event?.preventDefault();
if (event && this.yearAction === 'next') {
return;
}
this.yearAction = 'prev';
this.previousViewDate = this.viewDate;
this.viewDate = this.calendarModel.getPrevYear(this.viewDate);
}
/**
* @hidden
*/
public nextYear(event?: KeyboardEvent) {
event?.preventDefault();
if (event && this.yearAction === 'prev') {
return;
}
this.yearAction = 'next';
this.previousViewDate = this.viewDate;
this.viewDate = this.calendarModel.getNextYear(this.viewDate);
}
/**
* @hidden
*/
public onKeydownHome(event: KeyboardEvent) {
if (this.monthsView) {
this.monthsView.el.nativeElement.focus();
this.monthsView.onKeydownHome(event);
}
}
/**
* @hidden
*/
public onKeydownEnd(event: KeyboardEvent) {
if (this.monthsView) {
this.monthsView.el.nativeElement.focus();
this.monthsView.onKeydownEnd(event);
}
}
/**
* @hidden
*/
public animationDone(event) {
if ((event.fromState === 'void' && event.toState === '') ||
(event.fromState === '' && (event.toState === ScrollMonth.PREV || event.toState === ScrollMonth.NEXT))) {
this.viewDateChanged.emit({ previousValue: this.previousViewDate, currentValue: this.viewDate });
}
this.yearAction = '';
}
/**
* @hidden
*/
public viewRendered(event) {
if (event.fromState !== 'void') {
this.activeViewChanged.emit(this.activeView);
}
}
/**
* @hidden
*/
public override activeViewDecadeKB(event: KeyboardEvent) {
super.activeViewDecadeKB(event);
if (event.key === this.platform.KEYMAP.ARROW_RIGHT) {
this.nextYear(event);
}
if (event.key === this.platform.KEYMAP.ARROW_LEFT) {
this.previousYear(event);
}
requestAnimationFrame(() => {
if (this.dacadeView) {
this.dacadeView.el.nativeElement.focus();
}
});
}
/**
* @hidden
*/
public override activeViewDecade() {
super.activeViewDecade();
requestAnimationFrame(() => {
this.dacadeView.el.nativeElement.focus();
});
}
/**
* @hidden
*/
public changeYearKB(event: KeyboardEvent, next = true) {
if (this.platform.isActivationKey(event)) {
event.stopPropagation();
if (next) {
this.nextYear();
} else {
this.previousYear();
}
}
}
/**
* @hidden
*/
public selectYear(event: Date) {
this.previousViewDate = this.viewDate;
this.viewDate = new Date(event.getFullYear(), event.getMonth(), event.getDate());
this.activeView = IgxCalendarView.Month;
requestAnimationFrame(() => {
if (this.yearsBtn) {
this.yearsBtn.nativeElement.focus();
}
});
}
/**
* @hidden
*/
public selectMonth(event: Date) {
this.selectDate(event);
this.selected.emit(this.selectedDates);
}
/**
* Selects a date.
* ```typescript
* this.monthPicker.selectDate(new Date(`2018-06-12`));
* ```
*/
public override selectDate(value: Date) {
if (!value) {
return new Date();
}
super.selectDate(value);
this.viewDate = value;
}
/**
* @hidden
*/
public override writeValue(value: Date) {
if (value) {
this.viewDate = this.selectedDates = value;
}
}
/**
* @hidden
*/
public getNextYear() {
return this.calendarModel.getNextYear(this.viewDate).getFullYear();
}
/**
* @hidden
*/
public getPreviousYear() {
return this.calendarModel.getPrevYear(this.viewDate).getFullYear();
}
}