|
1 | 1 | import Sortable from 'sortablejs'; |
2 | 2 | import {Component} from './component'; |
3 | | - |
4 | | -/** |
5 | | - * @type {Object<string, function(HTMLElement, HTMLElement, HTMLElement)>} |
6 | | - */ |
7 | | -const itemActions = { |
8 | | - move_up(item) { |
9 | | - const list = item.parentNode; |
10 | | - const index = Array.from(list.children).indexOf(item); |
11 | | - const newIndex = Math.max(index - 1, 0); |
12 | | - list.insertBefore(item, list.children[newIndex] || null); |
13 | | - }, |
14 | | - move_down(item) { |
15 | | - const list = item.parentNode; |
16 | | - const index = Array.from(list.children).indexOf(item); |
17 | | - const newIndex = Math.min(index + 2, list.children.length); |
18 | | - list.insertBefore(item, list.children[newIndex] || null); |
19 | | - }, |
20 | | - remove(item, shelfBooksList, allBooksList) { |
21 | | - allBooksList.appendChild(item); |
22 | | - }, |
23 | | - add(item, shelfBooksList) { |
24 | | - shelfBooksList.appendChild(item); |
25 | | - }, |
26 | | -}; |
| 3 | +import {buildListActions, sortActionClickListener} from '../services/dual-lists.ts'; |
27 | 4 |
|
28 | 5 | export class ShelfSort extends Component { |
29 | 6 |
|
@@ -55,12 +32,9 @@ export class ShelfSort extends Component { |
55 | 32 | } |
56 | 33 |
|
57 | 34 | setupListeners() { |
58 | | - this.elem.addEventListener('click', event => { |
59 | | - const sortItemAction = event.target.closest('.scroll-box-item button[data-action]'); |
60 | | - if (sortItemAction) { |
61 | | - this.sortItemActionClick(sortItemAction); |
62 | | - } |
63 | | - }); |
| 35 | + const listActions = buildListActions(this.allBookList, this.shelfBookList); |
| 36 | + const sortActionListener = sortActionClickListener(listActions, this.onChange.bind(this)); |
| 37 | + this.elem.addEventListener('click', sortActionListener); |
64 | 38 |
|
65 | 39 | this.bookSearchInput.addEventListener('input', () => { |
66 | 40 | this.filterBooksByName(this.bookSearchInput.value); |
@@ -93,20 +67,6 @@ export class ShelfSort extends Component { |
93 | 67 | } |
94 | 68 | } |
95 | 69 |
|
96 | | - /** |
97 | | - * Called when a sort item action button is clicked. |
98 | | - * @param {HTMLElement} sortItemAction |
99 | | - */ |
100 | | - sortItemActionClick(sortItemAction) { |
101 | | - const sortItem = sortItemAction.closest('.scroll-box-item'); |
102 | | - const {action} = sortItemAction.dataset; |
103 | | - |
104 | | - const actionFunction = itemActions[action]; |
105 | | - actionFunction(sortItem, this.shelfBookList, this.allBookList); |
106 | | - |
107 | | - this.onChange(); |
108 | | - } |
109 | | - |
110 | 70 | onChange() { |
111 | 71 | const shelfBookElems = Array.from(this.shelfBookList.querySelectorAll('[data-id]')); |
112 | 72 | this.input.value = shelfBookElems.map(elem => elem.getAttribute('data-id')).join(','); |
|
0 commit comments