custom_variable.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. supportsMulti = true;
  23. /** @ngInject */
  24. constructor(private model, private timeSrv, private templateSrv, private variableSrv) {
  25. assignModelProperties(this, model, this.defaults);
  26. }
  27. setValue(option) {
  28. return this.variableSrv.setOptionAsCurrent(this, option);
  29. }
  30. getModel() {
  31. assignModelProperties(this.model, this, this.defaults);
  32. return this.model;
  33. }
  34. updateOptions() {
  35. // extract options in comma separated string
  36. this.options = _.map(this.query.split(/[,]+/), function(text) {
  37. return { text: text.trim(), value: text.trim() };
  38. });
  39. if (this.includeAll) {
  40. this.addAllOption();
  41. }
  42. return this.variableSrv.validateVariableSelectionState(this);
  43. }
  44. addAllOption() {
  45. this.options.unshift({text: 'All', value: "$__all"});
  46. }
  47. dependsOn(variable) {
  48. return false;
  49. }
  50. setValueFromUrl(urlValue) {
  51. return this.variableSrv.setOptionFromUrl(this, urlValue);
  52. }
  53. }
  54. variableConstructorMap['custom'] = CustomVariable;