templateSrv.js 3.9 KB

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