segment_srv.js 3.3 KB

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