Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / req-and-capabilities / requirements / requirementEditor / requirements-editor.component.ts
1 import {Component} from '@angular/core';
2 import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
3 import {Requirement, RelationshipTypeModel, NodeTypeModel, CapabilityTypeModel} from 'app/models';
4 import {TranslateService} from 'app/ng2/shared/translator/translate.service';
5 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
6 import {Subject} from "rxjs";
7
8 @Component({
9     selector: 'requirements-editor',
10     templateUrl: 'requirements-editor.component.html',
11     styleUrls: ['requirements-editor.component.less'],
12     providers: [ServiceServiceNg2, TranslateService]
13 })
14
15 export class RequirementsEditorComponent {
16
17     input: {
18         requirement: Requirement,
19         relationshipTypesList: Array<RelationshipTypeModel>;
20         nodeTypesList: Array<NodeTypeModel>;
21         capabilityTypesList: Array<CapabilityTypeModel>;
22         isReadonly: boolean;
23     };
24     requirementData: Requirement;
25     capabilityTypesMappedList: Array<DropdownValue>;
26     relationshipTypesMappedList: Array<DropdownValue>;
27     nodeTypesMappedList: Array<DropdownValue>;
28     isUnboundedChecked: boolean;
29     isReadonly: boolean;
30     translatedUnboundTxt: string;
31
32     public onValidationChange: Subject<boolean> = new Subject();
33
34     constructor(private translateService: TranslateService) {
35     }
36
37     ngOnInit() {
38         this.requirementData = new Requirement(this.input.requirement);
39         this.requirementData.minOccurrences = this.requirementData.minOccurrences || 0;
40         this.translatedUnboundTxt = '';
41         this.capabilityTypesMappedList = _.map(this.input.capabilityTypesList, capType => new DropdownValue(capType.toscaPresentation.type, capType.toscaPresentation.type));
42         this.relationshipTypesMappedList = _.map(this.input.relationshipTypesList, rType => new DropdownValue(rType.toscaPresentation.type, rType.toscaPresentation.type));
43         this.nodeTypesMappedList = _.map(this.input.nodeTypesList, nodeType => {
44             return new DropdownValue(
45                 nodeType.componentMetadataDefinition.componentMetadataDataDefinition.toscaResourceName,
46                 nodeType.componentMetadataDefinition.componentMetadataDataDefinition.toscaResourceName)
47         });
48         this.translateService.languageChangedObservable.subscribe(lang => {
49             this.translatedUnboundTxt = this.translateService.translate('REQ_CAP_OCCURRENCES_UNBOUNDED');
50             this.requirementData.maxOccurrences = this.requirementData.maxOccurrences || this.translatedUnboundTxt;
51             this.isUnboundedChecked = this.requirementData.maxOccurrences === this.translatedUnboundTxt;
52         });
53         this.isReadonly = this.input.isReadonly;
54         this.validityChanged();
55     }
56
57     onUnboundedChanged() {
58         this.isUnboundedChecked = !this.isUnboundedChecked;
59         this.requirementData.maxOccurrences = this.isUnboundedChecked ? this.translatedUnboundTxt : null;
60         this.validityChanged();
61     }
62
63     onCapabilityChanged(selectedCapability: DropdownValue) {
64         this.requirementData.capability = selectedCapability && selectedCapability.value;
65         this.validityChanged();
66     }
67
68     onNodeChanged(selectedNode: DropdownValue) {
69         this.requirementData.node = selectedNode && selectedNode.value;
70     }
71
72     onRelationshipChanged(selectedRelationship: DropdownValue) {
73         this.requirementData.relationship = selectedRelationship && selectedRelationship.value;
74     }
75
76     checkFormValidForSubmit() {
77         return this.requirementData.name && this.requirementData.name.length &&
78             this.requirementData.capability && this.requirementData.capability.length && !_.isEqual(this.requirementData.minOccurrences, "") && this.requirementData.minOccurrences >= 0 &&
79             (
80                 this.isUnboundedChecked ||
81                 (this.requirementData.maxOccurrences && (this.requirementData.minOccurrences <= parseInt(this.requirementData.maxOccurrences)))
82             );
83     }
84
85     validityChanged = () => {
86         let validState = this.checkFormValidForSubmit();
87         this.onValidationChange.next(validState);
88     }
89
90 }