| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import _ from 'lodash';
- import { Variable, assignModelProperties, variableTypes } from './variable';
- import { VariableSrv } from './variable_srv';
- export class CustomVariable implements Variable {
- query: string;
- options: any;
- includeAll: boolean;
- multi: boolean;
- current: any;
- skipUrlSync: boolean;
- defaults: any = {
- type: 'custom',
- name: '',
- label: '',
- hide: 0,
- options: [],
- current: {},
- query: '',
- includeAll: false,
- multi: false,
- allValue: null,
- skipUrlSync: false,
- };
- /** @ngInject */
- constructor(private model: any, private variableSrv: VariableSrv) {
- assignModelProperties(this, model, this.defaults);
- }
- setValue(option: any) {
- return this.variableSrv.setOptionAsCurrent(this, option);
- }
- getSaveModel() {
- assignModelProperties(this.model, this, this.defaults);
- return this.model;
- }
- updateOptions() {
- // extract options in comma separated string (use backslash to escape wanted commas)
- this.options = _.map(this.query.match(/(?:\\,|[^,])+/g), text => {
- text = text.replace(/\\,/g, ',');
- return { text: text.trim(), value: text.trim() };
- });
- if (this.includeAll) {
- this.addAllOption();
- }
- return this.variableSrv.validateVariableSelectionState(this);
- }
- addAllOption() {
- this.options.unshift({ text: 'All', value: '$__all' });
- }
- dependsOn(variable: any) {
- return false;
- }
- setValueFromUrl(urlValue: string[]) {
- return this.variableSrv.setOptionFromUrl(this, urlValue);
- }
- getValueForUrl() {
- if (this.current.text === 'All') {
- return 'All';
- }
- return this.current.value;
- }
- }
- variableTypes['custom'] = {
- name: 'Custom',
- ctor: CustomVariable,
- description: 'Define variable values manually',
- supportsMulti: true,
- };
|