d808c284a8f8f0a68ba6c113f23b52f957f66c90
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / tosca-function / tosca-concat-function / tosca-concat-function.component.ts
1 import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
2 import {FormArray, FormControl, FormGroup, Validators} from "@angular/forms";
3 import {ToscaConcatFunction} from "../../../../../models/tosca-concat-function";
4 import {ToscaFunctionParameter} from "../../../../../models/tosca-function-parameter";
5 import {ToscaStringParameter} from "../../../../../models/tosca-string-parameter";
6 import {ToscaFunctionType} from "../../../../../models/tosca-function-type.enum";
7 import {PropertyBEModel} from "../../../../../models/properties-inputs/property-be-model";
8 import {PROPERTY_TYPES} from "../../../../../utils/constants";
9 import {InstanceFeDetails} from "../../../../../models/instance-fe-details";
10 import {ToscaFunctionValidationEvent} from "../tosca-function.component";
11
12 @Component({
13     selector: 'app-tosca-concat-function',
14     templateUrl: './tosca-concat-function.component.html',
15     styleUrls: ['./tosca-concat-function.component.less']
16 })
17 export class ToscaConcatFunctionComponent implements OnInit {
18
19     @Input() toscaConcatFunction: ToscaConcatFunction;
20     @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
21     @Output() onValidFunction: EventEmitter<ToscaConcatFunction> = new EventEmitter<ToscaConcatFunction>();
22     @Output() onValidityChange: EventEmitter<ToscaConcatFunctionValidationEvent> = new EventEmitter<ToscaConcatFunctionValidationEvent>();
23
24     concatParameterFormArray: FormArray = new FormArray([], Validators.minLength(2));
25     formGroup: FormGroup = new FormGroup(
26         {
27             'concatParameterList': this.concatParameterFormArray
28         }
29     );
30
31     parameters: ToscaFunctionParameter[] = [];
32     propertyInputList: Array<PropertyBEModel> = [];
33
34     stringProperty: PropertyBEModel
35
36     STRING_FUNCTION_TYPE = ToscaFunctionType.STRING
37
38     constructor() {
39         this.stringProperty = new PropertyBEModel();
40         this.stringProperty.type = PROPERTY_TYPES.STRING
41     }
42
43     ngOnInit() {
44         this.initForm();
45     }
46
47     private initForm() {
48         this.formGroup.valueChanges.subscribe(() => {
49             this.onValidityChange.emit({
50                 isValid: this.formGroup.valid,
51                 toscaConcatFunction: this.formGroup.valid ? this.buildConcatFunctionFromForm() : undefined
52             })
53             if (this.formGroup.valid) {
54                 this.onValidFunction.emit(this.buildConcatFunctionFromForm());
55             }
56         });
57         if (!this.toscaConcatFunction) {
58             return;
59         }
60
61         if (this.toscaConcatFunction.parameters) {
62             this.parameters = Array.from(this.toscaConcatFunction.parameters);
63             for (const parameter of this.parameters) {
64                 if (parameter.type !== PROPERTY_TYPES.STRING) {
65                     this.propertyInputList.push(this.createStringProperty(parameter));
66                     this.concatParameterFormArray.push(
67                         new FormControl(parameter, [Validators.required, Validators.minLength(1)])
68                     );
69                 } else {
70                     this.propertyInputList.push(undefined);
71                     this.concatParameterFormArray.push(
72                         new FormControl(parameter.value, [Validators.required, Validators.minLength(1)])
73                     );
74                 }
75             }
76         }
77     }
78
79     private buildConcatFunctionFromForm(): ToscaConcatFunction {
80         const toscaConcatFunction1 = new ToscaConcatFunction();
81         this.concatParameterFormArray.controls.forEach(control => {
82             const value = control.value;
83             if (typeof value === 'string') {
84                 const stringParameter = new ToscaStringParameter();
85                 stringParameter.value = value;
86                 toscaConcatFunction1.parameters.push(stringParameter);
87             } else {
88                 toscaConcatFunction1.parameters.push(control.value);
89             }
90         });
91
92         return toscaConcatFunction1;
93     }
94
95     addFunction() {
96         this.propertyInputList.push(this.createStringProperty());
97         this.parameters.push({} as ToscaFunctionParameter);
98         this.concatParameterFormArray.push(
99             new FormControl(undefined, [Validators.required, Validators.minLength(1)])
100         );
101     }
102
103     addStringParameter() {
104         this.parameters.push({
105             type: ToscaFunctionType.STRING,
106             value: ''
107         });
108         this.propertyInputList.push(undefined);
109         this.concatParameterFormArray.push(
110             new FormControl('', [Validators.required, Validators.minLength(1)])
111         );
112     }
113
114     removeParameter(position) {
115         this.propertyInputList.splice(position, 1);
116         this.parameters.splice(position, 1);
117         this.concatParameterFormArray.removeAt(position);
118     }
119
120     createStringProperty(toscaFunctionParameter?: ToscaFunctionParameter) {
121         const property = new PropertyBEModel();
122         property.type = PROPERTY_TYPES.STRING;
123         property.toscaFunction = toscaFunctionParameter ? toscaFunctionParameter : undefined;
124         property.value = toscaFunctionParameter ? toscaFunctionParameter.value : undefined;
125         return property;
126     }
127
128     onFunctionValidityChange(event: ToscaFunctionValidationEvent, index: number) {
129         if (event.isValid && event.toscaFunction) {
130             this.concatParameterFormArray.controls[index].setValue(event.toscaFunction)
131         } else {
132             this.concatParameterFormArray.controls[index].setValue(undefined);
133         }
134     }
135 }
136
137 export interface ToscaConcatFunctionValidationEvent {
138     isValid: boolean,
139     toscaConcatFunction: ToscaConcatFunction,
140 }