custom_variable.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. defaults = {
  10. type: 'custom',
  11. name: '',
  12. label: '',
  13. hide: 0,
  14. options: [],
  15. current: {},
  16. query: '',
  17. includeAll: false,
  18. multi: false,
  19. allValue: null,
  20. };
  21. /** @ngInject **/
  22. constructor(private model, private variableSrv) {
  23. assignModelProperties(this, model, this.defaults);
  24. }
  25. setValue(option) {
  26. return this.variableSrv.setOptionAsCurrent(this, option);
  27. }
  28. getSaveModel() {
  29. assignModelProperties(this.model, this, this.defaults);
  30. return this.model;
  31. }
  32. updateOptions() {
  33. // extract options in comma separated string
  34. this.options = _.map(this.query.split(/[,]+/), function(text) {
  35. return { text: text.trim(), value: text.trim() };
  36. });
  37. if (this.includeAll) {
  38. this.addAllOption();
  39. }
  40. return this.variableSrv.validateVariableSelectionState(this);
  41. }
  42. addAllOption() {
  43. this.options.unshift({ text: 'All', value: '$__all' });
  44. }
  45. dependsOn(variable) {
  46. return false;
  47. }
  48. setValueFromUrl(urlValue) {
  49. return this.variableSrv.setOptionFromUrl(this, urlValue);
  50. }
  51. getValueForUrl() {
  52. if (this.current.text === 'All') {
  53. return 'All';
  54. }
  55. return this.current.value;
  56. }
  57. }
  58. variableTypes['custom'] = {
  59. name: 'Custom',
  60. ctor: CustomVariable,
  61. description: 'Define variable values manually',
  62. supportsMulti: true,
  63. };