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";
15 selector: 'app-tosca-concat-function',
16 templateUrl: './tosca-concat-function.component.html',
17 styleUrls: ['./tosca-concat-function.component.less']
19 export class ToscaConcatFunctionComponent implements OnInit {
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>();
27 concatParameterFormArray: FormArray = new FormArray([], Validators.minLength(2));
28 formGroup: FormGroup = new FormGroup(
30 'concatParameterList': this.concatParameterFormArray
34 parameters: ToscaFunctionParameter[] = [];
35 propertyInputList: Array<PropertyBEModel> = [];
36 stringProperty: PropertyBEModel
38 STRING_FUNCTION_TYPE = ToscaFunctionType.STRING
41 this.stringProperty = new PropertyBEModel();
42 this.stringProperty.type = PROPERTY_TYPES.STRING
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
55 if (this.formGroup.valid) {
56 this.onValidFunction.emit(this.buildConcatFunctionFromForm());
59 if (!this.toscaConcatFunction) {
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)])
73 this.propertyInputList.push(undefined);
74 this.concatParameterFormArray.push(
75 new FormControl(parameter.value, [Validators.required, Validators.minLength(1)])
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);
91 toscaConcatFunction1.parameters.push(control.value);
95 return toscaConcatFunction1;
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)])
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)])
116 removeParameter(position): void {
117 this.propertyInputList.splice(position, 1);
118 this.parameters.splice(position, 1);
119 this.concatParameterFormArray.removeAt(position);
122 createStringProperty(value?: any): PropertyBEModel {
123 const property = new PropertyBEModel();
124 property.type = PROPERTY_TYPES.STRING;
125 property.value = value ? value : undefined;
129 onFunctionValidityChange(event: ToscaFunctionValidationEvent, index: number): void {
130 if (event.isValid && event.toscaFunction) {
131 this.concatParameterFormArray.controls[index].setValue(event.toscaFunction)
133 this.concatParameterFormArray.controls[index].setValue(undefined);
138 export interface ToscaConcatFunctionValidationEvent {
140 toscaConcatFunction: ToscaConcatFunction,