custom_variable.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 (use backslash to escape wanted commas)
  36. this.options = _.map(this.query.match(/(?:\\,|[^,])+/g), text => {
  37. text = text.replace('\\,', ',');
  38. return { text: text.trim(), value: text.trim() };
  39. });
  40. if (this.includeAll) {
  41. this.addAllOption();
  42. }
  43. return this.variableSrv.validateVariableSelectionState(this);
  44. }
  45. addAllOption() {
  46. this.options.unshift({ text: 'All', value: '$__all' });
  47. }
  48. dependsOn(variable) {
  49. return false;
  50. }
  51. setValueFromUrl(urlValue) {
  52. return this.variableSrv.setOptionFromUrl(this, urlValue);
  53. }
  54. getValueForUrl() {
  55. if (this.current.text === 'All') {
  56. return 'All';
  57. }
  58. return this.current.value;
  59. }
  60. }
  61. variableTypes['custom'] = {
  62. name: 'Custom',
  63. ctor: CustomVariable,
  64. description: 'Define variable values manually',
  65. supportsMulti: true,
  66. };