diff --git a/CHANGELOG.md b/CHANGELOG.md index 148d4290907..523033ef243 100644 --- a/CHANGELOG.md +++ b/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** diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index 9dea95fb083..6db5ce595a2 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/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); }; diff --git a/public/test/specs/helpers.js b/public/test/specs/helpers.js index a9d8a09bfe4..0a0c7b77952 100644 --- a/public/test/specs/helpers.js +++ b/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) { diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index 323b1495404..2ff287b8b6d 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/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() {