From 97ae9ffe7fded45710004792ee2cfbcebae599e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= <cmartin@opennebula.org>
Date: Fri, 19 Jun 2015 12:04:05 +0200
Subject: [PATCH] Feature #3748: Finish host wilds panel

---
 .../public/app/tabs/hosts-tab/panels/wilds.js | 103 ++++++++++++------
 .../public/app/tabs/vms-tab/actions.js        |  41 +++----
 2 files changed, 88 insertions(+), 56 deletions(-)

diff --git a/src/sunstone/public/app/tabs/hosts-tab/panels/wilds.js b/src/sunstone/public/app/tabs/hosts-tab/panels/wilds.js
index c50bcb254f..ecf4fbd91a 100644
--- a/src/sunstone/public/app/tabs/hosts-tab/panels/wilds.js
+++ b/src/sunstone/public/app/tabs/hosts-tab/panels/wilds.js
@@ -6,6 +6,10 @@ define(function(require) {
   require('foundation-datatables');
   var Locale = require('utils/locale');
   var CanImportWilds = require('../utils/can-import-wilds');
+  var OpenNebulaVM = require('opennebula/vm');
+  var OpenNebulaAction = require('opennebula/action');
+  var Sunstone = require('sunstone');
+  var Notifier = require('utils/notifier');
 
   /*
     TEMPLATES
@@ -30,13 +34,8 @@ define(function(require) {
 
     this.element = info[RESOURCE.toUpperCase()];
 
-    // Do not create an instance of this panel if the Wilds cannot be imported
-    if (!CanImportWilds(this.element)) {
-      throw "Panel not available for this element";
-    }
-
     return this;
-  };
+  }
 
   Panel.PANEL_ID = PANEL_ID;
   Panel.prototype.html = _html;
@@ -54,59 +53,84 @@ define(function(require) {
 
   function _setup(context) {
     var that = this;
+
+    // Hide the import button if the Wilds cannot be imported
+    if (!CanImportWilds(this.element)) {
+      $("#import_wilds").hide();
+    }
+
     that.dataTableWildHosts = $("#datatable_host_wilds", context).dataTable({
       "bSortClasses" : false,
-      "bDeferRender": true
+      "bDeferRender": true,
+      "aoColumnDefs": [
+          {"bSortable": false, "aTargets": [0]},
+          {"sWidth": "35px", "aTargets": [0]}
+      ]
     });
 
-    var wilds_list_array = [];
-
     if (that.element.TEMPLATE.VM) {
       wilds = that.element.TEMPLATE.VM;
 
-      $.each(wilds, function() {
-        name      = this.VM_NAME;
-        safe_name = name.replace(/ /g, "_").replace(/\./g, "_");
-        deploy_id = this.DEPLOY_ID;
+      if (!$.isArray(wilds)) { // If only 1 VM convert to array
+        wilds = [wilds];
+      }
 
-        wilds_list_array.push([
-            '<input type="checkbox" id="import_wild_checker" class="import_' + safe_name + '" unchecked/>',
+      $.each(wilds, function() {
+        var name      = this.VM_NAME;
+        var safe_name = name.replace(/ /g, "_").replace(/\./g, "_");
+        var deploy_id = this.DEPLOY_ID;
+
+        var wilds_list_array = [
+          [
+            '<input type="checkbox" class="import_wild_checker import_' + safe_name + '" unchecked/>',
             name,
             deploy_id
-        ]);
+          ]
+        ];
 
         that.dataTableWildHosts.fnAddData(wilds_list_array);
 
         $(".import_" + safe_name, that.dataTableWildHosts).data("wild_template", atob(this.IMPORT_TEMPLATE));
-        $(".import_" + safe_name, that.dataTableWildHosts).data("host_id", that.element.ID);
-
-        wilds_list_array = [];
       });
     }
 
     delete that.element.TEMPLATE.WILDS;
     delete that.element.TEMPLATE.VM;
 
+    // Enable the import button when at least a VM is selected
+    $("#import_wilds", context).attr("disabled", "disabled").on("click.disable", function(e) { return false; });
+
+    $(".import_wild_checker", context).off("change");
+    $(".import_wild_checker", context).on("change", function(){
+      if ($(".import_wild_checker:checked", context).length == 0){
+        $("#import_wilds", context).attr("disabled", "disabled").on("click.disable", function(e) { return false; });
+      } else {
+        $("#import_wilds", context).removeAttr("disabled").off("click.disable");
+      }
+    });
+
     // Add event listener for importing WILDS
-    /* TODO Use only 1 action -----  
     context.off("click", '#import_wilds');
     context.on("click", '#import_wilds', function () {
-      $("#import_wild_checker:checked", "#datatable_host_wilds").each(function() {
+      $("#import_wilds", context).attr("disabled", "disabled").on("click.disable", function(e) { return false; });
+      $("#import_wilds", context).html('<i class="fa fa-spinner fa-spin"></i>');
+
+      $(".import_wild_checker:checked", "#datatable_host_wilds").each(function() {
         var vm_json = {
           "vm": {
             "vm_raw": $(this).data("wild_template")
           }
         };
 
-        var import_host_id = $(this).data("host_id");
+        var import_host_id = that.element.ID;
         var wild_row       = $(this).closest('tr');
 
         // Create the VM in OpenNebula
-        OpenNebula.VM.create({
+        OpenNebulaVM.create({
           timeout: true,
           data: vm_json,
           success: function(request, response) {
-            //TODO OpenNebula.Helper.clear_cache("VM");
+            OpenNebulaAction.clear_cache("VM");
 
             var extra_info = {};
 
@@ -115,23 +139,36 @@ define(function(require) {
             extra_info['enforce'] = false;
 
             // Deploy the VM
-            Sunstone.runAction("VM.silent_deploy_action", 
-                               response.VM.ID, 
+            Sunstone.runAction("VM.silent_deploy_action",
+                               response.VM.ID,
                                extra_info);
 
             // Notify
-            notifyCustom(tr("VM imported"), " ID: " + response.VM.ID, false);
+            Notifier.notifyCustom(Locale.tr("VM imported"), " ID: " + response.VM.ID, false);
 
             // Delete row (shouldn't be there in next monitorization)
-            dataTable_wilds_hosts = $("#datatable_host_wilds").dataTable();
-            dataTable_wilds_hosts.fnDeleteRow(wild_row);
+            that.dataTableWildHosts.fnDeleteRow(wild_row);
+
+            $("#import_wilds", context).removeAttr("disabled").off("click.disable");
+            $("#import_wilds", context).html(Locale.tr("Import Wilds"));
           },
           error: function (request, error_json) {
-            notifyError(error_json.error.message || tr("Cannot contact server: is it running and reachable?"));
+            var msg;
+            if (error_json.error.message){
+              msg = error_json.error.message;
+            } else {
+              msg = Locale.tr("Cannot contact server: is it running and reachable?");
+            }
+
+            Notifier.notifyError(msg);
+
+            $("#import_wilds", context).removeAttr("disabled").off("click.disable");
+            $("#import_wilds", context).html(Locale.tr("Import Wilds"));
           }
         });
-      })
-    });*/
+      });
+    });
+
     return false;
   }
-})
+});
\ No newline at end of file
diff --git a/src/sunstone/public/app/tabs/vms-tab/actions.js b/src/sunstone/public/app/tabs/vms-tab/actions.js
index 244a0b7984..b5556b8e58 100644
--- a/src/sunstone/public/app/tabs/vms-tab/actions.js
+++ b/src/sunstone/public/app/tabs/vms-tab/actions.js
@@ -66,12 +66,28 @@ define(function(require) {
         Sunstone.showFormPanel(TAB_ID, CREATE_DIALOG_ID, "create");
       }
     },
+    "VM.create" : {
+      type: "custom",
+      call: function(id, name) {
+        Sunstone.runAction("Template.instantiate", [id], name);
+        Sunstone.runAction("VM.refresh");
+      },
+      callback: function(request, response) {
+        Sunstone.getDataTable(TAB_ID).addElement(request, response);
+      },
+      error: Notifier.onError
+    },
     "VM.deploy" : {
       type: "custom",
       call: function() {
         Sunstone.getDialog(DEPLOY_DIALOG_ID).show();
       }
     },
+    "VM.silent_deploy_action" : {
+      type: "single",
+      call: OpenNebulaVM.deploy,
+      error: Notifier.onError
+    },
     "VM.migrate" : {
       type: "custom",
       call: function() {
@@ -160,29 +176,8 @@ define(function(require) {
     //  },
     //  notify: true
     //},
-    /*"VM.create" : {
-      type: "custom",
-      call: function(id, name) {
-        Sunstone.runAction("Template.instantiate", [id], name);
-        Sunstone.runAction("VM.list");
-      },
-      callback: function(request, response) {
-        //Sunstone.resetFormPanel(TAB_ID, CREATE_DIALOG_ID);
-        //Sunstone.hideFormPanel(TAB_ID);
-        Sunstone.getDataTable(TAB_ID).addElement(request, response);
-      },
-      error: onError
-    },
- 
- 
-    "VM.silent_deploy_action" : {
-      type: "single",
-      call: OpenNebula.VM.deploy,
-      error: onError
-    },    
- 
- 
- 
+    /*
+
     "VM.saveas" : {
       type: "single",
       call: OpenNebula.VM.saveas,