]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/shelf-sort.js
91713ab41f1ec212169b9c179bb628454dba75f6
[bookstack] / resources / assets / js / components / shelf-sort.js
1
2 class ShelfSort {
3
4     constructor(elem) {
5         this.elem = elem;
6         this.sortGroup = this.initSortable();
7         this.input = document.getElementById('books-input');
8     }
9
10     initSortable() {
11         const sortable = require('jquery-sortable');
12         const placeHolderContent = this.getPlaceholderHTML();
13
14         return $('.scroll-box').sortable({
15             group: 'shelf-books',
16             exclude: '.instruction,.scroll-box-placeholder',
17             containerSelector: 'div.scroll-box',
18             itemSelector: '.scroll-box-item',
19             placeholder: placeHolderContent,
20             onDrop: this.onDrop.bind(this)
21         });
22     }
23
24     onDrop($item, container, _super) {
25         const data = this.sortGroup.sortable('serialize').get();
26         this.input.value = data[0].map(item => item.id).join(',');
27         _super($item, container);
28     }
29
30     getPlaceholderHTML() {
31         const placeHolder = document.querySelector('.scroll-box-placeholder');
32         placeHolder.style.display = 'block';
33         const placeHolderContent = placeHolder.outerHTML;
34         placeHolder.style.display = 'none';
35         return placeHolderContent;
36     }
37
38
39 }
40
41 module.exports = ShelfSort;