segment_srv.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import _ 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.type = options.type;
  38. this.expandable = options.expandable;
  39. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  40. }
  41. }
  42. this.getSegmentForValue = function(value: string, fallbackText: string) {
  43. if (value) {
  44. return this.newSegment(value);
  45. } else {
  46. return this.newSegment({ value: fallbackText, fake: true });
  47. }
  48. };
  49. this.newSelectMeasurement = () => {
  50. return new MetricSegment({ value: 'select measurement', fake: true });
  51. };
  52. this.newFake = (text: string, type: string, cssClass: string) => {
  53. return new MetricSegment({ value: text, fake: true, type: type, cssClass: cssClass });
  54. };
  55. this.newSegment = (options: any) => {
  56. return new MetricSegment(options);
  57. };
  58. this.newKey = (key: string) => {
  59. return new MetricSegment({ value: key, type: 'key', cssClass: 'query-segment-key' });
  60. };
  61. this.newKeyValue = (value: string) => {
  62. return new MetricSegment({ value: value, type: 'value', cssClass: 'query-segment-value' });
  63. };
  64. this.newCondition = (condition: string) => {
  65. return new MetricSegment({ value: condition, type: 'condition', cssClass: 'query-keyword' });
  66. };
  67. this.newOperator = (op: string) => {
  68. return new MetricSegment({ value: op, type: 'operator', cssClass: 'query-segment-operator' });
  69. };
  70. this.newOperators = (ops: string[]) => {
  71. return _.map(ops, op => {
  72. return new MetricSegment({ value: op, type: 'operator', cssClass: 'query-segment-operator' });
  73. });
  74. };
  75. this.transformToSegments = (addTemplateVars: boolean, variableTypeFilter: string) => {
  76. return (results: any[]) => {
  77. const segments = _.map(results, segment => {
  78. return self.newSegment({ value: segment.text, expandable: segment.expandable });
  79. });
  80. if (addTemplateVars) {
  81. _.each(templateSrv.variables, variable => {
  82. if (variableTypeFilter === void 0 || variableTypeFilter === variable.type) {
  83. segments.unshift(self.newSegment({ type: 'value', value: '$' + variable.name, expandable: true }));
  84. }
  85. });
  86. }
  87. return segments;
  88. };
  89. };
  90. this.newSelectMetric = () => {
  91. return new MetricSegment({ value: 'select metric', fake: true });
  92. };
  93. this.newPlusButton = () => {
  94. return new MetricSegment({
  95. fake: true,
  96. html: '<i class="fa fa-plus "></i>',
  97. type: 'plus-button',
  98. cssClass: 'query-part',
  99. });
  100. };
  101. }
  102. coreModule.service('uiSegmentSrv', uiSegmentSrv);