fix bug 923; fix bug 940
[osm/LW-UI.git] / static / src / vimhandler / vim_list.js
1 /*
2 Copyright 2019 EveryUP srl
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 $(document).ready(function () {
18 table = $('#vims_table').DataTable({
19 responsive: true,
20 "ajax": {
21 "url": "/vims/list/",
22 "dataSrc": function (json) {
23 return json['datacenters'];
24 },
25 statusCode: {
26 401: function () {
27 console.log("no auth");
28 moveToLogin(window.location.pathname);
29 }
30 },
31 "error": function (hxr, error, thrown) {
32 console.log(hxr)
33 console.log(thrown)
34 console.log(error);
35 }
36
37 },
38 "columns": [
39 {
40 "render": function (data, type, row) {
41 return row["name"];
42 },
43 "targets": 0
44 },
45 {
46 "render": function (data, type, row) {
47 return row['_id'];
48 },
49 "targets": 1
50 },
51 {
52 "render": function (data, type, row) {
53 return row["vim_type"];
54 },
55 "targets": 2
56 },
57 {
58 "render": function (data, type, row) {
59 return row["_admin"]['operationalState'];
60 },
61 "targets": 3
62 },
63 {
64 "render": function (data, type, row) {
65 return row["_admin"]['description'] || '';
66 },
67 "targets": 4
68 },
69 {
70 "render": function (data, type, row) {
71 return '<div class="btn-group"><button type="button" class="btn btn-default" ' +
72 'onclick="location.href=\'/vims/' + row['_id'] + '\'" data-toggle="tooltip" data-placement="top" data-container="body" title="Show Info">' +
73 '<i class="fa fa-info"></i>' +
74 '</button> ' +
75 '<button type="button" class="btn btn-default"' +
76 'onclick="javascript:deleteVim(\'' + row['_id'] + '\', \'' + row["name"] + '\')" data-toggle="tooltip" data-placement="top" data-container="body" title="Delete">' +
77 '<i class="far fa-trash-alt" ></i></button></div>';
78 },
79 "targets": 5,
80 "orderable": false
81 }
82 ]
83 });
84
85 setInterval(function () {
86 table.ajax.reload();
87 }, 10000);
88
89 $("#formCreateVIM").submit(function (event) {
90 event.preventDefault(); //prevent default action
91 var post_url = $(this).attr("action"); //get form action url
92 var request_method = $(this).attr("method"); //get form GET/POST method
93 var form_data = new FormData(this); //Encode form elements for submission
94 $.ajax({
95 url: post_url,
96 type: request_method,
97 data: form_data,
98 headers: {
99 "Accept": 'application/json'
100 },
101 contentType: false,
102 processData: false
103 }).done(function (response, textStatus, jqXHR) {
104 table.ajax.reload();
105 $('#modal_new_vim').modal('hide');
106 }).fail(function (result) {
107 var data = result.responseJSON;
108 var title = "Error " + (data.code ? data.code : 'unknown');
109 var message = data.detail ? data.detail : 'No detail available.';
110 bootbox.alert({
111 title: title,
112 message: message
113 });
114 });
115 });
116 });
117
118 function openModalCreateVIM() {
119 $('#modal_new_vim').modal('show');
120 }
121
122 function deleteVim(vim_id, vim_name) {
123 var url = "/vims/" + vim_id + "/delete";
124 bootbox.confirm("Are you sure want to delete " + vim_name + "?", function (result) {
125 if (result) {
126 var dialog = bootbox.dialog({
127 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
128 closeButton: true
129 });
130 $.ajax({
131 url: url,
132 type: 'GET',
133 dataType: "json",
134 contentType: "application/json;charset=utf-8",
135 success: function (result) {
136 if (result['error'] == true) {
137 dialog.modal('hide');
138 bootbox.alert("An error occurred.");
139 }
140 else {
141 dialog.modal('hide');
142 table.ajax.reload();
143 }
144 },
145 error: function (error) {
146 dialog.modal('hide');
147 var data = result.responseJSON;
148 var title = "Error " + (data && data.code ? data.code : 'unknown');
149 var message = data && data.detail ? data.detail : 'No detail available.';
150 bootbox.alert({
151 title: title,
152 message: message
153 });
154 }
155 });
156 }
157 })
158 }