2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
23 app.controller('ToscaModelCtrl',
24 ['$scope', '$rootScope', '$modalInstance', '$location', 'dialogs', 'toscaModelService',
25 function($scope, $rootScope, $modalInstance, $location, dialogs, toscaModelService) {
27 $scope.jsonByPolicyType = function(selectedPolicy, oldSelectedPolicy, editorData){
28 if (selectedPolicy && selectedPolicy != '') {
29 toscaModelService.getHpModelJsonByPolicyType(selectedPolicy).then(function(response) {
32 var toscaModel = JSON.parse(response.toscaModelJson);
33 if($scope.policyList && toscaModel.schema.properties && toscaModel.schema.properties.policyList){
34 toscaModel.schema.properties.policyList.enum = $scope.policyList;
37 JSONEditor.defaults.options.theme = 'bootstrap3';
38 JSONEditor.defaults.options.iconlib = 'bootstrap2';
39 JSONEditor.defaults.options.object_layout = 'grid';
40 JSONEditor.defaults.options.disable_properties = true;
41 JSONEditor.defaults.options.disable_edit_json = true;
42 JSONEditor.defaults.options.disable_array_reorder = true;
43 JSONEditor.defaults.options.disable_array_delete_last_row = true;
44 JSONEditor.defaults.options.disable_array_delete_all_rows = false;
45 JSONEditor.defaults.options.show_errors = 'always';
47 if($scope.editor) { $scope.editor.destroy(); }
48 $scope.editor = new JSONEditor(document.getElementById("editor"),
49 { schema: toscaModel.schema, startval: editorData });
50 $scope.editor.watch('root.policy.recipe',function() {
61 if($rootScope.selectedBoxName) {
62 var policyType = $rootScope.selectedBoxName.split('_')[0].toLowerCase();
63 $scope.toscaModelName = policyType.toUpperCase() + " Microservice";
64 if(elementMap[lastElementSelected]) {
65 $scope.jsonByPolicyType(policyType, '', elementMap[lastElementSelected][policyType]);
67 $scope.jsonByPolicyType(policyType, '', '');
71 $scope.getEditorData = function(){
75 var errors = $scope.editor.validate();
76 var editorData = $scope.editor.getValue();
79 $scope.displayErrorMessage(errors);
83 console.log("there are NO validation errors........");
88 $scope.saveToscaProps = function(){
89 var policyType = $rootScope.selectedBoxName.split('_')[0].toLowerCase();
90 var data = $scope.getEditorData();
93 data = {[policyType]: data};
95 if($scope.editor) { $scope.editor.destroy(); $scope.editor = null; }
96 $modalInstance.close('closed');
100 $scope.displayErrorMessage = function(errors){
101 console.log("there are validation errors.....");
102 var all_errs = "Please address the following issues before selecting 'Done' or 'Policy Types':\n";
103 for (var i = 0; i < errors.length; i++) {
104 if(all_errs.indexOf(errors[i].message) < 0) {
105 all_errs += '\n' + errors[i].message;
108 window.alert(all_errs);
111 $scope.close = function(){
112 angular.copy(elementMap[lastElementSelected], $scope.hpPolicyList);
113 $modalInstance.close('closed');
114 if($scope.editor) { $scope.editor.destroy(); $scope.editor = null; }
117 $scope.checkDuplicateInObject = function(propertyName, inputArray) {
118 var seenDuplicate = false,
121 inputArray.map(function(item) {
122 var itemPropertyName = item[propertyName];
123 if (itemPropertyName in testObject) {
124 testObject[itemPropertyName].duplicate = true;
125 item.duplicate = true;
126 seenDuplicate = true;
129 testObject[itemPropertyName] = item;
130 delete item.duplicate;
134 return seenDuplicate;