3faf9f6315da9f889aeec6b959cc5033bb1f0eb8
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / DeploymentCtrl.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 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
24 .controller(
25 'DeploymentCtrl',
26 [
27 '$scope',
28 '$rootScope',
29 '$uibModalInstance',
30 'data',
31 'dialogs',
32 'cldsModelService',
33 function($scope, $rootScope, $uibModalInstance, data, dialogs, cldsModelService) {
34         function validate_and_set_deploy_parameters() {
35                 var inputList = document.getElementsByClassName("deployFormId");
36                 var jsonParameters = "{";
37                 $.each(inputList, function(key) {
38                         if (jsonParameters !== "{") {
39                                 jsonParameters = jsonParameters + ",";
40                         }
41                         jsonParameters = jsonParameters + '"' + inputList[key].id + '":'
42                         + '"' + inputList[key].value + '"'
43                 });
44                 jsonParameters = jsonParameters + "}";
45                 try {
46                         // Try to validate the json
47                         set_deploy_parameters(JSON.parse(jsonParameters));
48                 } catch (e) {
49                         console.error("Couldn't parse deploy parameters json");
50                 }
51         }
52         function set_deploy_parameters(parameters) {
53                 if (!'global' in elementMap) {
54                         elementMap["global"] = [];
55                 }
56                 var index = elementMap["global"].findIndex(function(e) {
57                         return (typeof e == "object" && !(e instanceof Array))
58                         && "deployParameters" == e["name"];
59                 });
60                 if (index == -1) {
61                         elementMap["global"].push({
62                         "name" : "deployParameters",
63                         "value" : parameters
64                         });
65                 } else {
66                         elementMap["global"][index]["value"] = parameters;
67                 }
68         }
69         $scope.load_deploy_parameters = function() {
70                 var index = elementMap["global"].findIndex(function(e) {
71                         return (typeof e == "object" && !(e instanceof Array))
72                         && "deployParameters" == e["name"];
73                 });
74                 if (index != -1) {
75                         $('#deployPropertiesDiv').append($('<br/>'));
76                         $.each(elementMap["global"][index].value, function(key) {
77                                 var propertyValue = elementMap["global"][index].value[key];
78                                 $('#deployPropertiesDiv').append(
79                                 $('<label class="control-label">' + key + '  </label>'));
80                                 $('#deployPropertiesDiv').append(
81                                 $(
82                                 '<input style="width: 100%; clear: both;" class="deployFormId" id="'
83                                 + key + '"></input>').val(propertyValue).html(propertyValue));
84                                 $('#deployPropertiesDiv').append($('<br/>'));
85                         });
86                 }
87         }
88         $scope.deploy = function() {
89                 validate_and_set_deploy_parameters();
90                 $uibModalInstance.close();
91         };
92         $scope.close = function() {
93                 $uibModalInstance.dismiss();
94         };
95 } ]);