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