checkbox.component.ts 2.33 KB
Newer Older
François Agneray's avatar
François Agneray committed
1
2
3
import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { FormControl, FormArray } from '@angular/forms';

Tifenn Guillas's avatar
Tifenn Guillas committed
4
5
import { Criterion, SelectMultipleCriterion } from '../../../store/model';
import { Option } from '../../../../metamodel/model';
François Agneray's avatar
François Agneray committed
6
7
8
9
10
11
12
13

@Component({
    selector: 'app-checkbox',
    templateUrl: 'checkbox.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class CheckboxComponent {
    @Input() id: number;
François Agneray's avatar
François Agneray committed
14
    @Input() operator: string;
François Agneray's avatar
François Agneray committed
15
16
17
18
19
20
21
22
23
24
    @Input() label: string;
    @Input()
    set options(options: Option[]) {
        this._options = options;
        this.setCheckboxes(options);
    }
    @Input()
    set criterion(criterion: Criterion) {
        this.getDefault(criterion);
    }
François Agneray's avatar
François Agneray committed
25
    @Output() add: EventEmitter<SelectMultipleCriterion> = new EventEmitter();
26
    @Output() delete: EventEmitter<number> = new EventEmitter();
François Agneray's avatar
François Agneray committed
27
28

    checkboxes: FormArray;
François Agneray's avatar
François Agneray committed
29
    checkboxesFormControls: {value: string, control: FormControl}[];
François Agneray's avatar
François Agneray committed
30
31
32
33
34
35
36
37
38
    _options: Option[];

    addCriterion() {
        const values: Option[] = [];
        this.checkboxes.controls.forEach((formControl, i) => {
            if (formControl.value) {
                values.push(this._options[i]);
            }
        });
François Agneray's avatar
François Agneray committed
39
        const cb = new SelectMultipleCriterion(this.id, this.operator, values);
François Agneray's avatar
François Agneray committed
40
41
42
        this.add.emit(cb);
    }

43
44
45
46
    deleteCriterion() {
        this.delete.emit(this.id);
    }

François Agneray's avatar
François Agneray committed
47
    setCheckboxes(options: Option[]) {
François Agneray's avatar
François Agneray committed
48
49
50
51
52
        this.checkboxesFormControls = [];
        this.checkboxes = new FormArray(options.map((option: Option) => {
            const control = new FormControl(false);
            this.checkboxesFormControls.push({value: option.value, control});
            return control;
François Agneray's avatar
François Agneray committed
53
54
55
56
57
58
59
60
        }));
    }

    getDefault(criterion: Criterion): void {
        if (!criterion) {
            this.checkboxes.reset();
            this.checkboxes.enable();
        } else {
François Agneray's avatar
François Agneray committed
61
            const checkboxCriterion = criterion as SelectMultipleCriterion;
62
            checkboxCriterion.options.forEach(option => {
François Agneray's avatar
François Agneray committed
63
                this.checkboxesFormControls.find(formControl => formControl.value === option.value).control.setValue(true);
Tifenn Guillas's avatar
Tifenn Guillas committed
64
            });
François Agneray's avatar
François Agneray committed
65
66
67
68
69
70
71
72
            this.checkboxes.disable();
        }
    }

    isChecked() {
        return this.checkboxes.controls.filter(formControl => formControl.value).length > 0;
    }
}