constant_variable.ts 1.2 KB

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