From 666013b0100a0c03452f36ef888a4b7085faade1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 17 Jul 2015 14:59:05 +0200 Subject: [PATCH] fix(panel repeat): fixed issue with repeat panels in combination with setting variable values from URL, fixes #2351 --- .../features/templating/templateValuesSrv.js | 39 +++++++++++-------- public/test/specs/templateValuesSrv-specs.js | 14 ++++--- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index a5767ffa312..6f7e5398618 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -80,6 +80,7 @@ function (angular, _, kbn) { if (_.isArray(variable.current.value)) { variable.current.text = variable.current.value.join(' + '); + this.selectOptionsForCurrentValue(variable); } templateSrv.updateTemplateData(); @@ -128,6 +129,18 @@ function (angular, _, kbn) { .then(_.partial(this.validateVariableSelectionState, variable)); }; + this.selectOptionsForCurrentValue = function(variable) { + for (var i = 0; i < variable.current.value.length; i++) { + var value = variable.current.value[i]; + for (var y = 0; y < variable.options.length; y++) { + var option = variable.options[y]; + if (option.value === value) { + option.selected = true; + } + } + } + }; + this.validateVariableSelectionState = function(variable) { if (!variable.current) { if (!variable.options.length) { return; } @@ -135,15 +148,7 @@ function (angular, _, kbn) { } if (_.isArray(variable.current.value)) { - for (var i = 0; i < variable.current.value.length; i++) { - var value = variable.current.value[i]; - for (var y = 0; y < variable.options.length; y++) { - var option = variable.options[y]; - if (option.value === value) { - option.selected = true; - } - } - } + this.selectOptionsForCurrentValue(variable); } else { var currentOption = _.findWhere(variable.options, { text: variable.current.text }); if (currentOption) { @@ -225,17 +230,17 @@ function (angular, _, kbn) { this.addAllOption = function(variable) { var allValue = ''; switch(variable.allFormat) { - case 'wildcard': - allValue = '*'; + case 'wildcard': + allValue = '*'; break; - case 'regex wildcard': - allValue = '.*'; + case 'regex wildcard': + allValue = '.*'; break; - case 'regex values': - allValue = '(' + _.pluck(variable.options, 'text').join('|') + ')'; + case 'regex values': + allValue = '(' + _.pluck(variable.options, 'text').join('|') + ')'; break; - default: - allValue = '{'; + default: + allValue = '{'; allValue += _.pluck(variable.options, 'text').join(','); allValue += '}'; } diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index 6c7a3035ff9..d0af589a068 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -52,23 +52,25 @@ define([ var variable = { name: 'apps', multi: true, - current: {text: "test", value: "test"}, - options: [{text: "test", value: "test"}] + current: {text: "val1", value: "val1"}, + options: [{text: "val1", value: "val1"}, {text: 'val2', value: 'val2'}] }; beforeEach(function() { var dashboard = { templating: { list: [variable] } }; var urlParams = {}; - urlParams["var-apps"] = ["new", "other"]; + urlParams["var-apps"] = ["val1", "val2"]; 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"); + expect(variable.current.value[0]).to.be("val1"); + expect(variable.current.value[1]).to.be("val2"); + expect(variable.current.text).to.be("val1 + val2"); + expect(variable.options[0].selected).to.be(true); + expect(variable.options[1].selected).to.be(true); }); });