From 09be9b3fe6ea65d655de538081cf955c705304ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 29 May 2015 13:11:59 +0200 Subject: [PATCH] Feature #3748: Refactor the VDC resources tab --- .../app/tabs/vdcs-tab/panels/resources.js | 22 ++- .../public/app/tabs/vdcs-tab/utils/common.js | 109 +---------- .../app/tabs/vdcs-tab/utils/resources-tab.js | 99 ++++++++++ .../resources-tab}/html.hbs | 0 .../app/tabs/vdcs-tab/utils/resources-zone.js | 176 ++++++++++++++++++ .../html.hbs} | 0 6 files changed, 289 insertions(+), 117 deletions(-) create mode 100644 src/sunstone/public/app/tabs/vdcs-tab/utils/resources-tab.js rename src/sunstone/public/app/tabs/vdcs-tab/{panels/resources => utils/resources-tab}/html.hbs (100%) create mode 100644 src/sunstone/public/app/tabs/vdcs-tab/utils/resources-zone.js rename src/sunstone/public/app/tabs/vdcs-tab/utils/{resourceTab.hbs => resources-zone/html.hbs} (100%) diff --git a/src/sunstone/public/app/tabs/vdcs-tab/panels/resources.js b/src/sunstone/public/app/tabs/vdcs-tab/panels/resources.js index 82db96d1ce..f8833d5c5c 100644 --- a/src/sunstone/public/app/tabs/vdcs-tab/panels/resources.js +++ b/src/sunstone/public/app/tabs/vdcs-tab/panels/resources.js @@ -3,9 +3,9 @@ define(function(require) { DEPENDENCIES */ - var TemplateHTML = require('hbs!./resources/html'); var Locale = require('utils/locale'); var Utils = require('../utils/common'); + var ResourcesTab = require('../utils/resources-tab'); /* CONSTANTS @@ -29,6 +29,8 @@ define(function(require) { this.element = info[XML_ROOT]; + this.resourcesTab = new ResourcesTab("vdc_info_panel"); + return this; } @@ -43,21 +45,23 @@ define(function(require) { */ function _html() { - return TemplateHTML(); + return this.resourcesTab.html(); } function _setup(context) { + var that = this; + var indexed_resources = Utils.indexedVdcResources(this.element); $.each(indexed_resources, function(zone_id,objects){ - Utils.addVdcResourceTab( - "vdc_info_panel", - zone_id, - Sunstone.getDataTable(ZONE_TAB_ID).getName(zone_id), - context, - indexed_resources); + that.resourcesTab.addResourcesZone( + zone_id, + Sunstone.getDataTable(ZONE_TAB_ID).getName(zone_id), + context, + indexed_resources); }); - Utils.setupVdcResourceTab("vdc_info_panel", context); + that.resourcesTab.setup(context); + that.resourcesTab.onShow(context); } }); diff --git a/src/sunstone/public/app/tabs/vdcs-tab/utils/common.js b/src/sunstone/public/app/tabs/vdcs-tab/utils/common.js index d1f6438747..1d212d96f5 100644 --- a/src/sunstone/public/app/tabs/vdcs-tab/utils/common.js +++ b/src/sunstone/public/app/tabs/vdcs-tab/utils/common.js @@ -5,14 +5,6 @@ define(function(require) { var VDC_ALL_RESOURCES = "-10"; - var ResourceTabHTML = require('hbs!./resourceTab'); - - var ClustersTable = require('tabs/clusters-tab/datatable'); - var HostsTable = require('tabs/hosts-tab/datatable'); - var VNetsTable = require('tabs/vnets-tab/datatable'); - var DatastoresTable = require('tabs/datastores-tab/datatable'); - var Tips = require('utils/tips'); - /** Return an object with the VDC resources indexed by zone_id. @@ -98,107 +90,8 @@ define(function(require) { return resources; } - function _addVdcResourceTab(unique_id_prefix, zone_id, zone_name, context, indexed_resources) { - var unique_id = unique_id_prefix+'_'+zone_id; - - var resources = undefined; - if(indexed_resources != undefined && indexed_resources[zone_id] != undefined){ - resources = indexed_resources[zone_id]; - } - - var opts = {}; - - $.each(["clusters", "hosts", "vnets", "datastores"], function(i,res_name){ - if(resources != undefined){ - opts[res_name] = { - info: true, - select: true, - selectOptions: { - read_only: true, - zone_id: zone_id - } - }; - - if (!(resources[res_name].length == 1 && resources[res_name][0] == VDC_ALL_RESOURCES)){ - opts[res_name].selectOptions.fixed_ids = resources[res_name]; - } - } else { - opts[res_name] = { - info: true, - select: true, - selectOptions: { - multiple_choice: true, - zone_id: zone_id - } - }; - } - }); - - var clustersTable = new ClustersTable("vdc_clusters_"+unique_id, opts["clusters"]); - var hostsTable = new HostsTable("vdc_hosts_"+unique_id, opts["hosts"]); - var vnetsTable = new VNetsTable("vdc_vnets_"+unique_id, opts["vnets"]); - var datastoresTable = new DatastoresTable("vdc_datastores_"+unique_id, opts["datastores"]); - - // Append the new div containing the tab and add the tab to the list - var html_tab_content = - '
'+ - ResourceTabHTML({ - 'unique_id': unique_id, - 'clustersTableHTML': clustersTable.dataTableHTML, - 'hostsTableHTML': hostsTable.dataTableHTML, - 'vnetsTableHTML': vnetsTable.dataTableHTML, - 'datastoresTableHTML': datastoresTable.dataTableHTML - })+ - '
'; - - $(html_tab_content).appendTo($(".vdc_zones_tabs_content", context)); - - var zone_section = $('#' +unique_id+'Tab', context); - - $("select.vdc_zones_select", context).append( - ''); - - zone_section.foundation(); - - $.each(["clusters", "hosts", "vnets", "datastores"], function(i,res_name){ - if(resources != undefined){ - if (resources[res_name].length == 1 && resources[res_name][0] == VDC_ALL_RESOURCES){ - $("#all_"+res_name+"_"+unique_id, zone_section).prop("checked", "checked"); - } - - $("#all_"+res_name+"_"+unique_id, zone_section).prop("disabled", true); - } - - $("input[name='all_"+res_name+"_"+unique_id+"']", zone_section).change(function(){ - if ($(this).prop("checked")){ - $("div.vdc_"+res_name+"_select", zone_section).hide(); - } else { - $("div.vdc_"+res_name+"_select", zone_section).show(); - } - }); - }); - - $.each([clustersTable, hostsTable, vnetsTable, datastoresTable], function(){ - this.initialize(); - this.refreshResourceTableSelect(); - }); - - Tips.setup(zone_section); - } - - function _setupVdcResourceTab(unique_id_prefix, context){ - $("select.vdc_zones_select", context).change(function(){ - context.find(".vdc_zone_content").hide(); - $('div#'+unique_id_prefix+'_'+$(this).val()+'Tab', context).show(); - }); - - $("select.vdc_zones_select", context)[0].selectedIndex = 0; - $("select.vdc_zones_select", context).change(); - } - return { 'indexedVdcResources': _indexedVdcResources, - 'addVdcResourceTab': _addVdcResourceTab, - 'setupVdcResourceTab': _setupVdcResourceTab + 'VDC_ALL_RESOURCES': VDC_ALL_RESOURCES }; }); diff --git a/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-tab.js b/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-tab.js new file mode 100644 index 0000000000..432b8bef1e --- /dev/null +++ b/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-tab.js @@ -0,0 +1,99 @@ +define(function(require) { + + var TemplateHTML = require('hbs!./resources-tab/html'); + var ResourcesZone = require('./resources-zone'); + + function ResourcesTab(unique_id_prefix) { + this.unique_id_prefix = unique_id_prefix; + this.zones = []; + } + + ResourcesTab.prototype.constructor = ResourcesTab; + ResourcesTab.prototype.html = _html; + ResourcesTab.prototype.setup = _setup; + ResourcesTab.prototype.onShow = _onShow; + ResourcesTab.prototype.retrieve = _retrieve; + //ResourcesTab.prototype.fill = _fill; + ResourcesTab.prototype.addResourcesZone = _addResourcesZone; + + return ResourcesTab; + + function _html() { + return TemplateHTML({}); + } + + function _onShow(context) { + $.each(this.zones,function(i,resourcesZone){ + resourcesZone.onShow(context); + }); + } + + function _setup(context) { + var that = this; + + $("select.vdc_zones_select", context).change(function(){ + context.find(".vdc_zone_content").hide(); + $('div#'+that.unique_id_prefix+'_'+$(this).val()+'Tab', context).show(); + }); + + $("select.vdc_zones_select", context)[0].selectedIndex = 0; + $("select.vdc_zones_select", context).change(); + } + + function _retrieve(context) { + var clusters = []; + var hosts = []; + var vnets = []; + var datastores = []; + + $.each(this.zones,function(i,resourcesZone){ + var resources = resourcesZone.retrieve(context); + var zone_id = resourcesZone.getZoneId(); + + $.each(resources.clusters,function(j,cluster_id){ + clusters.push({zone_id: zone_id, cluster_id: cluster_id}); + }); + + $.each(resources.hosts,function(j,host_id){ + hosts.push({zone_id: zone_id, host_id: host_id}); + }); + $.each(resources.vnets,function(j,vnet_id){ + vnets.push({zone_id: zone_id, vnet_id: vnet_id}); + }); + $.each(resources.datastores,function(j,ds_id){ + datastores.push({zone_id: zone_id, ds_id: ds_id}); + }); + }); + + return { + "clusters" : clusters, + "hosts" : hosts, + "vnets" : vnets, + "datastores" : datastores + }; + } + + function _addResourcesZone(zone_id, zone_name, context, indexed_resources) { + var unique_id = this.unique_id_prefix+'_'+zone_id; + + var resourcesZone = new ResourcesZone(unique_id, zone_id, zone_name, context, indexed_resources); + + // Append the new div containing the tab and add the tab to the list + var html_tab_content = + '
'+ + resourcesZone.html()+ + '
'; + + $(html_tab_content).appendTo($(".vdc_zones_tabs_content", context)); + + $("select.vdc_zones_select", context).append( + ''); + + var zoneSection = $('#' +unique_id+'Tab', context); + + resourcesZone.setup(zoneSection); + + this.zones.push(resourcesZone); + } + +}); \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/vdcs-tab/panels/resources/html.hbs b/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-tab/html.hbs similarity index 100% rename from src/sunstone/public/app/tabs/vdcs-tab/panels/resources/html.hbs rename to src/sunstone/public/app/tabs/vdcs-tab/utils/resources-tab/html.hbs diff --git a/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-zone.js b/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-zone.js new file mode 100644 index 0000000000..55d615e306 --- /dev/null +++ b/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-zone.js @@ -0,0 +1,176 @@ +define(function(require) { + + var TemplateHTML = require('hbs!./resources-zone/html'); + + var ClustersTable = require('tabs/clusters-tab/datatable'); + var HostsTable = require('tabs/hosts-tab/datatable'); + var VNetsTable = require('tabs/vnets-tab/datatable'); + var DatastoresTable = require('tabs/datastores-tab/datatable'); + var Tips = require('utils/tips'); + var Utils = require('../utils/common'); + + var VDC_ALL_RESOURCES = Utils.VDC_ALL_RESOURCES; + + function ResourcesZone(unique_id, zone_id, zone_name, context, indexed_resources) { + this.unique_id = unique_id; + this.zone_id = zone_id; + + this.resources = undefined; + if(indexed_resources != undefined && indexed_resources[zone_id] != undefined){ + this.resources = indexed_resources[zone_id]; + } + } + + ResourcesZone.prototype.constructor = ResourcesZone; + ResourcesZone.prototype.html = _html; + ResourcesZone.prototype.setup = _setup; + ResourcesZone.prototype.onShow = _onShow; + ResourcesZone.prototype.retrieve = _retrieve; + //ResourcesZone.prototype.fill = _fill; + ResourcesZone.prototype.getZoneId = _getZoneId; + + return ResourcesZone; + + function _getZoneId() { + return this.zone_id; + } + + function _html() { + var that = this; + + var opts = {}; + + $.each(["clusters", "hosts", "vnets", "datastores"], function(i,res_name){ + if(that.resources != undefined){ + opts[res_name] = { + info: true, + select: true, + selectOptions: { + read_only: true, + zone_id: that.zone_id + } + }; + + if (!(that.resources[res_name].length == 1 && + that.resources[res_name][0] == VDC_ALL_RESOURCES) ){ + + opts[res_name].selectOptions.fixed_ids = that.resources[res_name]; + } + } else { + opts[res_name] = { + info: false, + select: true, + selectOptions: { + multiple_choice: true, + zone_id: that.zone_id + } + }; + } + }); + + that.clustersTable = new ClustersTable("vdc_clusters_"+that.unique_id, opts["clusters"]); + that.hostsTable = new HostsTable("vdc_hosts_"+that.unique_id, opts["hosts"]); + that.vnetsTable = new VNetsTable("vdc_vnets_"+that.unique_id, opts["vnets"]); + that.datastoresTable = new DatastoresTable("vdc_datastores_"+that.unique_id, opts["datastores"]); + + return TemplateHTML({ + 'unique_id': that.unique_id, + 'clustersTableHTML': that.clustersTable.dataTableHTML, + 'hostsTableHTML': that.hostsTable.dataTableHTML, + 'vnetsTableHTML': that.vnetsTable.dataTableHTML, + 'datastoresTableHTML': that.datastoresTable.dataTableHTML + }); + } + + function _onShow(context) { + var that = this; + + $.each([that.clustersTable, that.hostsTable, + that.vnetsTable, that.datastoresTable], function(){ + + this.refreshResourceTableSelect(); + }); + } + + function _setup(context){ + var that = this; + + context.foundation(); + + $.each(["clusters", "hosts", "vnets", "datastores"], function(i,res_name){ + if(that.resources != undefined){ + if (that.resources[res_name].length == 1 && + that.resources[res_name][0] == VDC_ALL_RESOURCES){ + + $("#all_"+res_name+"_"+that.unique_id, context).prop("checked", "checked"); + } + + $("#all_"+res_name+"_"+that.unique_id, context).prop("disabled", true); + } + + $("input[name='all_"+res_name+"_"+that.unique_id+"']", context).change(function(){ + if ($(this).prop("checked")){ + $("div.vdc_"+res_name+"_select", context).hide(); + } else { + $("div.vdc_"+res_name+"_select", context).show(); + } + }); + }); + + $.each([that.clustersTable, that.hostsTable, + that.vnetsTable, that.datastoresTable], function(){ + + this.initialize(); + }); + + Tips.setup(context); + } + + /* + Return an object with the selected VDC resources in the zone tab + { clusters : [], + hosts : [], + vnets : [], + datastores : [] + } + */ + function _retrieve(context) { + var clusters; + var hosts; + var vnets; + var datastores; + + if ( $("input[name='all_clusters_"+this.unique_id+"']", context).prop("checked") ){ + clusters = [VDC_ALL_RESOURCES]; + } else { + clusters = this.clustersTable.retrieveResourceTableSelect(); + } + + if ( $("input[name='all_hosts_"+this.unique_id+"']", context).prop("checked") ){ + hosts = [VDC_ALL_RESOURCES]; + } else { + hosts = this.hostsTable.retrieveResourceTableSelect(); + } + + if ( $("input[name='all_vnets_"+this.unique_id+"']", context).prop("checked") ){ + vnets = [VDC_ALL_RESOURCES]; + } else { + vnets = this.vnetsTable.retrieveResourceTableSelect(); + } + + if ( $("input[name='all_datastores_"+this.unique_id+"']", context).prop("checked") ){ + datastores = [VDC_ALL_RESOURCES]; + } else { + datastores = this.datastoresTable.retrieveResourceTableSelect(); + } + + var resources = { + clusters : clusters, + hosts : hosts, + vnets : vnets, + datastores : datastores + }; + + return resources; + } +}); diff --git a/src/sunstone/public/app/tabs/vdcs-tab/utils/resourceTab.hbs b/src/sunstone/public/app/tabs/vdcs-tab/utils/resources-zone/html.hbs similarity index 100% rename from src/sunstone/public/app/tabs/vdcs-tab/utils/resourceTab.hbs rename to src/sunstone/public/app/tabs/vdcs-tab/utils/resources-zone/html.hbs