igniteui-angular-sovn
Version:
Ignite UI for Angular is a dependency-free Angular toolkit for building modern web apps
366 lines (324 loc) • 13.3 kB
text/typescript
/**
* This file contains all the directives used by the @link IgxTimePickerComponent.
* You should generally not use them directly.
*
* @preferred
*/
import {
Directive,
ElementRef,
HostBinding,
HostListener,
Inject,
Input,
OnDestroy,
OnInit
} from '@angular/core';
import { HammerGesturesManager } from '../core/touch';
import { DateTimeUtil } from '../date-common/util/date-time.util';
import { IgxTimePickerBase, IGX_TIME_PICKER_COMPONENT } from './time-picker.common';
/** @hidden */
export class IgxItemListDirective implements OnInit, OnDestroy {
public tabindex = 0;
public type: string;
public isActive: boolean;
constructor(
public timePicker: IgxTimePickerBase,
private elementRef: ElementRef,
private touchManager: HammerGesturesManager
) { }
public get defaultCSS(): boolean {
return true;
}
public get hourCSS(): boolean {
return this.type === 'hourList';
}
public get minuteCSS(): boolean {
return this.type === 'minuteList';
}
public get secondsCSS(): boolean {
return this.type === 'secondsList';
}
public get ampmCSS(): boolean {
return this.type === 'ampmList';
}
public onFocus() {
this.isActive = true;
}
public onBlur() {
this.isActive = false;
}
/**
* @hidden
*/
public onKeydownArrowDown(event: KeyboardEvent) {
event.preventDefault();
this.nextItem(1);
}
/**
* @hidden
*/
public onKeydownArrowUp(event: KeyboardEvent) {
event.preventDefault();
this.nextItem(-1);
}
/**
* @hidden
*/
public onKeydownArrowRight(event: KeyboardEvent) {
event.preventDefault();
const listName = (event.target as HTMLElement).className;
if (listName.indexOf('hourList') !== -1 && this.timePicker.minuteList) {
this.timePicker.minuteList.nativeElement.focus();
} else if ((listName.indexOf('hourList') !== -1 || listName.indexOf('minuteList') !== -1) && this.timePicker.secondsList) {
this.timePicker.secondsList.nativeElement.focus();
} else if ((listName.indexOf('hourList') !== -1 || listName.indexOf('minuteList') !== -1 ||
listName.indexOf('secondsList') !== -1) && this.timePicker.ampmList) {
this.timePicker.ampmList.nativeElement.focus();
}
}
/**
* @hidden
*/
public onKeydownArrowLeft(event: KeyboardEvent) {
event.preventDefault();
const listName = (event.target as HTMLElement).className;
if (listName.indexOf('ampmList') !== -1 && this.timePicker.secondsList) {
this.timePicker.secondsList.nativeElement.focus();
} else if (listName.indexOf('secondsList') !== -1 && this.timePicker.secondsList
&& listName.indexOf('minutesList') && this.timePicker.minuteList) {
this.timePicker.minuteList.nativeElement.focus();
} else if (listName.indexOf('ampmList') !== -1 && this.timePicker.minuteList) {
this.timePicker.minuteList.nativeElement.focus();
} else if ((listName.indexOf('ampmList') !== -1 || listName.indexOf('secondsList') !== -1 ||
listName.indexOf('minuteList') !== -1) && this.timePicker.hourList) {
this.timePicker.hourList.nativeElement.focus();
}
}
/**
* @hidden
*/
public onKeydownEnter(event: KeyboardEvent) {
event.preventDefault();
this.timePicker.okButtonClick();
}
/**
* @hidden
*/
public onKeydownEscape(event: KeyboardEvent) {
event.preventDefault();
this.timePicker.cancelButtonClick();
}
/**
* @hidden
*/
public onHover() {
this.elementRef.nativeElement.focus();
}
/**
* @hidden
*/
public onScroll(event) {
event.preventDefault();
event.stopPropagation();
const delta = event.deltaY;
if (delta !== 0) {
this.nextItem(delta);
}
}
/**
* @hidden @internal
*/
public ngOnInit() {
const hammerOptions: HammerOptions = { recognizers: [[Hammer.Pan, { direction: Hammer.DIRECTION_VERTICAL, threshold: 10 }]] };
this.touchManager.addEventListener(this.elementRef.nativeElement, 'pan', this.onPanMove, hammerOptions);
}
/**
* @hidden @internal
*/
public ngOnDestroy() {
this.touchManager.destroy();
}
private onPanMove = (event: HammerInput) => {
const delta = event.deltaY < 0 ? -1 : event.deltaY > 0 ? 1 : 0;
if (delta !== 0) {
this.nextItem(delta);
}
};
private nextItem(delta: number): void {
switch (this.type) {
case 'hourList': {
this.timePicker.nextHour(delta);
break;
}
case 'minuteList': {
this.timePicker.nextMinute(delta);
break;
}
case 'secondsList': {
this.timePicker.nextSeconds(delta);
break;
}
case 'ampmList': {
this.timePicker.nextAmPm(delta);
break;
}
}
}
}
/**
* @hidden
*/
export class IgxTimeItemDirective {
public value: string;
public get defaultCSS(): boolean {
return true;
}
public get selectedCSS(): boolean {
return this.isSelectedTime;
}
public get activeCSS(): boolean {
return this.isSelectedTime && this.itemList.isActive;
}
public get isSelectedTime(): boolean {
const currentValue = this.value.length < 2 ? `0${this.value}` : this.value;
const dateType = this.itemList.type;
const inputDateParts = DateTimeUtil.parseDateTimeFormat(this.timePicker.inputFormat);
switch (dateType) {
case 'hourList':
const hourPart = inputDateParts.find(element => element.type === 'hours');
return DateTimeUtil.getPartValue(this.timePicker.selectedDate, hourPart, hourPart.format.length) === currentValue;
case 'minuteList':
const minutePart = inputDateParts.find(element => element.type === 'minutes');
return DateTimeUtil.getPartValue(this.timePicker.selectedDate, minutePart, minutePart.format.length) === currentValue;
case 'secondsList':
const secondsPart = inputDateParts.find(element => element.type === 'seconds');
return DateTimeUtil.getPartValue(this.timePicker.selectedDate, secondsPart, secondsPart.format.length) === currentValue;
case 'ampmList':
const ampmPart = inputDateParts.find(element => element.format === 'tt');
return DateTimeUtil.getPartValue(this.timePicker.selectedDate, ampmPart, ampmPart.format.length) === this.value;
}
}
public get minValue(): string {
const dateType = this.itemList.type;
const inputDateParts = DateTimeUtil.parseDateTimeFormat(this.timePicker.inputFormat);
switch (dateType) {
case 'hourList':
return this.getHourPart(this.timePicker.minDropdownValue);
case 'minuteList':
if (this.timePicker.selectedDate.getHours() === this.timePicker.minDropdownValue.getHours()) {
const minutePart = inputDateParts.find(element => element.type === 'minutes');
return DateTimeUtil.getPartValue(this.timePicker.minDropdownValue, minutePart, minutePart.format.length);
}
return '00';
case 'secondsList':
const date = new Date(this.timePicker.selectedDate);
const min = new Date(this.timePicker.minDropdownValue);
date.setSeconds(0);
min.setSeconds(0);
if (date.getTime() === min.getTime()) {
const secondsPart = inputDateParts.find(element => element.type === 'seconds');
return DateTimeUtil.getPartValue(this.timePicker.minDropdownValue, secondsPart, secondsPart.format.length);
}
return '00';
case 'ampmList':
const ampmPart = inputDateParts.find(element => element.format === 'tt');
return DateTimeUtil.getPartValue(this.timePicker.minDropdownValue, ampmPart, ampmPart.format.length);
}
}
public get maxValue(): string {
const dateType = this.itemList.type;
const inputDateParts = DateTimeUtil.parseDateTimeFormat(this.timePicker.inputFormat);
switch (dateType) {
case 'hourList':
return this.getHourPart(this.timePicker.maxDropdownValue);
case 'minuteList':
if (this.timePicker.selectedDate.getHours() === this.timePicker.maxDropdownValue.getHours()) {
const minutePart = inputDateParts.find(element => element.type === 'minutes');
return DateTimeUtil.getPartValue(this.timePicker.maxDropdownValue, minutePart, minutePart.format.length);
} else {
const currentTime = new Date(this.timePicker.selectedDate);
const minDelta = this.timePicker.itemsDelta.minutes;
const remainder = 60 % minDelta;
const delta = remainder === 0 ? 60 - minDelta : 60 - remainder;
currentTime.setMinutes(delta);
const minutePart = inputDateParts.find(element => element.type === 'minutes');
return DateTimeUtil.getPartValue(currentTime, minutePart, minutePart.format.length);
}
case 'secondsList':
const date = new Date(this.timePicker.selectedDate);
const max = new Date(this.timePicker.maxDropdownValue);
date.setSeconds(0);
max.setSeconds(0);
if (date.getTime() === max.getTime()) {
const secondsPart = inputDateParts.find(element => element.type === 'seconds');
return DateTimeUtil.getPartValue(this.timePicker.maxDropdownValue, secondsPart, secondsPart.format.length);
} else {
const secDelta = this.timePicker.itemsDelta.seconds;
const remainder = 60 % secDelta;
const delta = remainder === 0 ? 60 - secDelta : 60 - remainder;
date.setSeconds(delta);
const secondsPart = inputDateParts.find(element => element.type === 'seconds');
return DateTimeUtil.getPartValue(date, secondsPart, secondsPart.format.length);
}
case 'ampmList':
const ampmPart = inputDateParts.find(element => element.format === 'tt');
return DateTimeUtil.getPartValue(this.timePicker.maxDropdownValue, ampmPart, ampmPart.format.length);
}
}
public get hourValue(): string {
return this.getHourPart(this.timePicker.selectedDate);
}
constructor(
public timePicker: IgxTimePickerBase,
private itemList: IgxItemListDirective) { }
public onClick(item) {
if (item !== '') {
const dateType = this.itemList.type;
this.timePicker.onItemClick(item, dateType);
}
}
private getHourPart(date: Date): string {
const inputDateParts = DateTimeUtil.parseDateTimeFormat(this.timePicker.inputFormat);
const hourPart = inputDateParts.find(element => element.type === 'hours');
const ampmPart = inputDateParts.find(element => element.format === 'tt');
const hour = DateTimeUtil.getPartValue(date, hourPart, hourPart.format.length);
if (ampmPart) {
const ampm = DateTimeUtil.getPartValue(date, ampmPart, ampmPart.format.length);
return `${hour} ${ampm}`;
}
return hour;
}
}