7746d3845f86889934d1a31a592128815b303195
[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 import {CustomToscaFunction} from "../../../../../models/default-custom-functions";
34
35 @Component({
36     selector: 'app-tosca-custom-function',
37     templateUrl: './tosca-custom-function.component.html',
38     styleUrls: ['./tosca-custom-function.component.less']
39 })
40 export class ToscaCustomFunctionComponent implements OnInit {
41
42     @Input() toscaCustomFunction: ToscaCustomFunction;
43     @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
44     @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
45     @Input() name: string;
46     @Input() isDefaultCustomFunction: boolean;
47     @Output() onValidFunction: EventEmitter<ToscaCustomFunction> = new EventEmitter<ToscaCustomFunction>();
48     @Output() onValidityChange: EventEmitter<ToscaCustomFunctionValidationEvent> = new EventEmitter<ToscaCustomFunctionValidationEvent>();
49
50     customFunctionFormName: FormControl = new FormControl('', [Validators.required, Validators.minLength(1)]);
51     customParameterFormArray: FormArray = new FormArray([], Validators.minLength(1));
52     formGroup: FormGroup = new FormGroup(
53         {
54             'customParameterList': this.customParameterFormArray,
55             'customName': this.customFunctionFormName
56         }
57     );
58
59     parameters: ToscaFunctionParameter[] = [];
60     propertyInputList: Array<PropertyBEModel> = [];
61
62     STRING_FUNCTION_TYPE = ToscaFunctionType.STRING
63
64     ngOnInit() {
65         this.initForm();
66     }
67
68     ngOnChanges() {
69         if (this.name && this.isDefaultCustomFunction) {
70             this.customFunctionFormName.setValue(this.name);
71             this.emitOnValidityChange();
72         } else {
73             this.name = "";
74         }
75     }
76
77     private initForm(): void {
78         this.formGroup.valueChanges.subscribe(() => {
79             this.emitOnValidityChange();
80             if (this.formGroup.valid) {
81                 this.onValidFunction.emit(this.buildCustomFunctionFromForm());
82             }
83         });
84         if (!this.toscaCustomFunction) {
85             return;
86         }
87         if (this.toscaCustomFunction.parameters) {
88             this.name = this.toscaCustomFunction.name;
89             this.customFunctionFormName.setValue(this.name)
90             this.parameters = Array.from(this.toscaCustomFunction.parameters);
91             for (const parameter of this.parameters) {
92                 if (parameter.type !== PROPERTY_TYPES.STRING) {
93                     const propertyBEModel = this.createProperty(parameter.value);
94                     propertyBEModel.toscaFunction = <ToscaFunction> parameter;
95                     this.propertyInputList.push(propertyBEModel);
96                     this.customParameterFormArray.push(
97                         new FormControl(parameter, [Validators.required, Validators.minLength(1)])
98                     );
99                 } else {
100                     this.propertyInputList.push(undefined);
101                     this.customParameterFormArray.push(
102                         new FormControl(parameter.value, [Validators.required, Validators.minLength(1)])
103                     );
104                 }
105             }
106         }
107     }
108
109     private buildCustomFunctionFromForm(): ToscaCustomFunction {
110         const toscaCustomFunction1 = new ToscaCustomFunction();
111         toscaCustomFunction1.name = this.customFunctionFormName.value;
112         this.customParameterFormArray.controls.forEach(control => {
113             const value = control.value;
114             if (typeof value === 'string') {
115                 const stringParameter = new ToscaStringParameter();
116                 stringParameter.value = value;
117                 toscaCustomFunction1.parameters.push(stringParameter);
118             } else {
119                 toscaCustomFunction1.parameters.push(control.value);
120             }
121         });
122
123         return toscaCustomFunction1;
124     }
125
126     private emitOnValidityChange() {
127         this.onValidityChange.emit({
128             isValid: this.formGroup.valid,
129             toscaCustomFunction: this.formGroup.valid ? this.buildCustomFunctionFromForm() : undefined
130         })
131     }
132
133     addFunction(): void {
134         this.propertyInputList.push(this.createProperty());
135         this.parameters.push({} as ToscaFunctionParameter);
136         this.customParameterFormArray.push(
137             new FormControl(undefined, [Validators.required, Validators.minLength(1)])
138         );
139     }
140
141     addStringParameter(): void {
142         const toscaStringParameter = new ToscaStringParameter();
143         toscaStringParameter.value = ''
144         this.parameters.push(toscaStringParameter);
145         this.propertyInputList.push(undefined);
146         this.customParameterFormArray.push(
147             new FormControl('', [Validators.required, Validators.minLength(1)])
148         );
149     }
150
151     removeParameter(position): void {
152         this.propertyInputList.splice(position, 1);
153         this.parameters.splice(position, 1);
154         this.customParameterFormArray.removeAt(position);
155     }
156
157     createProperty(value?: any): PropertyBEModel {
158         const property = new PropertyBEModel();
159         property.type = PROPERTY_TYPES.ANY;
160         property.value = value ? value : undefined;
161         return property;
162     }
163
164     onFunctionValidityChange(event: ToscaFunctionValidationEvent, index: number): void {
165         if (event.isValid && event.toscaFunction) {
166             this.customParameterFormArray.controls[index].setValue(event.toscaFunction)
167         } else {
168             this.customParameterFormArray.controls[index].setValue(undefined);
169         }
170     }
171 }
172
173 export interface ToscaCustomFunctionValidationEvent {
174     isValid: boolean,
175     toscaCustomFunction: ToscaCustomFunction,
176 }