segment_srv.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. ],
  6. function (angular, _, coreModule) {
  7. 'use strict';
  8. coreModule.default.service('uiSegmentSrv', function($sce, templateSrv) {
  9. var self = this;
  10. function MetricSegment(options) {
  11. if (options === '*' || options.value === '*') {
  12. this.value = '*';
  13. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  14. this.type = options.type;
  15. this.expandable = true;
  16. return;
  17. }
  18. if (_.isString(options)) {
  19. this.value = options;
  20. this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  21. return;
  22. }
  23. // temp hack to work around legacy inconsistency in segment model
  24. this.text = options.value;
  25. this.cssClass = options.cssClass;
  26. this.custom = options.custom;
  27. this.type = options.type;
  28. this.fake = options.fake;
  29. this.value = options.value;
  30. this.selectMode = options.selectMode;
  31. this.type = options.type;
  32. this.expandable = options.expandable;
  33. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  34. }
  35. this.getSegmentForValue = function(value, fallbackText) {
  36. if (value) {
  37. return this.newSegment(value);
  38. } else {
  39. return this.newSegment({value: fallbackText, fake: true});
  40. }
  41. };
  42. this.newSelectMeasurement = function() {
  43. return new MetricSegment({value: 'select measurement', fake: true});
  44. };
  45. this.newFake = function(text, type, cssClass) {
  46. return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
  47. };
  48. this.newSegment = function(options) {
  49. return new MetricSegment(options);
  50. };
  51. this.newKey = function(key) {
  52. return new MetricSegment({value: key, type: 'key', cssClass: 'query-segment-key' });
  53. };
  54. this.newKeyValue = function(value) {
  55. return new MetricSegment({value: value, type: 'value', cssClass: 'query-segment-value' });
  56. };
  57. this.newCondition = function(condition) {
  58. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  59. };
  60. this.newOperator = function(op) {
  61. return new MetricSegment({value: op, type: 'operator', cssClass: 'query-segment-operator' });
  62. };
  63. this.newOperators = function(ops) {
  64. return _.map(ops, function(op) {
  65. return new MetricSegment({value: op, type: 'operator', cssClass: 'query-segment-operator' });
  66. });
  67. };
  68. this.transformToSegments = function(addTemplateVars, variableTypeFilter) {
  69. return function(results) {
  70. var segments = _.map(results, function(segment) {
  71. return self.newSegment({value: segment.text, expandable: segment.expandable});
  72. });
  73. if (addTemplateVars) {
  74. _.each(templateSrv.variables, function(variable) {
  75. if (variableTypeFilter === void 0 || variableTypeFilter === variable.type) {
  76. segments.unshift(self.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  77. }
  78. });
  79. }
  80. return segments;
  81. };
  82. };
  83. this.newSelectMetric = function() {
  84. return new MetricSegment({value: 'select metric', fake: true});
  85. };
  86. this.newPlusButton = function() {
  87. return new MetricSegment({fake: true, html: '<i class="fa fa-plus "></i>', type: 'plus-button', cssClass: 'query-part' });
  88. };
  89. this.newSelectTagValue = function() {
  90. return new MetricSegment({value: 'select tag value', fake: true});
  91. };
  92. });
  93. });