form-group.component.ts 2.06 KB
Newer Older
François Agneray's avatar
François Agneray committed
1
import { Component, Input, Output, EventEmitter, ViewChild, SimpleChanges, OnChanges } from '@angular/core';
2
3
4
5
6
7
8
9
import { NgForm } from '@angular/forms';

import { Group, Dataset } from '../../store/model';

@Component({
    selector: 'app-form-group',
    templateUrl: 'form-group.component.html'
})
François Agneray's avatar
François Agneray committed
10
11
export class FormGroupComponent implements OnChanges {
    @ViewChild(NgForm, { static: true }) ngForm: NgForm;
12
13
14
    @Input() model: Group = new Group();
    @Input() datasetList: Dataset[];
    @Output() submitted: EventEmitter<Group> = new EventEmitter();
François Agneray's avatar
François Agneray committed
15
16
17
18
19
20
21
22
    public availableDatasets: string[];
    public groupDatasets: string[] = [];

    ngOnChanges(changes: SimpleChanges) {
        if (changes.model && changes.model.currentValue) {
            this.groupDatasets = [...this.model.datasets];
        }
    }
23
24

    emit(group: Group) {
François Agneray's avatar
François Agneray committed
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
        this.submitted.emit({
            id: this.model.id,
            label: group.label,
            instance_name: this.model.instance_name,
            datasets: this.groupDatasets
        });
    }

    getAvailableDatasets() {
        return this.datasetList.filter(d => !this.groupDatasets.includes(d.name));
    }

    addDatasets(selectElement) {
        let availableDatasetsSelected = [];
        for (var i = 0; i < selectElement.options.length; i++) {
            const optionElement = selectElement.options[i];
            if (optionElement.selected == true) {
                availableDatasetsSelected.push(optionElement.value);
            }
        }
        this.groupDatasets.push(...availableDatasetsSelected);
        this.ngForm.control.markAsDirty();
    }

    removeDatasets(selectElement) {
        let groupDatasetsSelected = [];
        for (var i = 0; i < selectElement.options.length; i++) {
            const optionElement = selectElement.options[i];
            if (optionElement.selected == true) {
                groupDatasetsSelected.push(optionElement.value);
            }
        }
        this.groupDatasets = [...this.groupDatasets.filter(d => !groupDatasetsSelected.includes(d))]
        this.ngForm.control.markAsDirty();
59
60
    }
}