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";
9 selector: 'capabilities-editor',
10 templateUrl: './capabilities-editor.component.html',
11 styleUrls: ['./capabilities-editor.component.less'],
12 providers: [ServiceServiceNg2]
15 export class CapabilitiesEditorComponent {
18 capability: Capability,
19 capabilityTypesList: Array<CapabilityTypeModel>,
22 capabilityData: Capability;
23 capabilityTypesMappedList: Array<DropdownValue>;
24 isUnboundedChecked: boolean;
26 translatedUnboundTxt: string;
28 public onValidationChange: Subject<boolean> = new Subject();
30 constructor(private translateService: TranslateService) {
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;
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();
47 onUnboundedChanged() {
48 this.isUnboundedChecked = !this.isUnboundedChecked;
49 this.capabilityData.maxOccurrences = this.isUnboundedChecked ? this.translatedUnboundTxt : null;
50 this.validityChanged();
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 &&
57 this.isUnboundedChecked ||
58 (this.capabilityData.maxOccurrences && (this.capabilityData.minOccurrences <= parseInt(this.capabilityData.maxOccurrences)))
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
73 this.validityChanged();
76 validityChanged = () => {
77 let validState = this.checkFormValidForSubmit();
78 this.onValidationChange.next(validState);