Draft operational policy rework
[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                 if (readMOnly) {
48                         $("#savePropsBtn").attr("disabled", "");
49                         $('select[multiple] option').each(function() {
50                                 var input = $('input[value="' + $(this).val() + '"]');
51                                 input.prop('disabled', true);
52                                 input.parent('li').addClass('disabled');
53                         });
54                         $('input[value="multiselect-all"]').prop('disabled', true).parent(
55                         'li').addClass('disabled');
56                         ($("select:not([multiple])")).multiselect("disable");
57                 }
58         });
59         $scope.retry = function() {
60                 console.log("retry");
61         }
62         $scope.close = function() {
63                 console.log("close");
64                 $uibModalInstance.close("closed");
65         };
66
67
68     function noRepeats(form) {
69         
70         var select = {};
71         for (var i = 0; i < form.length; i++) {
72                 if (form[i].hasOwnProperty("name")) {
73                         if (form[i].name === 'dcaeDeployParameters') {
74                                         // This is a special case, that value MUST not be a string but a JSON
75                                 select[form[i].name]=JSON.parse(form[i].value);
76                         } else {
77                                 if (select[form[i].name] === undefined)
78                                         select[form[i].name] = []
79                                 select[form[i].name].push(form[i].value);
80                         }
81                 }
82         }
83         var arr = []
84         for (s in select) {
85                 var f = {}
86                 f.name = s
87                 f.value = select[s]
88                 if (!(s == "service" && f.value == "")) {
89                         arr.push(f)
90                 }
91         }
92         return arr
93     }
94     
95     $scope.submitForm = function() {
96        var form = noRepeats($("#saveProps").serializeArray());
97        var obj = {};
98                 for( var i = 0; i < form.length; ++i ) {
99                         var name = form[i].name;
100                         var value = form[i].value;
101                         if( name ) {
102                                 obj[ name ] = value;
103                         }
104                 }
105
106         $scope.saveGlobalProperties(JSON.stringify(obj)).then(function(pars) {
107                 updateGlobalProperties(obj);
108                 }, function(data) {
109                 });
110         $uibModalInstance.close();
111     };
112         $scope.saveGlobalProperties = function(form) {
113                 var modelName = getLoopName();
114                  var def = $q.defer();
115                  var svcUrl = "/restservices/clds/v2/loop/updateGlobalProperties/" + modelName;
116                  $http.post(svcUrl, form).success(function(data) {
117                          def.resolve(data);
118                  }).error(function(data) {
119                          def.reject("Save Model not successful");
120                  });
121                return def.promise;
122            };
123 } ]);