All files / src/lib/forms/directives date-picker.directive.ts

100% Statements 73/73
100% Branches 29/29
100% Functions 18/18
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1591x                               1x 1x             1x     2x   47x 21x 21x 21x           2x 1x   1x 1x 1x               19x 19x 19x 19x     19x   19x 19x   19x   19x 16x     19x     1x 19x 16x 16x       1x 20x 2x         1x 31x     19x     19x     1x 16x     1x 16x         1x 2x 1x 1x 1x         1x 1x 1x 1x         1x 2x   2x 1x           1x       1x 3x 1x     2x 2x       1x 2x 2x     1x 4x 3x   4x   1x  
import {
    ChangeDetectorRef,
    Directive,
    Input,
    Optional,
    Self,
    OnDestroy,
    ElementRef,
    EventEmitter,
    HostListener,
    HostBinding,
    Inject,
    Renderer2,
    AfterViewInit,
    LOCALE_ID
} from '@angular/core';
import {ControlValueAccessor, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {DateAdapter} from '../services/date-picker.service';
@Directive({
    selector: 'anj-input[anjDatePicker]',
    exportAs: 'anjDatepickerInput'
})
 
 
export class DatePickerDirective implements OnDestroy, ControlValueAccessor, AfterViewInit {
    // Custom input to return inner value, but display masked value
    @Input()
    get value(): Date { return this.innerDateValue; }
    set value(dateValue: Date) {
        if (dateValue) {
            this.innerDateValue = this.adapterStrategy.processDateInput(dateValue);
            this.outerValue = this.adapterStrategy.prepDateStringOutput(dateValue);
            this.changeRef.markForCheck();
        }
    }
 
    // AdapterStrategy will be set by the DateAdapter injected provider. This is for an optional override.
    @Input()
    set adapter(adapter: any) {
        this.adapterStrategy = adapter;
    }
    @HostBinding('value') public outerValue: string;
    @HostBinding('attr.invalid') public invalid: boolean;
    @HostBinding('attr.language') public languageCode: string;
    public adapterStrategy: DateAdapter<Date>;
    public readonly valueChange: EventEmitter<Date | null>;
 
    private innerDateValue: Date;
    private listenerFn: () => void;
    private readonly parent: NgForm | FormGroupDirective | null;
 
    constructor(private renderer: Renderer2,
        private _element: ElementRef,
                private changeRef: ChangeDetectorRef,
                public dateAdapter: DateAdapter<Date>,
                @Optional() parentForm: NgForm,
                @Optional() parentFormGroup: FormGroupDirective,
                @Optional() @Self() private ngControl?: NgControl,
                @Optional() @Inject(LOCALE_ID) _locale?: string) {
        this.languageCode = (_locale) ? _locale.split('-')[0] : 'en';
        this.valueChange = new EventEmitter<Date | null>();
 
        this.adapterStrategy = dateAdapter;
 
        if (this.ngControl) {
            this.ngControl.valueAccessor = this;
        }
 
        this.parent = parentForm ? parentForm : parentFormGroup ? parentFormGroup : null;
    }
 
    public ngAfterViewInit() {
        if (this.ngControl) {
            this.value = this.ngControl.value;
            this.changeRef.detectChanges();
        }
    }
 
    public ngOnDestroy(): void {
        if (this.listenerFn) {
            this.listenerFn();
        }
    }
 
    // // Start: ControlValueAccessor requirements
    public writeValue(newValue: Date): void {
        this.value = newValue;
    }
 
    public onChange: Function = () => {
    }
 
    public onTouch: Function = () => {
    }
 
    public registerOnChange(fn: any): void {
        this.onChange = fn;
    }
 
    public registerOnTouched(fn: any): void {
        this.onTouch = fn;
    }
    // // End: ControlValueAccessor requirements
 
    @HostListener('anjBlur', ['$event'])
    public onInput(event: any) {
        if (event.target.tagName === 'ANJ-INPUT') {
            this.onTouch();
            this.updateInnerValue(event.target.value);
            this.valueChange.emit(this.innerDateValue);
        }
    }
 
    @HostListener('anjCalendarSelection', ['$event'])
    public onCalendarSelect(event: any) {
        this.onTouch();
        this.updateInnerValue(event.detail);
        this.valueChange.emit(this.innerDateValue);
    }
 
    /* Intercept and override native input validity, if ngControl has a validity issue */
    @HostListener('anjInvalid', ['$event'])
    public onAnjInvalid(event: any) {
        this.isInvalid();
 
        if (event.detail !== this.invalid && !event.detail) {
            this._element.nativeElement
                .dispatchEvent(new CustomEvent('anjInvalid', {
                    detail: this.invalid,
                    bubbles: true
                }));
 
            this.changeRef.markForCheck();
        }
    }
 
    private isInvalid(): boolean {
        if (!this.ngControl) {
            return;
        }
 
        const isSubmitted = (this.parent && this.parent.submitted);
        this.invalid = this.ngControl.invalid && (this.ngControl.touched || isSubmitted);
    }
 
    // convert the masked value to storage value
    private updateInnerValue(inputDate: string | Date): void {
        this.innerDateValue = this.adapterStrategy.processDateInput(inputDate);
        this.propogateInnerValueChange();
    }
 
    private propogateInnerValueChange(): void {
        if (this.ngControl) {
            this.ngControl.control.setValue(this.innerDateValue);
        }
        this.valueChange.emit(this.innerDateValue);
    }
}