aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/req-and-capabilities-editor/capabilities-editor/capabilities-editor.component.ts
blob: 32a73efa8546956e47fe8fd66f5ed2ebfe146c2b (plain)
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
import {Component} from '@angular/core';
import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
import {Capability, CapabilityTypeModel} from 'app/models';
import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
import {TranslateService} from 'app/ng2/shared/translator/translate.service';

@Component({
    selector: 'capabilities-editor',
    templateUrl: './capabilities-editor.component.html',
    styleUrls: ['./capabilities-editor.component.less'],
    providers: [ServiceServiceNg2]
})

export class CapabilitiesEditorComponent {
    input: {
        capability: Capability,
        capabilityTypesList: Array<CapabilityTypeModel>,
        isReadonly: boolean;
        validityChangedCallback: Function;
    };
    capabilityData: Capability;
    capabilityTypesMappedList: Array<DropdownValue>;
    isUnboundedChecked: boolean;
    isReadonly: boolean;
    translatedUnboundTxt: string;

    constructor(private translateService: TranslateService) {
    }

    ngOnInit() {
        this.capabilityData = new Capability(this.input.capability);
        this.translatedUnboundTxt = '';
        this.capabilityData.minOccurrences = this.capabilityData.minOccurrences || 0;
        this.translateService.languageChangedObservable.subscribe(lang => {
            this.translatedUnboundTxt = this.translateService.translate('REQ_CAP_OCCURRENCES_UNBOUNDED');
            this.capabilityData.maxOccurrences = this.capabilityData.maxOccurrences || this.translatedUnboundTxt;
            this.isUnboundedChecked = this.capabilityData.maxOccurrences === this.translatedUnboundTxt;
        });
        this.capabilityTypesMappedList = _.map(this.input.capabilityTypesList, capType => new DropdownValue(capType.toscaPresentation.type, capType.toscaPresentation.type));
        this.isReadonly = this.input.isReadonly;
        this.validityChanged();
    }

    onUnboundedChanged() {
        this.isUnboundedChecked = !this.isUnboundedChecked;
        this.capabilityData.maxOccurrences = this.isUnboundedChecked ? this.translatedUnboundTxt : null;
        this.validityChanged();
    }

    checkFormValidForSubmit() {
        return this.capabilityData.name && this.capabilityData.name.length &&
            this.capabilityData.type && this.capabilityData.type.length && !_.isEqual(this.capabilityData.minOccurrences, "") && this.capabilityData.minOccurrences >= 0 &&
            (
                this.isUnboundedChecked ||
                (this.capabilityData.maxOccurrences && (this.capabilityData.minOccurrences <= parseInt(this.capabilityData.maxOccurrences)))
            );
    }

    onSelectCapabilityType(selectedCapType: DropdownValue) {
        this.capabilityData.type = selectedCapType && selectedCapType.value;
        if (selectedCapType && selectedCapType.value) {
            let selectedCapabilityTypeObj: CapabilityTypeModel = this.input.capabilityTypesList.find(capType => capType.toscaPresentation.type === selectedCapType.value);
            this.capabilityData.description = selectedCapabilityTypeObj.toscaPresentation.description;
            this.capabilityData.validSourceTypes = selectedCapabilityTypeObj.toscaPresentation.validTargetTypes;
            this.capabilityData.properties = _.forEach(
                _.toArray(selectedCapabilityTypeObj.properties),
                prop => prop.uniqueId = null //a requirement for the BE
            );
        }
        this.validityChanged();
    }

    validityChanged = () => {
        let validState = this.checkFormValidForSubmit();
        this.input.validityChangedCallback(validState);
    }
}