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';
8 selector: 'capabilities-editor',
9 templateUrl: './capabilities-editor.component.html',
10 styleUrls: ['./capabilities-editor.component.less'],
11 providers: [ServiceServiceNg2]
14 export class CapabilitiesEditorComponent {
16 capability: Capability,
17 capabilityTypesList: Array<CapabilityTypeModel>,
19 validityChangedCallback: Function;
21 capabilityData: Capability;
22 capabilityTypesMappedList: Array<DropdownValue>;
23 isUnboundedChecked: boolean;
25 translatedUnboundTxt: string;
27 constructor(private translateService: TranslateService) {
31 this.capabilityData = new Capability(this.input.capability);
32 this.translatedUnboundTxt = '';
33 this.capabilityData.minOccurrences = this.capabilityData.minOccurrences || 0;
34 this.translateService.languageChangedObservable.subscribe(lang => {
35 this.translatedUnboundTxt = this.translateService.translate('REQ_CAP_OCCURRENCES_UNBOUNDED');
36 this.capabilityData.maxOccurrences = this.capabilityData.maxOccurrences || this.translatedUnboundTxt;
37 this.isUnboundedChecked = this.capabilityData.maxOccurrences === this.translatedUnboundTxt;
39 this.capabilityTypesMappedList = _.map(this.input.capabilityTypesList, capType => new DropdownValue(capType.toscaPresentation.type, capType.toscaPresentation.type));
40 this.isReadonly = this.input.isReadonly;
41 this.validityChanged();
44 onUnboundedChanged() {
45 this.isUnboundedChecked = !this.isUnboundedChecked;
46 this.capabilityData.maxOccurrences = this.isUnboundedChecked ? this.translatedUnboundTxt : null;
47 this.validityChanged();
50 checkFormValidForSubmit() {
51 return this.capabilityData.name && this.capabilityData.name.length &&
52 this.capabilityData.type && this.capabilityData.type.length && !_.isEqual(this.capabilityData.minOccurrences, "") && this.capabilityData.minOccurrences >= 0 &&
54 this.isUnboundedChecked ||
55 (this.capabilityData.maxOccurrences && (this.capabilityData.minOccurrences <= parseInt(this.capabilityData.maxOccurrences)))
59 onSelectCapabilityType(selectedCapType: DropdownValue) {
60 this.capabilityData.type = selectedCapType && selectedCapType.value;
61 if (selectedCapType && selectedCapType.value) {
62 let selectedCapabilityTypeObj: CapabilityTypeModel = this.input.capabilityTypesList.find(capType => capType.toscaPresentation.type === selectedCapType.value);
63 this.capabilityData.description = selectedCapabilityTypeObj.toscaPresentation.description;
64 this.capabilityData.validSourceTypes = selectedCapabilityTypeObj.toscaPresentation.validTargetTypes;
66 this.validityChanged();
69 validityChanged = () => {
70 let validState = this.checkFormValidForSubmit();
71 this.input.validityChangedCallback(validState);