import { Directive , Inject , Input , OnChanges , OnDestroy , Optional , Self , SimpleChanges } from '@angular/core' ; import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ; import { QueryParamGroupService } from './query-param-group.service' ; import { QueryParamAccessor } from './query-param-accessor.interface' ; import { selectValueAccessor } from '../accessors/util' ; /**
 * Binds a  QueryParam to a DOM element.
 *
 * This directive accepts the name of a  QueryParam inside its parent  QueryParamGroup .
 * It binds this parameter to the host element, which is required to have a [ControlValueAccessor]
 *  https .
  @ Directive ( { selector : '[queryParamName]' , } ) export class QueryParamNameDirective implements QueryParamAccessor , OnChanges , OnDestroy { /**
     * The name of the  QueryParam inside its parent  QueryParamGroup .
     * Note that this does not refer to the [parameter name] QueryParam .
      @ Input ( 'queryParamName' ) public set name ( name ) { this . _name = name ; } public get name ( ) : string { if ( ! this . _name ) { throw new Error ( `No queryParamName has been specified.` ) ; } return this . _name ; } internal public valueAccessor : ControlValueAccessor ; private _name : string | null = null ; internal constructor ( @ Optional ( ) private groupService , @ Optional ( ) @ Self ( ) @ Inject ( NG_VALUE_ACCESSOR ) valueAccessors ) { if ( ! this . groupService ) { throw new Error ( `No parent configuration found. Did you forget to add [queryParamGroup]?` ) ; } this . valueAccessor = selectValueAccessor ( valueAccessors ) ; } ignore public ngOnChanges ( changes ) { const nameChange = changes [ 'name' ] ; if ( nameChange ) { if ( ! nameChange . firstChange ) { this . groupService . deregisterQueryParamDirective ( nameChange . previousValue ) ; } if ( nameChange . currentValue ) { this . groupService . registerQueryParamDirective ( this ) ; } } } ignore public ngOnDestroy ( ) { if ( this . groupService ) { this . groupService . deregisterQueryParamDirective ( this . name ) ; } } }