custom_variable.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import _ from 'lodash';
  2. import { Variable, assignModelProperties, variableTypes } from './variable';
  3. export class CustomVariable implements Variable {
  4. query: string;
  5. options: any;
  6. includeAll: boolean;
  7. multi: boolean;
  8. current: any;
  9. skipUrlSync: boolean;
  10. defaults = {
  11. type: 'custom',
  12. name: '',
  13. label: '',
  14. hide: 0,
  15. options: [],
  16. current: {},
  17. query: '',
  18. includeAll: false,
  19. multi: false,
  20. allValue: null,
  21. skipUrlSync: false,
  22. };
  23. /** @ngInject */
  24. constructor(private model, private variableSrv) {
  25. assignModelProperties(this, model, this.defaults);
  26. }
  27. setValue(option) {
  28. return this.variableSrv.setOptionAsCurrent(this, option);
  29. }
  30. getSaveModel() {
  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(/[,]+/), 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. getValueForUrl() {
  54. if (this.current.text === 'All') {
  55. return 'All';
  56. }
  57. return this.current.value;
  58. }
  59. }
  60. variableTypes['custom'] = {
  61. name: 'Custom',
  62. ctor: CustomVariable,
  63. description: 'Define variable values manually',
  64. supportsMulti: true,
  65. };