From fa73b1ce802bc2f18eee3d63cd85f6eec9d9ac2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 1 Mar 2016 13:47:02 +0100 Subject: [PATCH] feat(templating): changed how the All templating value works --- public/app/features/templating/templateSrv.js | 44 +++++++++++++------ .../features/templating/templateValuesSrv.js | 5 +-- public/test/specs/templateSrv-specs.js | 35 +++++++++++++++ public/test/specs/templateValuesSrv-specs.js | 3 +- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/public/app/features/templating/templateSrv.js b/public/app/features/templating/templateSrv.js index 19847984393..1f1153bda44 100644 --- a/public/app/features/templating/templateSrv.js +++ b/public/app/features/templating/templateSrv.js @@ -13,7 +13,7 @@ function (angular, _) { var self = this; this._regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g; - this._values = {}; + this._index = {}; this._texts = {}; this._grafanaVariables = {}; @@ -23,14 +23,14 @@ function (angular, _) { }; this.updateTemplateData = function() { - this._values = {}; + this._index = {}; for (var i = 0; i < this.variables.length; i++) { var variable = this.variables[i]; if (!variable.current || !variable.current.isNone && !variable.current.value) { continue; } - this._values[variable.name] = variable.current; + this._index[variable.name] = variable; } }; @@ -80,7 +80,7 @@ function (angular, _) { this.variableExists = function(expression) { this._regex.lastIndex = 0; var match = this._regex.exec(expression); - return match && (self._values[match[1] || match[2]] !== void 0); + return match && (self._index[match[1] || match[2]] !== void 0); }; this.containsVariable = function(str, variableName) { @@ -96,17 +96,24 @@ function (angular, _) { str = _.escape(str); this._regex.lastIndex = 0; return str.replace(this._regex, function(match, g1, g2) { - if (self._values[g1 || g2]) { + if (self._index[g1 || g2]) { return '' + match + ''; } return match; }); }; + this.getAllValue = function(variable) { + if (variable.allValue) { + return variable.allValue; + } + return _.pluck(variable.options, 'value'); + }; + this.replace = function(target, scopedVars, format) { if (!target) { return target; } - var value, systemValue; + var variable, systemValue, value; this._regex.lastIndex = 0; return target.replace(this._regex, function(match, g1, g2) { @@ -117,25 +124,34 @@ function (angular, _) { } } - value = self._values[g1 || g2]; - if (!value) { + variable = self._index[g1 || g2]; + if (!variable) { return match; } - systemValue = self._grafanaVariables[value.value]; + systemValue = self._grafanaVariables[variable.current.value]; if (systemValue) { return self.formatValue(systemValue); } - var res = self.formatValue(value.value, format); + value = variable.current.value; + if (self.isAllValue(value)) { + value = self.getAllValue(variable); + } + + var res = self.formatValue(value, format); return res; }); }; + this.isAllValue = function(value) { + return value === '$__all' || Array.isArray(value) && value[0] === '$__all'; + }; + this.replaceWithText = function(target, scopedVars) { if (!target) { return target; } - var value; + var variable; this._regex.lastIndex = 0; return target.replace(this._regex, function(match, g1, g2) { @@ -144,10 +160,10 @@ function (angular, _) { if (option) { return option.text; } } - value = self._values[g1 || g2]; - if (!value) { return match; } + variable = self._index[g1 || g2]; + if (!variable) { return match; } - return self._grafanaVariables[value.value] || value.text; + return self._grafanaVariables[variable.current.value] || variable.current.text; }); }; diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index b0c56812ad5..46d2f90bc03 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -231,12 +231,11 @@ function (angular, _, kbn) { this.addAllOption = function(variable) { if (variable.allValue) { - variable.options.unshift({text: 'All', value: variable.allValue, isAll: true}); + variable.options.unshift({text: 'All', value: variable.allValue}); return; } - var value =_.pluck(variable.options, 'text'); - variable.options.unshift({text: 'All', value: value, isAll: true}); + variable.options.unshift({text: 'All', value: "$__all"}); }; }); diff --git a/public/test/specs/templateSrv-specs.js b/public/test/specs/templateSrv-specs.js index 1e06d66a2a1..669dc14c8e4 100644 --- a/public/test/specs/templateSrv-specs.js +++ b/public/test/specs/templateSrv-specs.js @@ -66,6 +66,41 @@ define([ }); }); + describe('variable with all option', function() { + beforeEach(function() { + _templateSrv.init([{ + name: 'test', + current: {value: '$__all' }, + options: [ + {value: 'value1'}, {value: 'value2'} + ] + }]); + }); + + it('should replace $test with formatted all value', function() { + var target = _templateSrv.replace('this.$test.filters', {}, 'glob'); + expect(target).to.be('this.{value1,value2}.filters'); + }); + }); + + describe('variable with all option and custom value', function() { + beforeEach(function() { + _templateSrv.init([{ + name: 'test', + current: {value: '$__all' }, + allValue: '*', + options: [ + {value: 'value1'}, {value: 'value2'} + ] + }]); + }); + + it('should replace $test with formatted all value', function() { + var target = _templateSrv.replace('this.$test.filters', {}, 'glob'); + expect(target).to.be('this.*.filters'); + }); + }); + describe('lucene format', function() { it('should properly escape $test with lucene escape sequences', function() { _templateSrv.init([{name: 'test', current: {value: 'value/4' }}]); diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index 44e648816f2..0eaa9d1d99f 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -267,8 +267,7 @@ define([ it('should add All option', function() { expect(scenario.variable.options[0].text).to.be('All'); - expect(scenario.variable.options[0].value).to.eql(['backend1', 'backend2', 'backend3']); - expect(scenario.variable.options[0].isAll).to.be(true); + expect(scenario.variable.options[0].value).to.be('$__all'); }); });