2019-06-06 18:49:51 +08:00
|
|
|
import Sortable from "sortablejs";
|
2018-08-27 21:18:09 +08:00
|
|
|
|
|
|
|
class ShelfSort {
|
|
|
|
|
2022-07-24 19:23:25 +08:00
|
|
|
setup() {
|
|
|
|
this.elem = this.$el;
|
|
|
|
this.input = this.$refs.input;
|
|
|
|
this.shelfBookList = this.$refs.shelfBookList;
|
|
|
|
this.allBookList = this.$refs.allBookList;
|
|
|
|
this.bookSearchInput = this.$refs.bookSearch;
|
2019-06-06 18:49:51 +08:00
|
|
|
|
|
|
|
this.initSortable();
|
2018-09-16 23:59:01 +08:00
|
|
|
this.setupListeners();
|
2018-08-27 21:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
initSortable() {
|
2019-06-06 18:49:51 +08:00
|
|
|
const scrollBoxes = this.elem.querySelectorAll('.scroll-box');
|
|
|
|
for (let scrollBox of scrollBoxes) {
|
|
|
|
new Sortable(scrollBox, {
|
|
|
|
group: 'shelf-books',
|
|
|
|
ghostClass: 'primary-background-light',
|
2022-09-29 03:48:29 +08:00
|
|
|
handle: '.handle',
|
2019-06-06 18:49:51 +08:00
|
|
|
animation: 150,
|
|
|
|
onSort: this.onChange.bind(this),
|
|
|
|
});
|
|
|
|
}
|
2018-08-27 21:18:09 +08:00
|
|
|
}
|
|
|
|
|
2018-09-16 23:59:01 +08:00
|
|
|
setupListeners() {
|
|
|
|
this.elem.addEventListener('click', event => {
|
2022-07-24 19:23:25 +08:00
|
|
|
const sortItem = event.target.closest('.scroll-box-item');
|
2018-09-16 23:59:01 +08:00
|
|
|
if (sortItem) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.sortItemClick(sortItem);
|
|
|
|
}
|
|
|
|
});
|
2022-07-24 19:23:25 +08:00
|
|
|
|
|
|
|
this.bookSearchInput.addEventListener('input', event => {
|
|
|
|
this.filterBooksByName(this.bookSearchInput.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} filterVal
|
|
|
|
*/
|
|
|
|
filterBooksByName(filterVal) {
|
|
|
|
|
|
|
|
// Set height on first search, if not already set, to prevent the distraction
|
|
|
|
// of the list height jumping around
|
|
|
|
if (!this.allBookList.style.height) {
|
|
|
|
this.allBookList.style.height = this.allBookList.getBoundingClientRect().height + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
const books = this.allBookList.children;
|
|
|
|
const lowerFilter = filterVal.trim().toLowerCase();
|
|
|
|
|
|
|
|
for (const bookEl of books) {
|
|
|
|
const show = !filterVal || bookEl.textContent.toLowerCase().includes(lowerFilter);
|
|
|
|
bookEl.style.display = show ? null : 'none';
|
|
|
|
}
|
2018-09-16 23:59:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a sort item is clicked.
|
|
|
|
* @param {Element} sortItem
|
|
|
|
*/
|
|
|
|
sortItemClick(sortItem) {
|
|
|
|
const lists = this.elem.querySelectorAll('.scroll-box');
|
|
|
|
const newList = Array.from(lists).filter(list => sortItem.parentElement !== list);
|
|
|
|
if (newList.length > 0) {
|
|
|
|
newList[0].appendChild(sortItem);
|
|
|
|
}
|
|
|
|
this.onChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange() {
|
2022-07-24 19:23:25 +08:00
|
|
|
const shelfBookElems = Array.from(this.shelfBookList.querySelectorAll('[data-id]'));
|
2019-06-06 18:49:51 +08:00
|
|
|
this.input.value = shelfBookElems.map(elem => elem.getAttribute('data-id')).join(',');
|
2018-08-27 21:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default ShelfSort;
|