all files / component/tag/ tag.component.ts

84.78% Statements 39/46
58.33% Branches 7/12
61.54% Functions 8/13
83.72% Lines 36/43
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                                                                                    42×                                                                                    
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { Component, HostBinding, Input, Optional, Self } from '@angular/core';
import { ControlValueAccessor, NgControl } from '@angular/forms';
import { KeyCodes } from '../../common/core/keycodes.enum';
 
const noop = () => { };
 
/**
 * Deja Tag Component for Angular
 *
 * This component allow you to manage element into an array of string
 */
@Component({
    selector: 'deja-tag',
    template: require('./tag.component.html'),
    styles: [require('./tag.component.scss')],
})
export class DejaTagComponent implements ControlValueAccessor {
 
    /**
     * Current value into the input
     */
    public text = '';
 
    /**
     * Current value of the array of string
     */
    public items: string[] = [];
 
    /**
     * Placeholder of the input
     */
    @Input() public placeholder: string;
 
    /** Allow to disabled the component */
    @Input()
    public set disabled(value: boolean | string) {
        const disabled = coerceBooleanProperty(value);
        this._disabled = disabled || null;
    }
 
    /**
     * Get disable value
     */
    public get disabled() {
        return this._control ? this._control.disabled : this._disabled;
    }
 
    @HostBinding('attr.disabled') private _disabled: boolean = null;
 
    // NgModel implementation
    protected onTouchedCallback: () => void = noop;
    protected onChangeCallback: (_: any) => void = noop;
    protected onValidatorChangeCallback: () => void = noop;
 
    constructor( @Self() @Optional() public _control: NgControl) {
        Iif (this._control) {
            this._control.valueAccessor = this;
        }
    }
 
    // ************* ControlValueAccessor Implementation **************
    public get value() {
        return this.items;
    }
 
    public set value(val) {
        this.writeValue(val);
        this.onChangeCallback(val);
        this.onTouchedCallback();
    }
 
    public writeValue(value: string[]) {
        this.items = value ? value : [];
    }
 
    public registerOnChange(fn: any) {
        this.onChangeCallback = fn;
    }
 
    public registerOnTouched(fn: any) {
        this.onTouchedCallback = fn;
    }
 
    public setDisabledState(isDisabled: boolean) {
        this.disabled = isDisabled;
    }
    // ************* End of ControlValueAccessor Implementation **************
 
    /**
     * Trigerred when user press key into the component
     */
    public onKeyDown(e: KeyboardEvent) {
        if (e.keyCode === KeyCodes.Enter) {
            const target = e.target as HTMLInputElement;
            this.onAddItem(target.value);
        }
    }
 
    /**
     * Add item into the the list
     * @param val : Value of the text to add
     */
    public onAddItem(val: string) {
        if (val) {
            this.items.push(val);
            this.value = this.items;
            this.text = '';
        }
    }
}