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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
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";
35 selector: 'app-tosca-custom-function',
36 templateUrl: './tosca-custom-function.component.html',
37 styleUrls: ['./tosca-custom-function.component.less']
39 export class ToscaCustomFunctionComponent implements OnInit {
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>();
47 customFunctionFormName: FormControl = new FormControl('', [Validators.required, Validators.minLength(1)]);
48 customParameterFormArray: FormArray = new FormArray([], Validators.minLength(1));
49 formGroup: FormGroup = new FormGroup(
51 'customParameterList': this.customParameterFormArray,
52 'customName': this.customFunctionFormName
56 parameters: ToscaFunctionParameter[] = [];
57 propertyInputList: Array<PropertyBEModel> = [];
59 STRING_FUNCTION_TYPE = ToscaFunctionType.STRING
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
71 if (this.formGroup.valid) {
72 this.onValidFunction.emit(this.buildCustomFunctionFromForm());
75 if (!this.toscaCustomFunction) {
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)])
91 this.propertyInputList.push(undefined);
92 this.customParameterFormArray.push(
93 new FormControl(parameter.value, [Validators.required, Validators.minLength(1)])
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);
110 toscaCustomFunction1.parameters.push(control.value);
114 return toscaCustomFunction1;
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)])
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)])
135 removeParameter(position): void {
136 this.propertyInputList.splice(position, 1);
137 this.parameters.splice(position, 1);
138 this.customParameterFormArray.removeAt(position);
141 createProperty(value?: any): PropertyBEModel {
142 const property = new PropertyBEModel();
143 property.type = PROPERTY_TYPES.ANY;
144 property.value = value ? value : undefined;
148 onFunctionValidityChange(event: ToscaFunctionValidationEvent, index: number): void {
149 if (event.isValid && event.toscaFunction) {
150 this.customParameterFormArray.controls[index].setValue(event.toscaFunction)
152 this.customParameterFormArray.controls[index].setValue(undefined);
157 export interface ToscaCustomFunctionValidationEvent {
159 toscaCustomFunction: ToscaCustomFunction,