2019-10-31 10:59:30 +01:00
|
|
|
import {
|
|
|
|
|
assignModelProperties,
|
|
|
|
|
ConstantVariableModel,
|
|
|
|
|
VariableActions,
|
|
|
|
|
VariableHide,
|
|
|
|
|
VariableOption,
|
|
|
|
|
variableTypes,
|
2020-03-24 05:46:31 +01:00
|
|
|
} from './types';
|
2019-07-16 11:35:42 +02:00
|
|
|
import { VariableSrv } from './all';
|
2020-04-05 21:44:49 -07:00
|
|
|
import { VariableType } from '@grafana/data';
|
2016-09-17 15:27:53 +02:00
|
|
|
|
2019-10-31 10:59:30 +01:00
|
|
|
export class ConstantVariable implements ConstantVariableModel, VariableActions {
|
|
|
|
|
type: VariableType;
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
hide: VariableHide;
|
2018-07-11 19:06:36 +02:00
|
|
|
skipUrlSync: boolean;
|
2019-10-31 10:59:30 +01:00
|
|
|
query: string;
|
|
|
|
|
options: VariableOption[];
|
|
|
|
|
current: VariableOption;
|
2016-09-17 15:27:53 +02:00
|
|
|
|
2019-10-31 10:59:30 +01:00
|
|
|
defaults: ConstantVariableModel = {
|
2017-12-20 12:33:33 +01:00
|
|
|
type: 'constant',
|
|
|
|
|
name: '',
|
2020-03-10 08:53:41 +01:00
|
|
|
hide: VariableHide.hideVariable,
|
2017-12-20 12:33:33 +01:00
|
|
|
label: '',
|
|
|
|
|
query: '',
|
2019-10-31 10:59:30 +01:00
|
|
|
current: {} as VariableOption,
|
2017-12-20 12:33:33 +01:00
|
|
|
options: [],
|
2018-07-11 19:06:36 +02:00
|
|
|
skipUrlSync: false,
|
2016-09-19 15:15:15 +02:00
|
|
|
};
|
|
|
|
|
|
2018-08-31 16:40:43 +02:00
|
|
|
/** @ngInject */
|
2019-07-16 11:35:42 +02:00
|
|
|
constructor(private model: any, private variableSrv: VariableSrv) {
|
2016-09-19 15:15:15 +02:00
|
|
|
assignModelProperties(this, model, this.defaults);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 11:28:33 +01:00
|
|
|
getSaveModel() {
|
2016-09-19 15:15:15 +02:00
|
|
|
assignModelProperties(this.model, this, this.defaults);
|
|
|
|
|
return this.model;
|
2016-09-17 15:27:53 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:35:42 +02:00
|
|
|
setValue(option: any) {
|
2016-09-17 15:27:53 +02:00
|
|
|
this.variableSrv.setOptionAsCurrent(this, option);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateOptions() {
|
2019-10-31 10:59:30 +01:00
|
|
|
this.options = [{ text: this.query.trim(), value: this.query.trim(), selected: false }];
|
2016-09-17 15:27:53 +02:00
|
|
|
this.setValue(this.options[0]);
|
2016-09-19 17:03:29 +02:00
|
|
|
return Promise.resolve();
|
2016-09-17 15:27:53 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:35:42 +02:00
|
|
|
dependsOn(variable: any) {
|
2016-09-17 15:27:53 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:35:42 +02:00
|
|
|
setValueFromUrl(urlValue: string) {
|
2016-09-17 15:27:53 +02:00
|
|
|
return this.variableSrv.setOptionFromUrl(this, urlValue);
|
|
|
|
|
}
|
2016-09-20 11:57:43 +02:00
|
|
|
|
|
|
|
|
getValueForUrl() {
|
|
|
|
|
return this.current.value;
|
|
|
|
|
}
|
2016-09-17 15:27:53 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
variableTypes['constant'] = {
|
|
|
|
|
name: 'Constant',
|
2016-09-19 18:32:09 +02:00
|
|
|
ctor: ConstantVariable,
|
2017-12-21 08:39:31 +01:00
|
|
|
description: 'Define a hidden constant variable, useful for metric prefixes in dashboards you want to share',
|
2016-09-19 18:32:09 +02:00
|
|
|
};
|