From f3aa147806adf2b14b8999329652302844629dca Mon Sep 17 00:00:00 2001 From: smlshn Date: Thu, 4 Jul 2019 11:59:17 +0300 Subject: [PATCH] ui: sort list of templates, serviceOfferings, diskOfferings etc in the deploy VM wizard (#3336) Adds functionality to sort the data that is available on each in the deploy VM wizard's step by their suitable fields: affinityGroups by name sshkeyPairs by name vpcs by name Fixes: #3050 --- ui/scripts/sharedFunctions.js | 33 ++++++++++++++++++++++++++ ui/scripts/ui-custom/instanceWizard.js | 9 +++++++ 2 files changed, 42 insertions(+) diff --git a/ui/scripts/sharedFunctions.js b/ui/scripts/sharedFunctions.js index a8049ee886f..d4a06e3228e 100644 --- a/ui/scripts/sharedFunctions.js +++ b/ui/scripts/sharedFunctions.js @@ -2557,6 +2557,39 @@ function strOrFunc(arg, args) { return arg; } +function sortArrayByKey(arrayToSort, sortKey, reverse) { + + if(!arrayToSort){ + return; + } + // Move smaller items towards the front + // or back of the array depending on if + // we want to sort the array in reverse + // order or not. + var moveSmaller = reverse ? 1 : -1; + + // Move larger items towards the front + // or back of the array depending on if + // we want to sort the array in reverse + // order or not. + var moveLarger = reverse ? -1 : 1; + + /** + * @param {*} a + * @param {*} b + * @return {Number} + */ + arrayToSort.sort(function(a, b) { + if (a[sortKey] < b[sortKey]) { + return moveSmaller; + } + if (a[sortKey] > b[sortKey]) { + return moveLarger; + } + return 0; + }); +} + $.validator.addMethod("netmask", function(value, element) { if (this.optional(element) && value.length == 0) return true; diff --git a/ui/scripts/ui-custom/instanceWizard.js b/ui/scripts/ui-custom/instanceWizard.js index 706c0bfa4b5..ef6d2246c47 100644 --- a/ui/scripts/ui-custom/instanceWizard.js +++ b/ui/scripts/ui-custom/instanceWizard.js @@ -809,6 +809,9 @@ $step.find('.main-desc, p.no-affinity-groups').remove(); if (args.data.affinityGroups && args.data.affinityGroups.length) { + + sortArrayByKey(args.data.affinityGroups, 'name'); + $step.prepend( $('
').addClass('main-desc').append( $('

').html(_l('message.select.affinity.groups')) @@ -855,6 +858,9 @@ $step.find('.main-desc, p.no-sshkey-pairs').remove(); if (args.data.sshkeyPairs && args.data.sshkeyPairs.length) { + + sortArrayByKey(args.data.sshkeyPairs, 'name'); + $step.prepend( $('

').addClass('main-desc').append( $('

').html(_l('message.please.select.ssh.key.pair.use.with.this.vm')) @@ -1024,6 +1030,9 @@ // Populate VPC drop-down $vpcSelect.html(''); + + sortArrayByKey(vpcs, 'name'); + $(vpcs).map(function(index, vpc) { var $option = $('