custom_variable.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import _ from 'lodash';
  2. import { Variable, assignModelProperties, variableTypes } from './variable';
  3. import { VariableSrv } from './variable_srv';
  4. export class CustomVariable implements Variable {
  5. query: string;
  6. options: any;
  7. includeAll: boolean;
  8. multi: boolean;
  9. current: any;
  10. skipUrlSync: boolean;
  11. defaults: any = {
  12. type: 'custom',
  13. name: '',
  14. label: '',
  15. hide: 0,
  16. options: [],
  17. current: {},
  18. query: '',
  19. includeAll: false,
  20. multi: false,
  21. allValue: null,
  22. skipUrlSync: false,
  23. };
  24. /** @ngInject */
  25. constructor(private model: any, private variableSrv: VariableSrv) {
  26. assignModelProperties(this, model, this.defaults);
  27. }
  28. setValue(option: any) {
  29. return this.variableSrv.setOptionAsCurrent(this, option);
  30. }
  31. getSaveModel() {
  32. assignModelProperties(this.model, this, this.defaults);
  33. return this.model;
  34. }
  35. updateOptions() {
  36. // extract options in comma separated string (use backslash to escape wanted commas)
  37. this.options = _.map(this.query.match(/(?:\\,|[^,])+/g), text => {
  38. text = text.replace(/\\,/g, ',');
  39. return { text: text.trim(), value: text.trim() };
  40. });
  41. if (this.includeAll) {
  42. this.addAllOption();
  43. }
  44. return this.variableSrv.validateVariableSelectionState(this);
  45. }
  46. addAllOption() {
  47. this.options.unshift({ text: 'All', value: '$__all' });
  48. }
  49. dependsOn(variable: any) {
  50. return false;
  51. }
  52. setValueFromUrl(urlValue: string[]) {
  53. return this.variableSrv.setOptionFromUrl(this, urlValue);
  54. }
  55. getValueForUrl() {
  56. if (this.current.text === 'All') {
  57. return 'All';
  58. }
  59. return this.current.value;
  60. }
  61. }
  62. variableTypes['custom'] = {
  63. name: 'Custom',
  64. ctor: CustomVariable,
  65. description: 'Define variable values manually',
  66. supportsMulti: true,
  67. };