2017-12-20 05:33:33 -06:00
|
|
|
import kbn from 'app/core/utils/kbn';
|
|
|
|
import _ from 'lodash';
|
2019-11-22 03:28:54 -06:00
|
|
|
import { escapeHtml } from 'app/core/utils/text';
|
2020-03-24 10:03:53 -05:00
|
|
|
import { deprecationWarning, ScopedVars, TimeRange } from '@grafana/data';
|
|
|
|
import { getFilteredVariables, getVariableClones, getVariableWithName } from '../variables/state/selectors';
|
2020-03-10 02:53:41 -05:00
|
|
|
import { getConfig } from 'app/core/config';
|
2020-03-23 23:46:31 -05:00
|
|
|
import { variableRegex } from './utils';
|
2020-03-23 03:00:36 -05:00
|
|
|
import { isAdHoc } from '../variables/guard';
|
2020-03-24 10:03:53 -05:00
|
|
|
import { VariableModel } from './types';
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
function luceneEscape(value: string) {
|
2017-12-21 01:39:31 -06:00
|
|
|
return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-09-13 09:38:21 -05:00
|
|
|
interface FieldAccessorCache {
|
|
|
|
[key: string]: (obj: any) => any;
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:03:26 -05:00
|
|
|
export class TemplateSrv {
|
2020-03-24 10:03:53 -05:00
|
|
|
private _variables: any[];
|
2018-10-24 05:06:09 -05:00
|
|
|
private regex = variableRegex;
|
2019-07-11 10:05:45 -05:00
|
|
|
private index: any = {};
|
|
|
|
private grafanaVariables: any = {};
|
2019-06-13 13:04:15 -05:00
|
|
|
private builtIns: any = {};
|
2020-03-10 02:53:41 -05:00
|
|
|
private timeRange?: TimeRange | null = null;
|
2019-09-13 09:38:21 -05:00
|
|
|
private fieldAccessorCache: FieldAccessorCache = {};
|
2017-10-22 00:03:26 -05:00
|
|
|
|
|
|
|
constructor() {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.builtIns['__interval'] = { text: '1s', value: '1s' };
|
|
|
|
this.builtIns['__interval_ms'] = { text: '100', value: '100' };
|
2020-03-24 10:03:53 -05:00
|
|
|
this._variables = [];
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
init(variables: any, timeRange?: TimeRange) {
|
2020-03-24 10:03:53 -05:00
|
|
|
this._variables = variables;
|
2019-01-29 08:05:28 -06:00
|
|
|
this.timeRange = timeRange;
|
2019-01-29 13:49:54 -06:00
|
|
|
this.updateIndex();
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-06-13 13:04:15 -05:00
|
|
|
getBuiltInIntervalValue() {
|
|
|
|
return this.builtIns.__interval.value;
|
|
|
|
}
|
|
|
|
|
2020-03-24 10:03:53 -05:00
|
|
|
/**
|
|
|
|
* @deprecated: this instance variable should not be used and will be removed in future releases
|
|
|
|
*
|
|
|
|
* Use getVariables function instead
|
|
|
|
*/
|
|
|
|
get variables(): any[] {
|
|
|
|
deprecationWarning('template_srv.ts', 'variables', 'getVariables');
|
|
|
|
return this.getVariables();
|
|
|
|
}
|
|
|
|
|
|
|
|
getVariables(): VariableModel[] {
|
|
|
|
if (getConfig().featureToggles.newVariables) {
|
|
|
|
return getVariableClones();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._variables;
|
|
|
|
}
|
|
|
|
|
2019-01-29 13:49:54 -06:00
|
|
|
updateIndex() {
|
2019-07-11 10:05:45 -05:00
|
|
|
const existsOrEmpty = (value: any) => value || value === '';
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2020-03-24 10:03:53 -05:00
|
|
|
this.index = this._variables.reduce((acc, currentValue) => {
|
2018-11-08 09:23:40 -06:00
|
|
|
if (currentValue.current && (currentValue.current.isNone || existsOrEmpty(currentValue.current.value))) {
|
2018-10-23 07:05:10 -05:00
|
|
|
acc[currentValue.name] = currentValue;
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
2018-10-23 07:05:10 -05:00
|
|
|
return acc;
|
|
|
|
}, {});
|
2019-01-29 08:05:28 -06:00
|
|
|
|
|
|
|
if (this.timeRange) {
|
|
|
|
const from = this.timeRange.from.valueOf().toString();
|
|
|
|
const to = this.timeRange.to.valueOf().toString();
|
|
|
|
|
|
|
|
this.index = {
|
|
|
|
...this.index,
|
|
|
|
['__from']: {
|
|
|
|
current: { value: from, text: from },
|
|
|
|
},
|
|
|
|
['__to']: {
|
|
|
|
current: { value: to, text: to },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 13:49:54 -06:00
|
|
|
updateTimeRange(timeRange: TimeRange) {
|
2019-01-29 08:05:28 -06:00
|
|
|
this.timeRange = timeRange;
|
2019-01-29 13:49:54 -06:00
|
|
|
this.updateIndex();
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
variableInitialized(variable: any) {
|
2017-10-22 00:03:26 -05:00
|
|
|
this.index[variable.name] = variable;
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
getAdhocFilters(datasourceName: string) {
|
|
|
|
let filters: any = [];
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2020-03-23 03:00:36 -05:00
|
|
|
for (const variable of this.getAdHocVariables()) {
|
|
|
|
if (variable.datasource === null || variable.datasource === datasourceName) {
|
|
|
|
filters = filters.concat(variable.filters);
|
|
|
|
} else if (variable.datasource.indexOf('$') === 0) {
|
|
|
|
if (this.replace(variable.datasource) === datasourceName) {
|
2017-10-22 00:03:26 -05:00
|
|
|
filters = filters.concat(variable.filters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
luceneFormat(value: any) {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return luceneEscape(value);
|
|
|
|
}
|
2018-05-08 07:04:20 -05:00
|
|
|
if (value instanceof Array && value.length === 0) {
|
2018-05-18 04:10:10 -05:00
|
|
|
return '__empty__';
|
2018-05-08 07:04:20 -05:00
|
|
|
}
|
2018-09-05 00:47:30 -05:00
|
|
|
const quotedValues = _.map(value, val => {
|
2017-12-19 09:06:54 -06:00
|
|
|
return '"' + luceneEscape(val) + '"';
|
2017-10-22 00:03:26 -05:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
return '(' + quotedValues.join(' OR ') + ')';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-01-22 08:09:58 -06:00
|
|
|
// encode string according to RFC 3986; in contrast to encodeURIComponent()
|
|
|
|
// also the sub-delims "!", "'", "(", ")" and "*" are encoded;
|
|
|
|
// unicode handling uses UTF-8 as in ECMA-262.
|
2019-07-11 10:05:45 -05:00
|
|
|
encodeURIComponentStrict(str: string) {
|
2019-01-29 08:05:28 -06:00
|
|
|
return encodeURIComponent(str).replace(/[!'()*]/g, c => {
|
|
|
|
return (
|
|
|
|
'%' +
|
|
|
|
c
|
|
|
|
.charCodeAt(0)
|
|
|
|
.toString(16)
|
|
|
|
.toUpperCase()
|
|
|
|
);
|
2018-08-06 14:54:12 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
formatValue(value: any, format: any, variable: any) {
|
2017-10-22 00:03:26 -05:00
|
|
|
// for some scopedVars there is no variable
|
|
|
|
variable = variable || {};
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (typeof format === 'function') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return format(value, variable, this.formatValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (format) {
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'regex': {
|
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return kbn.regexEscape(value);
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const escapedValues = _.map(value, kbn.regexEscape);
|
2018-02-15 11:44:03 -06:00
|
|
|
if (escapedValues.length === 1) {
|
|
|
|
return escapedValues[0];
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
return '(' + escapedValues.join('|') + ')';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'lucene': {
|
2017-10-22 00:03:26 -05:00
|
|
|
return this.luceneFormat(value);
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'pipe': {
|
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return value;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
return value.join('|');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'distributed': {
|
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return this.distributeVariable(value, variable.name);
|
|
|
|
}
|
2018-03-07 06:35:54 -06:00
|
|
|
case 'csv': {
|
|
|
|
if (_.isArray(value)) {
|
|
|
|
return value.join(',');
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2019-11-22 03:28:54 -06:00
|
|
|
case 'html': {
|
|
|
|
if (_.isArray(value)) {
|
|
|
|
return escapeHtml(value.join(', '));
|
|
|
|
}
|
|
|
|
return escapeHtml(value);
|
|
|
|
}
|
2019-02-06 23:07:41 -06:00
|
|
|
case 'json': {
|
|
|
|
return JSON.stringify(value);
|
|
|
|
}
|
2018-08-06 14:54:12 -05:00
|
|
|
case 'percentencode': {
|
2018-07-30 10:19:41 -05:00
|
|
|
// like glob, but url escaped
|
|
|
|
if (_.isArray(value)) {
|
2019-01-22 08:09:58 -06:00
|
|
|
return this.encodeURIComponentStrict('{' + value.join(',') + '}');
|
2018-07-30 10:19:41 -05:00
|
|
|
}
|
2019-01-22 08:09:58 -06:00
|
|
|
return this.encodeURIComponentStrict(value);
|
2018-07-30 10:19:41 -05:00
|
|
|
}
|
2020-03-24 10:26:58 -05:00
|
|
|
case 'singlequote': {
|
|
|
|
// escape single quotes with backslash
|
|
|
|
const regExp = new RegExp(`'`, 'g');
|
|
|
|
if (_.isArray(value)) {
|
|
|
|
return _.map(value, v => `'${_.replace(v, regExp, `\\'`)}'`).join(',');
|
|
|
|
}
|
|
|
|
return `'${_.replace(value, regExp, `\\'`)}'`;
|
|
|
|
}
|
|
|
|
case 'doublequote': {
|
|
|
|
// escape double quotes with backslash
|
|
|
|
const regExp = new RegExp('"', 'g');
|
|
|
|
if (_.isArray(value)) {
|
|
|
|
return _.map(value, v => `"${_.replace(v, regExp, '\\"')}"`).join(',');
|
|
|
|
}
|
|
|
|
return `"${_.replace(value, regExp, '\\"')}"`;
|
|
|
|
}
|
|
|
|
case 'sqlstring': {
|
|
|
|
// escape single quotes by pairing them
|
|
|
|
const regExp = new RegExp(`'`, 'g');
|
|
|
|
if (_.isArray(value)) {
|
|
|
|
return _.map(value, v => `'${_.replace(v, regExp, "''")}'`).join(',');
|
|
|
|
}
|
|
|
|
return `'${_.replace(value, regExp, "''")}'`;
|
|
|
|
}
|
2017-12-19 09:06:54 -06:00
|
|
|
default: {
|
2019-08-07 08:50:06 -05:00
|
|
|
if (_.isArray(value) && value.length > 1) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '{' + value.join(',') + '}';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
setGrafanaVariable(name: string, value: any) {
|
2017-10-22 00:03:26 -05:00
|
|
|
this.grafanaVariables[name] = value;
|
|
|
|
}
|
|
|
|
|
2020-01-21 09:06:04 -06:00
|
|
|
setGlobalVariable(name: string, variable: any) {
|
|
|
|
this.index = {
|
|
|
|
...this.index,
|
|
|
|
[name]: {
|
|
|
|
current: variable,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
getVariableName(expression: string) {
|
2017-10-22 00:03:26 -05:00
|
|
|
this.regex.lastIndex = 0;
|
2018-08-26 14:52:57 -05:00
|
|
|
const match = this.regex.exec(expression);
|
2017-10-22 00:03:26 -05:00
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-24 06:27:09 -05:00
|
|
|
const variableName = match.slice(1).find(match => match !== undefined);
|
|
|
|
return variableName;
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
variableExists(expression: string) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const name = this.getVariableName(expression);
|
2020-03-10 02:53:41 -05:00
|
|
|
return name && this.getVariableAtIndex(name) !== void 0;
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
highlightVariablesAsHtml(str: string) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!str || !_.isString(str)) {
|
|
|
|
return str;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
|
|
|
str = _.escape(str);
|
|
|
|
this.regex.lastIndex = 0;
|
2018-02-23 08:51:19 -06:00
|
|
|
return str.replace(this.regex, (match, var1, var2, fmt2, var3) => {
|
2020-03-10 02:53:41 -05:00
|
|
|
if (this.getVariableAtIndex(var1 || var2 || var3) || this.builtIns[var1 || var2 || var3]) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '<span class="template-variable">' + match + '</span>';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
getAllValue(variable: any) {
|
2017-10-22 00:03:26 -05:00
|
|
|
if (variable.allValue) {
|
|
|
|
return variable.allValue;
|
|
|
|
}
|
2018-08-26 14:52:57 -05:00
|
|
|
const values = [];
|
2018-08-30 01:58:43 -05:00
|
|
|
for (let i = 1; i < variable.options.length; i++) {
|
2017-10-22 00:03:26 -05:00
|
|
|
values.push(variable.options[i].value);
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2019-09-13 09:38:21 -05:00
|
|
|
getFieldAccessor(fieldPath: string) {
|
|
|
|
const accessor = this.fieldAccessorCache[fieldPath];
|
|
|
|
if (accessor) {
|
|
|
|
return accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (this.fieldAccessorCache[fieldPath] = _.property(fieldPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
getVariableValue(variableName: string, fieldPath: string | undefined, scopedVars: ScopedVars) {
|
|
|
|
const scopedVar = scopedVars[variableName];
|
|
|
|
if (!scopedVar) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fieldPath) {
|
|
|
|
return this.getFieldAccessor(fieldPath)(scopedVar.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return scopedVar.value;
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
replace(target: string, scopedVars?: ScopedVars, format?: string | Function): any {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!target) {
|
|
|
|
return target;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
|
|
|
this.regex.lastIndex = 0;
|
|
|
|
|
2019-09-13 09:38:21 -05:00
|
|
|
return target.replace(this.regex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
|
|
|
|
const variableName = var1 || var2 || var3;
|
2020-03-10 02:53:41 -05:00
|
|
|
const variable = this.getVariableAtIndex(variableName);
|
2019-09-13 09:38:21 -05:00
|
|
|
const fmt = fmt2 || fmt3 || format;
|
|
|
|
|
2017-10-22 00:03:26 -05:00
|
|
|
if (scopedVars) {
|
2019-09-13 09:38:21 -05:00
|
|
|
const value = this.getVariableValue(variableName, fieldPath, scopedVars);
|
|
|
|
if (value !== null && value !== undefined) {
|
|
|
|
return this.formatValue(value, fmt, variable);
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!variable) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2019-09-13 09:38:21 -05:00
|
|
|
const systemValue = this.grafanaVariables[variable.current.value];
|
2017-10-22 00:03:26 -05:00
|
|
|
if (systemValue) {
|
2018-05-02 16:03:37 -05:00
|
|
|
return this.formatValue(systemValue, fmt, variable);
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2019-09-13 09:38:21 -05:00
|
|
|
let value = variable.current.value;
|
2017-10-22 00:03:26 -05:00
|
|
|
if (this.isAllValue(value)) {
|
|
|
|
value = this.getAllValue(variable);
|
2018-04-13 12:48:37 -05:00
|
|
|
// skip formatting of custom all values
|
2017-10-22 00:03:26 -05:00
|
|
|
if (variable.allValue) {
|
2018-08-17 09:04:32 -05:00
|
|
|
return this.replace(value);
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 09:06:04 -06:00
|
|
|
if (fieldPath) {
|
|
|
|
const fieldValue = this.getVariableValue(variableName, fieldPath, {
|
|
|
|
[variableName]: { value: value, text: '' },
|
|
|
|
});
|
|
|
|
if (fieldValue !== null && fieldValue !== undefined) {
|
|
|
|
return this.formatValue(fieldValue, fmt, variable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const res = this.formatValue(value, fmt, variable);
|
2017-10-22 00:03:26 -05:00
|
|
|
return res;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
isAllValue(value: any) {
|
2017-12-21 01:39:31 -06:00
|
|
|
return value === '$__all' || (Array.isArray(value) && value[0] === '$__all');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 00:25:27 -06:00
|
|
|
replaceWithText(target: string, scopedVars?: ScopedVars) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!target) {
|
|
|
|
return target;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2018-08-30 01:58:43 -05:00
|
|
|
let variable;
|
2017-10-22 00:03:26 -05:00
|
|
|
this.regex.lastIndex = 0;
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
return target.replace(this.regex, (match: any, var1: any, var2: any, fmt2: any, var3: any) => {
|
2017-10-22 00:03:26 -05:00
|
|
|
if (scopedVars) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const option = scopedVars[var1 || var2 || var3];
|
2017-12-19 09:06:54 -06:00
|
|
|
if (option) {
|
|
|
|
return option.text;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2020-03-10 02:53:41 -05:00
|
|
|
variable = this.getVariableAtIndex(var1 || var2 || var3);
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!variable) {
|
|
|
|
return match;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2019-01-23 22:57:11 -06:00
|
|
|
const value = this.grafanaVariables[variable.current.value];
|
|
|
|
|
2019-01-29 08:05:28 -06:00
|
|
|
return typeof value === 'string' ? value : variable.current.text;
|
2017-10-22 00:03:26 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
fillVariableValuesForUrl(params: any, scopedVars?: ScopedVars) {
|
2020-03-24 10:03:53 -05:00
|
|
|
_.each(this._variables, variable => {
|
2017-10-22 00:03:26 -05:00
|
|
|
if (scopedVars && scopedVars[variable.name] !== void 0) {
|
2018-07-11 12:06:36 -05:00
|
|
|
if (scopedVars[variable.name].skipUrlSync) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
params['var-' + variable.name] = scopedVars[variable.name].value;
|
2017-10-22 00:03:26 -05:00
|
|
|
} else {
|
2018-07-11 12:06:36 -05:00
|
|
|
if (variable.skipUrlSync) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
params['var-' + variable.name] = variable.getValueForUrl();
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:05:45 -05:00
|
|
|
distributeVariable(value: any, variable: any) {
|
2019-04-15 05:11:52 -05:00
|
|
|
value = _.map(value, (val: any, index: number) => {
|
2017-10-22 00:03:26 -05:00
|
|
|
if (index !== 0) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return variable + '=' + val;
|
2017-10-22 00:03:26 -05:00
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
return value.join(',');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
2020-03-10 02:53:41 -05:00
|
|
|
|
|
|
|
private getVariableAtIndex = (name: string): any => {
|
|
|
|
if (!name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getConfig().featureToggles.newVariables && !this.index[name]) {
|
2020-03-23 07:45:08 -05:00
|
|
|
return getVariableWithName(name);
|
2020-03-10 02:53:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.index[name];
|
|
|
|
};
|
2020-03-23 03:00:36 -05:00
|
|
|
|
|
|
|
private getAdHocVariables = (): any[] => {
|
|
|
|
if (getConfig().featureToggles.newVariables) {
|
|
|
|
return getFilteredVariables(isAdHoc);
|
|
|
|
}
|
2020-03-24 10:03:53 -05:00
|
|
|
if (Array.isArray(this._variables)) {
|
|
|
|
return this._variables.filter(isAdHoc);
|
2020-03-23 03:00:36 -05:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
2017-12-14 05:31:57 -06:00
|
|
|
|
|
|
|
export default new TemplateSrv();
|