grafana/public/app/core/services/segment_srv.ts

112 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-04-05 07:09:32 -05:00
import _ from 'lodash';
import coreModule from '../core_module';
/** @ngInject */
export function uiSegmentSrv(this: any, $sce, templateSrv) {
const self = this;
2018-04-05 07:09:32 -05:00
function MetricSegment(this: any, options) {
2018-04-05 07:09:32 -05:00
if (options === '*' || options.value === '*') {
this.value = '*';
this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
this.type = options.type;
this.expandable = true;
return;
}
if (_.isString(options)) {
this.value = options;
this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
return;
}
// temp hack to work around legacy inconsistency in segment model
this.text = options.value;
this.cssClass = options.cssClass;
this.custom = options.custom;
this.type = options.type;
this.fake = options.fake;
this.value = options.value;
this.selectMode = options.selectMode;
this.type = options.type;
this.expandable = options.expandable;
this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
}
this.getSegmentForValue = function(value, fallbackText) {
if (value) {
return this.newSegment(value);
} else {
return this.newSegment({ value: fallbackText, fake: true });
}
};
this.newSelectMeasurement = () => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: 'select measurement', fake: true });
};
this.newFake = (text, type, cssClass) => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: text, fake: true, type: type, cssClass: cssClass });
};
this.newSegment = options => {
2018-04-05 07:09:32 -05:00
return new MetricSegment(options);
};
this.newKey = key => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: key, type: 'key', cssClass: 'query-segment-key' });
};
this.newKeyValue = value => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: value, type: 'value', cssClass: 'query-segment-value' });
};
this.newCondition = condition => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: condition, type: 'condition', cssClass: 'query-keyword' });
};
this.newOperator = op => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: op, type: 'operator', cssClass: 'query-segment-operator' });
};
this.newOperators = ops => {
return _.map(ops, op => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: op, type: 'operator', cssClass: 'query-segment-operator' });
});
};
this.transformToSegments = (addTemplateVars, variableTypeFilter) => {
return results => {
const segments = _.map(results, segment => {
2018-04-05 07:09:32 -05:00
return self.newSegment({ value: segment.text, expandable: segment.expandable });
});
if (addTemplateVars) {
_.each(templateSrv.variables, variable => {
2018-04-05 07:09:32 -05:00
if (variableTypeFilter === void 0 || variableTypeFilter === variable.type) {
segments.unshift(self.newSegment({ type: 'value', value: '$' + variable.name, expandable: true }));
}
});
}
return segments;
};
};
this.newSelectMetric = () => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({ value: 'select metric', fake: true });
};
this.newPlusButton = () => {
2018-04-05 07:09:32 -05:00
return new MetricSegment({
fake: true,
html: '<i class="fa fa-plus "></i>',
type: 'plus-button',
cssClass: 'query-part',
});
};
}
coreModule.service('uiSegmentSrv', uiSegmentSrv);