Bug 662 - Cosmetic: UI enhancement fixedgit add static/src/instancehandler/instance_l...
[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(instance_name, instance_id) {
18 var url = '/instances/ns/'+instance_id+'/action';
19 $("#formActionNS").attr("action", url);
20 $('#modal_instance_new_action').modal('show');
21 }
22
23 function exportMetricNs(instance_name, instance_id) {
24 var url = '/instances/ns/'+instance_id+'/monitoring/metric';
25 $("#formExportMetricNS").attr("action", url);
26 $('#modal_instance_export_metric').modal('show');
27 }
28
29 function newAlarmNs(instance_name, instance_id) {
30 var url = '/instances/ns/'+instance_id+'/monitoring/alarm';
31 $("#formAlarmNS").attr("action", url);
32 $('#modal_instance_new_alarm').modal('show');
33 }
34
35 function deleteNs(instance_name, instance_id, force) {
36 var url = '/instances/ns/'+instance_id+'/delete';
37 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
38 if (result) {
39 if (force)
40 url = url + '?force=true';
41 var dialog = bootbox.dialog({
42 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
43 closeButton: true
44 });
45 $.ajax({
46 url: url,
47 type: 'GET',
48 dataType: "json",
49 contentType: "application/json;charset=utf-8",
50 success: function (result) {
51 if (result['error'] == true){
52 dialog.modal('hide');
53 bootbox.alert("An error occurred.");
54 }
55 else {
56 dialog.modal('hide');
57 location.reload();
58 }
59 },
60 error: function (result) {
61 dialog.modal('hide');
62 var data = result.responseJSON;
63 var title = "Error " + (data && data.code ? data.code : 'unknown');
64 var message = data && data.detail ? data.detail : 'No detail available.';
65 bootbox.alert({
66 title: title,
67 message: message
68 });
69 }
70 });
71 }
72 })
73 }
74 function deleteNsi(instance_name, instance_id, force) {
75 var url = '/instances/nsi/'+instance_id+'/delete';
76 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
77 if (result) {
78 if (force)
79 url = url + '?force=true';
80 var dialog = bootbox.dialog({
81 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
82 closeButton: true
83 });
84 $.ajax({
85 url: url,
86 type: 'GET',
87 dataType: "json",
88 contentType: "application/json;charset=utf-8",
89 success: function (result) {
90 console.log(result)
91 if (result['error'] == true){
92 dialog.modal('hide');
93 var data = result.responseJSON;
94 var title = "Error " + (data && data.code ? data.code : 'unknown');
95 var message = data && data.detail ? data.detail : 'No detail available.';
96 bootbox.alert({
97 title: title,
98 message: message
99 });
100 }
101 else {
102 dialog.modal('hide');
103 location.reload();
104 }
105 },
106 error: function (result) {
107 dialog.modal('hide');
108 var data = result.responseJSON;
109 var title = "Error " + (data && data.code ? data.code : 'unknown');
110 var message = data && data.detail ? data.detail : 'No detail available.';
111 bootbox.alert({
112 title: title,
113 message: message
114 });
115 }
116 });
117 }
118 })
119 }
120
121 function deletePDU(instance_name, instance_id) {
122 var url = '/instances/pdu/'+instance_id+'/delete';
123 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
124 if (result) {
125 var dialog = bootbox.dialog({
126 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
127 closeButton: true
128 });
129 $.ajax({
130 url: url,
131 type: 'GET',
132 dataType: "json",
133 contentType: "application/json;charset=utf-8",
134 success: function (result) {
135 if (result['error'] == true){
136 dialog.modal('hide');
137 bootbox.alert("An error occurred.");
138 }
139 else {
140 dialog.modal('hide');
141 location.reload();
142 }
143 },
144 error: function (error) {
145 dialog.modal('hide');
146 bootbox.alert("An error occurred.");
147 }
148 });
149 }
150 })
151 }
152
153 var addFormGroup = function (event) {
154 event.preventDefault();
155
156 var $formGroup = $(this).closest('.form-group');
157 var $formGroupClone = $formGroup.clone();
158
159 $formGroupClone.find('input').val('');
160 $formGroupClone.find('button').toggleClass('btn-success btn-add btn-danger btn-remove');
161 $formGroupClone.find('button').text('–');
162 $formGroupClone.insertAfter($formGroup);
163
164 };
165
166 var removeFormGroup = function (event) {
167 event.preventDefault();
168 var $formGroup = $(this).closest('.form-group');
169 $formGroup.remove();
170 };
171
172 var addInterfaceGroup = function (event) {
173 event.preventDefault();
174
175 var $formGroup = $(this).closest('.interface-group');
176 var $formGroupClone = $formGroup.clone();
177
178 $(this)
179 .toggleClass('btn-success btn-add btn-danger btn-remove')
180 .html('–');
181
182 $formGroupClone.find('input').val('');
183 $formGroupClone.insertAfter($formGroup);
184
185 };
186
187 var removeInterfaceGroup = function (event) {
188 event.preventDefault();
189 var $formGroup = $(this).closest('.interface-group');
190 $formGroup.remove();
191 };
192
193 function showTopology(type, instance_id) {
194 var url = '/instances/'+type+'/'+instance_id+'/topology';
195 window.location = url;
196 }
197
198 function showInstanceDetails(type, instance_id) {
199 var url_info = '/instances/'+type+'/'+instance_id;
200 var dialog = bootbox.dialog({
201 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
202 closeButton: true
203 });
204 $.ajax({
205 url: url_info,
206 type: 'GET',
207 dataType: "json",
208 contentType: "application/json;charset=utf-8",
209 success: function (result) {
210
211 if (result['data'] !== undefined) {
212 editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
213 editorJSON.setOption("autoRefresh", true);
214 dialog.modal('hide');
215 $('#modal_show_instance').modal('show');
216 }
217 else {
218 dialog.modal('hide');
219 bootbox.alert("An error occurred while retrieving the information.");
220 }
221 },
222 error: function (result) {
223 dialog.modal('hide');
224 bootbox.alert("An error occurred while retrieving the information.");
225 }
226 });
227 }
228
229 var editorJSON;
230
231 $(document).ready(function () {
232 var json_editor_settings = {
233 mode: "javascript",
234 showCursorWhenSelecting: true,
235 autofocus: true,
236 lineNumbers: true,
237 lineWrapping: true,
238 foldGutter: true,
239 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
240 autoCloseBrackets: true,
241 matchBrackets: true,
242 extraKeys: {
243 "F11": function (cm) {
244 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
245 },
246 "Esc": function (cm) {
247 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
248 },
249 "Ctrl-Q": function (cm) {
250 cm.foldCode(cm.getCursor());
251 }
252 },
253 theme: "neat",
254 keyMap: "sublime"
255 };
256 var myJsonTextArea = document.getElementById("instance_view_json");
257 editorJSON = CodeMirror(function (elt) {
258 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
259 }, json_editor_settings);
260
261
262 $(document).on('click', '.primitive-group .btn-add', addFormGroup);
263 $(document).on('click', '.primitive-group .btn-remove', removeFormGroup);
264
265 $(document).on('click', '.interface-group .btn-add', addInterfaceGroup);
266 $(document).on('click', '.interface-group .btn-remove', removeInterfaceGroup);
267
268 $("#formCreatePDU").submit(function (event) {
269 event.preventDefault(); //prevent default action
270 var post_url = $(this).attr("action"); //get form action url
271 var request_method = $(this).attr("method"); //get form GET/POST method
272 var form_data = new FormData(this); //Encode form elements for submission
273 $.ajax({
274 url: post_url,
275 type: request_method,
276 data: form_data,
277 headers: {
278 "Accept": 'application/json'
279 },
280 contentType: false,
281 processData: false
282 }).done(function (response, textStatus, jqXHR) {
283 table.ajax.reload();
284 $('#modal_new_pdu').modal('hide');
285 }).fail(function (result) {
286 var data = result.responseJSON;
287 var title = "Error " + (data.code ? data.code : 'unknown');
288 var message = data.detail ? data.detail : 'No detail available.';
289 bootbox.alert({
290 title: title,
291 message: message
292 });
293 });
294 });
295
296 $("#formActionNS").submit(function (event) {
297 event.preventDefault(); //prevent default action
298 var post_url = $(this).attr("action"); //get form action url
299 var request_method = $(this).attr("method"); //get form GET/POST method
300 var form_data = new FormData(this); //Encode form elements for submission
301 console.log(post_url);
302 $.ajax({
303 url: post_url,
304 type: request_method,
305 data: form_data,
306 headers: {
307 "Accept": 'application/json'
308 },
309 contentType: false,
310 processData: false
311 }).done(function (response, textStatus, jqXHR) {
312 $('#modal_instance_new_action').modal('hide');
313 bootbox.alert({
314 title: "Action",
315 message: "Action received."
316 });
317 }).fail(function (result) {
318 var data = result.responseJSON;
319 var title = "Error " + (data.code ? data.code : 'unknown');
320 var message = data.detail ? data.detail : 'No detail available.';
321 bootbox.alert({
322 title: title,
323 message: message
324 });
325 });
326 });
327
328 $("#formAlarmNS").submit(function (event) {
329 event.preventDefault(); //prevent default action
330 var post_url = $(this).attr("action"); //get form action url
331 var request_method = $(this).attr("method"); //get form GET/POST method
332 var form_data = new FormData(this); //Encode form elements for submission
333 console.log(post_url);
334 $.ajax({
335 url: post_url,
336 type: request_method,
337 data: form_data,
338 headers: {
339 "Accept": 'application/json'
340 },
341 contentType: false,
342 processData: false
343 }).done(function (response, textStatus, jqXHR) {
344 $('#modal_instance_new_action').modal('hide');
345 bootbox.alert({
346 title: "Metric",
347 message: "Alarm created."
348 });
349 }).fail(function (result) {
350 var data = result.responseJSON;
351 var title = "Error " + (data.code ? data.code : 'unknown');
352 var message = data.detail ? data.detail : 'No detail available.';
353 bootbox.alert({
354 title: title,
355 message: message
356 });
357 });
358 });
359
360 $("#formExportMetricNS").submit(function (event) {
361 event.preventDefault(); //prevent default action
362 var post_url = $(this).attr("action"); //get form action url
363 var request_method = $(this).attr("method"); //get form GET/POST method
364 var form_data = new FormData(this); //Encode form elements for submission
365 console.log(post_url);
366 $.ajax({
367 url: post_url,
368 type: request_method,
369 data: form_data,
370 headers: {
371 "Accept": 'application/json'
372 },
373 contentType: false,
374 processData: false
375 }).done(function (response, textStatus, jqXHR) {
376 $('#modal_instance_new_action').modal('hide');
377 bootbox.alert({
378 title: "Metric",
379 message: "Metric exported."
380 });
381 }).fail(function (result) {
382 var data = result.responseJSON;
383 var title = "Error " + (data.code ? data.code : 'unknown');
384 var message = data.detail ? data.detail : 'No detail available.';
385 bootbox.alert({
386 title: title,
387 message: message
388 });
389 });
390 });
391
392 });