segment_srv.js 3.4 KB

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