segment_srv.ts 3.6 KB

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