f34267a2f9179b1982a3957573d729e475a2cf28
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / GlobalPropertiesCtrl.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * 
22  */
23 app.controller('GlobalPropertiesCtrl', [
24 '$scope',
25 '$rootScope',
26 '$uibModalInstance',
27 '$http',
28 '$q',
29 'cldsModelService',
30 '$location',
31 'dialogs',
32 function($scope, $rootScope, $uibModalInstance, $http, $q, cldsModelService, $location,
33          dialogs) {
34         $scope.$watch('name', function(newValue, oldValue) {
35
36                 var el = getGlobalProperty();
37                 if (el !== undefined) {
38                         for (var key in el) {
39                                 if (key === 'dcaeDeployParameters')
40                                 {
41                                         $("#" + key).val(JSON.stringify(el[key]));
42                                 } else {
43                                         $("#" + key).val(el[key]);
44                                 }
45                         }
46                 }
47                 setMultiSelect();
48                 if (readMOnly) {
49                         $("#savePropsBtn").attr("disabled", "");
50                         $('select[multiple] option').each(function() {
51                                 var input = $('input[value="' + $(this).val() + '"]');
52                                 input.prop('disabled', true);
53                                 input.parent('li').addClass('disabled');
54                         });
55                         $('input[value="multiselect-all"]').prop('disabled', true).parent(
56                         'li').addClass('disabled');
57                         ($("select:not([multiple])")).multiselect("disable");
58                 }
59         });
60         $scope.retry = function() {
61                 console.log("retry");
62         }
63         $scope.close = function() {
64                 console.log("close");
65                 $uibModalInstance.close("closed");
66         };
67
68
69     function noRepeats(form) {
70         
71         var select = {};
72         for (var i = 0; i < form.length; i++) {
73                 if (form[i].hasOwnProperty("name")) {
74                         if (form[i].name === 'dcaeDeployParameters') {
75                                         // This is a special case, that value MUST not be a string but a JSON
76                                 select[form[i].name]=JSON.parse(form[i].value);
77                         } else {
78                                 if (select[form[i].name] === undefined)
79                                         select[form[i].name] = []
80                                 select[form[i].name].push(form[i].value);
81                         }
82                 }
83         }
84         var arr = []
85         for (s in select) {
86                 var f = {}
87                 f.name = s
88                 f.value = select[s]
89                 if (!(s == "service" && f.value == "")) {
90                         arr.push(f)
91                 }
92         }
93         return arr
94     }
95     
96     $scope.submitForm = function() {
97        var form = noRepeats($("#saveProps").serializeArray());
98        var obj = {};
99                 for( var i = 0; i < form.length; ++i ) {
100                         var name = form[i].name;
101                         var value = form[i].value;
102                         if( name ) {
103                                 obj[ name ] = value;
104                         }
105                 }
106
107         $scope.saveGlobalProperties(JSON.stringify(obj)).then(function(pars) {
108                 updateGlobalProperties(obj);
109                 }, function(data) {
110                 });
111         $uibModalInstance.close();
112     };
113         $scope.saveGlobalProperties = function(form) {
114                 var modelName = getLoopName();
115                  var def = $q.defer();
116                  var svcUrl = "/restservices/clds/v2/loop/updateGlobalProperties/" + modelName;
117                  $http.post(svcUrl, form).success(function(data) {
118                          def.resolve(data);
119                  }).error(function(data) {
120                          def.reject("Save Model not successful");
121                  });
122                return def.promise;
123            };
124 } ]);