@@ -9,7 +9,10 @@ import {
9
9
events ,
10
10
vendorPrefix ,
11
11
limit ,
12
+ getEdgeOffset ,
12
13
getElementMargin ,
14
+ getLockPixelOffset ,
15
+ getPosition ,
13
16
provideDisplayName ,
14
17
omit ,
15
18
} from '../utils' ;
@@ -145,20 +148,17 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
145
148
}
146
149
}
147
150
148
- handleStart = e => {
151
+ handleStart = event => {
149
152
const { distance, shouldCancelStart} = this . props ;
150
153
151
- if ( e . button === 2 || shouldCancelStart ( e ) ) {
154
+ if ( event . button === 2 || shouldCancelStart ( event ) ) {
152
155
return false ;
153
156
}
154
157
155
158
this . _touched = true ;
156
- this . _pos = {
157
- x : e . pageX ,
158
- y : e . pageY ,
159
- } ;
159
+ this . _pos = getPosition ( event ) ;
160
160
161
- const node = closest ( e . target , el => el . sortableInfo != null ) ;
161
+ const node = closest ( event . target , el => el . sortableInfo != null ) ;
162
162
163
163
if (
164
164
node &&
@@ -170,7 +170,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
170
170
const { index, collection} = node . sortableInfo ;
171
171
172
172
if (
173
- useDragHandle && ! closest ( e . target , el => el . sortableHandle != null )
173
+ useDragHandle && ! closest ( event . target , el => el . sortableHandle != null )
174
174
)
175
175
return ;
176
176
@@ -181,16 +181,16 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
181
181
* prevent subsequent 'mousemove' events from being fired
182
182
* (see https://github.com/clauderic/react-sortable-hoc/issues/118)
183
183
*/
184
- if ( e . target . tagName . toLowerCase ( ) === 'a' ) {
185
- e . preventDefault ( ) ;
184
+ if ( event . target . tagName . toLowerCase ( ) === 'a' ) {
185
+ event . preventDefault ( ) ;
186
186
}
187
187
188
188
if ( ! distance ) {
189
189
if ( this . props . pressDelay === 0 ) {
190
- this . handlePress ( e ) ;
190
+ this . handlePress ( event ) ;
191
191
} else {
192
192
this . pressTimer = setTimeout (
193
- ( ) => this . handlePress ( e ) ,
193
+ ( ) => this . handlePress ( event ) ,
194
194
this . props . pressDelay
195
195
) ;
196
196
}
@@ -202,21 +202,22 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
202
202
return node . sortableInfo . manager === this . manager ;
203
203
} ;
204
204
205
- handleMove = e => {
205
+ handleMove = event => {
206
206
const { distance, pressThreshold} = this . props ;
207
207
208
208
if ( ! this . state . sorting && this . _touched ) {
209
- this . _delta = {
210
- x : this . _pos . x - e . pageX ,
211
- y : this . _pos . y - e . pageY ,
209
+ const position = getPosition ( event ) ;
210
+ const delta = this . _delta = {
211
+ x : this . _pos . x - position . x ,
212
+ y : this . _pos . y - position . y ,
212
213
} ;
213
- const delta = Math . abs ( this . _delta . x ) + Math . abs ( this . _delta . y ) ;
214
+ const combinedDelta = Math . abs ( delta . x ) + Math . abs ( delta . y ) ;
214
215
215
- if ( ! distance && ( ! pressThreshold || pressThreshold && delta >= pressThreshold ) ) {
216
+ if ( ! distance && ( ! pressThreshold || pressThreshold && combinedDelta >= pressThreshold ) ) {
216
217
clearTimeout ( this . cancelTimer ) ;
217
218
this . cancelTimer = setTimeout ( this . cancel , 0 ) ;
218
- } else if ( distance && delta >= distance && this . manager . isActive ( ) ) {
219
- this . handlePress ( e ) ;
219
+ } else if ( distance && combinedDelta >= distance && this . manager . isActive ( ) ) {
220
+ this . handlePress ( event ) ;
220
221
}
221
222
}
222
223
} ;
@@ -238,7 +239,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
238
239
}
239
240
} ;
240
241
241
- handlePress = e => {
242
+ handlePress = event => {
242
243
const active = this . manager . getActive ( ) ;
243
244
244
245
if ( active ) {
@@ -274,8 +275,8 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
274
275
x : axis . indexOf ( 'x' ) >= 0 ,
275
276
y : axis . indexOf ( 'y' ) >= 0 ,
276
277
} ;
277
- this . offsetEdge = this . getEdgeOffset ( node ) ;
278
- this . initialOffset = this . getOffset ( e ) ;
278
+ this . offsetEdge = getEdgeOffset ( node , this . container ) ;
279
+ this . initialOffset = getPosition ( event ) ;
279
280
this . initialScroll = {
280
281
top : this . container . scrollTop ,
281
282
left : this . container . scrollLeft ,
@@ -345,7 +346,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
345
346
this . helper . classList . add ( ...helperClass . split ( ' ' ) ) ;
346
347
}
347
348
348
- this . listenerNode = e . touches ? node : this . contentWindow ;
349
+ this . listenerNode = event . touches ? node : this . contentWindow ;
349
350
events . move . forEach ( eventName =>
350
351
this . listenerNode . addEventListener (
351
352
eventName ,
@@ -364,22 +365,26 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
364
365
sortingIndex : index ,
365
366
} ) ;
366
367
367
- if ( onSortStart ) onSortStart ( { node, index, collection} , e ) ;
368
+ if ( onSortStart ) {
369
+ onSortStart ( { node, index, collection} , event ) ;
370
+ }
368
371
}
369
372
} ;
370
373
371
- handleSortMove = e => {
374
+ handleSortMove = event => {
372
375
const { onSortMove} = this . props ;
373
- e . preventDefault ( ) ; // Prevent scrolling on mobile
376
+ event . preventDefault ( ) ; // Prevent scrolling on mobile
374
377
375
- this . updatePosition ( e ) ;
378
+ this . updatePosition ( event ) ;
376
379
this . animateNodes ( ) ;
377
380
this . autoscroll ( ) ;
378
381
379
- if ( onSortMove ) onSortMove ( e ) ;
382
+ if ( onSortMove ) {
383
+ onSortMove ( event ) ;
384
+ }
380
385
} ;
381
386
382
- handleSortEnd = e => {
387
+ handleSortEnd = event => {
383
388
const { hideSortableGhost, onSortEnd} = this . props ;
384
389
const { collection} = this . manager . active ;
385
390
@@ -434,113 +439,44 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
434
439
newIndex : this . newIndex ,
435
440
collection,
436
441
} ,
437
- e
442
+ event
438
443
) ;
439
444
}
440
445
441
446
this . _touched = false ;
442
447
} ;
443
448
444
- getEdgeOffset ( node , offset = { top : 0 , left : 0 } ) {
445
- // Get the actual offsetTop / offsetLeft value, no matter how deep the node is nested
446
- if ( node ) {
447
- const nodeOffset = {
448
- top : offset . top + node . offsetTop ,
449
- left : offset . left + node . offsetLeft ,
450
- } ;
451
- if ( node . parentNode !== this . container ) {
452
- return this . getEdgeOffset ( node . parentNode , nodeOffset ) ;
453
- } else {
454
- return nodeOffset ;
455
- }
456
- }
457
- }
458
-
459
- getOffset ( e ) {
460
- if ( e . touches && e . touches . length ) {
461
- return {
462
- x : e . touches [ 0 ] . pageX ,
463
- y : e . touches [ 0 ] . pageY ,
464
- } ;
465
- } else if ( e . changedTouches && e . changedTouches . length ) {
466
- return {
467
- x : e . changedTouches [ 0 ] . pageX ,
468
- y : e . changedTouches [ 0 ] . pageY ,
469
- } ;
470
- } else {
471
- return {
472
- x : e . pageX ,
473
- y : e . pageY ,
474
- } ;
475
- }
476
- }
477
-
478
449
getLockPixelOffsets ( ) {
479
- let { lockOffset } = this . props ;
480
-
481
- if ( ! Array . isArray ( lockOffset ) ) {
482
- lockOffset = [ lockOffset , lockOffset ] ;
483
- }
450
+ const { width , height } = this ;
451
+ const { lockOffset } = this . props ;
452
+ const offsets = Array . isArray ( lockOffset )
453
+ ? lockOffset
454
+ : [ lockOffset , lockOffset ] ;
484
455
485
456
invariant (
486
- lockOffset . length === 2 ,
457
+ offsets . length === 2 ,
487
458
'lockOffset prop of SortableContainer should be a single ' +
488
459
'value or an array of exactly two values. Given %s' ,
489
460
lockOffset
490
461
) ;
491
462
492
- const [ minLockOffset , maxLockOffset ] = lockOffset ;
463
+ const [ minLockOffset , maxLockOffset ] = offsets ;
493
464
494
465
return [
495
- this . getLockPixelOffset ( minLockOffset ) ,
496
- this . getLockPixelOffset ( maxLockOffset ) ,
466
+ getLockPixelOffset ( { offset : minLockOffset , width , height } ) ,
467
+ getLockPixelOffset ( { offset : maxLockOffset , width , height } ) ,
497
468
] ;
498
469
}
499
470
500
- getLockPixelOffset ( lockOffset ) {
501
- let offsetX = lockOffset ;
502
- let offsetY = lockOffset ;
503
- let unit = 'px' ;
504
-
505
- if ( typeof lockOffset === 'string' ) {
506
- const match = / ^ [ + - ] ? \d * (?: \. \d * ) ? ( p x | % ) $ / . exec ( lockOffset ) ;
507
-
508
- invariant (
509
- match !== null ,
510
- 'lockOffset value should be a number or a string of a ' +
511
- 'number followed by "px" or "%". Given %s' ,
512
- lockOffset
513
- ) ;
514
-
515
- offsetX = ( offsetY = parseFloat ( lockOffset ) ) ;
516
- unit = match [ 1 ] ;
517
- }
518
-
519
- invariant (
520
- isFinite ( offsetX ) && isFinite ( offsetY ) ,
521
- 'lockOffset value should be a finite. Given %s' ,
522
- lockOffset
523
- ) ;
524
-
525
- if ( unit === '%' ) {
526
- offsetX = offsetX * this . width / 100 ;
527
- offsetY = offsetY * this . height / 100 ;
528
- }
529
-
530
- return {
531
- x : offsetX ,
532
- y : offsetY ,
533
- } ;
534
- }
535
-
536
- updatePosition ( e ) {
471
+ updatePosition ( event ) {
537
472
const { lockAxis, lockToContainerEdges} = this . props ;
538
473
539
- const offset = this . getOffset ( e ) ;
474
+ const offset = getPosition ( event ) ;
540
475
const translate = {
541
476
x : offset . x - this . initialOffset . x ,
542
477
y : offset . y - this . initialOffset . y ,
543
478
} ;
479
+
544
480
// Adjust for window scroll
545
481
translate . y -= ( window . pageYOffset - this . initialWindowScroll . top ) ;
546
482
translate . x -= ( window . pageXOffset - this . initialWindowScroll . left ) ;
@@ -617,7 +553,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
617
553
618
554
// If we haven't cached the node's offsetTop / offsetLeft value
619
555
if ( ! edgeOffset ) {
620
- nodes [ i ] . edgeOffset = ( edgeOffset = this . getEdgeOffset ( node ) ) ;
556
+ nodes [ i ] . edgeOffset = ( edgeOffset = getEdgeOffset ( node , this . container ) ) ;
621
557
}
622
558
623
559
// Get a reference to the next and previous node
@@ -627,7 +563,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
627
563
// Also cache the next node's edge offset if needed.
628
564
// We need this for calculating the animation in a grid setup
629
565
if ( nextNode && ! nextNode . edgeOffset ) {
630
- nextNode . edgeOffset = this . getEdgeOffset ( nextNode . node ) ;
566
+ nextNode . edgeOffset = getEdgeOffset ( nextNode . node , this . container ) ;
631
567
}
632
568
633
569
// If the node is the one we're currently animating, skip it
0 commit comments