templateSrv.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. _.each(this.variables, function(variable) {
  23. if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
  24. this._values[variable.name] = variable.current.value;
  25. }, this);
  26. };
  27. this.regexEscape = function(value) {
  28. return value.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
  29. };
  30. this.formatValue = function(value, format) {
  31. if (_.isString(value)) {
  32. return value;
  33. } else {
  34. switch(format) {
  35. case "regex": {
  36. var escapedValues = _.map(value, this.regexEscape);
  37. return '(' + escapedValues.join('|') + ')';
  38. }
  39. case "lucene": {
  40. var quotedValues = _.map(value, function(val) {
  41. return '\\\"' + val + '\\\"';
  42. });
  43. return '(' + quotedValues.join(' OR ') + ')';
  44. }
  45. case "pipe": {
  46. return value.join('|');
  47. }
  48. default: {
  49. return '{' + value.join(',') + '}';
  50. }
  51. }
  52. }
  53. };
  54. this.setGrafanaVariable = function (name, value) {
  55. this._grafanaVariables[name] = value;
  56. };
  57. this.variableExists = function(expression) {
  58. this._regex.lastIndex = 0;
  59. var match = this._regex.exec(expression);
  60. return match && (self._values[match[1] || match[2]] !== void 0);
  61. };
  62. this.containsVariable = function(str, variableName) {
  63. if (!str) {
  64. return false;
  65. }
  66. return str.indexOf('$' + variableName) !== -1 || str.indexOf('[[' + variableName + ']]') !== -1;
  67. };
  68. this.highlightVariablesAsHtml = function(str) {
  69. if (!str || !_.isString(str)) { return str; }
  70. str = _.escape(str);
  71. this._regex.lastIndex = 0;
  72. return str.replace(this._regex, function(match, g1, g2) {
  73. if (self._values[g1 || g2]) {
  74. return '<span class="template-variable">' + match + '</span>';
  75. }
  76. return match;
  77. });
  78. };
  79. this.replace = function(target, scopedVars, format) {
  80. if (!target) { return target; }
  81. var value, systemValue;
  82. this._regex.lastIndex = 0;
  83. return target.replace(this._regex, function(match, g1, g2) {
  84. if (scopedVars) {
  85. value = scopedVars[g1 || g2];
  86. if (value) {
  87. return self.formatValue(value.value);
  88. }
  89. }
  90. value = self._values[g1 || g2];
  91. if (!value) {
  92. return match;
  93. }
  94. systemValue = self._grafanaVariables[value];
  95. if (systemValue) {
  96. return self.formatValue(systemValue);
  97. }
  98. return self.formatValue(value, format);
  99. });
  100. };
  101. this.replaceWithText = function(target, scopedVars) {
  102. if (!target) { return target; }
  103. var value;
  104. var text;
  105. this._regex.lastIndex = 0;
  106. return target.replace(this._regex, function(match, g1, g2) {
  107. if (scopedVars) {
  108. var option = scopedVars[g1 || g2];
  109. if (option) { return option.text; }
  110. }
  111. value = self._values[g1 || g2];
  112. text = self._texts[g1 || g2];
  113. if (!value) { return match; }
  114. return self._grafanaVariables[value] || text;
  115. });
  116. };
  117. this.fillVariableValuesForUrl = function(params, scopedVars) {
  118. _.each(this.variables, function(variable) {
  119. var current = variable.current;
  120. var value = current.value;
  121. if (current.text === 'All') {
  122. value = 'All';
  123. }
  124. if (scopedVars && scopedVars[variable.name] !== void 0) {
  125. value = scopedVars[variable.name].value;
  126. }
  127. params['var-' + variable.name] = value;
  128. });
  129. };
  130. });
  131. });