segment_srv.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import _ from 'lodash';
  2. import coreModule from '../core_module';
  3. /** @ngInject */
  4. export function uiSegmentSrv($sce, templateSrv) {
  5. let self = this;
  6. function MetricSegment(options) {
  7. if (options === '*' || options.value === '*') {
  8. this.value = '*';
  9. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  10. this.type = options.type;
  11. this.expandable = true;
  12. return;
  13. }
  14. if (_.isString(options)) {
  15. this.value = options;
  16. this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  17. return;
  18. }
  19. // temp hack to work around legacy inconsistency in segment model
  20. this.text = options.value;
  21. this.cssClass = options.cssClass;
  22. this.custom = options.custom;
  23. this.type = options.type;
  24. this.fake = options.fake;
  25. this.value = options.value;
  26. this.selectMode = options.selectMode;
  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. let 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: 'value', 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({
  84. fake: true,
  85. html: '<i class="fa fa-plus "></i>',
  86. type: 'plus-button',
  87. cssClass: 'query-part',
  88. });
  89. };
  90. }
  91. coreModule.service('uiSegmentSrv', uiSegmentSrv);