From 118ab360d02790842a3abfae2a5f6ebeeeb03346 Mon Sep 17 00:00:00 2001 From: Jessica Wang Date: Wed, 9 Feb 2011 14:26:47 -0800 Subject: [PATCH] create shared function populateClusterFieldInAddPoolDialog() - reduce code repetition. --- ui/scripts/cloud.core.pod.js | 24 +---------- ui/scripts/cloud.core.resource.js | 70 +++++++++++++++++-------------- ui/scripts/cloud.core.zone.js | 24 +---------- 3 files changed, 43 insertions(+), 75 deletions(-) diff --git a/ui/scripts/cloud.core.pod.js b/ui/scripts/cloud.core.pod.js index 72698e01bbd..c65c794aa6b 100644 --- a/ui/scripts/cloud.core.pod.js +++ b/ui/scripts/cloud.core.pod.js @@ -630,28 +630,8 @@ function bindAddPrimaryStorageButton($leftmenuItem1) { $dialogAddPool.find("#pod_name").text(fromdb(clusterObj.podname)); } - var $clusterSelect = $("#dialog_add_pool").find("#pool_cluster").empty(); - - $.ajax({ - data: createURL("command=listClusters&podid=" + podId), - async: false, - dataType: "json", - success: function(json) { - var items = json.listclustersresponse.cluster; - if(items != null && items.length > 0) { - clustersUnderPod = {}; - for(var i=0; i" + fromdb(items[i].name) + ""); - else - $clusterSelect.append(""); - } - } - } - }); - - bindAddPrimaryStorageDialog($dialogAddPool); + populateClusterFieldInAddPoolDialog($dialogAddPool, podId); + bindClusterFieldInAddPoolDialog($dialogAddPool); $button.unbind("click").bind("click", function(event) { $dialogAddPool.find("#info_container").hide(); diff --git a/ui/scripts/cloud.core.resource.js b/ui/scripts/cloud.core.resource.js index a72ce445081..56d1a997193 100644 --- a/ui/scripts/cloud.core.resource.js +++ b/ui/scripts/cloud.core.resource.js @@ -1617,17 +1617,44 @@ function initUpdateConsoleCertButton($midMenuAddLink2) { } var clustersUnderPod = {}; -function bindAddPrimaryStorageDialog($dialog) { - $dialog.find("#pool_cluster").unbind("change").change(function() { - var curOption = $(this).val(); - if(!curOption) + +function populateClusterFieldInAddPoolDialog($dialog, podId) { + $.ajax({ + data: createURL("command=listClusters&podid=" + podId), + dataType: "json", + async: false, + success: function(json) { + clustersUnderPod = {}; + var items = json.listclustersresponse.cluster; + var $clusterSelect = $dialog.find("#pool_cluster").empty(); + if(items != null && items.length > 0) { + for(var i=0; i" + fromdb(items[i].name) + ""); + } + } + $clusterSelect.change(); + } + }); +} + +function bindClusterFieldInAddPoolDialog($dialog) { + $dialog.find("#pool_cluster").unbind("change").change(function(event) { + event.stopPropagation();; + + var $protocolSelector = $dialog.find("#add_pool_protocol"); + + var clusterId = $(this).val(); + if(clusterId == null) { + $protocolSelector.empty(); return false; + } - var $protocolSelector = $dialog.find("#add_pool_protocol"); - var clusterObj = clustersUnderPod['cluster_'+curOption]; - - if(clusterObj == null) - return; + var clusterObj = clustersUnderPod['cluster_'+clusterId]; + if(clusterObj == null) { + $protocolSelector.empty(); + return false; + } if(clusterObj.hypervisortype == "KVM") { $protocolSelector.empty(); @@ -1682,30 +1709,11 @@ function initAddPrimaryStorageShortcut($midmenuAddLink2, currentPageInRightPanel $dialogAddPool.find("#pod_dropdown").bind("change", function(event) { var podId = $(this).val(); if(podId == null || podId.length == 0) - return; - var $clusterSelect = $dialogAddPool.find("#pool_cluster").empty(); - $.ajax({ - data: createURL("command=listClusters&podid=" + podId), - dataType: "json", - async: false, - success: function(json) { - clustersUnderPod = {}; - var items = json.listclustersresponse.cluster; - if(items != null && items.length > 0) { - for(var i=0; i" + fromdb(items[i].name) + ""); - } - - if(!$clusterSelect.val()) - $("option", $clusterSelect)[0].attr("selected", "selected"); - $clusterSelect.change(); - } - } - }); + return; + populateClusterFieldInAddPoolDialog($dialogAddPool, podId); }); - bindAddPrimaryStorageDialog($dialogAddPool); + bindClusterFieldInAddPoolDialog($dialogAddPool); $("#add_primarystorage_shortcut").unbind("click").bind("click", function(event) { $dialogAddPool.find("#zone_dropdown").change(); //refresh cluster dropdown (do it here to avoid race condition) diff --git a/ui/scripts/cloud.core.zone.js b/ui/scripts/cloud.core.zone.js index d2cc6cfe0f4..f698c6cdaf2 100644 --- a/ui/scripts/cloud.core.zone.js +++ b/ui/scripts/cloud.core.zone.js @@ -830,30 +830,10 @@ function bindAddPrimaryStorageButtonOnZonePage($button, zoneId, zoneName) { var podId = $(this).val(); if(podId == null || podId.length == 0) return; - var $clusterSelect = $dialogAddPool.find("#pool_cluster").empty(); - $.ajax({ - data: createURL("command=listClusters&podid=" + podId), - dataType: "json", - async: false, - success: function(json) { - var items = json.listclustersresponse.cluster; - if(items != null && items.length > 0) { - clustersUnderPod = {}; - for(var i=0; i" + fromdb(items[i].name) + ""); - } - - if(!$clusterSelect.val()) { - $("option", $clusterSelect)[0].attr("selected", "selected"); - } - $clusterSelect.change(); - } - } - }); + populateClusterFieldInAddPoolDialog($dialogAddPool, podId); }); - bindAddPrimaryStorageDialog($dialogAddPool); + bindClusterFieldInAddPoolDialog($dialogAddPool); $button.unbind("click").bind("click", function(event) { $dialogAddPool.find("#info_container").hide();