all files / component/combo-list/model/ combo-list.base.ts

95.92% Statements 47/49
75% Branches 12/16
87.5% Functions 14/16
95.65% Lines 44/46
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                      21× 21×                 15×         12×         15×             18×     15×     151× 15×   151×       15× 15× 15×                                                  
/*
 *  @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 { EventEmitter, Input, Output } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import { IDejaComboListAction } from '../model/combo-list-action.interface';
import { DejaComboListState } from './combo-list-state.class';
 
export abstract class DejaComboListBase<T> implements ControlValueAccessor {
 
    @Input() public set itemsSelected(aItem: T[]) {
        Eif (!!aItem && Array.isArray(aItem)) {
            this.state.selected = aItem;
        }
    }
    public get itemsSelected(): T[] {
        return this.state.selected;
    }
 
    @Input() public set itemsToSelect(aItem: T[]) {
        if (!!aItem && Array.isArray(aItem)) {
            this.state.selectable = aItem;
        }
    }
 
    @Input() public disableFastActions = false;
 
    @Input() public set labelFieldName(fieldName: string) {
        Eif (fieldName) {
            this.state.labelFieldName = fieldName;
            this.state.sortAll();
        }
    }
    public get labelFieldName(): string {
        return this.state.labelFieldName;
    }
 
    @Input() public set sortDirection(direction: null | 'asc' | 'desc') {
        this.state.sortDirection = direction || null;
        this.state.sortAll();
    }
 
    private _disabled = false;
 
    @Input()
    public set disabled(value: boolean) {
        this._disabled = coerceBooleanProperty(value);
    }
 
    public get disabled() {
        return this._disabled;
    }
 
    @Output() public action = new EventEmitter<IDejaComboListAction<T>>();
 
    private _state: DejaComboListState<T>;
    public get state() {
        if (!this._state) {
            this._state = new DejaComboListState();
        }
        return this._state;
    }
 
    constructor() {
        this.itemsSelected = [];
        this.state.selectable = [];
        this.state.action = this.action;
    }
 
    // ************* ControlValueAccessor Implementation **************
 
    /** get accessor */
    public get value(): T[] {
        return this.itemsSelected;
    }
 
    /** From ControlValueAccessor interface */
    public writeValue(value: T[]): void {
        Eif (value !== this.itemsSelected) {
            this.itemsSelected = value;
            this.state.onChangeCallback(this.value);
            this.state.sortAll();
        }
    }
 
    /** From ControlValueAccessor interface */
    public registerOnChange(fn: any) {
        this.state.onChangeCallback = fn;
    }
 
    /** From ControlValueAccessor interface */
    public registerOnTouched(fn: any) {
        this.state.onTouchedCallback = fn;
    }
 
    // Allows Angular to disable the input.
    public setDisabledState(isDisabled: boolean): void {
        this.disabled = isDisabled;
    }
 
    // ************* End of ControlValueAccessor Implementation **************
 
}