Browse Source

Templating: You can now select multiple template variables values at the same time. Closes #1922

Torkel Ödegaard 10 years ago
parent
commit
385048b620

+ 1 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@
 
 **New dashboard features**
 - [Issue #1144](https://github.com/grafana/grafana/issues/1144). Templating: You can now select multiple template variables values at the same time.
+- [Issue #1922](https://github.com/grafana/grafana/issues/1922). Templating: Specify multiple variable values via URL params.
 - [Issue #1888](https://github.com/grafana/grafana/issues/1144). Templating: Repeat panel or row for each selected template variable value
 
 **User or Organization admin**

+ 4 - 0
public/app/features/templating/templateValuesSrv.js

@@ -57,6 +57,10 @@ function (angular, _, kbn) {
       var option = _.findWhere(variable.options, { text: urlValue });
       option = option || { text: urlValue, value: urlValue };
 
+      if (_.isArray(urlValue)) {
+        option.text = urlValue.join(', ');
+      }
+
       this.updateAutoInterval(variable);
       return this.setVariableValue(variable, option);
     };

+ 1 - 0
public/test/specs/helpers.js

@@ -69,6 +69,7 @@ define([
     self.timeSrv = new TimeSrvStub();
     self.datasourceSrv = {};
     self.backendSrv = {};
+    self.$location = {};
     self.$routeParams = {};
 
     this.providePhase = function(mocks) {

+ 48 - 2
public/test/specs/templateValuesSrv-specs.js

@@ -10,7 +10,7 @@ define([
     var ctx = new helpers.ServiceTestContext();
 
     beforeEach(module('grafana.services'));
-    beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', "$routeParams"]));
+    beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location']));
     beforeEach(ctx.createService('templateValuesSrv'));
 
     describe('update interval variable options', function() {
@@ -27,11 +27,57 @@ define([
       });
     });
 
+    describe('when template variable is present in url', function() {
+      var variable = {
+        name: 'apps',
+        current: {text: "test", value: "test"},
+        options: [{text: "test", value: "test"}]
+      };
+
+      beforeEach(function() {
+        var dashboard = { templating: { list: [variable] } };
+        var urlParams = {};
+        urlParams["var-apps"] = "new";
+        ctx.$location.search = sinon.stub().returns(urlParams);
+        ctx.service.init(dashboard);
+      });
+
+      it('should update current value', function() {
+        expect(variable.current.value).to.be("new");
+        expect(variable.current.text).to.be("new");
+      });
+    });
+
+    describe('when template variable is present in url multiple times', function() {
+      var variable = {
+        name: 'apps',
+        multi: true,
+        current: {text: "test", value: "test"},
+        options: [{text: "test", value: "test"}]
+      };
+
+      beforeEach(function() {
+        var dashboard = { templating: { list: [variable] } };
+        var urlParams = {};
+        urlParams["var-apps"] = ["new", "other"];
+        ctx.$location.search = sinon.stub().returns(urlParams);
+        ctx.service.init(dashboard);
+      });
+
+      it('should update current value', function() {
+        expect(variable.current.value.length).to.be(2);
+        expect(variable.current.value[0]).to.be("new");
+        expect(variable.current.value[1]).to.be("other");
+        expect(variable.current.text).to.be("new, other");
+      });
+    });
+
+
     function describeUpdateVariable(desc, fn) {
       describe(desc, function() {
         var scenario = {};
         scenario.setup = function(setupFn) {
-         scenario.setupFn = setupFn;
+          scenario.setupFn = setupFn;
         };
 
         beforeEach(function() {