adapter.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { cloneDeep } from 'lodash';
  2. import { dispatch } from '../../../store/store';
  3. import { VariableAdapter } from '../adapters';
  4. import { ALL_VARIABLE_TEXT } from '../constants';
  5. import { optionPickerFactory } from '../pickers';
  6. import { setOptionAsCurrent, setOptionFromUrl } from '../state/actions';
  7. import { DataSourceVariableModel } from '../types';
  8. import { containsVariable, isAllVariable, toKeyedVariableIdentifier } from '../utils';
  9. import { DataSourceVariableEditor } from './DataSourceVariableEditor';
  10. import { updateDataSourceVariableOptions } from './actions';
  11. import { dataSourceVariableReducer, initialDataSourceVariableModelState } from './reducer';
  12. export const createDataSourceVariableAdapter = (): VariableAdapter<DataSourceVariableModel> => {
  13. return {
  14. id: 'datasource',
  15. description: 'Enabled you to dynamically switch the data source for multiple panels.',
  16. name: 'Data source',
  17. initialState: initialDataSourceVariableModelState,
  18. reducer: dataSourceVariableReducer,
  19. picker: optionPickerFactory<DataSourceVariableModel>(),
  20. editor: DataSourceVariableEditor,
  21. dependsOn: (variable, variableToTest) => {
  22. if (variable.regex) {
  23. return containsVariable(variable.regex, variableToTest.name);
  24. }
  25. return false;
  26. },
  27. setValue: async (variable, option, emitChanges = false) => {
  28. await dispatch(setOptionAsCurrent(toKeyedVariableIdentifier(variable), option, emitChanges));
  29. },
  30. setValueFromUrl: async (variable, urlValue) => {
  31. await dispatch(setOptionFromUrl(toKeyedVariableIdentifier(variable), urlValue));
  32. },
  33. updateOptions: async (variable) => {
  34. await dispatch(updateDataSourceVariableOptions(toKeyedVariableIdentifier(variable)));
  35. },
  36. getSaveModel: (variable) => {
  37. const { index, id, state, global, rootStateKey, ...rest } = cloneDeep(variable);
  38. return { ...rest, options: [] };
  39. },
  40. getValueForUrl: (variable) => {
  41. if (isAllVariable(variable)) {
  42. return ALL_VARIABLE_TEXT;
  43. }
  44. return variable.current.value;
  45. },
  46. };
  47. };