@@ -3,67 +3,75 @@ import {
33 index
44} from '../../src/utils.js' ;
55
6- let lastSwapEl ;
7-
6+ let swapValidEl ;
87
98function 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 ;
1517
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 ;
3221
33- if ( prevSwapEl && prevSwapEl !== lastSwapEl ) {
34- toggleClass ( prevSwapEl , options . swapClass , false ) ;
35- }
22+ completed ( false ) ;
23+ cancel ( ) ;
3624 }
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+
3736 changed ( ) ;
3837
3938 completed ( true ) ;
4039 cancel ( ) ;
41- } ,
40+ }
4241 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 ( ) ;
5461 }
5562 }
56- } ,
63+ }
5764 nulling ( ) {
58- lastSwapEl = null ;
65+ swapValidEl = null ;
5966 }
60- } ;
67+ }
68+
6169
6270 return Object . assign ( Swap , {
6371 pluginName : 'swap' ,
6472 eventProperties ( ) {
6573 return {
66- swapItem : lastSwapEl
74+ swapItem : swapValidEl
6775 } ;
6876 }
6977 } ) ;
@@ -75,7 +83,9 @@ function swapNodes(n1, n2) {
7583 p2 = n2 . parentNode ,
7684 i1 , i2 ;
7785
78- if ( ! p1 || ! p2 || p1 . isEqualNode ( n2 ) || p2 . isEqualNode ( n1 ) ) return ;
86+ if ( ! p1 || ! p2 || p1 . isEqualNode ( n2 ) || p2 . isEqualNode ( n1 ) ) {
87+ return ;
88+ }
7989
8090 i1 = index ( n1 ) ;
8191 i2 = index ( n2 ) ;
0 commit comments