Просмотр исходного кода

(templating) support asc/desc sort

Mitsuhiro Tanda 9 лет назад
Родитель
Сommit
16dc095329

+ 4 - 2
public/app/features/templating/editorCtrl.js

@@ -37,8 +37,10 @@ function (angular, _) {
 
 
     $scope.sortOptions = [
     $scope.sortOptions = [
       {value: 0, text: "Without Sort"},
       {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 = [
     $scope.hideOptions = [

+ 20 - 11
public/app/features/templating/templateValuesSrv.js

@@ -359,18 +359,27 @@ function (angular, _, kbn) {
       }
       }
       options = _.uniq(options, 'value');
       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 {
       } 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;
         return options;
       }
       }
     };
     };

+ 28 - 2
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.setup(function() {
         scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 1};
         scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 1};
         scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
         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.setup(function() {
         scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 2};
         scenario.variable = {type: 'query', query: 'apps.*', name: 'test', sort: 2};
         scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
         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() {
       it('should return options with numerical sort', function() {
         expect(scenario.variable.options[0].text).to.be('bbb2');
         expect(scenario.variable.options[0].text).to.be('bbb2');
         expect(scenario.variable.options[1].text).to.be('ccc3');
         expect(scenario.variable.options[1].text).to.be('ccc3');
         expect(scenario.variable.options[2].text).to.be('aaa10');
         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');
+      });
+    });
   });
   });
 });
 });