Commit 4883b2a2 by Torkel Ödegaard

Fixed issue with using template variables in panel titles, and text panel, when…

Fixed issue with using template variables in panel titles, and text panel, when selecting All option in variable
parent b1abe72a
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"company": "Coding Instinct AB" "company": "Coding Instinct AB"
}, },
"name": "grafana", "name": "grafana",
"version": "1.8.0", "version": "1.8.0-rc1",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "http://github.com/torkelo/grafana.git" "url": "http://github.com/torkelo/grafana.git"
......
...@@ -57,7 +57,7 @@ define(['angular', 'jquery', 'lodash', 'moment'], function (angular, $, _, momen ...@@ -57,7 +57,7 @@ define(['angular', 'jquery', 'lodash', 'moment'], function (angular, $, _, momen
module.filter('interpolateTemplateVars', function(templateSrv) { module.filter('interpolateTemplateVars', function(templateSrv) {
return function(text) { return function(text) {
return templateSrv.replace(text); return templateSrv.replaceWithText(text);
}; };
}); });
......
...@@ -11,26 +11,25 @@ function (angular, _) { ...@@ -11,26 +11,25 @@ function (angular, _) {
var self = this; var self = this;
this._regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g; this._regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g;
this._templateData = {}; this._values = {};
this._texts = {};
this._grafanaVariables = {}; this._grafanaVariables = {};
this.init = function(variables) { this.init = function(variables) {
this.variables = variables; this.variables = variables;
this.updateTemplateData(true); this.updateTemplateData();
}; };
this.updateTemplateData = function() { this.updateTemplateData = function() {
var data = {}; this._values = {};
this._texts = {};
_.each(this.variables, function(variable) { _.each(this.variables, function(variable) {
if (!variable.current || !variable.current.value) { if (!variable.current || !variable.current.value) { return; }
return;
}
data[variable.name] = variable.current.value;
});
this._templateData = data; this._values[variable.name] = variable.current.value;
this._texts[variable.name] = variable.current.text;
}, this);
}; };
this.setGrafanaVariable = function (name, value) { this.setGrafanaVariable = function (name, value) {
...@@ -40,7 +39,7 @@ function (angular, _) { ...@@ -40,7 +39,7 @@ function (angular, _) {
this.variableExists = function(expression) { this.variableExists = function(expression) {
this._regex.lastIndex = 0; this._regex.lastIndex = 0;
var match = this._regex.exec(expression); var match = this._regex.exec(expression);
return match && (self._templateData[match[1] || match[2]] !== void 0); return match && (self._values[match[1] || match[2]] !== void 0);
}; };
this.containsVariable = function(str, variableName) { this.containsVariable = function(str, variableName) {
...@@ -52,7 +51,7 @@ function (angular, _) { ...@@ -52,7 +51,7 @@ function (angular, _) {
this._regex.lastIndex = 0; this._regex.lastIndex = 0;
return str.replace(this._regex, function(match, g1, g2) { return str.replace(this._regex, function(match, g1, g2) {
if (self._templateData[g1 || g2]) { if (self._values[g1 || g2]) {
return '<span class="template-variable">' + match + '</span>'; return '<span class="template-variable">' + match + '</span>';
} }
return match; return match;
...@@ -66,13 +65,29 @@ function (angular, _) { ...@@ -66,13 +65,29 @@ function (angular, _) {
this._regex.lastIndex = 0; this._regex.lastIndex = 0;
return target.replace(this._regex, function(match, g1, g2) { return target.replace(this._regex, function(match, g1, g2) {
value = self._templateData[g1 || g2]; value = self._values[g1 || g2];
if (!value) { return match; } if (!value) { return match; }
return self._grafanaVariables[value] || value; return self._grafanaVariables[value] || value;
}); });
}; };
this.replaceWithText = function(target) {
if (!target) { return; }
var value;
var text;
this._regex.lastIndex = 0;
return target.replace(this._regex, function(match, g1, g2) {
value = self._values[g1 || g2];
text = self._texts[g1 || g2];
if (!value) { return match; }
return self._grafanaVariables[value] || text;
});
};
}); });
}); });
...@@ -92,6 +92,23 @@ define([ ...@@ -92,6 +92,23 @@ define([
}); });
}); });
describe('replaceWithText', function() {
beforeEach(function() {
_templateSrv.init([
{ name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
{ name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
]);
_templateSrv.setGrafanaVariable('$__auto_interval', '13m');
_templateSrv.updateTemplateData();
});
it('should replace with text except for grafanaVariables', function() {
var target = _templateSrv.replaceWithText('Server: $server, period: $period');
expect(target).to.be('Server: All, period: 13m');
});
});
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment