templateSrv.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. define([
  2. 'angular',
  3. 'lodash',
  4. './editorCtrl',
  5. './templateValuesSrv',
  6. ],
  7. function (angular, _) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.service('templateSrv', function() {
  11. var self = this;
  12. this._regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g;
  13. this._values = {};
  14. this._texts = {};
  15. this._grafanaVariables = {};
  16. this.init = function(variables) {
  17. this.variables = variables;
  18. this.updateTemplateData();
  19. };
  20. this.updateTemplateData = function() {
  21. this._values = {};
  22. this._texts = {};
  23. _.each(this.variables, function(variable) {
  24. if (!variable.current || !variable.current.value) { return; }
  25. this._values[variable.name] = this.renderVariableValue(variable);
  26. this._texts[variable.name] = variable.current.text;
  27. }, this);
  28. };
  29. this.renderVariableValue = function(variable) {
  30. var value = variable.current.value;
  31. if (_.isString(value)) {
  32. return value;
  33. } else {
  34. if (variable.multiFormat === 'regex values') {
  35. return '(' + value.join('|') + ')';
  36. }
  37. return '{' + value.join(',') + '}';
  38. }
  39. };
  40. this.setGrafanaVariable = function (name, value) {
  41. this._grafanaVariables[name] = value;
  42. };
  43. this.variableExists = function(expression) {
  44. this._regex.lastIndex = 0;
  45. var match = this._regex.exec(expression);
  46. return match && (self._values[match[1] || match[2]] !== void 0);
  47. };
  48. this.containsVariable = function(str, variableName) {
  49. if (!str) {
  50. return false;
  51. }
  52. return str.indexOf('$' + variableName) !== -1 || str.indexOf('[[' + variableName + ']]') !== -1;
  53. };
  54. this.highlightVariablesAsHtml = function(str) {
  55. if (!str || !_.isString(str)) { return str; }
  56. this._regex.lastIndex = 0;
  57. return str.replace(this._regex, function(match, g1, g2) {
  58. if (self._values[g1 || g2]) {
  59. return '<span class="template-variable">' + match + '</span>';
  60. }
  61. return match;
  62. });
  63. };
  64. this.replace = function(target) {
  65. if (!target) { return; }
  66. var value;
  67. this._regex.lastIndex = 0;
  68. return target.replace(this._regex, function(match, g1, g2) {
  69. value = self._values[g1 || g2];
  70. if (!value) { return match; }
  71. return self._grafanaVariables[value] || value;
  72. });
  73. };
  74. this.replaceWithText = function(target) {
  75. if (!target) { return; }
  76. var value;
  77. var text;
  78. this._regex.lastIndex = 0;
  79. return target.replace(this._regex, function(match, g1, g2) {
  80. value = self._values[g1 || g2];
  81. text = self._texts[g1 || g2];
  82. if (!value) { return match; }
  83. return self._grafanaVariables[value] || text;
  84. });
  85. };
  86. });
  87. });