1 import Sortable from "sortablejs";
7 this.input = this.$refs.input;
8 this.shelfBookList = this.$refs.shelfBookList;
9 this.allBookList = this.$refs.allBookList;
10 this.bookSearchInput = this.$refs.bookSearch;
13 this.setupListeners();
17 const scrollBoxes = this.elem.querySelectorAll('.scroll-box');
18 for (let scrollBox of scrollBoxes) {
19 new Sortable(scrollBox, {
21 ghostClass: 'primary-background-light',
24 onSort: this.onChange.bind(this),
30 this.elem.addEventListener('click', event => {
31 const sortItem = event.target.closest('.scroll-box-item');
33 event.preventDefault();
34 this.sortItemClick(sortItem);
38 this.bookSearchInput.addEventListener('input', event => {
39 this.filterBooksByName(this.bookSearchInput.value);
44 * @param {String} filterVal
46 filterBooksByName(filterVal) {
48 // Set height on first search, if not already set, to prevent the distraction
49 // of the list height jumping around
50 if (!this.allBookList.style.height) {
51 this.allBookList.style.height = this.allBookList.getBoundingClientRect().height + 'px';
54 const books = this.allBookList.children;
55 const lowerFilter = filterVal.trim().toLowerCase();
57 for (const bookEl of books) {
58 const show = !filterVal || bookEl.textContent.toLowerCase().includes(lowerFilter);
59 bookEl.style.display = show ? null : 'none';
64 * Called when a sort item is clicked.
65 * @param {Element} sortItem
67 sortItemClick(sortItem) {
68 const lists = this.elem.querySelectorAll('.scroll-box');
69 const newList = Array.from(lists).filter(list => sortItem.parentElement !== list);
70 if (newList.length > 0) {
71 newList[0].appendChild(sortItem);
77 const shelfBookElems = Array.from(this.shelfBookList.querySelectorAll('[data-id]'));
78 this.input.value = shelfBookElems.map(elem => elem.getAttribute('data-id')).join(',');
83 export default ShelfSort;