@@ -56,9 +56,11 @@ export class DatatableDraggableDirective implements OnDestroy {
5656 if ( this . enabled ( ) ) {
5757 this . element . addEventListener ( 'mousedown' , this . mousedown ) ;
5858 this . element . addEventListener ( 'touchstart' , this . touchstart ) ;
59+ this . element . addEventListener ( 'contextmenu' , this . contextmenu ) ;
5960 this . removePointerListeners = ( ) => {
6061 this . element . removeEventListener ( 'mousedown' , this . mousedown ) ;
6162 this . element . removeEventListener ( 'touchstart' , this . touchstart ) ;
63+ this . element . removeEventListener ( 'contextmenu' , this . contextmenu ) ;
6264 } ;
6365 } else {
6466 this . removePointerListeners ?.( ) ;
@@ -83,30 +85,44 @@ export class DatatableDraggableDirective implements OnDestroy {
8385 this . delay ( this . dragStartDelay ( ) ) . then ( ( ) => {
8486 this . document . addEventListener ( 'mousemove' , this . mousemove ) ;
8587 this . starting ( event . clientX , event . clientY ) ;
88+ this . setDragging ( true ) ;
8689 } ) ;
8790 } ;
8891
8992 private mousemove = ( event : MouseEvent ) : void => this . moving ( event . clientX , event . clientY ) ;
9093
94+ // Prevent context menu on long-press drag. Since we don't call preventDefault() on
95+ // touchstart to allow click events (sorting), the browser would show a context menu
96+ // after a long press. We prevent this when dragging is active.
97+ private contextmenu = ( event : MouseEvent ) : void => {
98+ if ( this . isDragging ( ) ) {
99+ event . preventDefault ( ) ;
100+ }
101+ } ;
102+
91103 protected readonly touchstart = ( event : TouchEvent ) : void => {
92104 if ( ! this . enabled ( ) ) {
93105 return ;
94106 }
95107 event . stopPropagation ( ) ;
96- event . preventDefault ( ) ;
97108 const touch = event . touches . item ( 0 ) ! ;
98109 this . touchId = touch . identifier ;
99110
100111 this . document . addEventListener ( 'touchend' , this . ending ) ;
101112 this . delay ( this . dragStartDelay ( ) ) . then ( ( ) => {
102- this . document . addEventListener ( 'touchmove' , this . touchmove ) ;
103- this . starting ( touch . clientX , touch . clientY ) ;
113+ if ( this . touchId === touch . identifier ) {
114+ this . document . addEventListener ( 'touchmove' , this . touchmove , { passive : false } ) ;
115+ this . starting ( touch . clientX , touch . clientY ) ;
116+ this . setDragging ( true ) ;
117+ }
104118 } ) ;
105119 } ;
106120
107121 private touchmove = ( event : TouchEvent ) : void => {
108- const touchMove = this . findTouch ( event ) ! ;
122+ const touchMove = this . findTouch ( event ) ;
109123 if ( touchMove ) {
124+ // Prevent scrolling and other default touch behaviors during drag
125+ event . preventDefault ( ) ;
110126 this . moving ( touchMove . clientX , touchMove . clientY ) ;
111127 }
112128 } ;
@@ -139,10 +155,18 @@ export class DatatableDraggableDirective implements OnDestroy {
139155 // This function is also called if the long press was aborted before the delay.
140156 // In that case, we don't want to emit dragEnd.
141157 if ( dragged ) {
158+ this . setDragging ( false ) ;
142159 this . dragEnd . emit ( ) ;
143160 }
144161 } ;
145162
163+ private setDragging ( dragging : boolean ) : void {
164+ const model = this . dragModel ( ) ;
165+ if ( model ) {
166+ model . dragging = dragging ;
167+ }
168+ }
169+
146170 private findTouch ( event : TouchEvent ) : Touch | undefined {
147171 return Array . from ( event . touches ) . find ( touch => touch . identifier === this . touchId ) ;
148172 }
0 commit comments