@@ -3,67 +3,75 @@ import {
3
3
index
4
4
} from '../../src/utils.js' ;
5
5
6
- let lastSwapEl ;
7
-
6
+ let swapValidEl ;
8
7
9
8
function SwapPlugin ( ) {
10
- function Swap ( ) {
11
- this . defaults = {
12
- swapClass : 'sortable-swap-highlight'
13
- } ;
14
- }
9
+ class Swap {
10
+ constructor ( ) {
11
+ this . defaults = {
12
+ swapClass : 'sortable-swap-highlight'
13
+ } ;
14
+ }
15
+ dragOver ( { activeSortable, target, dragEl, onMove, completed, cancel } ) {
16
+ let el = this . sortable . el , options = this . options ;
15
17
16
- Swap . prototype = {
17
- dragStart ( { dragEl } ) {
18
- lastSwapEl = dragEl ;
19
- } ,
20
- dragOverValid ( { completed, target, onMove, activeSortable, changed, cancel } ) {
21
- if ( ! activeSortable . options . swap ) return ;
22
- let el = this . sortable . el ,
23
- options = this . options ;
24
- if ( target && target !== el ) {
25
- let prevSwapEl = lastSwapEl ;
26
- if ( onMove ( target ) !== false ) {
27
- toggleClass ( target , options . swapClass , true ) ;
28
- lastSwapEl = target ;
29
- } else {
30
- lastSwapEl = null ;
31
- }
18
+ if ( ! activeSortable . options . swap || ! target || target === el || target . contains ( dragEl ) || onMove ( target ) === false ) {
19
+ swapValidEl && toggleClass ( swapValidEl , options . swapClass , false ) ;
20
+ swapValidEl = null ;
32
21
33
- if ( prevSwapEl && prevSwapEl !== lastSwapEl ) {
34
- toggleClass ( prevSwapEl , options . swapClass , false ) ;
35
- }
22
+ completed ( false ) ;
23
+ cancel ( ) ;
36
24
}
25
+ }
26
+ dragOverValid ( { target, changed, completed, cancel } ) {
27
+ let options = this . options ;
28
+
29
+ if ( swapValidEl && swapValidEl !== target ) {
30
+ toggleClass ( swapValidEl , options . swapClass , false ) ;
31
+ }
32
+
33
+ toggleClass ( target , options . swapClass , true ) ;
34
+ swapValidEl = target ;
35
+
37
36
changed ( ) ;
38
37
39
38
completed ( true ) ;
40
39
cancel ( ) ;
41
- } ,
40
+ }
42
41
drop ( { activeSortable, putSortable, dragEl } ) {
43
- let toSortable = ( putSortable || this . sortable ) ;
44
- let options = this . options ;
45
- lastSwapEl && toggleClass ( lastSwapEl , options . swapClass , false ) ;
46
- if ( lastSwapEl && ( options . swap || putSortable && putSortable . options . swap ) ) {
47
- if ( dragEl !== lastSwapEl ) {
48
- toSortable . captureAnimationState ( ) ;
49
- if ( toSortable !== activeSortable ) activeSortable . captureAnimationState ( ) ;
50
- swapNodes ( dragEl , lastSwapEl ) ;
51
-
52
- toSortable . animateAll ( ) ;
53
- if ( toSortable !== activeSortable ) activeSortable . animateAll ( ) ;
42
+ if ( ! swapValidEl ) {
43
+ return ;
44
+ }
45
+
46
+ let toSortable = putSortable || this . sortable , options = this . options ;
47
+
48
+ toggleClass ( swapValidEl , options . swapClass , false ) ;
49
+
50
+ if ( options . swap || putSortable && putSortable . options . swap ) {
51
+ toSortable . captureAnimationState ( ) ;
52
+ if ( toSortable !== activeSortable ) {
53
+ activeSortable . captureAnimationState ( ) ;
54
+ }
55
+
56
+ swapNodes ( dragEl , swapValidEl ) ;
57
+
58
+ toSortable . animateAll ( ) ;
59
+ if ( toSortable !== activeSortable ) {
60
+ activeSortable . animateAll ( ) ;
54
61
}
55
62
}
56
- } ,
63
+ }
57
64
nulling ( ) {
58
- lastSwapEl = null ;
65
+ swapValidEl = null ;
59
66
}
60
- } ;
67
+ }
68
+
61
69
62
70
return Object . assign ( Swap , {
63
71
pluginName : 'swap' ,
64
72
eventProperties ( ) {
65
73
return {
66
- swapItem : lastSwapEl
74
+ swapItem : swapValidEl
67
75
} ;
68
76
}
69
77
} ) ;
@@ -75,7 +83,9 @@ function swapNodes(n1, n2) {
75
83
p2 = n2 . parentNode ,
76
84
i1 , i2 ;
77
85
78
- if ( ! p1 || ! p2 || p1 . isEqualNode ( n2 ) || p2 . isEqualNode ( n1 ) ) return ;
86
+ if ( ! p1 || ! p2 || p1 . isEqualNode ( n2 ) || p2 . isEqualNode ( n1 ) ) {
87
+ return ;
88
+ }
79
89
80
90
i1 = index ( n1 ) ;
81
91
i2 = index ( n2 ) ;
0 commit comments