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',
23 onSort: this.onChange.bind(this),
29 this.elem.addEventListener('click', event => {
30 const sortItem = event.target.closest('.scroll-box-item');
32 event.preventDefault();
33 this.sortItemClick(sortItem);
37 this.bookSearchInput.addEventListener('input', event => {
38 this.filterBooksByName(this.bookSearchInput.value);
43 * @param {String} filterVal
45 filterBooksByName(filterVal) {
47 // Set height on first search, if not already set, to prevent the distraction
48 // of the list height jumping around
49 if (!this.allBookList.style.height) {
50 this.allBookList.style.height = this.allBookList.getBoundingClientRect().height + 'px';
53 const books = this.allBookList.children;
54 const lowerFilter = filterVal.trim().toLowerCase();
56 for (const bookEl of books) {
57 const show = !filterVal || bookEl.textContent.toLowerCase().includes(lowerFilter);
58 bookEl.style.display = show ? null : 'none';
63 * Called when a sort item is clicked.
64 * @param {Element} sortItem
66 sortItemClick(sortItem) {
67 const lists = this.elem.querySelectorAll('.scroll-box');
68 const newList = Array.from(lists).filter(list => sortItem.parentElement !== list);
69 if (newList.length > 0) {
70 newList[0].appendChild(sortItem);
76 const shelfBookElems = Array.from(this.shelfBookList.querySelectorAll('[data-id]'));
77 this.input.value = shelfBookElems.map(elem => elem.getAttribute('data-id')).join(',');
82 export default ShelfSort;