constant_variable.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import {Variable, assignModelProperties, variableTypes} from './variable';
  4. import {VariableSrv} from './variable_srv';
  5. export class ConstantVariable implements Variable {
  6. query: string;
  7. options: any[];
  8. current: any;
  9. defaults = {
  10. type: 'constant',
  11. name: '',
  12. hide: 2,
  13. label: '',
  14. query: '',
  15. current: {},
  16. options: [],
  17. };
  18. /** @ngInject **/
  19. constructor(private model, private 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) {
  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) {
  35. return false;
  36. }
  37. setValueFromUrl(urlValue) {
  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. };