custom_variable.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, assignModelProperties} from './variable';
  5. import {VariableSrv, variableConstructorMap} from './variable_srv';
  6. export class CustomVariable implements Variable {
  7. query: string;
  8. options: any;
  9. includeAll: boolean;
  10. multi: boolean;
  11. defaults = {
  12. type: 'custom',
  13. name: '',
  14. label: '',
  15. hide: 0,
  16. options: [],
  17. current: {text: '', value: ''},
  18. query: '',
  19. includeAll: false,
  20. multi: false,
  21. };
  22. /** @ngInject */
  23. constructor(private model, private timeSrv, private templateSrv, private variableSrv) {
  24. assignModelProperties(this, model, this.defaults);
  25. }
  26. setValue(option) {
  27. this.variableSrv.setOptionAsCurrent(this, option);
  28. }
  29. getModel() {
  30. assignModelProperties(this.model, this, this.defaults);
  31. return this.model;
  32. }
  33. updateOptions() {
  34. // extract options in comma separated string
  35. this.options = _.map(this.query.split(/[,]+/), function(text) {
  36. return { text: text.trim(), value: text.trim() };
  37. });
  38. if (this.includeAll) {
  39. this.addAllOption();
  40. }
  41. return Promise.resolve();
  42. }
  43. addAllOption() {
  44. this.options.unshift({text: 'All', value: "$__all"});
  45. }
  46. dependsOn(variable) {
  47. return false;
  48. }
  49. setValueFromUrl(urlValue) {
  50. return this.variableSrv.setOptionFromUrl(this, urlValue);
  51. }
  52. }
  53. variableConstructorMap['custom'] = CustomVariable;