2ef866c8b78316755c26874bb18ea2373fc4c011
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / ToscaModelCtrl.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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('ToscaModelCtrl',
24     ['$scope', '$rootScope', '$modalInstance', '$location', 'dialogs', 'toscaModelService',
25     function($scope, $rootScope, $modalInstance, $location, dialogs, toscaModelService) {
26
27         $scope.jsonByPolicyType = function(selectedPolicy, oldSelectedPolicy){
28                 if (selectedPolicy && selectedPolicy != '') {
29
30                                 $('#editor').empty();
31
32                                 var toscaModel = getMsUI(selectedPolicy);
33                                 if (toscaModel == null) {
34                                         $modalInstance.close('closed');
35                                         return;
36                                 }
37                                 var editorData = getMsProperty(selectedPolicy);
38
39                                 JSONEditor.defaults.options.theme = 'bootstrap4';
40                                 //JSONEditor.defaults.options.iconlib = 'bootstrap2';
41                                 JSONEditor.defaults.options.object_layout = 'grid';
42                                 JSONEditor.defaults.options.disable_properties = true;
43                                 JSONEditor.defaults.options.disable_edit_json = false;
44                                 JSONEditor.defaults.options.disable_array_reorder = true;
45                                 JSONEditor.defaults.options.disable_array_delete_last_row = true;
46                                 JSONEditor.defaults.options.disable_array_delete_all_rows = false;
47                                 JSONEditor.defaults.options.show_errors = 'always';
48
49                                 if($scope.editor) { $scope.editor.destroy(); }
50                                 $scope.editor = new JSONEditor(document.getElementById("editor"),
51                                                       { schema: toscaModel.schema, startval: editorData });
52                                 $scope.editor.watch('root.policy.recipe',function() {
53
54                                 });
55                                 $('#form1').show();
56
57                 } else {
58                                 $('#editor').empty();
59                                 $('#form1').hide();
60                         }
61         }
62
63         $scope.$watch('name', function() {
64             if($rootScope.selectedBoxName) {
65                 var policyType = $rootScope.selectedBoxName.split('_')[0].toLowerCase();
66                 $scope.toscaModelName = policyType.toUpperCase() + " Microservice";
67                 $scope.jsonByPolicyType($rootScope.selectedBoxName, '', '');
68             }
69         });
70
71         $scope.getEditorData = function(){
72                 if(!$scope.editor){
73                         return null;
74                 }
75                 var errors = $scope.editor.validate();
76                 var editorData = $scope.editor.getValue();
77
78                 if(errors.length) {
79                         $scope.displayErrorMessage(errors);
80                         return null;
81                 }
82                 else{
83                         console.log("there are NO validation errors........");
84                 }
85                 return editorData;
86         }
87
88         $scope.saveToscaProps = function(){
89                 var policyType = $rootScope.selectedBoxName;
90             var data = $scope.getEditorData();
91             if(data !== null) {
92                 var msJson = getMsJson(policyType);
93                 msJson["properties"] = data[0];
94                 toscaModelService.saveMsProperties(msJson).then(function(pars) {
95                         updateMsProperties(policyType, msJson);
96                 }, function(data) {
97                 });
98                 if($scope.editor) { $scope.editor.destroy(); $scope.editor = null; }
99                 $modalInstance.close('closed');
100             }
101         }
102
103         $scope.displayErrorMessage = function(errors){
104                 console.log("there are validation errors.....");
105                 var all_errs = "Please address the following issues before selecting 'Done' or 'Policy Types':\n";
106                 for (var i = 0; i < errors.length; i++) {
107                   if(all_errs.indexOf(errors[i].message) < 0) {
108                         all_errs += '\n' + errors[i].message;
109                   }
110                 }
111             window.alert(all_errs);
112         };
113
114         $scope.close = function(){
115                         $modalInstance.close('closed');
116                         if($scope.editor) { $scope.editor.destroy(); $scope.editor = null; }
117         }
118
119         $scope.checkDuplicateInObject = function(propertyName, inputArray) {
120                   var seenDuplicate = false,
121                       testObject = {};
122
123                   inputArray.map(function(item) {
124                     var itemPropertyName = item[propertyName];
125                     if (itemPropertyName in testObject) {
126                       testObject[itemPropertyName].duplicate = true;
127                       item.duplicate = true;
128                       seenDuplicate = true;
129                     }
130                     else {
131                       testObject[itemPropertyName] = item;
132                       delete item.duplicate;
133                     }
134                   });
135
136                   return seenDuplicate;
137                 }
138 }
139 ]);