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