2014-08-27 08:54:30 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
|
|
|
],
|
2014-08-28 09:44:16 -05:00
|
|
|
function (angular, _) {
|
2014-08-27 08:54:30 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
2014-09-08 04:03:14 -05:00
|
|
|
module.service('templateSrv', function() {
|
2014-09-04 10:34:36 -05:00
|
|
|
var self = this;
|
2014-08-27 08:54:30 -05:00
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
this._regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g;
|
|
|
|
this._templateData = {};
|
|
|
|
this._grafanaVariables = {};
|
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
this.init = function(variables) {
|
|
|
|
this.variables = variables;
|
2014-08-27 10:58:49 -05:00
|
|
|
this.updateTemplateData(true);
|
|
|
|
};
|
|
|
|
|
2014-09-08 04:03:14 -05:00
|
|
|
this.updateTemplateData = function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
var data = {};
|
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
_.each(this.variables, function(variable) {
|
|
|
|
if (!variable.current || !variable.current.value) {
|
2014-08-27 10:58:49 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-08-28 09:03:13 -05:00
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
data[variable.name] = variable.current.value;
|
2014-08-27 10:58:49 -05:00
|
|
|
});
|
2014-09-05 05:07:48 -05:00
|
|
|
|
|
|
|
this._templateData = data;
|
2014-08-27 10:58:49 -05:00
|
|
|
};
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
this.setGrafanaVariable = function (name, value) {
|
|
|
|
this._grafanaVariables[name] = value;
|
2014-09-02 00:05:24 -05:00
|
|
|
};
|
|
|
|
|
2014-09-05 01:26:50 -05:00
|
|
|
this.variableExists = function(expression) {
|
2014-09-05 05:07:48 -05:00
|
|
|
this._regex.lastIndex = 0;
|
|
|
|
var match = this._regex.exec(expression);
|
2014-09-05 01:26:50 -05:00
|
|
|
return match && (self._templateData[match[1] || match[2]] !== void 0);
|
|
|
|
};
|
|
|
|
|
2014-09-08 04:03:14 -05:00
|
|
|
this.containsVariable = function(str, variableName) {
|
|
|
|
return str.indexOf('$' + variableName) !== -1 || str.indexOf('[[' + variableName + ']]') !== -1;
|
|
|
|
};
|
|
|
|
|
2014-09-05 01:26:50 -05:00
|
|
|
this.highlightVariablesAsHtml = function(str) {
|
2014-09-05 02:11:50 -05:00
|
|
|
if (!str || !_.isString(str)) { return str; }
|
2014-09-05 01:26:50 -05:00
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
this._regex.lastIndex = 0;
|
|
|
|
return str.replace(this._regex, function(match, g1, g2) {
|
2014-09-05 01:26:50 -05:00
|
|
|
if (self._templateData[g1 || g2]) {
|
|
|
|
return '<span class="template-variable">' + match + '</span>';
|
|
|
|
}
|
2014-09-05 07:03:36 -05:00
|
|
|
return match;
|
2014-09-05 01:26:50 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
this.replace = function(target) {
|
2014-09-04 10:34:36 -05:00
|
|
|
if (!target) { return; }
|
2014-08-27 08:54:30 -05:00
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
var value;
|
|
|
|
this._regex.lastIndex = 0;
|
|
|
|
|
|
|
|
return target.replace(this._regex, function(match, g1, g2) {
|
|
|
|
value = self._templateData[g1 || g2];
|
|
|
|
if (!value) { return match; }
|
|
|
|
|
|
|
|
return self._grafanaVariables[value] || value;
|
2014-09-04 10:34:36 -05:00
|
|
|
});
|
2014-08-27 08:54:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|