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