2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 import * as _ from "lodash";
23 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
24 import {ComponentInstance, InstancesInputsOrPropertiesMapData, Service, IAppMenu, InputModel, PropertyModel, InputPropertyBase} from "app/models";
25 import {DataTypesService} from "app/services";
26 import {ModalsHandler, ResourceType} from "app/utils";
29 interface IServiceInputsViewModelScope extends IWorkspaceViewModelScope {
31 vfInstancesList:Array<ComponentInstance>;
32 instanceInputsMap:InstancesInputsOrPropertiesMapData; //this is tha map object that hold the selected inputs and the inputs we already used
33 instancePropertiesMap:InstancesInputsOrPropertiesMapData;
37 isArrowDisabled:boolean;
38 onArrowPressed():void;
39 checkArrowState():void;
40 loadComponentInputs():void;
41 loadInstanceInputs(instance:ComponentInstance):ng.IPromise<boolean> ;
42 loadInstanceProperties(instance:ComponentInstance):ng.IPromise<boolean> ;
43 loadInputPropertiesForInstance(instanceId:string, input:InputModel):ng.IPromise<boolean> ;
44 loadInputInputs(input:InputModel):ng.IPromise<boolean>;
45 deleteInput(input:InputModel):void
46 openEditValueModal(input:InputModel):void;
47 openSelectPropertyDataTypeViewModel(instanceId:string, property:PropertyModel):void;
48 openEditPropertyDataTypeViewModel(property:PropertyModel):void;
49 dataTypesService:DataTypesService;
52 export class ServiceInputsViewModel {
58 'Sdc.Services.DataTypesService'
61 constructor(private $scope:IServiceInputsViewModelScope,
62 private $q:ng.IQService,
63 private ModalsHandler:ModalsHandler,
64 private DataTypesService:DataTypesService) {
66 this.$scope.isViewOnly = this.$scope.isViewMode();
70 private getInputsOrPropertiesAlreadySelected = (instanceNormalizeName:string, arrayToFilter:Array<InputPropertyBase>):Array<any> => {
71 let alreadySelectedInput = [];
72 _.forEach(arrayToFilter, (inputOrProperty:InputPropertyBase) => {
73 let expectedServiceInputName = instanceNormalizeName + '_' + inputOrProperty.name;
74 let inputAlreadyInService = _.find(this.$scope.component.inputs, (serviceInput:InputModel) => {
75 //Checking if the input prefix is the instance name + '_' + property/input name (prefix because we don't need to check full name for complex dataType)
76 return serviceInput.name.substring(0, expectedServiceInputName.length) === expectedServiceInputName;
78 if (inputAlreadyInService) {
79 inputOrProperty.isAlreadySelected = true;
80 alreadySelectedInput.push(inputOrProperty);
82 inputOrProperty.isAlreadySelected = false;
85 return alreadySelectedInput;
90 * When loading the screen again, we need to disabled the inputs that already created on the service,
91 * we do that by comparing the service input name, to the instance name + '_' + the resource instance input name.
93 private disableEnableSelectedInputsOrPropertiesOnInit = (instance:ComponentInstance):void => {
95 if (instance.originType === ResourceType.VF) {
96 this.$scope.instanceInputsMap[instance.uniqueId] = this.getInputsOrPropertiesAlreadySelected(instance.normalizedName, instance.inputs);
98 this.$scope.instancePropertiesMap[instance.uniqueId] = this.getInputsOrPropertiesAlreadySelected(instance.normalizedName, instance.properties);
103 * Enable Input/Property after delete
105 private enableInputsAfterDelete = (propertiesOrInputsDeletes:Array<InputPropertyBase>):void => {
107 _.forEach(propertiesOrInputsDeletes, (deletedInputInput:InputPropertyBase) => { //Enable all component instance inputs deleted
109 let inputOrPropertyDeleted:InputPropertyBase = _.find(this.$scope.instanceInputsMap[deletedInputInput.componentInstanceId], (inputOrProperty:InputPropertyBase) => {
110 return inputOrProperty.uniqueId === deletedInputInput.uniqueId;
112 inputOrPropertyDeleted.isAlreadySelected = false;
113 delete _.remove(this.$scope.instanceInputsMap[deletedInputInput.componentInstanceId], {uniqueId: inputOrPropertyDeleted.uniqueId})[0];
118 * Enable Input/Property after delete
120 private enablePropertiesAfterDelete = (propertiesOrInputsDeletes:Array<InputPropertyBase>):void => {
122 _.forEach(propertiesOrInputsDeletes, (deletedInputInput:InputPropertyBase) => { //Enable all component instance inputs deleted
123 let componentInstance = _.find(this.$scope.vfInstancesList, (instance:ComponentInstance) => {
124 return instance.uniqueId === deletedInputInput.componentInstanceId;
126 let inputOrPropertyDeleted:InputPropertyBase = _.find(this.$scope.instancePropertiesMap[deletedInputInput.componentInstanceId], (inputOrProperty:InputPropertyBase) => {
127 return inputOrProperty.uniqueId === deletedInputInput.uniqueId;
130 let expectedName = componentInstance.normalizedName + '_' + inputOrPropertyDeleted.name;
131 let isAnotherInputExist = _.find(this.$scope.component.inputs, (input:InputModel) => {
132 return input.name.substring(0, expectedName.length) === expectedName;
134 if (!isAnotherInputExist) {
135 inputOrPropertyDeleted.isAlreadySelected = false;
136 delete _.remove(this.$scope.instancePropertiesMap[deletedInputInput.componentInstanceId], {uniqueId: inputOrPropertyDeleted.uniqueId})[0];
141 private initScope = ():void => {
143 this.$scope.instanceInputsMap = new InstancesInputsOrPropertiesMapData();
144 this.$scope.instancePropertiesMap = new InstancesInputsOrPropertiesMapData();
145 this.$scope.isLoading = true;
146 this.$scope.isArrowDisabled = true;
147 // Why do we need this? we call this later.
148 //this.$scope.component.getComponentInputs();
150 let onSuccess = (componentInstances:Array<ComponentInstance>) => {
151 console.log("component instances loaded: ", componentInstances);
152 this.$scope.vfInstancesList = componentInstances;
153 this.$scope.isLoading = false;
156 //This function will get al component instance for the left table - in
157 // future the instances will be filter according to search text
158 this.$scope.component.getComponentInstancesFilteredByInputsAndProperties().then(onSuccess);
160 // This function will get the service inputs for the right table
161 this.$scope.component.getComponentInputs();
164 * When clicking on instance in the left table, this function will load all instance inputs
166 this.$scope.loadInstanceInputs = (instance:ComponentInstance):ng.IPromise<boolean> => {
167 let deferred = this.$q.defer();
169 let onSuccess = (inputs:Array<InputModel>) => {
170 instance.inputs = inputs;
171 this.disableEnableSelectedInputsOrPropertiesOnInit(instance);
172 deferred.resolve(true);
175 let onError = () => {
176 deferred.resolve(false);
179 if (!instance.inputs) {
180 this.$scope.component.getComponentInstanceInputs(instance.uniqueId, instance.componentUid).then(onSuccess, onError);
181 //this.disableEnableSelectedInputs(instance);
183 deferred.resolve(true);
185 return deferred.promise;
189 this.$scope.loadInstanceProperties = (instance:ComponentInstance):ng.IPromise<boolean> => {
190 let deferred = this.$q.defer();
192 let onSuccess = (properties:Array<PropertyModel>) => {
193 instance.properties = properties;
194 this.disableEnableSelectedInputsOrPropertiesOnInit(instance);
195 deferred.resolve(true);
198 let onError = () => {
199 deferred.resolve(false);
202 if (!instance.properties) {
203 this.$scope.component.getComponentInstanceProperties(instance.uniqueId).then(onSuccess, onError);
205 deferred.resolve(true);
207 return deferred.promise;
211 * When clicking on instance input in the left or right table, this function will load all properties of the selected input
213 this.$scope.loadInputPropertiesForInstance = (instanceId:string, input:InputModel):ng.IPromise<boolean> => {
214 let deferred = this.$q.defer();
216 let onSuccess = (properties:Array<PropertyModel>) => {
217 input.properties = properties;
218 deferred.resolve(true);
221 let onError = () => {
222 deferred.resolve(false)
225 if (!input.properties) {
226 this.$scope.component.getComponentInstanceInputProperties(instanceId, input.uniqueId).then(onSuccess, onError);
228 deferred.resolve(true);
230 return deferred.promise;
234 * When clicking on input in the right table, this function will load all inputs of the selected input
236 this.$scope.loadInputInputs = (input:InputModel):ng.IPromise<boolean> => {
237 let deferred = this.$q.defer();
239 let onSuccess = () => {
240 deferred.resolve(true);
242 let onError = () => {
243 deferred.resolve(false);
246 if (!input.inputs) { // Caching, if exists do not get it.
247 this.$scope.component.getServiceInputInputsAndProperties(input.uniqueId).then(onSuccess, onError);
249 deferred.resolve(true);
251 return deferred.promise;
255 * When pressing the arrow, we create service inputs from the inputs selected
257 this.$scope.onArrowPressed = ():void => {
258 let onSuccess = (inputsCreated:Array<InputModel>) => {
260 //disabled all the inputs in the left table
261 _.forEach(this.$scope.instanceInputsMap, (inputs:Array<InputModel>, instanceId:string) => {
262 _.forEach(inputs, (input:InputModel) => {
263 input.isAlreadySelected = true;
266 _.forEach(this.$scope.instancePropertiesMap, (properties:Array<PropertyModel>, instanceId:string) => {
267 _.forEach(properties, (property:PropertyModel) => {
268 property.isAlreadySelected = true;
271 this.addColorToItems(inputsCreated);
274 let onFailed = (error:any) => {
275 this.$scope.isArrowDisabled = false;
276 console.log("Error declaring input/property");
279 this.$scope.isArrowDisabled = true;
280 this.$scope.component.createInputsFormInstances(this.$scope.instanceInputsMap, this.$scope.instancePropertiesMap).then(onSuccess, onFailed);
284 /* Iterates through array of selected inputs and properties and returns true if there is at least one new selection on left */
285 this.$scope.checkArrowState = ()=> {
287 let newInputSelected:boolean = _.some(this.$scope.instanceInputsMap, (inputs:Array<InputModel>) => {
288 return _.some(inputs, (input:InputModel)=> {
289 return input.isAlreadySelected === false;
293 let newPropSelected:boolean = _.some(this.$scope.instancePropertiesMap, (properties:Array<PropertyModel>) => {
294 return _.some(properties, (property:PropertyModel) => {
295 return property.isAlreadySelected === false;
299 this.$scope.isArrowDisabled = !(newInputSelected || newPropSelected);
303 this.$scope.deleteInput = (inputToDelete:InputModel):void => {
305 let onDelete = ():void => {
307 let onSuccess = (deletedInput:InputModel):void => {
308 if (deletedInput.inputs && deletedInput.inputs.length > 0) { // Enable input declared from input
309 this.enableInputsAfterDelete(deletedInput.inputs);
312 if (deletedInput.properties && deletedInput.properties.length > 0) { // Enable properties
313 this.enablePropertiesAfterDelete(deletedInput.properties);
315 deletedInput.isDeleteDisabled = false;
316 this.$scope.checkArrowState();
320 let onFailed = (error:any):void => {
321 console.log("Error deleting input");
322 inputToDelete.isDeleteDisabled = false;
325 inputToDelete.isDeleteDisabled = true;
326 this.addColorToItems([inputToDelete]);
327 this.$scope.component.deleteServiceInput(inputToDelete.uniqueId).then((deletedInput:InputModel):void => {
328 onSuccess(deletedInput);
332 // Get confirmation modal text from menu.json
333 let state = "deleteInput";
334 let title:string = this.$scope.sdcMenu.alertMessages[state].title;
335 let message:string = this.$scope.sdcMenu.alertMessages[state].message.format([inputToDelete.name]);
337 // Open confirmation modal
338 this.ModalsHandler.openAlertModal(title, message).then(onDelete);
341 this.$scope.openEditValueModal = (input:InputModel) => {
342 this.ModalsHandler.openEditInputValueModal(input);
345 this.$scope.openSelectPropertyDataTypeViewModel = (instanceId:string, property:PropertyModel) => {
346 //to open the select data type modal
347 let selectedInstance = _.find(this.$scope.vfInstancesList, {uniqueId: instanceId});
348 this.DataTypesService.selectedInstance = selectedInstance; //set the selected instance on the service for compering the input name on the service & the complex property
349 this.DataTypesService.selectedComponentInputs = this.$scope.component.inputs; // set all the service inputs on the data type service
350 let filteredPropertiesMap = _.filter(this.$scope.instancePropertiesMap[instanceId], (instanceProperty)=> {
351 return instanceProperty.name == property.name;
352 });//get all properties under the specific property
353 this.DataTypesService.selectedPropertiesName = property.propertiesName;
355 this.ModalsHandler.openSelectDataTypeModal(property, this.$scope.component, this.$scope.component.properties, filteredPropertiesMap).then((selectedProperty:PropertyModel)=> {
356 if (selectedProperty && selectedProperty.propertiesName) {
357 let propertyToUpdate:PropertyModel = _.find(selectedInstance.properties, {uniqueId: selectedProperty.uniqueId});
358 let existingProperty:PropertyModel = (<PropertyModel>_.find(this.$scope.instancePropertiesMap[instanceId], {uniqueId: propertyToUpdate.uniqueId}));
360 if (existingProperty) {
361 existingProperty.propertiesName = selectedProperty.propertiesName;
362 existingProperty.input = selectedProperty.input;
363 existingProperty.isAlreadySelected = false;
365 propertyToUpdate.propertiesName = selectedProperty.propertiesName;
366 propertyToUpdate.input = selectedProperty.input;
367 this.$scope.instancePropertiesMap[instanceId].push(propertyToUpdate);
370 this.$scope.checkArrowState();
377 this.$scope.openEditPropertyDataTypeViewModel = (property:PropertyModel)=> {
378 this.ModalsHandler.openEditPropertyModal(property, this.$scope.component, this.$scope.component.properties, false).then(() => {
383 private addColorToItems = (inputsCreated:Array<InputModel>):void => {
385 // Adding color to the new inputs (right table)
386 _.forEach(inputsCreated, (input) => {
390 // Removing color to the new inputs (right table)
392 _.forEach(inputsCreated, (input) => {
395 this.$scope.$apply();