templateSrv.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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] = variable.current.value;
  26. this._texts[variable.name] = variable.current.text;
  27. }, this);
  28. };
  29. this.setGrafanaVariable = function (name, value) {
  30. this._grafanaVariables[name] = value;
  31. };
  32. this.variableExists = function(expression) {
  33. this._regex.lastIndex = 0;
  34. var match = this._regex.exec(expression);
  35. return match && (self._values[match[1] || match[2]] !== void 0);
  36. };
  37. this.containsVariable = function(str, variableName) {
  38. if (!str) {
  39. return false;
  40. }
  41. return str.indexOf('$' + variableName) !== -1 || str.indexOf('[[' + variableName + ']]') !== -1;
  42. };
  43. this.highlightVariablesAsHtml = function(str) {
  44. if (!str || !_.isString(str)) { return str; }
  45. this._regex.lastIndex = 0;
  46. return str.replace(this._regex, function(match, g1, g2) {
  47. if (self._values[g1 || g2]) {
  48. return '<span class="template-variable">' + match + '</span>';
  49. }
  50. return match;
  51. });
  52. };
  53. this.replace = function(target, scopedVars) {
  54. if (!target) { return; }
  55. var value;
  56. this._regex.lastIndex = 0;
  57. return target.replace(this._regex, function(match, g1, g2) {
  58. if (scopedVars) {
  59. value = scopedVars[g1 || g2];
  60. if (value) { return value.value; }
  61. }
  62. value = self._values[g1 || g2];
  63. if (!value) { return match; }
  64. return self._grafanaVariables[value] || value;
  65. });
  66. };
  67. this.replaceWithText = function(target, scopedVars) {
  68. if (!target) { return; }
  69. var value;
  70. var text;
  71. this._regex.lastIndex = 0;
  72. return target.replace(this._regex, function(match, g1, g2) {
  73. if (scopedVars) {
  74. var option = scopedVars[g1 || g2];
  75. if (option) { return option.text; }
  76. }
  77. value = self._values[g1 || g2];
  78. text = self._texts[g1 || g2];
  79. if (!value) { return match; }
  80. return self._grafanaVariables[value] || text;
  81. });
  82. };
  83. });
  84. });