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