automatic reload on lists; new django decorator for ajax request
[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 (error) {
61 dialog.modal('hide');
62 bootbox.alert("An error occurred.");
63 }
64 });
65 }
66 })
67 }
68
69 var addFormGroup = function (event) {
70 event.preventDefault();
71
72 var $formGroup = $(this).closest('.form-group');
73 var $formGroupClone = $formGroup.clone();
74
75 $(this)
76 .toggleClass('btn-success btn-add btn-danger btn-remove')
77 .html('–');
78
79 $formGroupClone.find('input').val('');
80 $formGroupClone.insertAfter($formGroup);
81
82 };
83
84 var removeFormGroup = function (event) {
85 event.preventDefault();
86 var $formGroup = $(this).closest('.form-group');
87 $formGroup.remove();
88 };
89
90 function showInstanceDetails(type, instance_id) {
91 var url_info = '/instances/'+type+'/'+instance_id;
92 var dialog = bootbox.dialog({
93 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
94 closeButton: true
95 });
96 $.ajax({
97 url: url_info,
98 type: 'GET',
99 dataType: "json",
100 contentType: "application/json;charset=utf-8",
101 success: function (result) {
102
103 if (result['data'] !== undefined) {
104 editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
105 editorJSON.setOption("autoRefresh", true);
106 dialog.modal('hide');
107 $('#modal_show_instance').modal('show');
108 }
109 else {
110 dialog.modal('hide');
111 bootbox.alert("An error occurred while retrieving the information.");
112 }
113 },
114 error: function (result) {
115 dialog.modal('hide');
116 bootbox.alert("An error occurred while retrieving the information.");
117 }
118 });
119 }
120
121 var editorJSON;
122
123 $(document).ready(function () {
124 var json_editor_settings = {
125 mode: "javascript",
126 showCursorWhenSelecting: true,
127 autofocus: true,
128 lineNumbers: true,
129 lineWrapping: true,
130 foldGutter: true,
131 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
132 autoCloseBrackets: true,
133 matchBrackets: true,
134 extraKeys: {
135 "F11": function (cm) {
136 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
137 },
138 "Esc": function (cm) {
139 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
140 },
141 "Ctrl-Q": function (cm) {
142 cm.foldCode(cm.getCursor());
143 }
144 },
145 theme: "neat",
146 keyMap: "sublime"
147 };
148 var myJsonTextArea = document.getElementById("instance_view_json");
149 editorJSON = CodeMirror(function (elt) {
150 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
151 }, json_editor_settings);
152
153
154 $(document).on('click', '.btn-add', addFormGroup);
155 $(document).on('click', '.btn-remove', removeFormGroup);
156
157 $("#formActionNS").submit(function (event) {
158 event.preventDefault(); //prevent default action
159 var post_url = $(this).attr("action"); //get form action url
160 var request_method = $(this).attr("method"); //get form GET/POST method
161 var form_data = new FormData(this); //Encode form elements for submission
162 console.log(post_url);
163 $.ajax({
164 url: post_url,
165 type: request_method,
166 data: form_data,
167 headers: {
168 "Accept": 'application/json'
169 },
170 contentType: false,
171 processData: false
172 }).done(function (response, textStatus, jqXHR) {
173 $('#modal_instance_new_action').modal('hide');
174 bootbox.alert({
175 title: "Action",
176 message: "Action received."
177 });
178 }).fail(function (result) {
179 var data = result.responseJSON;
180 var title = "Error " + (data.code ? data.code : 'unknown');
181 var message = data.detail ? data.detail : 'No detail available.';
182 bootbox.alert({
183 title: title,
184 message: message
185 });
186 });
187 });
188
189 $("#formAlarmNS").submit(function (event) {
190 event.preventDefault(); //prevent default action
191 var post_url = $(this).attr("action"); //get form action url
192 var request_method = $(this).attr("method"); //get form GET/POST method
193 var form_data = new FormData(this); //Encode form elements for submission
194 console.log(post_url);
195 $.ajax({
196 url: post_url,
197 type: request_method,
198 data: form_data,
199 headers: {
200 "Accept": 'application/json'
201 },
202 contentType: false,
203 processData: false
204 }).done(function (response, textStatus, jqXHR) {
205 $('#modal_instance_new_action').modal('hide');
206 bootbox.alert({
207 title: "Metric",
208 message: "Alarm created."
209 });
210 }).fail(function (result) {
211 var data = result.responseJSON;
212 var title = "Error " + (data.code ? data.code : 'unknown');
213 var message = data.detail ? data.detail : 'No detail available.';
214 bootbox.alert({
215 title: title,
216 message: message
217 });
218 });
219 });
220
221 $("#formExportMetricNS").submit(function (event) {
222 event.preventDefault(); //prevent default action
223 var post_url = $(this).attr("action"); //get form action url
224 var request_method = $(this).attr("method"); //get form GET/POST method
225 var form_data = new FormData(this); //Encode form elements for submission
226 console.log(post_url);
227 $.ajax({
228 url: post_url,
229 type: request_method,
230 data: form_data,
231 headers: {
232 "Accept": 'application/json'
233 },
234 contentType: false,
235 processData: false
236 }).done(function (response, textStatus, jqXHR) {
237 $('#modal_instance_new_action').modal('hide');
238 bootbox.alert({
239 title: "Metric",
240 message: "Metric exported."
241 });
242 }).fail(function (result) {
243 var data = result.responseJSON;
244 var title = "Error " + (data.code ? data.code : 'unknown');
245 var message = data.detail ? data.detail : 'No detail available.';
246 bootbox.alert({
247 title: title,
248 message: message
249 });
250 });
251 });
252
253 });