import { AfterViewInit , Directive , ElementRef , forwardRef , Host , HostListener , Input , OnDestroy , Optional , Renderer2 , } from '@angular/core' ; import { FormControlState } from '../state' ; import { FormViewAdapter , NGRX_FORM_VIEW_ADAPTER } from './view-adapter' ; @ Directive ( { selector : 'select:not([multiple])[ngrxFormControlState]' , providers : [ { provide : NGRX_FORM_VIEW_ADAPTER , useExisting : forwardRef ( ( ) => NgrxSelectViewAdapter ) , multi : true , } ] , } ) export class NgrxSelectViewAdapter implements FormViewAdapter , AfterViewInit { private state : FormControlState < any > ; private optionMap : { [ id ] : any ; } = { } ; private idCounter = 0 ; private selectedId : string | null = null ; private value : any = undefined ; private nativeIdWasSet = false ; onChangeFn : ( value : any ) => void = ( ) => void 0 ; @ HostListener ( 'blur' ) onTouched : ( ) => void = ( ) => void 0 ; @ Input ( ) set ngrxFormControlState ( value ) { if ( ! value ) { throw new Error ( 'The control state must not be undefined!' ) ; } this . state = value ; const nativeId = this . elementRef . nativeElement . id ; const shouldSetNativeId = value . id !== nativeId && this . nativeIdWasSet ; if ( shouldSetNativeId ) { this . renderer . setProperty ( this . elementRef . nativeElement , 'id' , value . id ) ; } } constructor ( private renderer , private elementRef ) { } ngAfterViewInit ( ) { const nativeId = this . elementRef . nativeElement . id ; const shouldSetNativeId = this . state . id !== nativeId && ! nativeId ; if ( shouldSetNativeId ) { this . renderer . setProperty ( this . elementRef . nativeElement , 'id' , this . state . id ) ; this . nativeIdWasSet = true ; } } setViewValue ( value ) { this . value = value ; this . selectedId = this . getOptionId ( value ) ; if ( this . selectedId === null ) { this . renderer . setProperty ( this . elementRef . nativeElement , 'selectedIndex' , - 1 ) ; } this . renderer . setProperty ( this . elementRef . nativeElement , 'value' , this . selectedId ) ; } @ HostListener ( 'change' , [ '$event' ] ) onChange ( { target } ) { this . selectedId = target . value ; const value = this . optionMap [ this . selectedId ] ; this . value = value ; this . onChangeFn ( value ) ; } setOnChangeCallback ( fn ) { this . onChangeFn = fn ; } setOnTouchedCallback ( fn ) { this . onTouched = fn ; } setIsDisabled ( isDisabled ) { this . renderer . setProperty ( this . elementRef . nativeElement , 'disabled' , isDisabled ) ; } createOptionId ( ) { const id = this . idCounter . toString ( ) ; this . idCounter += 1 ; return id ; } updateOptionValue ( id , value ) { this . optionMap [ id ] = value ; if ( this . selectedId === id ) { this . onChangeFn ( value ) ; } else if ( value === this . value ) { this . setViewValue ( value ) ; } } deregisterOption ( id ) { delete this . optionMap [ id ] ; } private getOptionId ( value ) { for ( const id of Array . from ( Object . keys ( this . optionMap ) ) ) { if ( this . optionMap [ id ] === value ) { return id ; } } return null ; } } const NULL_VIEW_ADAPTER = { createOptionId : ( ) => '' , deregisterOption : ( ) => void 0 , updateOptionValue : ( ) => void 0 , } as any ; const NULL_RENDERER = { setProperty : ( ) => void 0 , } as any ; @ Directive ( { selector : 'option' , } ) export class NgrxSelectOption implements OnDestroy { private isInitialized = false ; id : string ; constructor ( private element , private renderer , @ Host ( ) @ Optional ( ) private viewAdapter ) { this . renderer = viewAdapter ? renderer : NULL_RENDERER ; this . viewAdapter = viewAdapter || NULL_VIEW_ADAPTER ; this . id = this . viewAdapter . createOptionId ( ) ; } @ Input ( 'value' ) set value ( value ) { if ( ! this . isInitialized ) { this . isInitialized = true ; this . renderer . setProperty ( this . element . nativeElement , 'value' , this . id ) ; } this . viewAdapter . updateOptionValue ( this . id , value ) ; } ngOnDestroy ( ) { this . viewAdapter . deregisterOption ( this . id ) ; } }