summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/workspace/req-and-capabilities/requirements/requirementEditor/requirements-editor.component.ts
blob: 2c5c96f3da17f0f97dd87240a4dfdec08b30d877 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
import {Component} from '@angular/core';
import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
import {Requirement, RelationshipTypeModel, NodeTypeModel, CapabilityTypeModel} from 'app/models';
import {TranslateService} from 'app/ng2/shared/translator/translate.service';
import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
import {Subject} from "rxjs";

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

export class RequirementsEditorComponent {

    input: {
        requirement: Requirement,
        relationshipTypesList: Array<RelationshipTypeModel>;
        nodeTypesList: Array<NodeTypeModel>;
        capabilityTypesList: Array<CapabilityTypeModel>;
        isReadonly: boolean;
    };
    requirementData: Requirement;
    capabilityTypesMappedList: Array<DropdownValue>;
    relationshipTypesMappedList: Array<DropdownValue>;
    nodeTypesMappedList: Array<DropdownValue>;
    isUnboundedChecked: boolean;
    isReadonly: boolean;
    translatedUnboundTxt: string;

    public onValidationChange: Subject<boolean> = new Subject();

    constructor(private translateService: TranslateService) {
    }

    ngOnInit() {
        this.requirementData = new Requirement(this.input.requirement);
        this.requirementData.minOccurrences = this.requirementData.minOccurrences || 0;
        this.translatedUnboundTxt = '';
        this.capabilityTypesMappedList = _.map(this.input.capabilityTypesList, capType => new DropdownValue(capType.toscaPresentation.type, capType.toscaPresentation.type));
        this.relationshipTypesMappedList = _.map(this.input.relationshipTypesList, rType => new DropdownValue(rType.toscaPresentation.type, rType.toscaPresentation.type));
        this.nodeTypesMappedList = _.map(this.input.nodeTypesList, nodeType => {
            return new DropdownValue(
                nodeType.componentMetadataDefinition.componentMetadataDataDefinition.toscaResourceName,
                nodeType.componentMetadataDefinition.componentMetadataDataDefinition.toscaResourceName)
        });
        this.translateService.languageChangedObservable.subscribe(lang => {
            this.translatedUnboundTxt = this.translateService.translate('REQ_CAP_OCCURRENCES_UNBOUNDED');
            this.requirementData.maxOccurrences = this.requirementData.maxOccurrences || this.translatedUnboundTxt;
            this.isUnboundedChecked = this.requirementData.maxOccurrences === this.translatedUnboundTxt;
        });
        this.isReadonly = this.input.isReadonly;
        this.validityChanged();
    }

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

    onCapabilityChanged(selectedCapability: DropdownValue) {
        this.requirementData.capability = selectedCapability && selectedCapability.value;
        this.validityChanged();
    }

    onNodeChanged(selectedNode: DropdownValue) {
        this.requirementData.node = selectedNode && selectedNode.value;
    }

    onRelationshipChanged(selectedRelationship: DropdownValue) {
        this.requirementData.relationship = selectedRelationship && selectedRelationship.value;
    }

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

    validityChanged = () => {
        let validState = this.checkFormValidForSubmit();
        this.onValidationChange.next(validState);
    }

}