Sync Integ to Master
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / inputs / resource-input / resource-inputs-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
24 import {ComponentInstance, InstancesInputsOrPropertiesMapData, Resource, PropertyModel, InputModel} from "app/models";
25 import {ModalsHandler} from "app/utils";
26
27 export interface IInputsViewModelScope extends IWorkspaceViewModelScope {
28     InstanceInputsProperties:InstancesInputsOrPropertiesMapData; //this is tha map object that hold the selected inputs and the inputs we already used
29     vfInstancesList:Array<ComponentInstance>;
30     component:Resource;
31
32     onArrowPressed():void;
33     getInputPropertiesForInstance(instanceId:string, instance:ComponentInstance):ng.IPromise<boolean> ;
34     loadInputPropertiesForInstance(instanceId:string, input:InputModel):ng.IPromise<boolean> ;
35     openEditValueModal(input:InputModel):void;
36     openEditPropertyModal(property:PropertyModel):void;
37 }
38
39 export class ResourceInputsViewModel {
40
41     static '$inject' = [
42         '$scope',
43         '$q',
44         'ModalsHandler'
45     ];
46
47     constructor(private $scope:IInputsViewModelScope, private $q:ng.IQService, private ModalsHandler:ModalsHandler) {
48         this.initScope();
49     }
50
51     private initScope = ():void => {
52
53         this.$scope.InstanceInputsProperties = new InstancesInputsOrPropertiesMapData();
54         this.$scope.vfInstancesList = this.$scope.component.componentInstances;
55
56         // Need to cast all inputs to InputModel for the search to work
57         let tmpInputs:Array<InputModel> = new Array<InputModel>();
58         _.each(this.$scope.component.inputs, (input):void => {
59             tmpInputs.push(new InputModel(input));
60         });
61         this.$scope.component.inputs = tmpInputs;
62         // This function is not supported for resource
63         //this.$scope.component.getComponentInputs();
64
65         /*
66          * When clicking on instance input in the left or right table, this function will load all properties of the selected input
67          */
68         this.$scope.getInputPropertiesForInstance = (instanceId:string, instance:ComponentInstance):ng.IPromise<boolean> => {
69             let deferred = this.$q.defer();
70             instance.properties = this.$scope.component.componentInstancesProperties[instanceId];
71             deferred.resolve(true);
72             return deferred.promise;
73         };
74
75         /*
76          * When clicking on instance input in the left or right table, this function will load all properties of the selected input
77          */
78         this.$scope.loadInputPropertiesForInstance = (instanceId:string, input:InputModel):ng.IPromise<boolean> => {
79             let deferred = this.$q.defer();
80
81             let onSuccess = (properties:Array<PropertyModel>) => {
82                 input.properties = properties;
83                 deferred.resolve(true);
84             };
85
86             let onError = () => {
87                 deferred.resolve(false)
88             };
89
90             if (!input.properties) {
91                 this.$scope.component.getComponentInstanceInputProperties(instanceId, input.uniqueId).then(onSuccess, onError);
92             } else {
93                 deferred.resolve(true);
94             }
95             return deferred.promise;
96         };
97
98         /*
99          * When pressing the arrow, we create service inputs from the inputs selected
100          */
101         this.$scope.onArrowPressed = ():void => {
102             let onSuccess = (inputsCreated:Array<InputModel>) => {
103
104                 //disabled  all the inputs in the left table
105                 _.forEach(this.$scope.InstanceInputsProperties, (properties:Array<PropertyModel>) => {
106                     _.forEach(properties, (property:PropertyModel) => {
107                         property.isAlreadySelected = true;
108                     });
109                 });
110
111                 // Adding color to the new inputs (right table)
112                 _.forEach(inputsCreated, (input) => {
113                     input.isNew = true;
114                 });
115
116                 // Removing color to the new inputs (right table)
117                 setTimeout(() => {
118                     _.forEach(inputsCreated, (input) => {
119                         input.isNew = false;
120                     });
121                     this.$scope.$apply();
122                 }, 3000);
123             };
124
125             this.$scope.component.createInputsFormInstances(this.$scope.InstanceInputsProperties).then(onSuccess);
126         };
127
128         this.$scope.openEditValueModal = (input:InputModel) => {
129             this.ModalsHandler.openEditInputValueModal(input);
130         };
131
132         this.$scope.openEditPropertyModal = (property:PropertyModel):void => {
133             this.ModalsHandler.openEditPropertyModal(property, this.$scope.component, this.$scope.component.componentInstancesProperties[property.resourceInstanceUniqueId], false).then(() => {
134             });
135         }
136     }
137 }