Skip to content

Commit c1e1b22

Browse files
spike-rabbitfh1ch
authored andcommitted
fix(header): allow sorting on mobile devices with reorderable enabled
The touchstart handler was calling preventDefault() immediately, which prevented synthetic click events from firing. This blocked sorting when tapping headers on mobile devices with reorderable enabled. Removed preventDefault() from touchstart and added proper dragging state management to the draggable directive. Closes #692
1 parent 9a89a6c commit c1e1b22

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

‎projects/ngx-datatable/src/lib/directives/datatable-draggable.directive.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎projects/ngx-datatable/src/lib/directives/draggable.directive.spec.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,16 @@ describe('DraggableDirective', () => {
161161
await fixture.whenStable();
162162
});
163163

164+
afterEach(() => {
165+
vi.useRealTimers();
166+
});
167+
164168
it('should start dragging after the specified delay', async () => {
165169
vi.useFakeTimers();
166170
await harness.touchStart(0);
167171
vi.advanceTimersByTime(100);
168172
await fixture.whenStable();
169173
expect(dragStartSpy).toHaveBeenCalled();
170-
vi.useRealTimers();
171174
});
172175

173176
it('should skip dragging if not pressed long enough', async () => {
@@ -177,7 +180,6 @@ describe('DraggableDirective', () => {
177180
await harness.mouseUp();
178181
expect(dragStartSpy).not.toHaveBeenCalled();
179182
expect(dragEndSpy).not.toHaveBeenCalled();
180-
vi.useRealTimers();
181183
});
182184

183185
it('should not start dragging if waiting for the delay', async () => {
@@ -186,7 +188,14 @@ describe('DraggableDirective', () => {
186188
vi.advanceTimersByTime(50);
187189
await harness.mouseMove(100);
188190
expect(dragMoveSpy).not.toHaveBeenCalled();
189-
vi.useRealTimers();
191+
});
192+
193+
it('should not start dragging after short touch before delay', async () => {
194+
vi.useFakeTimers();
195+
await harness.touchStart(0);
196+
vi.advanceTimersByTime(50);
197+
await harness.touchEnd();
198+
expect(dragStartSpy).not.toHaveBeenCalled();
190199
});
191200
});
192201
});

0 commit comments

Comments
 (0)