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