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