import { Component , ElementRef , HostListener , Input , OnInit , ViewChild , ViewEncapsulation , forwardRef } from '@angular/core' ; import { AbstractControl , ControlValueAccessor , FormControl , NG_VALIDATORS , NG_VALUE_ACCESSOR , ValidationErrors , Validator } from '@angular/forms' ; import { NgbDateStruct , NgbInputDatepicker } from '@ng-bootstrap/ng-bootstrap' ; import { ObThemeService } from '../theme.service' ; import { ObDatepickerConfigService } from './datepicker-config.service' ; import { ObIDatepickerOptions } from './datepicker.model' ; deprecated @ Component ( { selector : 'ob-date-picker' , exportAs : 'obDatePicker' , styleUrls : [ './datepicker.component.scss' ] , templateUrl : './datepicker.component.html' , encapsulation : ViewEncapsulation . None , providers : [ { provide : NG_VALUE_ACCESSOR , multi : true , useExisting : forwardRef ( ( ) => ObDatepickerComponent ) } , { provide : NG_VALIDATORS , multi : true , useExisting : forwardRef ( ( ) => ObDatepickerComponent ) } ] , host : { class : 'ob-date-picker datepicker input-group' } } ) export class ObDatepickerComponent implements OnInit , ControlValueAccessor , Validator { datePicker = new FormControl ( ) ; opts = { } as ObIDatepickerOptions ; @ Input ( ) size : 'sm' | 'lg' | null ; @ Input ( ) maxDate : NgbDateStruct ; @ Input ( ) minDate : NgbDateStruct ; @ Input ( ) startDate : NgbDateStruct ; @ Input ( ) placeholder : string ; @ Input ( ) options = { } as ObIDatepickerOptions ; @ ViewChild ( NgbInputDatepicker , { static : true } ) ngbDatePicker : NgbInputDatepicker ; get disabled ( ) { return this . datePicker . disabled ; } constructor ( private readonly element , private readonly config , theme ) { theme . deprecated ( 'datepicker' , 'datepicker' ) ; } ngOnInit ( ) { this . opts = { ... this . config . options , ... this . options } ; } writeValue ( obj ) { if ( obj ) { this . datePicker . setValue ( obj ) ; } } registerOnChange ( fn ) { this . datePicker . valueChanges . subscribe ( fn ) ; } registerOnTouched ( fn ) { } setDisabledState ( isDisabled ) { isDisabled ? this . datePicker . disable ( ) : this . datePicker . enable ( ) ; } validate ( control ) { return this . datePicker . valid ? null : this . datePicker . errors ; } @ HostListener ( 'keydown' , [ '$event' ] ) onKeydown ( $event ) { if ( $event . target . attributes . ngbdatepicker ) { if ( $event . keyCode === 40 ) { this . ngbDatePicker . open ( ) ; } else if ( $event . keyCode === 38 || $event . keyCode === 9 ) { this . ngbDatePicker . close ( ) ; } } } }