@@ -67,6 +67,11 @@ export interface PointerEvent {
67
67
event : MouseEvent | TouchEvent ;
68
68
}
69
69
70
+ export interface TimeLongPress {
71
+ timerBegin : number ;
72
+ timerEnd : number ;
73
+ }
74
+
70
75
@Directive ( {
71
76
selector : '[mwlDraggable]'
72
77
} )
@@ -194,6 +199,8 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
194
199
195
200
private destroy$ = new Subject ( ) ;
196
201
202
+ private timeLongPress : TimeLongPress = { timerBegin : 0 , timerEnd : 0 } ;
203
+
197
204
/**
198
205
* @hidden
199
206
*/
@@ -215,7 +222,7 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
215
222
mergeMap ( ( pointerDownEvent : PointerEvent ) => {
216
223
// fix for https://github.com/mattlewis92/angular-draggable-droppable/issues/61
217
224
// stop mouse events propagating up the chain
218
- if ( pointerDownEvent . event . stopPropagation ) {
225
+ if ( pointerDownEvent . event . stopPropagation && ! this . scrollContainer ) {
219
226
pointerDownEvent . event . stopPropagation ( ) ;
220
227
}
221
228
@@ -601,16 +608,49 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
601
608
}
602
609
603
610
private onTouchStart ( event : TouchEvent ) : void {
611
+ if ( ! this . scrollContainer ) {
612
+ try {
613
+ event . preventDefault ( ) ;
614
+ } catch ( e ) { }
615
+ }
616
+ let hasContainerScrollbar : boolean ;
617
+ let startScrollPosition : any ;
618
+ let isDragActivated : boolean ;
619
+ if ( this . scrollContainer && this . scrollContainer . activeLongPressDrag ) {
620
+ this . timeLongPress . timerBegin = Date . now ( ) ;
621
+ isDragActivated = false ;
622
+ hasContainerScrollbar = this . scrollContainer . hasScrollbar ( ) ;
623
+ startScrollPosition = this . getScrollPosition ( ) ;
624
+ }
604
625
if ( ! this . eventListenerSubscriptions . touchmove ) {
605
626
this . eventListenerSubscriptions . touchmove = this . renderer . listen (
606
627
'document' ,
607
628
'touchmove' ,
608
629
( touchMoveEvent : TouchEvent ) => {
609
- this . pointerMove$ . next ( {
610
- event : touchMoveEvent ,
611
- clientX : touchMoveEvent . targetTouches [ 0 ] . clientX ,
612
- clientY : touchMoveEvent . targetTouches [ 0 ] . clientY
613
- } ) ;
630
+ if (
631
+ this . scrollContainer &&
632
+ this . scrollContainer . activeLongPressDrag &&
633
+ ! isDragActivated &&
634
+ hasContainerScrollbar
635
+ ) {
636
+ isDragActivated = this . shouldBeginDrag (
637
+ event ,
638
+ touchMoveEvent ,
639
+ startScrollPosition
640
+ ) ;
641
+ }
642
+ if (
643
+ ! this . scrollContainer ||
644
+ ! this . scrollContainer . activeLongPressDrag ||
645
+ ! hasContainerScrollbar ||
646
+ isDragActivated
647
+ ) {
648
+ this . pointerMove$ . next ( {
649
+ event : touchMoveEvent ,
650
+ clientX : touchMoveEvent . targetTouches [ 0 ] . clientX ,
651
+ clientY : touchMoveEvent . targetTouches [ 0 ] . clientY
652
+ } ) ;
653
+ }
614
654
}
615
655
) ;
616
656
}
@@ -625,6 +665,9 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
625
665
if ( this . eventListenerSubscriptions . touchmove ) {
626
666
this . eventListenerSubscriptions . touchmove ( ) ;
627
667
delete this . eventListenerSubscriptions . touchmove ;
668
+ if ( this . scrollContainer && this . scrollContainer . activeLongPressDrag ) {
669
+ this . scrollContainer . enableScroll ( ) ;
670
+ }
628
671
}
629
672
this . pointerUp$ . next ( {
630
673
event,
@@ -678,4 +721,40 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
678
721
} ;
679
722
}
680
723
}
724
+
725
+ private shouldBeginDrag (
726
+ event : TouchEvent ,
727
+ touchMoveEvent : TouchEvent ,
728
+ startScrollPosition : any
729
+ ) : boolean {
730
+ const moveScrollPosition = this . getScrollPosition ( ) ;
731
+ const deltaScroll = {
732
+ top : Math . abs ( moveScrollPosition . top - startScrollPosition . top ) ,
733
+ left : Math . abs ( moveScrollPosition . left - startScrollPosition . left )
734
+ } ;
735
+ const deltaX =
736
+ Math . abs (
737
+ touchMoveEvent . targetTouches [ 0 ] . clientX - event . touches [ 0 ] . clientX
738
+ ) - deltaScroll . left ;
739
+ const deltaY =
740
+ Math . abs (
741
+ touchMoveEvent . targetTouches [ 0 ] . clientY - event . touches [ 0 ] . clientY
742
+ ) - deltaScroll . top ;
743
+ const deltaTotal = deltaX + deltaY ;
744
+ if (
745
+ deltaTotal > this . scrollContainer . longPressConfig . delta ||
746
+ deltaScroll . top > 0 ||
747
+ deltaScroll . left > 0
748
+ ) {
749
+ this . timeLongPress . timerBegin = Date . now ( ) ;
750
+ }
751
+ this . timeLongPress . timerEnd = Date . now ( ) ;
752
+ const duration =
753
+ this . timeLongPress . timerEnd - this . timeLongPress . timerBegin ;
754
+ if ( duration >= this . scrollContainer . longPressConfig . duration ) {
755
+ this . scrollContainer . disableScroll ( ) ;
756
+ return true ;
757
+ }
758
+ return false ;
759
+ }
681
760
}
0 commit comments