UI support for custom functions
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / tosca-function / tosca-custom-function / tosca-custom-function.component.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2023 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
23 import {FormArray, FormControl, FormGroup, Validators} from "@angular/forms";
24 import {ToscaCustomFunction} from "../../../../../models/tosca-custom-function";
25 import {ToscaFunctionParameter} from "../../../../../models/tosca-function-parameter";
26 import {ToscaStringParameter} from "../../../../../models/tosca-string-parameter";
27 import {ToscaFunctionType} from "../../../../../models/tosca-function-type.enum";
28 import {PropertyBEModel} from "../../../../../models/properties-inputs/property-be-model";
29 import {PROPERTY_TYPES} from "../../../../../utils/constants";
30 import {InstanceFeDetails} from "../../../../../models/instance-fe-details";
31 import {ToscaFunctionValidationEvent} from "../tosca-function.component";
32 import {ToscaFunction} from "../../../../../models/tosca-function";
33
34 @Component({
35     selector: 'app-tosca-custom-function',
36     templateUrl: './tosca-custom-function.component.html',
37     styleUrls: ['./tosca-custom-function.component.less']
38 })
39 export class ToscaCustomFunctionComponent implements OnInit {
40
41     @Input() toscaCustomFunction: ToscaCustomFunction;
42     @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
43     @Output() onValidFunction: EventEmitter<ToscaCustomFunction> = new EventEmitter<ToscaCustomFunction>();
44     @Output() onValidityChange: EventEmitter<ToscaCustomFunctionValidationEvent> = new EventEmitter<ToscaCustomFunctionValidationEvent>();
45
46     name: string = '';
47     customFunctionFormName: FormControl = new FormControl('', [Validators.required, Validators.minLength(1)]);
48     customParameterFormArray: FormArray = new FormArray([], Validators.minLength(1));
49     formGroup: FormGroup = new FormGroup(
50         {
51             'customParameterList': this.customParameterFormArray,
52             'customName': this.customFunctionFormName
53         }
54     );
55
56     parameters: ToscaFunctionParameter[] = [];
57     propertyInputList: Array<PropertyBEModel> = [];
58
59     STRING_FUNCTION_TYPE = ToscaFunctionType.STRING
60
61     ngOnInit() {
62         this.initForm();
63     }
64
65     private initForm(): void {
66         this.formGroup.valueChanges.subscribe(() => {
67             this.onValidityChange.emit({
68                 isValid: this.formGroup.valid,
69                 toscaCustomFunction: this.formGroup.valid ? this.buildCustomFunctionFromForm() : undefined
70             })
71             if (this.formGroup.valid) {
72                 this.onValidFunction.emit(this.buildCustomFunctionFromForm());
73             }
74         });
75         if (!this.toscaCustomFunction) {
76             return;
77         }
78         if (this.toscaCustomFunction.parameters) {
79             this.name = this.toscaCustomFunction.name;
80             this.customFunctionFormName.setValue(this.name)
81             this.parameters = Array.from(this.toscaCustomFunction.parameters);
82             for (const parameter of this.parameters) {
83                 if (parameter.type !== PROPERTY_TYPES.STRING) {
84                     const propertyBEModel = this.createProperty(parameter.value);
85                     propertyBEModel.toscaFunction = <ToscaFunction> parameter;
86                     this.propertyInputList.push(propertyBEModel);
87                     this.customParameterFormArray.push(
88                         new FormControl(parameter, [Validators.required, Validators.minLength(1)])
89                     );
90                 } else {
91                     this.propertyInputList.push(undefined);
92                     this.customParameterFormArray.push(
93                         new FormControl(parameter.value, [Validators.required, Validators.minLength(1)])
94                     );
95                 }
96             }
97         }
98     }
99
100     private buildCustomFunctionFromForm(): ToscaCustomFunction {
101         const toscaCustomFunction1 = new ToscaCustomFunction();
102         toscaCustomFunction1.name = this.customFunctionFormName.value;
103         this.customParameterFormArray.controls.forEach(control => {
104             const value = control.value;
105             if (typeof value === 'string') {
106                 const stringParameter = new ToscaStringParameter();
107                 stringParameter.value = value;
108                 toscaCustomFunction1.parameters.push(stringParameter);
109             } else {
110                 toscaCustomFunction1.parameters.push(control.value);
111             }
112         });
113
114         return toscaCustomFunction1;
115     }
116
117     addFunction(): void {
118         this.propertyInputList.push(this.createProperty());
119         this.parameters.push({} as ToscaFunctionParameter);
120         this.customParameterFormArray.push(
121             new FormControl(undefined, [Validators.required, Validators.minLength(1)])
122         );
123     }
124
125     addStringParameter(): void {
126         const toscaStringParameter = new ToscaStringParameter();
127         toscaStringParameter.value = ''
128         this.parameters.push(toscaStringParameter);
129         this.propertyInputList.push(undefined);
130         this.customParameterFormArray.push(
131             new FormControl('', [Validators.required, Validators.minLength(1)])
132         );
133     }
134
135     removeParameter(position): void {
136         this.propertyInputList.splice(position, 1);
137         this.parameters.splice(position, 1);
138         this.customParameterFormArray.removeAt(position);
139     }
140
141     createProperty(value?: any): PropertyBEModel {
142         const property = new PropertyBEModel();
143         property.type = PROPERTY_TYPES.ANY;
144         property.value = value ? value : undefined;
145         return property;
146     }
147
148     onFunctionValidityChange(event: ToscaFunctionValidationEvent, index: number): void {
149         if (event.isValid && event.toscaFunction) {
150             this.customParameterFormArray.controls[index].setValue(event.toscaFunction)
151         } else {
152             this.customParameterFormArray.controls[index].setValue(undefined);
153         }
154     }
155 }
156
157 export interface ToscaCustomFunctionValidationEvent {
158     isValid: boolean,
159     toscaCustomFunction: ToscaCustomFunction,
160 }