X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/b89411c108f08248215421a34bbf66bdf072d049..4c574c22a80a6838f0cf0b37b9fab1242e4fdf8c:/resources/assets/js/components/shelf-sort.js diff --git a/resources/assets/js/components/shelf-sort.js b/resources/assets/js/components/shelf-sort.js index 91713ab41..86fb5e143 100644 --- a/resources/assets/js/components/shelf-sort.js +++ b/resources/assets/js/components/shelf-sort.js @@ -1,3 +1,4 @@ +import "jquery-sortable"; class ShelfSort { @@ -5,12 +6,12 @@ class ShelfSort { this.elem = elem; this.sortGroup = this.initSortable(); this.input = document.getElementById('books-input'); + this.setupListeners(); } initSortable() { - const sortable = require('jquery-sortable'); const placeHolderContent = this.getPlaceholderHTML(); - + // TODO - Load sortable at this point return $('.scroll-box').sortable({ group: 'shelf-books', exclude: '.instruction,.scroll-box-placeholder', @@ -21,10 +22,39 @@ class ShelfSort { }); } + setupListeners() { + this.elem.addEventListener('click', event => { + const sortItem = event.target.closest('.scroll-box-item:not(.instruction)'); + if (sortItem) { + event.preventDefault(); + this.sortItemClick(sortItem); + } + }); + } + + /** + * 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(); + } + onDrop($item, container, _super) { + this.onChange(); + _super($item, container); + } + + onChange() { const data = this.sortGroup.sortable('serialize').get(); this.input.value = data[0].map(item => item.id).join(','); - _super($item, container); + const instruction = this.elem.querySelector('.scroll-box-item.instruction'); + instruction.parentNode.insertBefore(instruction, instruction.parentNode.children[0]); } getPlaceholderHTML() { @@ -38,4 +68,4 @@ class ShelfSort { } -module.exports = ShelfSort; \ No newline at end of file +export default ShelfSort; \ No newline at end of file