constant_variable.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Variable, assignModelProperties, variableTypes } from './variable';
  2. import { VariableSrv } from './all';
  3. export class ConstantVariable implements Variable {
  4. query: string;
  5. options: any[];
  6. current: any;
  7. skipUrlSync: boolean;
  8. defaults: any = {
  9. type: 'constant',
  10. name: '',
  11. hide: 2,
  12. label: '',
  13. query: '',
  14. current: {},
  15. options: [],
  16. skipUrlSync: false,
  17. };
  18. /** @ngInject */
  19. constructor(private model: any, private variableSrv: VariableSrv) {
  20. assignModelProperties(this, model, this.defaults);
  21. }
  22. getSaveModel() {
  23. assignModelProperties(this.model, this, this.defaults);
  24. return this.model;
  25. }
  26. setValue(option: any) {
  27. this.variableSrv.setOptionAsCurrent(this, option);
  28. }
  29. updateOptions() {
  30. this.options = [{ text: this.query.trim(), value: this.query.trim() }];
  31. this.setValue(this.options[0]);
  32. return Promise.resolve();
  33. }
  34. dependsOn(variable: any) {
  35. return false;
  36. }
  37. setValueFromUrl(urlValue: string) {
  38. return this.variableSrv.setOptionFromUrl(this, urlValue);
  39. }
  40. getValueForUrl() {
  41. return this.current.value;
  42. }
  43. }
  44. variableTypes['constant'] = {
  45. name: 'Constant',
  46. ctor: ConstantVariable,
  47. description: 'Define a hidden constant variable, useful for metric prefixes in dashboards you want to share',
  48. };