FORCE option when deleting a NS instance
[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 deleteNs(url, force) {
23 bootbox.confirm("Are you sure want to delete?", function (result) {
24 if (result) {
25 if(force)
26 url = url + '?force=true';
27 location.href = url
28 }
29 })
30 }
31
32 var addFormGroup = function (event) {
33 event.preventDefault();
34
35 var $formGroup = $(this).closest('.form-group');
36 var $formGroupClone = $formGroup.clone();
37
38 $(this)
39 .toggleClass('btn-success btn-add btn-danger btn-remove')
40 .html('–');
41
42 $formGroupClone.find('input').val('');
43 $formGroupClone.insertAfter($formGroup);
44
45 };
46
47 var removeFormGroup = function (event) {
48 event.preventDefault();
49 var $formGroup = $(this).closest('.form-group');
50 $formGroup.remove();
51 };
52
53 function showInstanceDetails(url_info) {
54 var dialog = bootbox.dialog({
55 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
56 closeButton: true
57 });
58 $.ajax({
59 url: url_info,
60 type: 'GET',
61 dataType: "json",
62 contentType: "application/json;charset=utf-8",
63 success: function (result) {
64 editorJSON.setValue(JSON.stringify(result, null, "\t"));
65 editorJSON.setOption("autoRefresh", true);
66 dialog.modal('hide');
67 $('#modal_show_instance').modal('show');
68 },
69 error: function (result) {
70 dialog.modal('hide');
71 bootbox.alert("An error occurred while retrieving the information.");
72 }
73 });
74 }
75
76 var editorJSON;
77
78 $(document).ready(function () {
79 var json_editor_settings = {
80 mode: "javascript",
81 showCursorWhenSelecting: true,
82 autofocus: true,
83 lineNumbers: true,
84 lineWrapping: true,
85 foldGutter: true,
86 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
87 autoCloseBrackets: true,
88 matchBrackets: true,
89 extraKeys: {
90 "F11": function (cm) {
91 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
92 },
93 "Esc": function (cm) {
94 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
95 },
96 "Ctrl-Q": function (cm) {
97 cm.foldCode(cm.getCursor());
98 }
99 },
100 theme: "neat",
101 keyMap: "sublime"
102 };
103 var myJsonTextArea = document.getElementById("instance_view_json");
104 editorJSON = CodeMirror(function (elt) {
105 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
106 }, json_editor_settings);
107
108
109 $(document).on('click', '.btn-add', addFormGroup);
110 $(document).on('click', '.btn-remove', removeFormGroup);
111
112 $("#formActionNS").submit(function (event) {
113 event.preventDefault(); //prevent default action
114 var post_url = $(this).attr("action"); //get form action url
115 var request_method = $(this).attr("method"); //get form GET/POST method
116 var form_data = new FormData(this); //Encode form elements for submission
117 console.log(post_url);
118 $.ajax({
119 url: post_url,
120 type: request_method,
121 data: form_data,
122 headers: {
123 "Accept": 'application/json'
124 },
125 contentType: false,
126 processData: false
127 }).done(function (response,textStatus, jqXHR) {
128 $('#modal_instance_new_action').modal('hide');
129 bootbox.alert({
130 title: "Action",
131 message: "Action received."
132 });
133 }).fail(function(result){
134 var data = result.responseJSON;
135 var title = "Error " + (data.code ? data.code: 'unknown');
136 var message = data.detail ? data.detail: 'No detail available.';
137 bootbox.alert({
138 title: title,
139 message: message
140 });
141 });
142 });
143
144 });