custom_variable.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import {Variable, assignModelProperties, variableTypes} from './variable';
  4. export class CustomVariable implements Variable {
  5. query: string;
  6. options: any;
  7. includeAll: boolean;
  8. multi: boolean;
  9. current: any;
  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. };
  22. /** @ngInject **/
  23. constructor(private model, private variableSrv) {
  24. assignModelProperties(this, model, this.defaults);
  25. }
  26. setValue(option) {
  27. return this.variableSrv.setOptionAsCurrent(this, option);
  28. }
  29. getSaveModel() {
  30. assignModelProperties(this.model, this, this.defaults);
  31. return this.model;
  32. }
  33. updateOptions() {
  34. // extract options in comma separated string
  35. this.options = _.map(this.query.split(/[,]+/), function(text) {
  36. return { text: text.trim(), value: text.trim() };
  37. });
  38. if (this.includeAll) {
  39. this.addAllOption();
  40. }
  41. return this.variableSrv.validateVariableSelectionState(this);
  42. }
  43. addAllOption() {
  44. this.options.unshift({text: 'All', value: "$__all"});
  45. }
  46. dependsOn(variable) {
  47. return false;
  48. }
  49. setValueFromUrl(urlValue) {
  50. return this.variableSrv.setOptionFromUrl(this, urlValue);
  51. }
  52. getValueForUrl() {
  53. if (this.current.text === 'All') {
  54. return 'All';
  55. }
  56. return this.current.value;
  57. }
  58. }
  59. variableTypes['custom'] = {
  60. name: 'Custom',
  61. ctor: CustomVariable,
  62. description: 'Define variable values manually' ,
  63. supportsMulti: true,
  64. };