fix error propagation
[osm/LW-UI.git] / static / src / packagehandler / packages_list.js
1 /*
2 Copyright 2018 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 $(document).ready(function () {
17 $("#formCreateNS").submit(function (event) {
18 event.preventDefault(); //prevent default action
19 var post_url = $(this).attr("action"); //get form action url
20 var request_method = $(this).attr("method"); //get form GET/POST method
21 var form_data = new FormData(this); //Encode form elements for submission
22 $.ajax({
23 url: post_url,
24 type: request_method,
25 data: form_data,
26 headers: {
27 "Accept": 'application/json'
28 },
29 contentType: false,
30 processData: false
31 }).done(function (response, textStatus, jqXHR) {
32 window.location.href = '/instances/ns/list/';
33
34 }).fail(function (result) {
35 var data = result.responseJSON;
36 var title = "Error " + (data.code ? data.code : 'unknown');
37 var message = data.detail ? data.detail : 'No detail available.';
38 bootbox.alert({
39 title: title,
40 message: message
41 });
42 });
43 });
44
45
46 });
47
48 function deletePackage(package_type, package_id, package_name) {
49
50 bootbox.confirm("Are you sure want to delete " + package_name + "?", function (result) {
51 if (result) {
52 var dialog = bootbox.dialog({
53 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
54 closeButton: true
55 });
56 $.ajax({
57 url: '/packages/' + package_type + '/' + package_id + '/delete',
58 type: 'GET',
59 dataType: "json",
60 contentType: "application/json;charset=utf-8",
61 success: function (result) {
62 dialog.modal('hide');
63 table.ajax.reload();
64 },
65 error: function (result) {
66 dialog.modal('hide');
67 var data = result.responseJSON;
68 var title = "Error " + (data && data.code ? data.code : 'unknown');
69 var message = data && data.detail ? data.detail : 'No detail available.';
70 bootbox.alert({
71 title: title,
72 message: message
73 });
74 }
75 });
76 }
77 })
78 }
79
80 function clonePackage(package_type, package_id) {
81
82 bootbox.confirm("Are you sure want to clone?", function (result) {
83 if (result) {
84 var dialog = bootbox.dialog({
85 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
86 closeButton: true
87 });
88 $.ajax({
89 url: '/packages/' + package_type + '/' + package_id + '/clone',
90 type: 'GET',
91 dataType: "json",
92 contentType: "application/json;charset=utf-8",
93 success: function (result) {
94 dialog.modal('hide');
95 table.ajax.reload();
96 },
97 error: function (result) {
98 dialog.modal('hide');
99 var data = result.responseJSON;
100 var title = "Error " + (data && data.code ? data.code : 'unknown');
101 var message = data && data.detail ? data.detail : 'No detail available.';
102 bootbox.alert({
103 title: title,
104 message: message
105 });
106 }
107 });
108 }
109 })
110 }
111
112
113 function openPackageContentList(type, pkg_id) {
114 var dialog = bootbox.dialog({
115 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
116 closeButton: true
117 });
118 $.ajax({
119 url: '/packages/' + type + '/' + pkg_id + '/action/get_package_files_list',
120 type: 'GET',
121 dataType: "json",
122 contentType: "application/json;charset=utf-8",
123 success: function (result) {
124 //$('#modal_show_vim_body').empty();
125 dialog.modal('hide');
126 build_file_list("Files in " + pkg_id, result.files);
127 },
128 error: function (result) {
129 dialog.modal('hide');
130 bootbox.alert("An error occurred while retrieving the package content.");
131 }
132 });
133 }
134
135
136 function build_file_list(title, list) {
137 $('#files_list_tbody').find('tr:gt(0)').remove();
138 $('#files_list_tbody_title').text(title)
139 for (var i in list) {
140 var template = '<tr><td>-</td><td>' + list[i] + '</td><td><button type="button" class="btn btn-default" onclick="" disabled><i class="fa fa-folder-open"></i></button></td></tr>'
141 $('#files_list_tbody').append(template)
142 }
143 $('#modal_files_list').modal('show');
144 }
145