igniteui-angular-sovn
Version:
Ignite UI for Angular is a dependency-free Angular toolkit for building modern web apps
230 lines (191 loc) • 5.21 kB
text/typescript
/**
* This file contains all the directives used by the @link IgxCalendarComponent.
* Except for the directives which are used for templating the calendar itself
* you should generally not use them directly.
*
* @preferred
*/
import {
Directive,
EventEmitter,
HostBinding,
HostListener,
Input,
Output,
TemplateRef,
ElementRef,
AfterViewInit,
OnDestroy,
NgZone
} from '@angular/core';
import { fromEvent, Subject, interval } from 'rxjs';
import { takeUntil, debounce, tap } from 'rxjs/operators';
import { PlatformUtil } from '../core/utils';
/**
* @hidden
*/
export class IgxCalendarYearDirective {
public value: Date;
public date: Date;
public yearSelection = new EventEmitter<Date>();
public get currentCSS(): boolean {
return this.isCurrentYear;
}
public get role(): string {
return this.isCurrentYear ? 'spinbutton' : null;
}
public get valuenow(): number {
return this.isCurrentYear ? this.date.getFullYear() : null;
}
public get tabIndex(): number {
return this.isCurrentYear ? 0 : -1;
}
public get isCurrentYear(): boolean {
return this.date.getFullYear() === this.value.getFullYear();
}
public get nativeElement() {
return this.elementRef.nativeElement;
}
constructor(public elementRef: ElementRef) { }
public onClick() {
this.yearSelection.emit(this.value);
}
}
export class IgxCalendarMonthDirective {
public value: Date;
public date: Date;
public index;
public monthSelection = new EventEmitter<Date>();
public get currentCSS(): boolean {
return this.isCurrentMonth;
}
public get isCurrentMonth(): boolean {
return this.date.getMonth() === this.value.getMonth();
}
public get nativeElement() {
return this.elementRef.nativeElement;
}
constructor(public elementRef: ElementRef) { }
public onClick() {
const date = new Date(this.value.getFullYear(), this.value.getMonth(), this.date.getDate());
this.monthSelection.emit(date);
}
}
/**
* @hidden
*/
export class IgxCalendarHeaderTemplateDirective {
constructor(public template: TemplateRef<any>) { }
}
/**
* @hidden
*/
export class IgxCalendarSubheaderTemplateDirective {
constructor(public template: TemplateRef<any>) { }
}
/**
* @hidden
*/
export class IgxCalendarScrollMonthDirective implements AfterViewInit, OnDestroy {
/**
* A callback function to be invoked when month increment/decrement starts.
*
* @hidden
*/
public startScroll: (keydown?: boolean) => void;
/**
* A callback function to be invoked when month increment/decrement stops.
*
* @hidden
*/
public stopScroll: (event: any) => void;
/**
* @hidden
*/
private destroy$ = new Subject<boolean>();
constructor(private element: ElementRef, private zone: NgZone, protected platform: PlatformUtil) { }
/**
* @hidden
*/
public onMouseDown() {
this.startScroll();
}
/**
* @hidden
*/
public onMouseUp(event: MouseEvent) {
this.stopScroll(event);
}
/**
* @hidden
*/
public ngAfterViewInit() {
fromEvent(this.element.nativeElement, 'keyup').pipe(
debounce(() => interval(100)),
takeUntil(this.destroy$)
).subscribe((event: KeyboardEvent) => {
this.stopScroll(event);
});
this.zone.runOutsideAngular(() => {
fromEvent(this.element.nativeElement, 'keydown').pipe(
tap((event: KeyboardEvent) => {
if (this.platform.isActivationKey(event)) {
event.preventDefault();
event.stopPropagation();
}
}),
debounce(() => interval(100)),
takeUntil(this.destroy$)
).subscribe((event: KeyboardEvent) => {
if (this.platform.isActivationKey(event)) {
this.zone.run(() => this.startScroll(true));
}
});
});
}
/**
* @hidden
*/
public ngOnDestroy() {
this.destroy$.next(true);
this.destroy$.complete();
}
}