diff --git a/public/app/features/templating/editorCtrl.js b/public/app/features/templating/editorCtrl.js index a8181ee1db3..5c923271278 100644 --- a/public/app/features/templating/editorCtrl.js +++ b/public/app/features/templating/editorCtrl.js @@ -37,8 +37,10 @@ function (angular, _) { $scope.sortOptions = [ {value: 0, text: "Without Sort"}, - {value: 1, text: "Alphabetical"}, - {value: 2, text: "Numerical"}, + {value: 1, text: "Alphabetical (asc)"}, + {value: 2, text: "Alphabetical (desc)"}, + {value: 3, text: "Numerical (asc)"}, + {value: 4, text: "Numerical (desc)"}, ]; $scope.hideOptions = [ diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index 896c152ce35..38f305f3f40 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -359,18 +359,27 @@ function (angular, _, kbn) { } options = _.uniq(options, 'value'); - if (variable.sort === 1) { - return _.sortBy(options, 'text'); - } else if (variable.sort === 2) { - return _.sortBy(options, function(opt) { - var matches = opt.text.match(/.*?(\d+).*/); - if (!matches) { - return 0; - } else { - return parseInt(matches[1], 10); - } - }); + var sortType = Math.ceil(variable.sort / 2); + if (sortType === 0) { + return options; } else { + if (sortType === 1) { + options = _.sortBy(options, 'text'); + } else if (sortType === 2) { + options = _.sortBy(options, function(opt) { + var matches = opt.text.match(/.*?(\d+).*/); + if (!matches) { + return 0; + } else { + return parseInt(matches[1], 10); + } + }); + } + + if (variable.sort % 2 === 0) { + options = options.reverse(); + } + return options; } }; diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index df59b798f3d..1742f5739c3 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -399,7 +399,7 @@ define([ }); }); - describeUpdateVariable('with alphabetical sort', function(scenario) { + describeUpdateVariable('with alphabetical sort (asc)', function(scenario) { scenario.setup(function() { scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 1}; scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}]; @@ -412,17 +412,43 @@ define([ }); }); - describeUpdateVariable('with numerical sort', function(scenario) { + describeUpdateVariable('with alphabetical sort (desc)', function(scenario) { scenario.setup(function() { scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 2}; scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}]; }); + it('should return options with alphabetical sort', function() { + expect(scenario.variable.options[0].text).to.be('ccc3'); + expect(scenario.variable.options[1].text).to.be('bbb2'); + expect(scenario.variable.options[2].text).to.be('aaa10'); + }); + }); + + describeUpdateVariable('with numerical sort (asc)', function(scenario) { + scenario.setup(function() { + scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 3}; + scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}]; + }); + it('should return options with numerical sort', function() { expect(scenario.variable.options[0].text).to.be('bbb2'); expect(scenario.variable.options[1].text).to.be('ccc3'); expect(scenario.variable.options[2].text).to.be('aaa10'); }); }); + + describeUpdateVariable('with numerical sort (desc)', function(scenario) { + scenario.setup(function() { + scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 4}; + scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}]; + }); + + it('should return options with numerical sort', function() { + expect(scenario.variable.options[0].text).to.be('aaa10'); + expect(scenario.variable.options[1].text).to.be('ccc3'); + expect(scenario.variable.options[2].text).to.be('bbb2'); + }); + }); }); });