segment_srv.js 3.5 KB

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