custom_variable.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, assignModelProperties, variableTypes} from './variable';
  5. import {VariableSrv} from './variable_srv';
  6. export class CustomVariable implements Variable {
  7. query: string;
  8. options: any;
  9. includeAll: boolean;
  10. multi: boolean;
  11. current: any;
  12. defaults = {
  13. type: 'custom',
  14. name: '',
  15. label: '',
  16. hide: 0,
  17. options: [],
  18. current: {},
  19. query: '',
  20. includeAll: false,
  21. multi: false,
  22. allValue: null,
  23. };
  24. /** @ngInject **/
  25. constructor(private model, private timeSrv, private templateSrv, private variableSrv) {
  26. assignModelProperties(this, model, this.defaults);
  27. }
  28. setValue(option) {
  29. return this.variableSrv.setOptionAsCurrent(this, option);
  30. }
  31. getModel() {
  32. assignModelProperties(this.model, this, this.defaults);
  33. return this.model;
  34. }
  35. updateOptions() {
  36. // extract options in comma separated string
  37. this.options = _.map(this.query.split(/[,]+/), function(text) {
  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. };