1618af4aea0eee82e38b82c4ac34da9f5e170ffb
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / interface-operatons / operation-creator / interface-operation-handler.component.ts
1 /*
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 *  Copyright (C) 2021 Nordix Foundation. All rights reserved.
6 *  ================================================================================
7 *  Licensed under the Apache License, Version 2.0 (the "License");
8 *  you may not use this file except in compliance with the License.
9 *  You may obtain a copy of the License at
10 *
11 *        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.
17 *
18 *  SPDX-License-Identifier: Apache-2.0
19 *  ============LICENSE_END=========================================================
20 */
21
22 import {Component} from '@angular/core';
23 import {UIInterfaceModel} from "../interface-operations.component";
24 import {
25     InputOperationParameter,
26     InterfaceOperationModel,
27     IOperationParamsList
28 } from "../../../../../models/interfaceOperation";
29 import {TranslateService} from "../../../../shared/translator/translate.service";
30
31 @Component({
32     selector: 'operation-handler',
33     templateUrl: './interface-operation-handler.component.html',
34     styleUrls: ['./interface-operation-handler.component.less'],
35     providers: [TranslateService]
36 })
37
38 export class InterfaceOperationHandlerComponent {
39
40     input: {
41         selectedInterface: UIInterfaceModel;
42         selectedInterfaceOperation: InterfaceOperationModel;
43         validityChangedCallback: Function;
44     };
45
46     interfaceType: string;
47     interfaceOperationName: string;
48     operationToUpdate: InterfaceOperationModel;
49     inputs: Array<InputOperationParameter> = [];
50     isLoading: boolean = false;
51     readonly: boolean;
52
53     ngOnInit() {
54         this.interfaceType = this.input.selectedInterface.displayType();
55         this.operationToUpdate = new InterfaceOperationModel(this.input.selectedInterfaceOperation);
56         this.operationToUpdate.interfaceId = this.input.selectedInterface.uniqueId;
57         this.operationToUpdate.interfaceType = this.input.selectedInterface.type;
58         if (!this.operationToUpdate.inputs) {
59             this.operationToUpdate.inputs = new class implements IOperationParamsList {
60                 listToscaDataDefinition: Array<InputOperationParameter> = [];
61             }
62         }
63         this.inputs = this.operationToUpdate.inputs.listToscaDataDefinition;
64         this.removeImplementationQuote();
65         this.validityChanged();
66     }
67
68     onAddInput(inputOperationParameter?: InputOperationParameter): void {
69         let newInput = new InputOperationParameter(inputOperationParameter)
70         newInput.type = "string";
71         newInput.inputId = this.generateUniqueId();
72         this.inputs.push(newInput);
73         this.validityChanged();
74     }
75
76     onRemoveInput = (inputParam: InputOperationParameter): void => {
77         let index = this.inputs.indexOf(inputParam);
78         this.inputs.splice(index, 1);
79         this.validityChanged();
80     }
81
82     private generateUniqueId = (): string => {
83         let result = '';
84         const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
85         const charactersLength = characters.length;
86         for (let i = 0; i < 36; i++ ) {
87             result += characters.charAt(Math.floor(Math.random() * charactersLength));
88         }
89         return result;
90     }
91
92     validityChanged = () => {
93         let validState = this.checkFormValidForSubmit();
94         this.input.validityChangedCallback(validState);
95         if (validState) {
96             this.readonly = false;
97         }
98     }
99
100     onDescriptionChange= (value: any): void => {
101         this.operationToUpdate.description = value;
102     }
103
104     private checkFormValidForSubmit = (): boolean => {
105         return this.operationToUpdate.name && this.isParamsValid();
106     }
107
108     private isParamsValid = (): boolean => {
109         const isInputValid = (input) => input.name && input.inputId;
110         const isValid = this.inputs.every(isInputValid);
111         if (!isValid) {
112             this.readonly = true;
113         }
114         return isValid;
115     }
116
117     private removeImplementationQuote(): void {
118         if (!this.operationToUpdate.implementation
119             || !this.operationToUpdate.implementation.artifactName) {
120             return;
121         }
122
123         let implementation = this.operationToUpdate.implementation.artifactName.trim();
124
125         if (implementation.startsWith("'") && implementation.endsWith("'")) {
126             this.operationToUpdate.implementation.artifactName = implementation.slice(1, -1);
127         }
128     }
129
130 }