automatic reload on lists; new django decorator for ajax request
[osm/LW-UI.git] / static / src / instancehandler / instance_list.js
index af66f16..fa9bef4 100644 (file)
    limitations under the License.
 */
 
-function performAction(url) {
+function performAction(instance_name, instance_id) {
+    var url = '/instances/ns/'+instance_id+'/action';
     $("#formActionNS").attr("action", url);
     $('#modal_instance_new_action').modal('show');
 }
 
-function deleteNs(url) {
-    bootbox.confirm("Are you sure want to delete?", function (result) {
+function exportMetricNs(instance_name, instance_id) {
+    var url = '/instances/ns/'+instance_id+'/monitoring/metric';
+    $("#formExportMetricNS").attr("action", url);
+    $('#modal_instance_export_metric').modal('show');
+}
+
+function newAlarmNs(instance_name, instance_id) {
+    var url = '/instances/ns/'+instance_id+'/monitoring/alarm';
+    $("#formAlarmNS").attr("action", url);
+    $('#modal_instance_new_alarm').modal('show');
+}
+
+function deleteNs(instance_name, instance_id, force) {
+    var url = '/instances/ns/'+instance_id+'/delete';
+    bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
         if (result) {
-            location.href = url
+            if (force)
+                url = url + '?force=true';
+            var dialog = bootbox.dialog({
+                message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
+                closeButton: true
+            });
+            $.ajax({
+                url: url,
+                type: 'GET',
+                dataType: "json",
+                contentType: "application/json;charset=utf-8",
+                success: function (result) {
+                    if (result['error'] == true){
+                        dialog.modal('hide');
+                        bootbox.alert("An error occurred.");
+                    }
+                    else {
+                        dialog.modal('hide');
+                        location.reload();
+                    }
+                },
+                error: function (error) {
+                    dialog.modal('hide');
+                    bootbox.alert("An error occurred.");
+                }
+            });
         }
     })
 }
@@ -48,7 +87,8 @@ var removeFormGroup = function (event) {
     $formGroup.remove();
 };
 
-function showInstanceDetails(url_info) {
+function showInstanceDetails(type, instance_id) {
+    var url_info = '/instances/'+type+'/'+instance_id;
     var dialog = bootbox.dialog({
         message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
         closeButton: true
@@ -59,10 +99,17 @@ function showInstanceDetails(url_info) {
         dataType: "json",
         contentType: "application/json;charset=utf-8",
         success: function (result) {
-            editorJSON.setValue(JSON.stringify(result, null, "\t"));
-            editorJSON.setOption("autoRefresh", true);
-            dialog.modal('hide');
-            $('#modal_show_instance').modal('show');
+
+            if (result['data'] !== undefined) {
+                editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
+                editorJSON.setOption("autoRefresh", true);
+                dialog.modal('hide');
+                $('#modal_show_instance').modal('show');
+            }
+            else {
+                dialog.modal('hide');
+                bootbox.alert("An error occurred while retrieving the information.");
+            }
         },
         error: function (result) {
             dialog.modal('hide');
@@ -122,20 +169,84 @@ $(document).ready(function () {
             },
             contentType: false,
             processData: false
-        }).done(function (response,textStatus, jqXHR) {
+        }).done(function (response, textStatus, jqXHR) {
             $('#modal_instance_new_action').modal('hide');
             bootbox.alert({
-                    title: "Action",
-                    message: "Action received."
-                });
-        }).fail(function(result){
-            var data  = result.responseJSON;
-            var title = "Error " + (data.code ? data.code: 'unknown');
-                var message = data.detail ? data.detail: 'No detail available.';
-                bootbox.alert({
-                    title: title,
-                    message: message
-                });
+                title: "Action",
+                message: "Action received."
+            });
+        }).fail(function (result) {
+            var data = result.responseJSON;
+            var title = "Error " + (data.code ? data.code : 'unknown');
+            var message = data.detail ? data.detail : 'No detail available.';
+            bootbox.alert({
+                title: title,
+                message: message
+            });
+        });
+    });
+
+    $("#formAlarmNS").submit(function (event) {
+        event.preventDefault(); //prevent default action
+        var post_url = $(this).attr("action"); //get form action url
+        var request_method = $(this).attr("method"); //get form GET/POST method
+        var form_data = new FormData(this); //Encode form elements for submission
+        console.log(post_url);
+        $.ajax({
+            url: post_url,
+            type: request_method,
+            data: form_data,
+            headers: {
+                "Accept": 'application/json'
+            },
+            contentType: false,
+            processData: false
+        }).done(function (response, textStatus, jqXHR) {
+            $('#modal_instance_new_action').modal('hide');
+            bootbox.alert({
+                title: "Metric",
+                message: "Alarm created."
+            });
+        }).fail(function (result) {
+            var data = result.responseJSON;
+            var title = "Error " + (data.code ? data.code : 'unknown');
+            var message = data.detail ? data.detail : 'No detail available.';
+            bootbox.alert({
+                title: title,
+                message: message
+            });
+        });
+    });
+
+    $("#formExportMetricNS").submit(function (event) {
+        event.preventDefault(); //prevent default action
+        var post_url = $(this).attr("action"); //get form action url
+        var request_method = $(this).attr("method"); //get form GET/POST method
+        var form_data = new FormData(this); //Encode form elements for submission
+        console.log(post_url);
+        $.ajax({
+            url: post_url,
+            type: request_method,
+            data: form_data,
+            headers: {
+                "Accept": 'application/json'
+            },
+            contentType: false,
+            processData: false
+        }).done(function (response, textStatus, jqXHR) {
+            $('#modal_instance_new_action').modal('hide');
+            bootbox.alert({
+                title: "Metric",
+                message: "Metric exported."
+            });
+        }).fail(function (result) {
+            var data = result.responseJSON;
+            var title = "Error " + (data.code ? data.code : 'unknown');
+            var message = data.detail ? data.detail : 'No detail available.';
+            bootbox.alert({
+                title: title,
+                message: message
+            });
         });
     });