Commit abf498fc by Dan Cech

improve maintainability

parent dc6f9773
...@@ -8,6 +8,12 @@ function luceneEscape(value) { ...@@ -8,6 +8,12 @@ function luceneEscape(value) {
export class TemplateSrv { export class TemplateSrv {
variables: any[]; variables: any[];
/*
* This regex matches 3 types of variable reference with an optional format specifier
* \$(\w+) $var1
* \[\[([\s\S]+?)(?::(\w+))?\]\] [[var2]] or [[var2:fmt2]]
* \${(\w+)(?::(\w+))?} ${var3} or ${var3:fmt3}
*/
private regex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?::(\w+))?}/g; private regex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?::(\w+))?}/g;
private index = {}; private index = {};
private grafanaVariables = {}; private grafanaVariables = {};
...@@ -143,8 +149,8 @@ export class TemplateSrv { ...@@ -143,8 +149,8 @@ export class TemplateSrv {
str = _.escape(str); str = _.escape(str);
this.regex.lastIndex = 0; this.regex.lastIndex = 0;
return str.replace(this.regex, (match, g1, g2, g3, g4) => { return str.replace(this.regex, (match, var1, var2, fmt2, var3) => {
if (this.index[g1 || g2 || g4] || this.builtIns[g1 || g2 || g4]) { if (this.index[var1 || var2 || var3] || this.builtIns[var1 || var2 || var3]) {
return '<span class="template-variable">' + match + '</span>'; return '<span class="template-variable">' + match + '</span>';
} }
return match; return match;
...@@ -170,11 +176,11 @@ export class TemplateSrv { ...@@ -170,11 +176,11 @@ export class TemplateSrv {
var variable, systemValue, value; var variable, systemValue, value;
this.regex.lastIndex = 0; this.regex.lastIndex = 0;
return target.replace(this.regex, (match, g1, g2, g3, g4, g5) => { return target.replace(this.regex, (match, var1, var2, fmt2, var3, fmt3) => {
variable = this.index[g1 || g2 || g4]; variable = this.index[var1 || var2 || var3];
format = g3 || g5 || format; format = fmt2 || fmt3 || format;
if (scopedVars) { if (scopedVars) {
value = scopedVars[g1 || g2 || g4]; value = scopedVars[var1 || var2 || var3];
if (value) { if (value) {
return this.formatValue(value.value, format, variable); return this.formatValue(value.value, format, variable);
} }
...@@ -215,15 +221,15 @@ export class TemplateSrv { ...@@ -215,15 +221,15 @@ export class TemplateSrv {
var variable; var variable;
this.regex.lastIndex = 0; this.regex.lastIndex = 0;
return target.replace(this.regex, (match, g1, g2, g3, g4) => { return target.replace(this.regex, (match, var1, var2, fmt2, var3) => {
if (scopedVars) { if (scopedVars) {
var option = scopedVars[g1 || g2 || g4]; var option = scopedVars[var1 || var2 || var3];
if (option) { if (option) {
return option.text; return option.text;
} }
} }
variable = this.index[g1 || g2 || g4]; variable = this.index[var1 || var2 || var3];
if (!variable) { if (!variable) {
return match; return match;
} }
......
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