import { ControlValueAccessor } from '@angular/forms' ; import { Input , Directive } from '@angular/core' ; import { OptionsProvider } from './providers/options-provider' ; import { TagInputDropdown } from '../components/dropdown/tag-input-dropdown.component' ; import { TagModel } from './tag-model' ; export function isObject ( obj ) { return obj === Object ( obj ) ; } @ Directive ( ) export class TagInputAccessor implements ControlValueAccessor { private _items : TagModel [ ] = [ ] ; private _onTouchedCallback : ( ) => void ; private _onChangeCallback : ( items : TagModel [ ] ) => void ; public dropdown : TagInputDropdown ; name @ Input ( ) public displayBy : string = OptionsProvider . defaults . tagInput . displayBy ; name @ Input ( ) public identifyBy : string = OptionsProvider . defaults . tagInput . identifyBy ; public get items ( ) : TagModel [ ] { return this . _items ; } public set items ( items ) { this . _items = items ; this . _onChangeCallback ( this . _items ) ; } public onTouched ( ) { this . _onTouchedCallback ( ) ; } public writeValue ( items ) { this . _items = items || [ ] ; } public registerOnChange ( fn ) { this . _onChangeCallback = fn ; } public registerOnTouched ( fn ) { this . _onTouchedCallback = fn ; } name param item param fromDropdown public getItemValue ( item , fromDropdown = false ) { const property = fromDropdown && this . dropdown ? this . dropdown . identifyBy : this . identifyBy ; return isObject ( item ) ? item [ property ] : item ; } name param item param fromDropdown public getItemDisplay ( item , fromDropdown = false ) { const property = fromDropdown && this . dropdown ? this . dropdown . displayBy : this . displayBy ; return isObject ( item ) ? item [ property ] : item ; } name param index protected getItemsWithout ( index ) { return this . items . filter ( ( item , position ) => position !== index ) ; } }