summaryrefslogtreecommitdiffstats
path: root/catalog-ui/app/scripts/view-models/workspace/tabs/inputs/resource-input/resource-inputs-view-model.ts
blob: 2dc1b1d9ff62e84a89f83db77e3df1b5837b80c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
/// <reference path="../../../../../references"/>
module Sdc.ViewModels {
    'use strict';
    import Dictionary = Sdc.Utils.Dictionary;
    import InputModel = Sdc.Models.InputModel;

    export interface IInputsViewModelScope extends IWorkspaceViewModelScope {
        InstanceInputsProperties:Models.InstanceInputsPropertiesMapData; //this is tha map object that hold the selected inputs and the inputs we already used
        vfInstancesList: Array<Models.ComponentsInstances.ComponentInstance>;
        component:Models.Components.Resource;

        onArrowPressed():void;
        getInputPropertiesForInstance(instanceId:string, instance:Models.ComponentsInstances.ComponentInstance): ng.IPromise<boolean> ;
        loadInputPropertiesForInstance(instanceId:string, input:Models.InputModel): ng.IPromise<boolean> ;
        loadInputInputs(input:Models.InputModel): ng.IPromise<boolean>;
    }

    export class ResourceInputsViewModel {

        static '$inject' = [
            '$scope',
            '$q'
        ];

        constructor(private $scope:IInputsViewModelScope, private $q: ng.IQService) {
            this.initScope();
        }

        private initScope = (): void => {

            this.$scope.InstanceInputsProperties = new Models.InstanceInputsPropertiesMapData();
            this.$scope.vfInstancesList = this.$scope.component.componentInstances;

            // Need to cast all inputs to InputModel for the search to work
            let tmpInputs:Array<Models.InputModel> = new Array<Models.InputModel>();
            _.each(this.$scope.component.inputs, (input):void => {
                tmpInputs.push(new Models.InputModel(input));
            });
            this.$scope.component.inputs = tmpInputs;
            // This function is not supported for resource
            //this.$scope.component.getComponentInputs();

            /*
             * When clicking on instance input in the left or right table, this function will load all properties of the selected input
             */
            this.$scope.getInputPropertiesForInstance = (instanceId:string, instance:Models.ComponentsInstances.ComponentInstance): ng.IPromise<boolean> => {
                let deferred = this.$q.defer();
                instance.properties = this.$scope.component.componentInstancesProperties[instanceId];
                deferred.resolve(true);
                return deferred.promise;
            };

            /*
             * When clicking on input in the right table, this function will load all inputs of the selected input
             */
            this.$scope.loadInputInputs = (input:Models.InputModel): ng.IPromise<boolean> => {
                let deferred = this.$q.defer();

                let onSuccess = () => { deferred.resolve(true); };
                let onError = () => { deferred.resolve(false); };

                if(!input.inputs) {
                    this.$scope.component.getResourceInputInputs(input.uniqueId).then(onSuccess, onError);
                } else {
                    deferred.resolve(true);
                }
                return deferred.promise;
            };

            /*
             * When clicking on instance input in the left or right table, this function will load all properties of the selected input
             */
            this.$scope.loadInputPropertiesForInstance = (instanceId:string, input:Models.InputModel): ng.IPromise<boolean> => {
                let deferred = this.$q.defer();

                let onSuccess = (properties:Array<Models.PropertyModel>) => {
                    input.properties = properties;
                    deferred.resolve(true);
                };

                let onError = () => {
                    deferred.resolve(false)
                };

                if(!input.properties) {
                    this.$scope.component.getComponentInstanceInputProperties(instanceId, input.uniqueId).then(onSuccess, onError);
                } else {
                    deferred.resolve(true);
                }
                return deferred.promise;
            };

            /*
             * When pressing the arrow, we create service inputs from the inputs selected
             */
            this.$scope.onArrowPressed = ():void => {
                let onSuccess = (inputsCreated: Array<Models.InputModel>) => {

                    //disabled  all the inputs in the left table
                    _.forEach(this.$scope.InstanceInputsProperties, (properties:Array<Models.PropertyModel>) => {
                        _.forEach(properties, (property:Models.PropertyModel) => {
                            property.isAlreadySelected = true;
                        });
                    });

                    // Adding color to the new inputs (right table)
                    _.forEach(inputsCreated, (input) => {
                        input.isNew = true;
                    });

                    // Removing color to the new inputs (right table)
                    setTimeout(() => {
                        _.forEach(inputsCreated, (input) => {
                            input.isNew = false;
                        });
                        this.$scope.$apply();
                    }, 3000);
                };

                this.$scope.component.createInputsFormInstances(this.$scope.InstanceInputsProperties).then(onSuccess);
            };

        }

    }
}