fa06c3eabcd0524882557110c8b5765fb5c6230d
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / interface-operatons / operation-creator / input-list / input-list.component.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 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, Output} from '@angular/core';
23 import {InputOperationParameter} from "../../../../../../models/interfaceOperation";
24 import {DataTypeModel} from "../../../../../../models/data-types";
25 import {DerivedPropertyType} from "../../../../../../models/properties-inputs/property-be-model";
26 import {PROPERTY_DATA, PROPERTY_TYPES} from "../../../../../../utils/constants";
27 import {InstanceFeDetails} from "../../../../../../models/instance-fe-details";
28
29 @Component({
30   selector: 'input-list',
31   templateUrl: './input-list.component.html',
32   styleUrls: ['./input-list.component.less']
33 })
34 export class InputListComponent {
35
36   @Input() set inputs(inputs: Array<InputOperationParameter>) {
37     this._inputs = new Array<InputOperationParameter>();
38     if (inputs) {
39       inputs.forEach(input => {
40         const inputCopy = new InputOperationParameter(input);
41         this.initValue(inputCopy);
42
43         this._inputs.push(inputCopy);
44       });
45     }
46   }
47   @Input() dataTypeMap: Map<string, DataTypeModel>;
48   @Input() isViewOnly: boolean;
49   @Input() title: string;
50   @Input() emptyMessage: string;
51   @Input() showToscaFunctionOption: boolean = false;
52   @Input() allowDeletion: boolean = false;
53   @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map();
54   @Output('onInputsValidityChange') inputsValidityChangeEvent: EventEmitter<boolean> = new EventEmitter<boolean>();
55   @Output('onValueChange') inputValueChangeEvent: EventEmitter<InputOperationParameter> = new EventEmitter<InputOperationParameter>();
56   @Output('onDelete') inputDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
57
58   _inputs: Array<InputOperationParameter> = [];
59
60   getDataType(type: string): DataTypeModel {
61     return this.dataTypeMap.get(type);
62   }
63
64   private initValue(input: InputOperationParameter): void {
65     if (input.value) {
66       try {
67         input.value = JSON.parse(input.value);
68       } catch (e) {
69         console.debug('Could not parse value', input.value, e);
70       }
71       return;
72     }
73
74     if (input.toscaDefaultValue) {
75       try {
76         input.value = JSON.parse(input.toscaDefaultValue);
77         return;
78       } catch (e) {
79         console.debug('Could not parse value', input.value, e);
80       }
81     }
82
83     if (this.isTypeComplex(input.type) || this.isTypeMap(input.type)) {
84       input.value = {};
85     } else if (this.isTypeList(input.type)) {
86       input.value = [];
87     } else {
88       input.value = undefined;
89     }
90   }
91
92   getType(typeName: string): DerivedPropertyType {
93     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
94       return DerivedPropertyType.SIMPLE;
95     } else if (typeName === PROPERTY_TYPES.LIST) {
96       return DerivedPropertyType.LIST;
97     } else if (typeName === PROPERTY_TYPES.MAP) {
98       return DerivedPropertyType.MAP;
99     } else {
100       return DerivedPropertyType.COMPLEX;
101     }
102   }
103
104   isTypeSimple(typeName: string): boolean {
105     return this.getType(typeName) == DerivedPropertyType.SIMPLE;
106   }
107
108   isTypeList(typeName: string): boolean {
109     return this.getType(typeName) == DerivedPropertyType.LIST;
110   }
111
112   isTypeMap(typeName: string): boolean {
113     return this.getType(typeName) == DerivedPropertyType.MAP;
114   }
115
116   isTypeComplex(typeName: string): boolean {
117     return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
118   }
119
120   onValueChange($event: any) {
121     const inputOperationParameter = this._inputs.find(input => input.name == $event.name);
122     if (inputOperationParameter) {
123       inputOperationParameter.valid = true;
124       if ($event.isToscaFunction) {
125         inputOperationParameter.toscaFunction = $event.value;
126         if (!inputOperationParameter.toscaFunction) {
127           inputOperationParameter.valid = false;
128         }
129       } else {
130         inputOperationParameter.value = $event.value;
131         inputOperationParameter.toscaFunction = null;
132       }
133       this.inputsValidityChangeEvent.emit(this._inputs.every(input => input.valid === true));
134       this.inputValueChangeEvent.emit(new InputOperationParameter(inputOperationParameter));
135     }
136   }
137
138   onDelete(inputName: string) {
139     this.inputDeleteEvent.emit(inputName);
140   }
141
142 }