mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
(templating) support asc/desc sort
This commit is contained in:
parent
8987cfe0a2
commit
16dc095329
@ -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 = [
|
||||
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user