all files / component/combo-list/model/ combo-list-state.class.ts

99.05% Statements 104/105
71.88% Branches 23/32
100% Functions 23/23
98.84% Lines 85/86
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                  26× 26× 26× 26×   26×     26×   26× 26×                                           26× 26× 26×     52× 26×   26× 26× 26×         20× 10× 10×     26× 18×             18× 18×        
/*
 *  @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 { EventEmitter } from '@angular/core';
import { IDejaComboListAction, noop } from '../model/combo-list-action.interface';
 
export class DejaComboListState<T> {
 
    public selectable: T[] = [];
    public selectableBuffer: T[] = [];
    public selected: T[] = [];
    public selectedBuffer: T[] = [];
 
    public labelFieldName = 'label';
    public sortDirection: 'asc' | 'desc' | null;
 
    public action = new EventEmitter<IDejaComboListAction<T>>();
 
    public onChangeCallback: (_: any) => void = noop;
    public onTouchedCallback: () => void = noop;
 
    public toggleSelectable(item: T, Eadd = true) {
        const index = this.selectableBuffer.indexOf(item, 0);
        if (index > -1) {
            this.selectableBuffer = this.selectableBuffer.filter((_, idx) => idx !== index);
            this.emit('selectable_deselected', item);
        } else Eif (add) {
            this.selectableBuffer = this.selectableBuffer.concat([item]);
            this.emit('selectable_selected', item);
        }
    }
 
    public toggleSelected(item: T, Eadd = true) {
        const index = this.selectedBuffer.indexOf(item, 0);
        if (index > -1) {
            this.selectedBuffer = this.selectedBuffer.filter((_, idx) => idx !== index);
            this.emit('selected_deselected', item);
        } else Eif (add) {
            this.selectedBuffer = this.selectedBuffer.concat([item]);
            this.emit('selected_selected', item);
        }
    }
 
    public raiseBuffer() {
        this.selectable = this.selectable.filter((i: T) => this.selectableBuffer.indexOf(i, 0) === -1);
        this.selected = this.selectableBuffer.concat(this.selected);
        this.selectableBuffer = [];
        this.sortAll();
        this.emitAndChange('selectable_raised');
    }
 
    public dropBuffer() {
        this.selected = this.selected.filter((i: T) => this.selectedBuffer.indexOf(i, 0) === -1);
        this.selectable = this.selectedBuffer.concat(this.selectable);
        this.selectedBuffer = [];
        this.sortAll();
        this.emitAndChange('selected_dropped');
    }
 
    public raiseOne(item: T) {
        this.selectable = this.selectable.filter((i: T) => i !== item);
        this.selectableBuffer = this.selectableBuffer.filter((i: T) => i !== item);
        Eif (this.selected.indexOf(item, 0) === -1) {
            this.selected = [item].concat(this.selected);
        }
        this.sortAll();
        this.emitAndChange('raised_one', item);
    }
 
    public dropOne(item: T) {
        this.selected = this.selected.filter((i: T) => i !== item);
        this.selectedBuffer = this.selectedBuffer.filter((i: T) => i !== item);
        Eif (this.selectable.indexOf(item, 0) === -1) {
            this.selectable = [item].concat(this.selectable);
        }
        this.sortAll();
        this.emitAndChange('dropped_one', item);
    }
 
    public raiseAll() {
        this.selected = this.selectable.concat(this.selected);
        this.selectable = [];
        this.selectableBuffer = [];
        this.sortAll();
        this.emitAndChange('raised_all');
    }
 
    public dropAll() {
        this.selectable = this.selected.concat(this.selectable);
        this.selected = [];
        this.selectedBuffer = [];
        this.sortAll();
        this.emitAndChange('dropped_all');
    }
 
    public sortAll() {
        this.sort(this.selectable);
        this.sort(this.selected);
        this.onChangeCallback(this.selected);
    }
 
    private sort(aItem: T[]): T[] {
        if (!this.sortDirection) {
            return aItem;
        }
        const fieldname = this.labelFieldName;
        const coeff = this.sortDirection === 'asc' ? 1 : -1;
        return aItem.sort((a, b) => {
            if (a[fieldname] < b[fieldname]) { return -1 * coeff; }
            Eif (a[fieldname] > b[fieldname]) { return 1 * coeff; }
            return 0;
        });
    }
 
    private emitAndChange(type: string, currentItem: T = null, EselectedItems = this.selected) {
        this.onChangeCallback(this.selected);
        this.emit(type, currentItem, selectedItems);
    }
 
    private emit(type: string, IcurrentItem: T = null, selectedItems = this.selected) {
        const action: IDejaComboListAction<T> = {
            type,
            payload: {
                currentItem,
                selectedItems,
            }
        };
        this.action.emit(action);
        this.onTouchedCallback();
    }
 
}