Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / req-and-capabilities / capabilities / capabilityEditor / capabilities-editor.component.ts
1 import {Component} from '@angular/core';
2 import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
3 import {Capability, CapabilityTypeModel} from 'app/models';
4 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
5 import {TranslateService} from 'app/ng2/shared/translator/translate.service';
6 import {Subject} from "rxjs";
7
8 @Component({
9     selector: 'capabilities-editor',
10     templateUrl: './capabilities-editor.component.html',
11     styleUrls: ['./capabilities-editor.component.less'],
12     providers: [ServiceServiceNg2]
13 })
14
15 export class CapabilitiesEditorComponent {
16     input: {
17         test: string,
18         capability: Capability,
19         capabilityTypesList: Array<CapabilityTypeModel>,
20         isReadonly: boolean;
21     };
22     capabilityData: Capability;
23     capabilityTypesMappedList: Array<DropdownValue>;
24     isUnboundedChecked: boolean;
25     isReadonly: boolean;
26     translatedUnboundTxt: string;
27
28     public onValidationChange: Subject<boolean> = new Subject();
29
30     constructor(private translateService: TranslateService) {
31     }
32
33     ngOnInit() {
34         this.capabilityData = new Capability(this.input.capability);
35         this.translatedUnboundTxt = '';
36         this.capabilityData.minOccurrences = this.capabilityData.minOccurrences || 0;
37         this.translateService.languageChangedObservable.subscribe(lang => {
38             this.translatedUnboundTxt = this.translateService.translate('REQ_CAP_OCCURRENCES_UNBOUNDED');
39             this.capabilityData.maxOccurrences = this.capabilityData.maxOccurrences || this.translatedUnboundTxt;
40             this.isUnboundedChecked = this.capabilityData.maxOccurrences === this.translatedUnboundTxt;
41         });
42         this.capabilityTypesMappedList = _.map(this.input.capabilityTypesList, capType => new DropdownValue(capType.toscaPresentation.type, capType.toscaPresentation.type));
43         this.isReadonly = this.input.isReadonly;
44         this.validityChanged();
45     }
46
47     onUnboundedChanged() {
48         this.isUnboundedChecked = !this.isUnboundedChecked;
49         this.capabilityData.maxOccurrences = this.isUnboundedChecked ? this.translatedUnboundTxt : null;
50         this.validityChanged();
51     }
52
53     checkFormValidForSubmit() {
54         return this.capabilityData.name && this.capabilityData.name.length &&
55             this.capabilityData.type && this.capabilityData.type.length && !_.isEqual(this.capabilityData.minOccurrences, "") && this.capabilityData.minOccurrences >= 0 &&
56             (
57                 this.isUnboundedChecked ||
58                 (this.capabilityData.maxOccurrences && (this.capabilityData.minOccurrences <= parseInt(this.capabilityData.maxOccurrences)))
59             );
60     }
61
62     onSelectCapabilityType(selectedCapType: DropdownValue) {
63         this.capabilityData.type = selectedCapType && selectedCapType.value;
64         if (selectedCapType && selectedCapType.value) {
65             let selectedCapabilityTypeObj: CapabilityTypeModel = this.input.capabilityTypesList.find(capType => capType.toscaPresentation.type === selectedCapType.value);
66             this.capabilityData.description = selectedCapabilityTypeObj.toscaPresentation.description;
67             this.capabilityData.validSourceTypes = selectedCapabilityTypeObj.toscaPresentation.validTargetTypes;
68             this.capabilityData.properties = _.forEach(
69                 _.toArray(selectedCapabilityTypeObj.properties),
70                 prop => prop.uniqueId = null //a requirement for the BE
71             );
72         }
73         this.validityChanged();
74     }
75
76     validityChanged = () => {
77         let validState = this.checkFormValidForSubmit();
78         this.onValidationChange.next(validState);
79     }
80
81 }