custom_variable.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable} from './variable';
  5. import {VariableSrv, variableConstructorMap} from './variable_srv';
  6. export class CustomVariable implements Variable {
  7. query: string;
  8. options: any;
  9. includeAll: boolean;
  10. /** @ngInject */
  11. constructor(private model, private timeSrv, private templateSrv, private variableSrv) {
  12. _.extend(this, model);
  13. }
  14. setValue(option) {
  15. this.variableSrv.setOptionAsCurrent(this, option);
  16. }
  17. updateOptions() {
  18. // extract options in comma separated string
  19. this.options = _.map(this.query.split(/[,]+/), function(text) {
  20. return { text: text.trim(), value: text.trim() };
  21. });
  22. if (this.includeAll) {
  23. this.addAllOption();
  24. }
  25. }
  26. addAllOption() {
  27. this.options.unshift({text: 'All', value: "$__all"});
  28. }
  29. dependsOn(variable) {
  30. return false;
  31. }
  32. setValueFromUrl(urlValue) {
  33. return this.variableSrv.setOptionFromUrl(this, urlValue);
  34. }
  35. }
  36. variableConstructorMap['custom'] = CustomVariable;