adhoc_variable.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 AdhocVariable implements Variable {
  7. filters: any[];
  8. defaults = {
  9. type: 'adhoc',
  10. name: '',
  11. label: '',
  12. hide: 0,
  13. datasource: null,
  14. filters: [],
  15. };
  16. /** @ngInject **/
  17. constructor(private model) {
  18. assignModelProperties(this, model, this.defaults);
  19. }
  20. setValue(option) {
  21. return Promise.resolve();
  22. }
  23. getModel() {
  24. assignModelProperties(this.model, this, this.defaults);
  25. return this.model;
  26. }
  27. updateOptions() {
  28. return Promise.resolve();
  29. }
  30. dependsOn(variable) {
  31. return false;
  32. }
  33. setValueFromUrl(urlValue) {
  34. if (!_.isArray(urlValue)) {
  35. urlValue = [urlValue];
  36. }
  37. this.filters = urlValue.map(item => {
  38. var values = item.split('|');
  39. return {
  40. key: values[0],
  41. operator: values[1],
  42. value: values[2],
  43. };
  44. });
  45. return Promise.resolve();
  46. }
  47. getValueForUrl() {
  48. return this.filters.map(filter => {
  49. return filter.key + '|' + filter.operator + '|' + filter.value;
  50. });
  51. }
  52. setFilters(filters: any[]) {
  53. this.filters = filters;
  54. }
  55. }
  56. variableTypes['adhoc'] = {
  57. name: 'Ad hoc filters',
  58. ctor: AdhocVariable,
  59. description: 'Add key/value filters on the fly',
  60. };