b4c0edcbccee523740cdb9170b1b8d53b7310156
[osm/LW-UI.git] / static / src / instancehandler / instance_list.js
1 /*
2 Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 function performAction(url) {
18 $("#formActionNS").attr("action", url);
19 $('#modal_instance_new_action').modal('show');
20 }
21
22 function exportMetricNs(url) {
23 console.log(url)
24 $("#formExportMetricNS").attr("action", url);
25 $('#modal_instance_export_metric').modal('show');
26 }
27
28 function newAlarmNs(url) {
29 $("#formAlarmNS").attr("action", url);
30 $('#modal_instance_new_alarm').modal('show');
31 }
32
33 function deleteNs(url, force) {
34 bootbox.confirm("Are you sure want to delete?", function (result) {
35 if (result) {
36 if(force)
37 url = url + '?force=true';
38 location.href = url
39 }
40 })
41 }
42
43 var addFormGroup = function (event) {
44 event.preventDefault();
45
46 var $formGroup = $(this).closest('.form-group');
47 var $formGroupClone = $formGroup.clone();
48
49 $(this)
50 .toggleClass('btn-success btn-add btn-danger btn-remove')
51 .html('–');
52
53 $formGroupClone.find('input').val('');
54 $formGroupClone.insertAfter($formGroup);
55
56 };
57
58 var removeFormGroup = function (event) {
59 event.preventDefault();
60 var $formGroup = $(this).closest('.form-group');
61 $formGroup.remove();
62 };
63
64 function showInstanceDetails(url_info) {
65 var dialog = bootbox.dialog({
66 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
67 closeButton: true
68 });
69 $.ajax({
70 url: url_info,
71 type: 'GET',
72 dataType: "json",
73 contentType: "application/json;charset=utf-8",
74 success: function (result) {
75 editorJSON.setValue(JSON.stringify(result, null, "\t"));
76 editorJSON.setOption("autoRefresh", true);
77 dialog.modal('hide');
78 $('#modal_show_instance').modal('show');
79 },
80 error: function (result) {
81 dialog.modal('hide');
82 bootbox.alert("An error occurred while retrieving the information.");
83 }
84 });
85 }
86
87 var editorJSON;
88
89 $(document).ready(function () {
90 var json_editor_settings = {
91 mode: "javascript",
92 showCursorWhenSelecting: true,
93 autofocus: true,
94 lineNumbers: true,
95 lineWrapping: true,
96 foldGutter: true,
97 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
98 autoCloseBrackets: true,
99 matchBrackets: true,
100 extraKeys: {
101 "F11": function (cm) {
102 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
103 },
104 "Esc": function (cm) {
105 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
106 },
107 "Ctrl-Q": function (cm) {
108 cm.foldCode(cm.getCursor());
109 }
110 },
111 theme: "neat",
112 keyMap: "sublime"
113 };
114 var myJsonTextArea = document.getElementById("instance_view_json");
115 editorJSON = CodeMirror(function (elt) {
116 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
117 }, json_editor_settings);
118
119
120 $(document).on('click', '.btn-add', addFormGroup);
121 $(document).on('click', '.btn-remove', removeFormGroup);
122
123 $("#formActionNS").submit(function (event) {
124 event.preventDefault(); //prevent default action
125 var post_url = $(this).attr("action"); //get form action url
126 var request_method = $(this).attr("method"); //get form GET/POST method
127 var form_data = new FormData(this); //Encode form elements for submission
128 console.log(post_url);
129 $.ajax({
130 url: post_url,
131 type: request_method,
132 data: form_data,
133 headers: {
134 "Accept": 'application/json'
135 },
136 contentType: false,
137 processData: false
138 }).done(function (response,textStatus, jqXHR) {
139 $('#modal_instance_new_action').modal('hide');
140 bootbox.alert({
141 title: "Action",
142 message: "Action received."
143 });
144 }).fail(function(result){
145 var data = result.responseJSON;
146 var title = "Error " + (data.code ? data.code: 'unknown');
147 var message = data.detail ? data.detail: 'No detail available.';
148 bootbox.alert({
149 title: title,
150 message: message
151 });
152 });
153 });
154
155 $("#formAlarmNS").submit(function (event) {
156 event.preventDefault(); //prevent default action
157 var post_url = $(this).attr("action"); //get form action url
158 var request_method = $(this).attr("method"); //get form GET/POST method
159 var form_data = new FormData(this); //Encode form elements for submission
160 console.log(post_url);
161 $.ajax({
162 url: post_url,
163 type: request_method,
164 data: form_data,
165 headers: {
166 "Accept": 'application/json'
167 },
168 contentType: false,
169 processData: false
170 }).done(function (response,textStatus, jqXHR) {
171 $('#modal_instance_new_action').modal('hide');
172 bootbox.alert({
173 title: "Metric",
174 message: "Alarm created."
175 });
176 }).fail(function(result){
177 var data = result.responseJSON;
178 var title = "Error " + (data.code ? data.code: 'unknown');
179 var message = data.detail ? data.detail: 'No detail available.';
180 bootbox.alert({
181 title: title,
182 message: message
183 });
184 });
185 });
186
187 $("#formExportMetricNS").submit(function (event) {
188 event.preventDefault(); //prevent default action
189 var post_url = $(this).attr("action"); //get form action url
190 var request_method = $(this).attr("method"); //get form GET/POST method
191 var form_data = new FormData(this); //Encode form elements for submission
192 console.log(post_url);
193 $.ajax({
194 url: post_url,
195 type: request_method,
196 data: form_data,
197 headers: {
198 "Accept": 'application/json'
199 },
200 contentType: false,
201 processData: false
202 }).done(function (response,textStatus, jqXHR) {
203 $('#modal_instance_new_action').modal('hide');
204 bootbox.alert({
205 title: "Metric",
206 message: "Metric exported."
207 });
208 }).fail(function(result){
209 var data = result.responseJSON;
210 var title = "Error " + (data.code ? data.code: 'unknown');
211 var message = data.detail ? data.detail: 'No detail available.';
212 bootbox.alert({
213 title: title,
214 message: message
215 });
216 });
217 });
218
219 });