Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(drag-drop): avoid disrupting drag sequence if event propagation is stopped #13841

Merged
merged 1 commit into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/cdk/drag-drop/drag-drop-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ describe('DragDropRegistry', () => {
subscription.unsubscribe();
});

it('should dispatch pointer move events if event propagation is stopped', () => {
const spy = jasmine.createSpy('pointerMove spy');
const subscription = registry.pointerMove.subscribe(spy);

fixture.nativeElement.addEventListener('mousemove', (e: MouseEvent) => e.stopPropagation());
registry.startDragging(testComponent.dragItems.first, createMouseEvent('mousedown'));
dispatchMouseEvent(fixture.nativeElement.querySelector('div'), 'mousemove');

expect(spy).toHaveBeenCalled();

subscription.unsubscribe();
});

it('should dispatch `mouseup` events after ending the drag via the mouse', () => {
const spy = jasmine.createSpy('pointerUp spy');
const subscription = registry.pointerUp.subscribe(spy);
Expand All @@ -113,6 +126,19 @@ describe('DragDropRegistry', () => {
subscription.unsubscribe();
});

it('should dispatch pointer up events if event propagation is stopped', () => {
const spy = jasmine.createSpy('pointerUp spy');
const subscription = registry.pointerUp.subscribe(spy);

fixture.nativeElement.addEventListener('mouseup', (e: MouseEvent) => e.stopPropagation());
registry.startDragging(testComponent.dragItems.first, createMouseEvent('mousedown'));
dispatchMouseEvent(fixture.nativeElement.querySelector('div'), 'mouseup');

expect(spy).toHaveBeenCalled();

subscription.unsubscribe();
});

it('should complete the pointer event streams on destroy', () => {
const pointerUpSpy = jasmine.createSpy('pointerUp complete spy');
const pointerMoveSpy = jasmine.createSpy('pointerMove complete spy');
Expand Down Expand Up @@ -155,6 +181,17 @@ describe('DragDropRegistry', () => {
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented).toBe(true);
});

it('should prevent the default `touchmove` if event propagation is stopped', () => {
registry.startDragging(testComponent.dragItems.first,
createTouchEvent('touchstart') as TouchEvent);

fixture.nativeElement.addEventListener('touchmove', (e: TouchEvent) => e.stopPropagation());

const event = dispatchTouchEvent(fixture.nativeElement.querySelector('div'), 'touchmove');

expect(event.defaultPrevented).toBe(true);
});

});

@Component({
Expand Down
23 changes: 16 additions & 7 deletions src/cdk/drag-drop/drag-drop-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {Subject} from 'rxjs';
import {toggleNativeDragInteractions} from './drag-styling';

/** Event options that can be used to bind an active event. */
const activeEventOptions = normalizePassiveListenerOptions({passive: false});
/** Event options that can be used to bind an active, capturing event. */
const activeCapturingEventOptions = normalizePassiveListenerOptions({
passive: false,
capture: true
});

/** Handler for a pointer event callback. */
type PointerEventHandler = (event: TouchEvent | MouseEvent) => void;
Expand Down Expand Up @@ -42,7 +45,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
/** Keeps track of the event listeners that we've bound to the `document`. */
private _globalListeners = new Map<'touchmove' | 'mousemove' | 'touchend' | 'mouseup', {
handler: PointerEventHandler,
options?: any
options?: AddEventListenerOptions | boolean
}>();

/**
Expand Down Expand Up @@ -83,7 +86,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
// The event handler has to be explicitly active, because
// newer browsers make it passive by default.
this._document.addEventListener('touchmove', this._preventScrollListener,
activeEventOptions);
activeCapturingEventOptions);
});
}
}
Expand All @@ -100,7 +103,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {

if (this._dragInstances.size === 0) {
this._document.removeEventListener('touchmove', this._preventScrollListener,
activeEventOptions as any);
activeCapturingEventOptions);
}
}

Expand All @@ -125,8 +128,14 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
// passive ones for `mousemove` and `touchmove`. The events need to be active, because we
// use `preventDefault` to prevent the page from scrolling while the user is dragging.
this._globalListeners
.set(moveEvent, {handler: e => this.pointerMove.next(e), options: activeEventOptions})
.set(upEvent, {handler: e => this.pointerUp.next(e)})
.set(moveEvent, {
handler: e => this.pointerMove.next(e),
options: activeCapturingEventOptions
})
.set(upEvent, {
handler: e => this.pointerUp.next(e),
options: true
})
.forEach((config, name) => {
this._ngZone.runOutsideAngular(() => {
this._document.addEventListener(name, config.handler, config.options);
Expand Down