2023-04-18 17:20:02 -04:00
|
|
|
import Sortable from 'sortablejs';
|
|
|
|
import {Component} from './component';
|
2018-08-27 09:18:09 -04:00
|
|
|
|
2023-02-17 10:53:24 -05:00
|
|
|
/**
|
|
|
|
* @type {Object<string, function(HTMLElement, HTMLElement, HTMLElement)>}
|
|
|
|
*/
|
|
|
|
const itemActions = {
|
2023-04-19 10:20:04 -04:00
|
|
|
move_up(item) {
|
2023-02-17 10:53:24 -05:00
|
|
|
const list = item.parentNode;
|
|
|
|
const index = Array.from(list.children).indexOf(item);
|
|
|
|
const newIndex = Math.max(index - 1, 0);
|
|
|
|
list.insertBefore(item, list.children[newIndex] || null);
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
move_down(item) {
|
2023-02-17 10:53:24 -05:00
|
|
|
const list = item.parentNode;
|
|
|
|
const index = Array.from(list.children).indexOf(item);
|
|
|
|
const newIndex = Math.min(index + 2, list.children.length);
|
|
|
|
list.insertBefore(item, list.children[newIndex] || null);
|
|
|
|
},
|
|
|
|
remove(item, shelfBooksList, allBooksList) {
|
|
|
|
allBooksList.appendChild(item);
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
add(item, shelfBooksList) {
|
2023-02-17 10:53:24 -05:00
|
|
|
shelfBooksList.appendChild(item);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
export class ShelfSort extends Component {
|
2018-08-27 09:18:09 -04:00
|
|
|
|
2022-07-24 07:23:25 -04: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;
|
2023-02-17 10:53:24 -05:00
|
|
|
this.sortButtonContainer = this.$refs.sortButtonContainer;
|
|
|
|
|
|
|
|
this.lastSort = null;
|
2019-06-06 06:49:51 -04:00
|
|
|
|
|
|
|
this.initSortable();
|
2018-09-16 11:59:01 -04:00
|
|
|
this.setupListeners();
|
2018-08-27 09:18:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
initSortable() {
|
2019-06-06 06:49:51 -04:00
|
|
|
const scrollBoxes = this.elem.querySelectorAll('.scroll-box');
|
2022-11-15 11:04:46 -05:00
|
|
|
for (const scrollBox of scrollBoxes) {
|
2019-06-06 06:49:51 -04:00
|
|
|
new Sortable(scrollBox, {
|
|
|
|
group: 'shelf-books',
|
|
|
|
ghostClass: 'primary-background-light',
|
2022-09-28 15:48:29 -04:00
|
|
|
handle: '.handle',
|
2019-06-06 06:49:51 -04:00
|
|
|
animation: 150,
|
|
|
|
onSort: this.onChange.bind(this),
|
|
|
|
});
|
|
|
|
}
|
2018-08-27 09:18:09 -04:00
|
|
|
}
|
|
|
|
|
2018-09-16 11:59:01 -04:00
|
|
|
setupListeners() {
|
|
|
|
this.elem.addEventListener('click', event => {
|
2023-02-17 10:53:24 -05:00
|
|
|
const sortItemAction = event.target.closest('.scroll-box-item button[data-action]');
|
|
|
|
if (sortItemAction) {
|
|
|
|
this.sortItemActionClick(sortItemAction);
|
2018-09-16 11:59:01 -04:00
|
|
|
}
|
|
|
|
});
|
2022-07-24 07:23:25 -04:00
|
|
|
|
2023-04-19 10:20:04 -04:00
|
|
|
this.bookSearchInput.addEventListener('input', () => {
|
2022-07-24 07:23:25 -04:00
|
|
|
this.filterBooksByName(this.bookSearchInput.value);
|
|
|
|
});
|
2023-02-17 10:53:24 -05:00
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
this.sortButtonContainer.addEventListener('click', event => {
|
2023-02-17 10:53:24 -05:00
|
|
|
const button = event.target.closest('button[data-sort]');
|
|
|
|
if (button) {
|
|
|
|
this.sortShelfBooks(button.dataset.sort);
|
|
|
|
}
|
|
|
|
});
|
2022-07-24 07:23:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2023-04-18 17:20:02 -04:00
|
|
|
this.allBookList.style.height = `${this.allBookList.getBoundingClientRect().height}px`;
|
2022-07-24 07:23:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-17 10:53:24 -05:00
|
|
|
* Called when a sort item action button is clicked.
|
|
|
|
* @param {HTMLElement} sortItemAction
|
2018-09-16 11:59:01 -04:00
|
|
|
*/
|
2023-02-17 10:53:24 -05:00
|
|
|
sortItemActionClick(sortItemAction) {
|
|
|
|
const sortItem = sortItemAction.closest('.scroll-box-item');
|
2023-04-18 17:20:02 -04:00
|
|
|
const {action} = sortItemAction.dataset;
|
2023-02-17 10:53:24 -05:00
|
|
|
|
|
|
|
const actionFunction = itemActions[action];
|
|
|
|
actionFunction(sortItem, this.shelfBookList, this.allBookList);
|
|
|
|
|
2018-09-16 11:59:01 -04:00
|
|
|
this.onChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange() {
|
2022-07-24 07:23:25 -04:00
|
|
|
const shelfBookElems = Array.from(this.shelfBookList.querySelectorAll('[data-id]'));
|
2019-06-06 06:49:51 -04:00
|
|
|
this.input.value = shelfBookElems.map(elem => elem.getAttribute('data-id')).join(',');
|
2018-08-27 09:18:09 -04:00
|
|
|
}
|
|
|
|
|
2023-02-17 10:53:24 -05:00
|
|
|
sortShelfBooks(sortProperty) {
|
|
|
|
const books = Array.from(this.shelfBookList.children);
|
|
|
|
const reverse = sortProperty === this.lastSort;
|
|
|
|
|
|
|
|
books.sort((bookA, bookB) => {
|
|
|
|
const aProp = bookA.dataset[sortProperty].toLowerCase();
|
|
|
|
const bProp = bookB.dataset[sortProperty].toLowerCase();
|
|
|
|
|
|
|
|
if (reverse) {
|
2023-04-19 10:20:04 -04:00
|
|
|
return bProp.localeCompare(aProp);
|
2023-02-17 10:53:24 -05:00
|
|
|
}
|
|
|
|
|
2023-04-19 10:20:04 -04:00
|
|
|
return aProp.localeCompare(bProp);
|
2023-02-17 10:53:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const book of books) {
|
|
|
|
this.shelfBookList.append(book);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastSort = (this.lastSort === sortProperty) ? null : sortProperty;
|
|
|
|
this.onChange();
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|