2 * ============LICENSE_START=======================================================
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
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 import {Component} from '@angular/core';
23 import {UIInterfaceModel} from "../interface-operations.component";
25 InputOperationParameter,
26 InterfaceOperationModel,
28 } from "../../../../../models/interfaceOperation";
29 import {TranslateService} from "../../../../shared/translator/translate.service";
32 selector: 'operation-handler',
33 templateUrl: './interface-operation-handler.component.html',
34 styleUrls: ['./interface-operation-handler.component.less'],
35 providers: [TranslateService]
38 export class InterfaceOperationHandlerComponent {
41 selectedInterface: UIInterfaceModel;
42 selectedInterfaceOperation: InterfaceOperationModel;
43 validityChangedCallback: Function;
46 interfaceType: string;
47 interfaceOperationName: string;
48 operationToUpdate: InterfaceOperationModel;
49 inputs: Array<InputOperationParameter> = [];
50 isLoading: boolean = false;
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> = [];
63 this.inputs = this.operationToUpdate.inputs.listToscaDataDefinition;
64 this.removeImplementationQuote();
65 this.validityChanged();
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();
76 onRemoveInput = (inputParam: InputOperationParameter): void => {
77 let index = this.inputs.indexOf(inputParam);
78 this.inputs.splice(index, 1);
79 this.validityChanged();
82 private generateUniqueId = (): string => {
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));
92 validityChanged = () => {
93 let validState = this.checkFormValidForSubmit();
94 this.input.validityChangedCallback(validState);
96 this.readonly = false;
100 onDescriptionChange= (value: any): void => {
101 this.operationToUpdate.description = value;
104 private checkFormValidForSubmit = (): boolean => {
105 return this.operationToUpdate.name && this.isParamsValid();
108 private isParamsValid = (): boolean => {
109 const isInputValid = (input) => input.name && input.inputId;
110 const isValid = this.inputs.every(isInputValid);
112 this.readonly = true;
117 private removeImplementationQuote(): void {
118 if (!this.operationToUpdate.implementation
119 || !this.operationToUpdate.implementation.artifactName) {
123 let implementation = this.operationToUpdate.implementation.artifactName.trim();
125 if (implementation.startsWith("'") && implementation.endsWith("'")) {
126 this.operationToUpdate.implementation.artifactName = implementation.slice(1, -1);