82e2e464cc95fb3821bcc96791f28135fc1902a7
[sdc.git] /
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
7 @Component({
8     selector: 'capabilities-editor',
9     templateUrl: './capabilities-editor.component.html',
10     styleUrls: ['./capabilities-editor.component.less'],
11     providers: [ServiceServiceNg2]
12 })
13
14 export class CapabilitiesEditorComponent {
15     input: {
16         capability: Capability,
17         capabilityTypesList: Array<CapabilityTypeModel>,
18         isReadonly: boolean;
19         validityChangedCallback: Function;
20     };
21     capabilityData: Capability;
22     capabilityTypesMappedList: Array<DropdownValue>;
23     isUnboundedChecked: boolean;
24     isReadonly: boolean;
25     translatedUnboundTxt: string;
26
27     constructor(private translateService: TranslateService) {
28     }
29
30     ngOnInit() {
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;
38         });
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();
42     }
43
44     onUnboundedChanged() {
45         this.isUnboundedChecked = !this.isUnboundedChecked;
46         this.capabilityData.maxOccurrences = this.isUnboundedChecked ? this.translatedUnboundTxt : null;
47         this.validityChanged();
48     }
49
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 &&
53             (
54                 this.isUnboundedChecked ||
55                 (this.capabilityData.maxOccurrences && (this.capabilityData.minOccurrences <= parseInt(this.capabilityData.maxOccurrences)))
56             );
57     }
58
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;
65         }
66         this.validityChanged();
67     }
68
69     validityChanged = () => {
70         let validState = this.checkFormValidForSubmit();
71         this.input.validityChangedCallback(validState);
72     }
73 }