]> BookStack Code Mirror - bookstack/blob - resources/js/components/sortable-list.js
0af0e11c901a5b58d98f4ef7687004c117334e3d
[bookstack] / resources / js / components / sortable-list.js
1 import Sortable from "sortablejs";
2
3 /**
4  * SortableList
5  *
6  * Can have data set on the dragged items by setting a 'data-drag-content' attribute.
7  * This attribute must contain JSON where the keys are content types and the values are
8  * the data to set on the data-transfer.
9  *
10  * @extends {Component}
11  */
12 class SortableList {
13     setup() {
14         this.container = this.$el;
15         this.handleSelector = this.$opts.handleSelector;
16
17         const sortable = new Sortable(this.container, {
18             handle: this.handleSelector,
19             animation: 150,
20             onSort: () => {
21                 this.$emit('sort', {ids: sortable.toArray()});
22             },
23             setData(dataTransferItem, dragEl) {
24                 const jsonContent = dragEl.getAttribute('data-drag-content');
25                 if (jsonContent) {
26                     const contentByType = JSON.parse(jsonContent);
27                     for (const [type, content] of Object.entries(contentByType)) {
28                         dataTransferItem.setData(type, content);
29                     }
30                 }
31             },
32             revertOnSpill: true,
33             dropBubble: true,
34             dragoverBubble: false,
35         });
36     }
37 }
38
39 export default SortableList;