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