fix(drag-drop): error on touch end (#14392)

Fixes an error being thrown by `CdkDrag` when the `touchend` event fires.

Fixes #14390.

Marking as P2 since this will throw consistently on touch devices.
This commit is contained in:
Kristiyan Kostadinov
2018-12-05 23:06:33 +01:00
committed by mmalerba
parent 74e945adc9
commit 53cecbb5f9
2 changed files with 26 additions and 1 deletions
+24
View File
@@ -1895,6 +1895,30 @@ describe('CdkDrag', () => {
.toEqual(['Zero', 'One', 'Two', 'Three']);
}));
it('should not throw if the `touches` array is empty', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
dispatchTouchEvent(item, 'touchstart');
fixture.detectChanges();
dispatchTouchEvent(document, 'touchmove');
fixture.detectChanges();
dispatchTouchEvent(document, 'touchmove', 50, 50);
fixture.detectChanges();
expect(() => {
const endEvent = createTouchEvent('touchend', 50, 50);
Object.defineProperty(endEvent, 'touches', {get: () => []});
dispatchEvent(document, endEvent);
fixture.detectChanges();
}).not.toThrow();
}));
});
describe('in a connected drop container', () => {
+2 -1
View File
@@ -790,7 +790,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
/** Determines the point of the page that was touched by the user. */
private _getPointerPositionOnPage(event: MouseEvent | TouchEvent): Point {
const point = this._isTouchEvent(event) ? event.touches[0] : event;
// `touches` will be empty for start/end events so we have to fall back to `changedTouches`.
const point = this._isTouchEvent(event) ? (event.touches[0] || event.changedTouches[0]) : event;
return {
x: point.pageX - this._scrollPosition.left,