2022-03-07 13:06:11 +01:00
|
|
|
import { get, writable } from "svelte/store"
|
2022-02-11 12:55:35 +01:00
|
|
|
|
|
|
|
const createRowSelectionStore = () => {
|
2022-02-22 16:18:08 +01:00
|
|
|
const store = writable({})
|
2022-02-11 12:55:35 +01:00
|
|
|
|
2022-03-07 13:06:11 +01:00
|
|
|
function updateSelection(componentId, tableId, selectedRows) {
|
2022-02-11 12:55:35 +01:00
|
|
|
store.update(state => {
|
2022-03-07 13:06:11 +01:00
|
|
|
state[componentId] = { tableId: tableId, selectedRows: selectedRows }
|
2022-02-11 12:55:35 +01:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
2022-02-22 16:18:08 +01:00
|
|
|
|
2022-03-07 13:06:11 +01:00
|
|
|
function getSelection(tableId) {
|
|
|
|
const selection = get(store)
|
|
|
|
const componentId = Object.keys(selection).find(
|
|
|
|
componentId => selection[componentId].tableId === tableId
|
|
|
|
)
|
|
|
|
return componentId ? selection[componentId] : {}
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:55:35 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
2022-02-22 16:18:08 +01:00
|
|
|
set: store.set,
|
2022-02-11 12:55:35 +01:00
|
|
|
actions: {
|
2022-02-22 16:18:08 +01:00
|
|
|
updateSelection,
|
2022-03-07 13:06:11 +01:00
|
|
|
getSelection,
|
2022-02-11 12:55:35 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const rowSelectionStore = createRowSelectionStore()
|