2 * Service for helping manage common dual-list scenarios.
3 * (Shelf book manager, sort set manager).
6 type ListActionsSet = Record<string, ((item: HTMLElement) => void)>;
8 export function buildListActions(
9 availableList: HTMLElement,
10 configuredList: HTMLElement,
14 const list = item.parentNode as HTMLElement;
15 const index = Array.from(list.children).indexOf(item);
16 const newIndex = Math.max(index - 1, 0);
17 list.insertBefore(item, list.children[newIndex] || null);
20 const list = item.parentNode as HTMLElement;
21 const index = Array.from(list.children).indexOf(item);
22 const newIndex = Math.min(index + 2, list.children.length);
23 list.insertBefore(item, list.children[newIndex] || null);
26 availableList.appendChild(item);
29 configuredList.appendChild(item);
34 export function sortActionClickListener(actions: ListActionsSet, onChange: () => void) {
35 return (event: MouseEvent) => {
36 const sortItemAction = (event.target as Element).closest('.scroll-box-item button[data-action]') as HTMLElement|null;
38 const sortItem = sortItemAction.closest('.scroll-box-item') as HTMLElement;
39 const action = sortItemAction.dataset.action;
41 throw new Error('No action defined for clicked button');
44 const actionFunction = actions[action];
45 actionFunction(sortItem);