1 import Sortable from "sortablejs";
7 this.input = document.getElementById('books-input');
8 this.shelfBooksList = elem.querySelector('[shelf-sort-assigned-books]');
11 this.setupListeners();
15 const scrollBoxes = this.elem.querySelectorAll('.scroll-box');
16 for (let scrollBox of scrollBoxes) {
17 new Sortable(scrollBox, {
19 ghostClass: 'primary-background-light',
21 onSort: this.onChange.bind(this),
27 this.elem.addEventListener('click', event => {
28 const sortItem = event.target.closest('.scroll-box-item:not(.instruction)');
30 event.preventDefault();
31 this.sortItemClick(sortItem);
37 * Called when a sort item is clicked.
38 * @param {Element} sortItem
40 sortItemClick(sortItem) {
41 const lists = this.elem.querySelectorAll('.scroll-box');
42 const newList = Array.from(lists).filter(list => sortItem.parentElement !== list);
43 if (newList.length > 0) {
44 newList[0].appendChild(sortItem);
50 const shelfBookElems = Array.from(this.shelfBooksList.querySelectorAll('[data-id]'));
51 this.input.value = shelfBookElems.map(elem => elem.getAttribute('data-id')).join(',');
56 export default ShelfSort;