2014-08-27 17:58:49 +02:00
|
|
|
define([
|
|
|
|
|
'angular',
|
|
|
|
|
'lodash',
|
2016-07-06 08:25:46 +02:00
|
|
|
'jquery',
|
2015-10-30 14:44:40 +01:00
|
|
|
'app/core/utils/kbn',
|
2014-08-27 17:58:49 +02:00
|
|
|
],
|
2016-07-06 08:25:46 +02:00
|
|
|
function (angular, _, $, kbn) {
|
2014-08-27 17:58:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
|
2016-07-06 08:25:46 +02:00
|
|
|
module.service('templateValuesSrv', function($q, $rootScope, datasourceSrv, $location, templateSrv, timeSrv) {
|
2014-08-27 17:58:49 +02:00
|
|
|
var self = this;
|
2016-07-25 12:02:59 +02:00
|
|
|
this.variableLock = {};
|
2015-07-06 13:43:08 +02:00
|
|
|
|
2015-06-26 14:05:37 -04:00
|
|
|
function getNoneOption() { return { text: 'None', value: '', isNone: true }; }
|
2014-08-27 17:58:49 +02:00
|
|
|
|
2016-03-09 13:10:02 +01:00
|
|
|
// update time variant variables
|
|
|
|
|
$rootScope.onAppEvent('refresh', function() {
|
|
|
|
|
|
|
|
|
|
// look for interval variables
|
|
|
|
|
var intervalVariable = _.findWhere(self.variables, { type: 'interval' });
|
|
|
|
|
if (intervalVariable) {
|
|
|
|
|
self.updateAutoInterval(intervalVariable);
|
2014-09-05 12:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-09 13:10:02 +01:00
|
|
|
// update variables with refresh === 2
|
|
|
|
|
var promises = self.variables
|
|
|
|
|
.filter(function(variable) {
|
|
|
|
|
return variable.refresh === 2;
|
|
|
|
|
}).map(function(variable) {
|
2016-07-06 08:25:46 +02:00
|
|
|
var previousOptions = variable.options.slice();
|
|
|
|
|
|
2016-05-13 16:29:11 +09:00
|
|
|
return self.updateOptions(variable).then(function () {
|
|
|
|
|
return self.variableUpdated(variable).then(function () {
|
2016-07-06 08:25:46 +02:00
|
|
|
// check if current options changed due to refresh
|
|
|
|
|
if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) {
|
|
|
|
|
$rootScope.appEvent('template-variable-value-updated');
|
2016-05-16 19:18:50 +09:00
|
|
|
}
|
2016-05-13 16:29:11 +09:00
|
|
|
});
|
|
|
|
|
});
|
2016-03-09 13:10:02 +01:00
|
|
|
});
|
|
|
|
|
|
2015-12-10 23:14:37 -05:00
|
|
|
return $q.all(promises);
|
2016-03-09 13:10:02 +01:00
|
|
|
|
2015-12-10 23:14:37 -05:00
|
|
|
}, $rootScope);
|
|
|
|
|
|
2015-03-14 15:29:41 -04:00
|
|
|
this.init = function(dashboard) {
|
2016-05-13 16:29:11 +09:00
|
|
|
this.dashboard = dashboard;
|
2014-08-28 12:44:01 +02:00
|
|
|
this.variables = dashboard.templating.list;
|
|
|
|
|
templateSrv.init(this.variables);
|
2014-08-27 21:47:41 +02:00
|
|
|
|
2015-03-14 15:29:41 -04:00
|
|
|
var queryParams = $location.search();
|
|
|
|
|
var promises = [];
|
|
|
|
|
|
2016-03-08 09:32:54 +01:00
|
|
|
// use promises to delay processing variables that
|
|
|
|
|
// depend on other variables.
|
2016-03-08 15:03:34 +08:00
|
|
|
this.variableLock = {};
|
|
|
|
|
_.forEach(this.variables, function(variable) {
|
|
|
|
|
self.variableLock[variable.name] = $q.defer();
|
|
|
|
|
});
|
|
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
for (var i = 0; i < this.variables.length; i++) {
|
2014-09-08 11:03:14 +02:00
|
|
|
var variable = this.variables[i];
|
2016-03-08 15:03:34 +08:00
|
|
|
promises.push(this.processVariable(variable, queryParams));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $q.all(promises);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.processVariable = function(variable, queryParams) {
|
|
|
|
|
var dependencies = [];
|
2016-03-08 09:32:54 +01:00
|
|
|
var lock = self.variableLock[variable.name];
|
|
|
|
|
|
2016-03-08 15:03:34 +08:00
|
|
|
// determine our dependencies.
|
|
|
|
|
if (variable.type === "query") {
|
|
|
|
|
_.forEach(this.variables, function(v) {
|
2016-04-28 19:31:43 +02:00
|
|
|
// both query and datasource can contain variable
|
|
|
|
|
if (templateSrv.containsVariable(variable.query, v.name) ||
|
|
|
|
|
templateSrv.containsVariable(variable.datasource, v.name)) {
|
2016-03-08 15:03:34 +08:00
|
|
|
dependencies.push(self.variableLock[v.name].promise);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-03-08 09:32:54 +01:00
|
|
|
|
2016-03-08 15:03:34 +08:00
|
|
|
return $q.all(dependencies).then(function() {
|
2015-03-14 15:29:41 -04:00
|
|
|
var urlValue = queryParams['var-' + variable.name];
|
2014-09-08 11:03:14 +02:00
|
|
|
if (urlValue !== void 0) {
|
2016-03-08 09:32:54 +01:00
|
|
|
return self.setVariableFromUrl(variable, urlValue).then(lock.resolve);
|
2014-08-27 21:47:41 +02:00
|
|
|
}
|
2016-03-10 13:14:24 +09:00
|
|
|
else if (variable.refresh === 1 || variable.refresh === 2) {
|
2016-03-08 15:03:34 +08:00
|
|
|
return self.updateOptions(variable).then(function() {
|
2016-03-08 15:41:24 +08:00
|
|
|
if (_.isEmpty(variable.current) && variable.options.length) {
|
2016-03-08 09:32:54 +01:00
|
|
|
self.setVariableValue(variable, variable.options[0]);
|
2016-03-08 15:41:24 +08:00
|
|
|
}
|
2016-03-08 09:32:54 +01:00
|
|
|
lock.resolve();
|
2016-03-08 15:03:34 +08:00
|
|
|
});
|
2014-09-08 11:03:14 +02:00
|
|
|
}
|
|
|
|
|
else if (variable.type === 'interval') {
|
2016-03-08 15:03:34 +08:00
|
|
|
self.updateAutoInterval(variable);
|
2016-03-08 09:32:54 +01:00
|
|
|
lock.resolve();
|
2016-03-08 15:03:34 +08:00
|
|
|
} else {
|
2016-03-08 09:32:54 +01:00
|
|
|
lock.resolve();
|
2014-09-05 12:07:48 +02:00
|
|
|
}
|
2016-07-25 12:02:59 +02:00
|
|
|
}).finally(function() {
|
|
|
|
|
delete self.variableLock[variable.name];
|
2016-03-08 15:03:34 +08:00
|
|
|
});
|
2014-09-05 12:07:48 +02:00
|
|
|
};
|
|
|
|
|
|
2015-04-29 14:55:39 +08:00
|
|
|
this.setVariableFromUrl = function(variable, urlValue) {
|
2016-03-08 09:32:54 +01:00
|
|
|
var promise = $q.when(true);
|
2016-03-08 15:03:34 +08:00
|
|
|
|
2016-03-08 09:32:54 +01:00
|
|
|
if (variable.refresh) {
|
|
|
|
|
promise = this.updateOptions(variable);
|
2016-03-08 15:03:34 +08:00
|
|
|
}
|
2015-04-29 14:55:39 +08:00
|
|
|
|
2016-03-08 09:32:54 +01:00
|
|
|
return promise.then(function() {
|
2016-05-24 16:34:17 +02:00
|
|
|
var option = _.find(variable.options, function(op) {
|
|
|
|
|
return op.text === urlValue || op.value === urlValue;
|
|
|
|
|
});
|
|
|
|
|
|
2016-05-27 09:23:08 +02:00
|
|
|
option = option || { text: urlValue, value: urlValue };
|
|
|
|
|
|
2016-03-08 09:32:54 +01:00
|
|
|
self.updateAutoInterval(variable);
|
|
|
|
|
return self.setVariableValue(variable, option, true);
|
|
|
|
|
});
|
2015-04-29 14:55:39 +08:00
|
|
|
};
|
|
|
|
|
|
2014-09-05 12:07:48 +02:00
|
|
|
this.updateAutoInterval = function(variable) {
|
|
|
|
|
if (!variable.auto) { return; }
|
|
|
|
|
|
|
|
|
|
// add auto option if missing
|
2015-04-22 12:27:12 +02:00
|
|
|
if (variable.options.length && variable.options[0].text !== 'auto') {
|
2014-09-05 12:07:48 +02:00
|
|
|
variable.options.unshift({ text: 'auto', value: '$__auto_interval' });
|
2014-08-27 21:47:41 +02:00
|
|
|
}
|
2014-09-05 12:07:48 +02:00
|
|
|
|
2016-02-22 10:27:08 +01:00
|
|
|
var interval = kbn.calculateInterval(timeSrv.timeRange(), variable.auto_count, (variable.auto_min ? ">"+variable.auto_min : null));
|
2014-09-05 12:07:48 +02:00
|
|
|
templateSrv.setGrafanaVariable('$__auto_interval', interval);
|
2014-08-27 21:47:41 +02:00
|
|
|
};
|
|
|
|
|
|
2016-07-25 12:02:59 +02:00
|
|
|
this.setVariableValue = function(variable, option) {
|
2015-05-29 17:51:07 +02:00
|
|
|
variable.current = angular.copy(option);
|
2015-06-19 15:10:34 -04:00
|
|
|
|
2016-05-26 13:18:02 +02:00
|
|
|
if (_.isArray(variable.current.text)) {
|
|
|
|
|
variable.current.text = variable.current.text.join(' + ');
|
2015-06-19 15:10:34 -04:00
|
|
|
}
|
|
|
|
|
|
2015-08-17 15:01:16 +02:00
|
|
|
self.selectOptionsForCurrentValue(variable);
|
2014-08-27 17:58:49 +02:00
|
|
|
templateSrv.updateTemplateData();
|
2016-03-08 09:32:54 +01:00
|
|
|
|
2016-07-25 12:02:59 +02:00
|
|
|
return this.updateOptionsInChildVariables(variable);
|
2014-08-27 17:58:49 +02:00
|
|
|
};
|
|
|
|
|
|
2015-03-19 23:09:50 -04:00
|
|
|
this.variableUpdated = function(variable) {
|
|
|
|
|
templateSrv.updateTemplateData();
|
2016-05-13 16:29:11 +09:00
|
|
|
return self.updateOptionsInChildVariables(variable);
|
2015-03-19 23:09:50 -04:00
|
|
|
};
|
|
|
|
|
|
2014-09-01 11:13:18 +02:00
|
|
|
this.updateOptionsInChildVariables = function(updatedVariable) {
|
2016-07-25 12:02:59 +02:00
|
|
|
// if there is a variable lock ignore cascading update because we are in a boot up scenario
|
|
|
|
|
if (self.variableLock[updatedVariable.name]) {
|
|
|
|
|
return $q.when();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
var promises = _.map(self.variables, function(otherVariable) {
|
|
|
|
|
if (otherVariable === updatedVariable) {
|
2014-08-27 17:58:49 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2016-08-05 04:03:46 -04:00
|
|
|
if ((otherVariable.type === "datasource" &&
|
|
|
|
|
templateSrv.containsVariable(otherVariable.regex, updatedVariable.name)) ||
|
|
|
|
|
templateSrv.containsVariable(otherVariable.query, updatedVariable.name) ||
|
2016-04-28 19:31:43 +02:00
|
|
|
templateSrv.containsVariable(otherVariable.datasource, updatedVariable.name)) {
|
2014-08-28 12:44:01 +02:00
|
|
|
return self.updateOptions(otherVariable);
|
2014-08-27 17:58:49 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $q.all(promises);
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-05 14:03:36 +02:00
|
|
|
this._updateNonQueryVariable = function(variable) {
|
2016-04-16 12:03:29 -04:00
|
|
|
if (variable.type === 'datasource') {
|
|
|
|
|
self.updateDataSourceVariable(variable);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 16:59:29 +02:00
|
|
|
if (variable.type === 'constant') {
|
|
|
|
|
variable.options = [{text: variable.query, value: variable.query}];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-23 09:32:18 +09:00
|
|
|
// extract options in comma separated string
|
2015-01-23 10:41:23 +01:00
|
|
|
variable.options = _.map(variable.query.split(/[,]+/), function(text) {
|
2015-01-23 11:36:18 +01:00
|
|
|
return { text: text.trim(), value: text.trim() };
|
2014-09-05 14:03:36 +02:00
|
|
|
});
|
2014-09-05 12:07:48 +02:00
|
|
|
|
2014-09-05 14:03:36 +02:00
|
|
|
if (variable.type === 'interval') {
|
2014-09-05 12:07:48 +02:00
|
|
|
self.updateAutoInterval(variable);
|
2016-05-28 16:59:29 +02:00
|
|
|
return;
|
2014-09-05 14:03:36 +02:00
|
|
|
}
|
2015-10-30 22:34:40 -07:00
|
|
|
|
|
|
|
|
if (variable.type === 'custom' && variable.includeAll) {
|
|
|
|
|
self.addAllOption(variable);
|
|
|
|
|
}
|
2014-09-05 14:03:36 +02:00
|
|
|
};
|
|
|
|
|
|
2016-04-16 12:03:29 -04:00
|
|
|
this.updateDataSourceVariable = function(variable) {
|
|
|
|
|
var options = [];
|
2016-04-16 13:43:16 -04:00
|
|
|
var sources = datasourceSrv.getMetricSources({skipVariables: true});
|
2016-04-16 12:03:29 -04:00
|
|
|
var regex;
|
|
|
|
|
|
|
|
|
|
if (variable.regex) {
|
|
|
|
|
regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < sources.length; i++) {
|
|
|
|
|
var source = sources[i];
|
|
|
|
|
// must match on type
|
|
|
|
|
if (source.meta.id !== variable.query) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (regex && !regex.exec(source.name)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.push({text: source.name, value: source.name});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.length === 0) {
|
2016-05-11 13:21:25 +02:00
|
|
|
options.push({text: 'No data sources found', value: ''});
|
2016-04-16 12:03:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
variable.options = options;
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-05 14:03:36 +02:00
|
|
|
this.updateOptions = function(variable) {
|
|
|
|
|
if (variable.type !== 'query') {
|
|
|
|
|
self._updateNonQueryVariable(variable);
|
2016-05-09 10:17:30 +02:00
|
|
|
return self.validateVariableSelectionState(variable);
|
2014-08-28 12:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 13:38:13 +02:00
|
|
|
return datasourceSrv.get(variable.datasource)
|
|
|
|
|
.then(_.partial(this.updateOptionsFromMetricFindQuery, variable))
|
2016-03-08 09:32:54 +01:00
|
|
|
.then(_.partial(this.updateTags, variable))
|
|
|
|
|
.then(_.partial(this.validateVariableSelectionState, variable));
|
2015-06-11 13:38:13 +02:00
|
|
|
};
|
2014-08-27 17:58:49 +02:00
|
|
|
|
2015-07-17 14:59:05 +02:00
|
|
|
this.selectOptionsForCurrentValue = function(variable) {
|
2015-08-17 15:01:16 +02:00
|
|
|
var i, y, value, option;
|
2016-05-26 13:18:02 +02:00
|
|
|
var selected = [];
|
2015-08-17 15:01:16 +02:00
|
|
|
|
|
|
|
|
for (i = 0; i < variable.options.length; i++) {
|
|
|
|
|
option = variable.options[i];
|
|
|
|
|
option.selected = false;
|
|
|
|
|
if (_.isArray(variable.current.value)) {
|
|
|
|
|
for (y = 0; y < variable.current.value.length; y++) {
|
2015-08-21 10:40:14 +02:00
|
|
|
value = variable.current.value[y];
|
2015-08-17 15:01:16 +02:00
|
|
|
if (option.value === value) {
|
|
|
|
|
option.selected = true;
|
2016-05-26 13:18:02 +02:00
|
|
|
selected.push(option);
|
2015-08-17 15:01:16 +02:00
|
|
|
}
|
2015-07-17 14:59:05 +02:00
|
|
|
}
|
2015-08-17 15:01:16 +02:00
|
|
|
} else if (option.value === variable.current.value) {
|
|
|
|
|
option.selected = true;
|
2016-05-26 13:18:02 +02:00
|
|
|
selected.push(option);
|
2015-07-17 14:59:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-26 13:18:02 +02:00
|
|
|
|
|
|
|
|
return selected;
|
2015-07-17 14:59:05 +02:00
|
|
|
};
|
|
|
|
|
|
2015-06-11 13:38:13 +02:00
|
|
|
this.validateVariableSelectionState = function(variable) {
|
|
|
|
|
if (!variable.current) {
|
2015-06-19 15:10:34 -04:00
|
|
|
if (!variable.options.length) { return; }
|
2016-05-24 09:50:23 +02:00
|
|
|
return self.setVariableValue(variable, variable.options[0], false);
|
2015-06-11 13:38:13 +02:00
|
|
|
}
|
2014-08-27 17:58:49 +02:00
|
|
|
|
2015-06-11 13:38:13 +02:00
|
|
|
if (_.isArray(variable.current.value)) {
|
2016-05-26 13:18:02 +02:00
|
|
|
var selected = self.selectOptionsForCurrentValue(variable);
|
|
|
|
|
|
2016-05-24 09:50:23 +02:00
|
|
|
// if none pick first
|
2016-05-26 13:18:02 +02:00
|
|
|
if (selected.length === 0) {
|
2016-05-24 09:50:23 +02:00
|
|
|
selected = variable.options[0];
|
2016-05-26 13:18:02 +02:00
|
|
|
} else {
|
|
|
|
|
selected = {
|
|
|
|
|
value: _.map(selected, function(val) {return val.value;}),
|
|
|
|
|
text: _.map(selected, function(val) {return val.text;}).join(' + '),
|
|
|
|
|
};
|
2016-05-24 09:50:23 +02:00
|
|
|
}
|
2016-05-26 13:18:02 +02:00
|
|
|
|
2016-05-24 09:50:23 +02:00
|
|
|
return self.setVariableValue(variable, selected, false);
|
2015-06-11 13:38:13 +02:00
|
|
|
} else {
|
2016-05-09 10:17:30 +02:00
|
|
|
var currentOption = _.findWhere(variable.options, {text: variable.current.text});
|
2015-06-11 13:38:13 +02:00
|
|
|
if (currentOption) {
|
2016-05-24 09:50:23 +02:00
|
|
|
return self.setVariableValue(variable, currentOption, false);
|
2015-06-23 20:09:17 +02:00
|
|
|
} else {
|
2016-05-28 16:59:29 +02:00
|
|
|
if (!variable.options.length) { return $q.when(null); }
|
2015-06-23 20:09:17 +02:00
|
|
|
return self.setVariableValue(variable, variable.options[0]);
|
2015-06-11 13:38:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-08-27 17:58:49 +02:00
|
|
|
|
2015-06-11 13:38:13 +02:00
|
|
|
this.updateTags = function(variable, datasource) {
|
|
|
|
|
if (variable.useTags) {
|
|
|
|
|
return datasource.metricFindQuery(variable.tagsQuery).then(function (results) {
|
|
|
|
|
variable.tags = [];
|
|
|
|
|
for (var i = 0; i < results.length; i++) {
|
|
|
|
|
variable.tags.push(results[i].text);
|
|
|
|
|
}
|
|
|
|
|
return datasource;
|
2014-08-27 17:58:49 +02:00
|
|
|
});
|
2015-06-11 13:38:13 +02:00
|
|
|
} else {
|
|
|
|
|
delete variable.tags;
|
|
|
|
|
}
|
2015-05-20 18:18:03 +02:00
|
|
|
|
2015-06-11 13:38:13 +02:00
|
|
|
return datasource;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.updateOptionsFromMetricFindQuery = function(variable, datasource) {
|
|
|
|
|
return datasource.metricFindQuery(variable.query).then(function (results) {
|
|
|
|
|
variable.options = self.metricNamesToVariableValues(variable, results);
|
|
|
|
|
if (variable.includeAll) {
|
|
|
|
|
self.addAllOption(variable);
|
2015-05-20 18:18:03 +02:00
|
|
|
}
|
2015-06-26 14:05:37 -04:00
|
|
|
if (!variable.options.length) {
|
|
|
|
|
variable.options.push(getNoneOption());
|
|
|
|
|
}
|
2015-06-11 13:38:13 +02:00
|
|
|
return datasource;
|
2015-02-28 10:29:17 +01:00
|
|
|
});
|
2014-08-27 17:58:49 +02:00
|
|
|
};
|
|
|
|
|
|
2015-05-30 09:34:11 +02:00
|
|
|
this.getValuesForTag = function(variable, tagKey) {
|
|
|
|
|
return datasourceSrv.get(variable.datasource).then(function(datasource) {
|
|
|
|
|
var query = variable.tagValuesQuery.replace('$tag', tagKey);
|
|
|
|
|
return datasource.metricFindQuery(query).then(function (results) {
|
|
|
|
|
return _.map(results, function(value) {
|
|
|
|
|
return value.text;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-01 11:13:18 +02:00
|
|
|
this.metricNamesToVariableValues = function(variable, metricNames) {
|
|
|
|
|
var regex, options, i, matches;
|
2016-05-21 17:56:57 +09:00
|
|
|
options = [];
|
2014-09-01 11:13:18 +02:00
|
|
|
|
|
|
|
|
if (variable.regex) {
|
2014-09-05 15:46:29 +02:00
|
|
|
regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
|
2014-09-01 11:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < metricNames.length; i++) {
|
2016-04-07 19:23:20 -04:00
|
|
|
var item = metricNames[i];
|
|
|
|
|
var value = item.value || item.text;
|
|
|
|
|
var text = item.text || item.value;
|
2014-09-01 11:13:18 +02:00
|
|
|
|
2016-05-20 10:24:24 +02:00
|
|
|
if (_.isNumber(value)) {
|
|
|
|
|
value = value.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isNumber(text)) {
|
|
|
|
|
text = text.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-01 11:13:18 +02:00
|
|
|
if (regex) {
|
|
|
|
|
matches = regex.exec(value);
|
|
|
|
|
if (!matches) { continue; }
|
|
|
|
|
if (matches.length > 1) {
|
|
|
|
|
value = matches[1];
|
2016-04-13 16:41:19 -04:00
|
|
|
text = value;
|
2014-09-01 11:13:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 17:56:57 +09:00
|
|
|
options.push({text: text, value: value});
|
2014-09-01 11:13:18 +02:00
|
|
|
}
|
2016-05-21 17:56:57 +09:00
|
|
|
options = _.uniq(options, 'value');
|
2014-09-01 11:13:18 +02:00
|
|
|
|
2016-09-13 08:11:04 +02:00
|
|
|
return this.sortVariableValues(options, variable.sort);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.addAllOption = function(variable) {
|
|
|
|
|
variable.options.unshift({text: 'All', value: "$__all"});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.sortVariableValues = function(options, sortOrder) {
|
|
|
|
|
if (sortOrder === 0) {
|
2016-06-18 02:08:24 +09:00
|
|
|
return options;
|
2016-06-26 22:15:57 +09:00
|
|
|
}
|
2016-06-18 02:08:24 +09:00
|
|
|
|
2016-09-13 08:11:04 +02:00
|
|
|
var sortType = Math.ceil(sortOrder / 2);
|
|
|
|
|
var reverseSort = (sortOrder % 2 === 0);
|
2016-06-26 22:15:57 +09:00
|
|
|
if (sortType === 1) {
|
|
|
|
|
options = _.sortBy(options, 'text');
|
|
|
|
|
} else if (sortType === 2) {
|
|
|
|
|
options = _.sortBy(options, function(opt) {
|
|
|
|
|
var matches = opt.text.match(/.*?(\d+).*/);
|
|
|
|
|
if (!matches) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return parseInt(matches[1], 10);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (reverseSort) {
|
|
|
|
|
options = options.reverse();
|
2014-09-01 11:13:18 +02:00
|
|
|
}
|
2016-06-26 22:15:57 +09:00
|
|
|
|
|
|
|
|
return options;
|
2014-09-01 11:13:18 +02:00
|
|
|
};
|
|
|
|
|
|
2014-08-27 17:58:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|