2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
2019-10-31 04:59:30 -05:00
|
|
|
import {
|
|
|
|
assignModelProperties,
|
|
|
|
CustomVariableModel,
|
|
|
|
VariableActions,
|
|
|
|
VariableHide,
|
|
|
|
VariableOption,
|
|
|
|
VariableType,
|
|
|
|
variableTypes,
|
|
|
|
} from './variable';
|
2019-07-16 04:35:42 -05:00
|
|
|
import { VariableSrv } from './variable_srv';
|
2016-09-16 09:50:30 -05:00
|
|
|
|
2019-10-31 04:59:30 -05:00
|
|
|
export class CustomVariable implements CustomVariableModel, VariableActions {
|
|
|
|
type: VariableType;
|
|
|
|
name: string;
|
|
|
|
label: string;
|
|
|
|
hide: VariableHide;
|
|
|
|
skipUrlSync: boolean;
|
2016-09-16 09:50:30 -05:00
|
|
|
query: string;
|
2019-10-31 04:59:30 -05:00
|
|
|
options: VariableOption[];
|
2016-09-16 09:50:30 -05:00
|
|
|
includeAll: boolean;
|
2016-09-19 10:03:29 -05:00
|
|
|
multi: boolean;
|
2019-10-31 04:59:30 -05:00
|
|
|
current: VariableOption;
|
|
|
|
allValue: string;
|
2016-09-19 10:03:29 -05:00
|
|
|
|
2019-10-31 04:59:30 -05:00
|
|
|
defaults: CustomVariableModel = {
|
2017-12-20 05:33:33 -06:00
|
|
|
type: 'custom',
|
|
|
|
name: '',
|
|
|
|
label: '',
|
2019-10-31 04:59:30 -05:00
|
|
|
hide: VariableHide.dontHide,
|
|
|
|
skipUrlSync: false,
|
2017-12-20 05:33:33 -06:00
|
|
|
query: '',
|
2019-10-31 04:59:30 -05:00
|
|
|
options: [],
|
2016-09-19 10:03:29 -05:00
|
|
|
includeAll: false,
|
|
|
|
multi: false,
|
2019-10-31 04:59:30 -05:00
|
|
|
current: {} as VariableOption,
|
2017-12-20 05:33:33 -06:00
|
|
|
allValue: null,
|
2016-09-19 10:03:29 -05:00
|
|
|
};
|
2016-09-16 09:50:30 -05:00
|
|
|
|
2018-08-31 09:40:43 -05:00
|
|
|
/** @ngInject */
|
2019-07-16 04:35:42 -05:00
|
|
|
constructor(private model: any, private variableSrv: VariableSrv) {
|
2016-09-19 10:03:29 -05:00
|
|
|
assignModelProperties(this, model, this.defaults);
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValue(option: any) {
|
2016-09-19 11:06:36 -05:00
|
|
|
return this.variableSrv.setOptionAsCurrent(this, option);
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
2016-11-17 04:28:33 -06:00
|
|
|
getSaveModel() {
|
2016-09-19 10:03:29 -05:00
|
|
|
assignModelProperties(this.model, this, this.defaults);
|
|
|
|
return this.model;
|
|
|
|
}
|
|
|
|
|
2016-09-16 09:50:30 -05:00
|
|
|
updateOptions() {
|
2018-12-08 15:53:24 -06:00
|
|
|
// extract options in comma separated string (use backslash to escape wanted commas)
|
2018-12-08 17:23:08 -06:00
|
|
|
this.options = _.map(this.query.match(/(?:\\,|[^,])+/g), text => {
|
2019-03-13 10:12:19 -05:00
|
|
|
text = text.replace(/\\,/g, ',');
|
2019-10-31 04:59:30 -05:00
|
|
|
return { text: text.trim(), value: text.trim(), selected: false };
|
2016-09-16 09:50:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.includeAll) {
|
|
|
|
this.addAllOption();
|
|
|
|
}
|
2016-09-19 10:03:29 -05:00
|
|
|
|
2016-09-19 11:06:36 -05:00
|
|
|
return this.variableSrv.validateVariableSelectionState(this);
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
addAllOption() {
|
2019-10-31 04:59:30 -05:00
|
|
|
this.options.unshift({ text: 'All', value: '$__all', selected: false });
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
dependsOn(variable: any) {
|
2016-09-16 09:50:30 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-17 04:28:45 -05:00
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValueFromUrl(urlValue: string[]) {
|
2016-09-17 04:28:45 -05:00
|
|
|
return this.variableSrv.setOptionFromUrl(this, urlValue);
|
|
|
|
}
|
2016-09-20 04:57:43 -05:00
|
|
|
|
|
|
|
getValueForUrl() {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (this.current.text === 'All') {
|
|
|
|
return 'All';
|
2016-09-20 04:57:43 -05:00
|
|
|
}
|
|
|
|
return this.current.value;
|
|
|
|
}
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
variableTypes['custom'] = {
|
|
|
|
name: 'Custom',
|
2016-09-19 11:32:09 -05:00
|
|
|
ctor: CustomVariable,
|
2017-12-20 05:33:33 -06:00
|
|
|
description: 'Define variable values manually',
|
|
|
|
supportsMulti: true,
|
2016-09-19 11:32:09 -05:00
|
|
|
};
|