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