@@ -22,6 +22,9 @@ exports['default'] = {
22
22
EVENT_MOUSEOVER : 'mouseover' ,
23
23
EVENT_MOUSEMOVE : 'mousemove' ,
24
24
EVENT_MOUSEUP : 'mouseup' ,
25
+ EVENT_TOUCHSTART : 'touchstart' ,
26
+ EVENT_TOUCHMOVE : 'touchmove' ,
27
+ EVENT_TOUCHEND : 'touchend' ,
25
28
EVENT_RESIZE : 'resize' ,
26
29
EVENT_CELL_RESIZE : 'cellresize' ,
27
30
EVENT_CELL_DRAG : 'celldrag' ,
@@ -140,7 +143,7 @@ var _methods = require('./methods');
140
143
autoPadNonContiguousCells : true , // toggle adding non-contiguous cells automatically on drag or as needed.
141
144
updateCoordinatesOnWindowResize : true , // enable window resize event handler.
142
145
debug : false , // toggle console output.
143
- dragMouseoverThrottle : 500 , // throttle cell mouseover events for rearranging.
146
+ dragMouseoverThrottle : 150 , // throttle cell mouseover events for rearranging.
144
147
windowResizeDebounce : 50 , // debounce redraw on window resize.
145
148
mousemoveDebounce : 0 // debounce mousemove for dragging cells.
146
149
} ;
@@ -183,6 +186,17 @@ var Handlers = (function () {
183
186
}
184
187
} ;
185
188
189
+ Handlers . prototype . onTouchStart = function onTouchStart ( touchEvent , $cell , gridstrapContext ) {
190
+ var $ = this . setup . jQuery ;
191
+ var options = this . setup . Options ;
192
+
193
+ touchEvent . preventDefault ( ) ;
194
+
195
+ if ( touchEvent . touches . length ) {
196
+ this . onMousedown ( _utils . Utils . ConvertTouchToMouseEvent ( touchEvent ) , $cell , gridstrapContext ) ;
197
+ }
198
+ } ;
199
+
186
200
Handlers . prototype . onMousedown = function onMousedown ( mouseEvent , $cell , gridstrapContext ) {
187
201
var $ = this . setup . jQuery ;
188
202
var context = this . setup . Context ;
@@ -218,6 +232,8 @@ var Handlers = (function () {
218
232
} ;
219
233
220
234
Handlers . prototype . onMouseover = function onMouseover ( mouseEvent , $cell , gridstrapContext ) {
235
+ var _this = this ;
236
+
221
237
var $ = this . setup . jQuery ;
222
238
var context = this . setup . Context ;
223
239
var options = this . setup . Options ;
@@ -238,21 +254,24 @@ var Handlers = (function () {
238
254
239
255
this . internal . LastMouseOverCellTarget = $cell ;
240
256
241
- if ( ! _utils . Utils . IsElementThrottled ( $ , $cell , options . dragMouseoverThrottle ) ) {
242
- // do not move two cells that have recently already moved.
243
-
257
+ _utils . Utils . Limit ( function ( ) {
244
258
if ( gridstrapContext . options . rearrangeOnDrag ) {
245
259
246
- this . internal . MoveCell ( $draggedCell , $cell , gridstrapContext ) ;
260
+ _this . internal . MoveCell ( $draggedCell , $cell , gridstrapContext ) ;
247
261
248
262
// reset dragged object to mouse pos, not pos of hidden cells.
249
- this . internal . MoveDraggedCell ( mouseEvent , $draggedCell ) ;
263
+ _this . internal . MoveDraggedCell ( mouseEvent , $draggedCell ) ;
250
264
}
251
- }
265
+ } , options . dragMouseoverThrottle ) ;
252
266
}
253
267
}
254
268
} ;
255
269
270
+ Handlers . prototype . onTouchmove = function onTouchmove ( touchEvent ) {
271
+
272
+ this . onMousemove ( _utils . Utils . ConvertTouchToMouseEvent ( touchEvent ) ) ;
273
+ } ;
274
+
256
275
Handlers . prototype . onMousemove = function onMousemove ( mouseEvent ) {
257
276
var $ = this . setup . jQuery ;
258
277
var context = this . setup . Context ;
@@ -294,6 +313,11 @@ var Handlers = (function () {
294
313
}
295
314
} ;
296
315
316
+ Handlers . prototype . onTouchend = function onTouchend ( touchEvent ) {
317
+ // don't convert to mouseEVent becuase there are no touches.
318
+ this . onMouseup ( touchEvent ) ;
319
+ } ;
320
+
297
321
Handlers . prototype . onMouseup = function onMouseup ( mouseEvent ) {
298
322
var $ = this . setup . jQuery ;
299
323
var context = this . setup . Context ;
@@ -409,11 +433,14 @@ var Internal = (function () {
409
433
this . HandleCellMouseEvent ( context , '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_DRAGSTART ) , true , eventHandlers . onDragstart . bind ( eventHandlers ) ) ;
410
434
411
435
this . HandleCellMouseEvent ( context , '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_MOUSEDOWN ) , true , eventHandlers . onMousedown . bind ( eventHandlers ) ) ;
436
+
437
+ this . HandleCellMouseEvent ( context , '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_TOUCHSTART ) , true , eventHandlers . onTouchStart . bind ( eventHandlers ) ) ;
438
+
412
439
// pass false as param because we need to do non-contiguous stuff in there.
413
440
this . HandleCellMouseEvent ( context , '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_MOUSEOVER ) , false , eventHandlers . onMouseover . bind ( eventHandlers ) ) ;
414
441
415
442
// it is not appropriate to confine the events to the visible cell wrapper.
416
- $ ( options . mouseMoveSelector ) . on ( '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_MOUSEMOVE ) , _utils . Utils . Debounce ( eventHandlers . onMousemove . bind ( eventHandlers ) , options . mousemoveDebounce ) ) . on ( '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_MOUSEUP ) , eventHandlers . onMouseup . bind ( eventHandlers ) ) ;
443
+ $ ( options . mouseMoveSelector ) . on ( '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_MOUSEMOVE ) , _utils . Utils . Debounce ( eventHandlers . onMousemove . bind ( eventHandlers ) , options . mousemoveDebounce ) ) . on ( '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_TOUCHMOVE ) , _utils . Utils . Debounce ( eventHandlers . onTouchmove . bind ( eventHandlers ) , options . mousemoveDebounce ) ) . on ( '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_MOUSEUP ) , eventHandlers . onMouseup . bind ( eventHandlers ) ) . on ( '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_TOUCHEND ) , eventHandlers . onTouchend . bind ( eventHandlers ) ) ;
417
444
418
445
if ( options . updateCoordinatesOnWindowResize ) {
419
446
$ ( window ) . on ( '' + appendNamespace ( _constants2 [ 'default' ] . EVENT_RESIZE ) , _utils . Utils . Debounce ( context . updateVisibleCellCoordinates , options . windowResizeDebounce ) ) ;
@@ -500,24 +527,23 @@ var Internal = (function () {
500
527
} ) ;
501
528
} ;
502
529
503
- Internal . prototype . $GetNonDraggedCellFromPoint = function $GetNonDraggedCellFromPoint ( $draggedCell , mouseEvent ) {
530
+ Internal . prototype . GetNonDraggedElementFromPoint = function GetNonDraggedElementFromPoint ( $draggedCell , mouseEvent ) {
504
531
var document = this . setup . Document ;
505
532
var $ = this . setup . jQuery ;
506
533
507
534
//remove mouse events from dragged cell, because we need to test for overlap of underneath things.
508
535
var oldPointerEvents = $draggedCell . css ( 'pointer-events' ) ;
536
+ var oldTouchAction = $draggedCell . css ( 'touch-action' ) ;
509
537
$draggedCell . css ( 'pointer-events' , 'none' ) ;
538
+ $draggedCell . css ( 'touch-action' , 'none' ) ;
510
539
511
540
var element = document . elementFromPoint ( mouseEvent . clientX , mouseEvent . clientY ) ;
512
- var cellAndIndex = this . GetCellAndInternalIndex ( element ) ;
513
541
514
542
// restore pointer-events css.
515
543
$draggedCell . css ( 'pointer-events' , oldPointerEvents ) ;
544
+ $draggedCell . css ( 'touch-action' , oldTouchAction ) ;
516
545
517
- if ( ! cellAndIndex ) {
518
- return $ ( ) ;
519
- }
520
- return cellAndIndex . $cell ;
546
+ return element ;
521
547
} ;
522
548
523
549
Internal . prototype . MoveDraggedCell = function MoveDraggedCell ( mouseEvent , $cell ) {
@@ -553,7 +579,8 @@ var Internal = (function () {
553
579
} ) ) ;
554
580
} ;
555
581
556
- var $overlappedCell = this . $GetNonDraggedCellFromPoint ( $cell , mouseEvent ) ;
582
+ var overlappedElement = this . GetNonDraggedElementFromPoint ( $cell , mouseEvent ) ;
583
+ var $overlappedCell = context . $getCellOfElement ( overlappedElement ) ;
557
584
558
585
if ( $overlappedCell . length ) {
559
586
// have to create event here like this other mouse coords are missing.
@@ -568,7 +595,7 @@ var Internal = (function () {
568
595
var additionalContext = $ ( this ) . data ( _constants2 [ 'default' ] . DATA_GRIDSTRAP ) ;
569
596
if ( additionalContext ) {
570
597
// $getCellOfElement is a 'public' method.
571
- var $additionalContextCell = additionalContext . $getCellOfElement ( element ) ;
598
+ var $additionalContextCell = additionalContext . $getCellOfElement ( overlappedElement ) ;
572
599
if ( $additionalContextCell . length ) {
573
600
// have to create event here like this other mouse coords are missing.
574
601
triggerMouseOverEvent ( $additionalContextCell ) ;
@@ -1427,46 +1454,42 @@ var Utils = (function () {
1427
1454
return cssClass . replace ( / ( ^ * | + ) / g, '.' ) ;
1428
1455
} ;
1429
1456
1430
- Utils . Debounce = function Debounce ( callback , milliseconds , leading ) {
1431
- var timeout = undefined ;
1457
+ Utils . Debounce = function Debounce ( callback , milliseconds , leading , timeout ) {
1458
+ if ( typeof timeout === 'undefined' ) {
1459
+ timeout = null ;
1460
+ }
1432
1461
return function ( ) {
1433
1462
var context = this ;
1434
1463
var args = arguments ;
1435
- var callNow = leading || ! milliseconds ;
1436
1464
var later = function later ( ) {
1437
1465
timeout = null ;
1438
- if ( ! callNow ) {
1466
+ if ( ! leading ) {
1439
1467
callback . apply ( context , args ) ;
1440
1468
}
1441
1469
} ;
1470
+ var callNow = leading && ! timeout ;
1471
+
1472
+ if ( milliseconds == 500 ) console . log ( 'callNow: ' + callNow ) ;
1473
+
1442
1474
clearTimeout ( timeout ) ;
1443
1475
timeout = setTimeout ( later , milliseconds ) ;
1444
1476
if ( callNow ) {
1445
1477
callback . apply ( context , args ) ;
1446
1478
}
1479
+
1480
+ return timeout ;
1447
1481
} ;
1448
1482
} ;
1449
1483
1450
- Utils . IsElementThrottled = function IsElementThrottled ( $ , element , milliseconds ) {
1451
-
1452
- Utils . recentDragMouseOvers = Utils . recentDragMouseOvers || [ ] ;
1453
-
1484
+ Utils . Limit = function Limit ( callback , milliseconds ) {
1454
1485
var d = new Date ( ) ;
1455
1486
var n = d . getTime ( ) ;
1456
- for ( var i = 0 ; i < Utils . recentDragMouseOvers . length ; i ++ ) {
1457
- if ( Utils . recentDragMouseOvers [ i ] . n + milliseconds < n ) {
1458
- // expired.
1459
- Utils . recentDragMouseOvers . splice ( i , 1 ) ;
1460
- }
1461
- if ( i < Utils . recentDragMouseOvers . length && $ ( Utils . recentDragMouseOvers [ i ] . e ) . is ( element ) ) {
1462
- return true ;
1463
- }
1487
+ if ( n - ( Utils . limit || 0 ) > milliseconds ) {
1488
+
1489
+ callback ( ) ;
1490
+
1491
+ Utils . limit = n ;
1464
1492
}
1465
- Utils . recentDragMouseOvers . push ( {
1466
- n : n ,
1467
- e : element
1468
- } ) ;
1469
- return false ;
1470
1493
} ;
1471
1494
1472
1495
Utils . SwapJQueryElements = function SwapJQueryElements ( $a , $b ) {
@@ -1565,6 +1588,22 @@ var Utils = (function () {
1565
1588
} ;
1566
1589
} ;
1567
1590
1591
+ Utils . ConvertTouchToMouseEvent = function ConvertTouchToMouseEvent ( touchEvent ) {
1592
+ var touch = null ;
1593
+ for ( var i = 0 ; ! touch && i < touchEvent . changedTouches . length ; i ++ ) {
1594
+ if ( touchEvent . changedTouches [ i ] . identifier === 0 ) {
1595
+ touch = touchEvent . changedTouches [ i ] ;
1596
+ }
1597
+ }
1598
+
1599
+ touchEvent . pageX = touch . pageX ;
1600
+ touchEvent . pageY = touch . pageY ;
1601
+ touchEvent . clientX = touch . clientX ;
1602
+ touchEvent . clientY = touch . clientY ;
1603
+
1604
+ return touchEvent ;
1605
+ } ;
1606
+
1568
1607
return Utils ;
1569
1608
} ) ( ) ;
1570
1609
0 commit comments