-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathSwap.js
More file actions
95 lines (73 loc) · 1.97 KB
/
Swap.js
File metadata and controls
95 lines (73 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {
toggleClass,
index
} from '../../src/utils.js';
let swapValidEl;
function SwapPlugin() {
function Swap() {
this.defaults = {
swapClass: 'sortable-swap-highlight'
};
}
Swap.prototype = {
dragOver({ activeSortable, target, dragEl, onMove, completed, cancel }) {
let el = this.sortable.el,
options = this.options;
if (!activeSortable.options.swap || !target || target === el || target.contains(dragEl) || onMove(target) === false) {
swapValidEl && toggleClass(swapValidEl, options.swapClass, false);
swapValidEl = null;
completed(false);
cancel();
}
},
dragOverValid({ target, changed, completed, cancel }) {
let options = this.options;
if (swapValidEl && swapValidEl !== target) {
toggleClass(swapValidEl, options.swapClass, false);
}
toggleClass(target, options.swapClass, true);
swapValidEl = target;
changed();
completed(true);
cancel();
},
drop({ activeSortable, putSortable, dragEl }) {
if (!swapValidEl) return
let toSortable = putSortable || this.sortable,
options = this.options;
toggleClass(swapValidEl, options.swapClass, false);
if (options.swap || putSortable && putSortable.options.swap) {
toSortable.captureAnimationState();
if (toSortable !== activeSortable) activeSortable.captureAnimationState();
swapNodes(dragEl, swapValidEl);
toSortable.animateAll();
if (toSortable !== activeSortable) activeSortable.animateAll();
}
},
nulling() {
swapValidEl = null;
}
};
return Object.assign(Swap, {
pluginName: 'swap',
eventProperties() {
return {
swapItem: swapValidEl
};
}
});
}
function swapNodes(n1, n2) {
let p1 = n1.parentNode,
p2 = n2.parentNode,
i1, i2;
if (!p1 || !p2 || p1.isEqualNode(n2) || p2.isEqualNode(n1)) return;
i1 = index(n1);
i2 = index(n2);
if (p1.isEqualNode(p2) && i1 < i2) {
i2++;
}
p1.insertBefore(n2, p1.children[i1]);
p2.insertBefore(n1, p2.children[i2]);
}
export default SwapPlugin;