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";
9 selector: 'requirements-editor',
10 templateUrl: 'requirements-editor.component.html',
11 styleUrls: ['requirements-editor.component.less'],
12 providers: [ServiceServiceNg2, TranslateService]
15 export class RequirementsEditorComponent {
18 requirement: Requirement,
19 relationshipTypesList: Array<RelationshipTypeModel>;
20 nodeTypesList: Array<NodeTypeModel>;
21 capabilityTypesList: Array<CapabilityTypeModel>;
24 requirementData: Requirement;
25 capabilityTypesMappedList: Array<DropdownValue>;
26 relationshipTypesMappedList: Array<DropdownValue>;
27 nodeTypesMappedList: Array<DropdownValue>;
28 isUnboundedChecked: boolean;
30 translatedUnboundTxt: string;
32 public onValidationChange: Subject<boolean> = new Subject();
34 constructor(private translateService: TranslateService) {
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)
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;
53 this.isReadonly = this.input.isReadonly;
54 this.validityChanged();
57 onUnboundedChanged() {
58 this.isUnboundedChecked = !this.isUnboundedChecked;
59 this.requirementData.maxOccurrences = this.isUnboundedChecked ? this.translatedUnboundTxt : null;
60 this.validityChanged();
63 onCapabilityChanged(selectedCapability: DropdownValue) {
64 this.requirementData.capability = selectedCapability && selectedCapability.value;
65 this.validityChanged();
68 onNodeChanged(selectedNode: DropdownValue) {
69 this.requirementData.node = selectedNode && selectedNode.value;
72 onRelationshipChanged(selectedRelationship: DropdownValue) {
73 this.requirementData.relationship = selectedRelationship && selectedRelationship.value;
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 &&
80 this.isUnboundedChecked ||
81 (this.requirementData.maxOccurrences && (this.requirementData.minOccurrences <= parseInt(this.requirementData.maxOccurrences)))
85 validityChanged = () => {
86 let validState = this.checkFormValidForSubmit();
87 this.onValidationChange.next(validState);