Skip to content

feat(drag-drop): indicate in dropped event whether item was dropped outside of container #14140

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

Merged
merged 1 commit into from
Nov 28, 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
2 changes: 2 additions & 0 deletions src/cdk/drag-drop/drag-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface CdkDragDrop<T, O = T> {
container: CdkDropListContainer<T>;
/** Container from which the item was picked up. Can be the same as the `container`. */
previousContainer: CdkDropListContainer<O>;
/** Whether the user's pointer was over the container when the item was dropped. */
isPointerOverContainer: boolean;
}

/** Event emitted as the user is dragging a draggable item. */
Expand Down
84 changes: 69 additions & 15 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,13 +765,55 @@ describe('CdkDrag', () => {
currentIndex: 2,
item: firstItem,
container: fixture.componentInstance.dropInstance,
previousContainer: fixture.componentInstance.dropInstance
previousContainer: fixture.componentInstance.dropInstance,
isPointerOverContainer: true
});

expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
.toEqual(['One', 'Two', 'Zero', 'Three']);
}));

it('should expose whether an item was dropped over a container', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
const dragItems = fixture.componentInstance.dragItems;
const firstItem = dragItems.first;
const thirdItemRect = dragItems.toArray()[2].element.nativeElement.getBoundingClientRect();

dragElementViaMouse(fixture, firstItem.element.nativeElement,
thirdItemRect.left + 1, thirdItemRect.top + 1);
flush();
fixture.detectChanges();

expect(fixture.componentInstance.droppedSpy).toHaveBeenCalledTimes(1);

const event: CdkDragDrop<any> =
fixture.componentInstance.droppedSpy.calls.mostRecent().args[0];

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

it('should expose whether an item was dropped outside of a container', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
const dragItems = fixture.componentInstance.dragItems;
const firstItem = dragItems.first;
const containerRect = fixture.componentInstance.dropInstance.element
.nativeElement.getBoundingClientRect();

dragElementViaMouse(fixture, firstItem.element.nativeElement,
containerRect.right + 10, containerRect.bottom + 10);
flush();
fixture.detectChanges();

expect(fixture.componentInstance.droppedSpy).toHaveBeenCalledTimes(1);

const event: CdkDragDrop<any> =
fixture.componentInstance.droppedSpy.calls.mostRecent().args[0];

expect(event.isPointerOverContainer).toBe(false);
}));

it('should dispatch the `sorted` event as an item is being sorted', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
Expand Down Expand Up @@ -830,7 +872,8 @@ describe('CdkDrag', () => {
currentIndex: 0,
item: firstItem,
container: fixture.componentInstance.dropInstance,
previousContainer: fixture.componentInstance.dropInstance
previousContainer: fixture.componentInstance.dropInstance,
isPointerOverContainer: false
});

expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
Expand Down Expand Up @@ -887,7 +930,8 @@ describe('CdkDrag', () => {
currentIndex: 2,
item: firstItem,
container: fixture.componentInstance.dropInstance,
previousContainer: fixture.componentInstance.dropInstance
previousContainer: fixture.componentInstance.dropInstance,
isPointerOverContainer: true
});

expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
Expand Down Expand Up @@ -926,7 +970,8 @@ describe('CdkDrag', () => {
currentIndex: 2,
item: firstItem,
container: fixture.componentInstance.dropInstance,
previousContainer: fixture.componentInstance.dropInstance
previousContainer: fixture.componentInstance.dropInstance,
isPointerOverContainer: true
});

expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
Expand Down Expand Up @@ -961,7 +1006,8 @@ describe('CdkDrag', () => {
currentIndex: 0,
item: firstItem,
container: fixture.componentInstance.dropInstance,
previousContainer: fixture.componentInstance.dropInstance
previousContainer: fixture.componentInstance.dropInstance,
isPointerOverContainer: false
});

expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
Expand Down Expand Up @@ -1797,7 +1843,8 @@ describe('CdkDrag', () => {
currentIndex: 3,
item,
container: fixture.componentInstance.dropInstances.toArray()[1],
previousContainer: fixture.componentInstance.dropInstances.first
previousContainer: fixture.componentInstance.dropInstances.first,
isPointerOverContainer: true
});
}));

Expand Down Expand Up @@ -1898,7 +1945,8 @@ describe('CdkDrag', () => {
currentIndex: 3,
item: groups[0][1],
container: dropInstances[1],
previousContainer: dropInstances[0]
previousContainer: dropInstances[0],
isPointerOverContainer: true
});
}));

Expand Down Expand Up @@ -1927,7 +1975,8 @@ describe('CdkDrag', () => {
currentIndex: 1,
item: groups[0][1],
container: dropInstances[0],
previousContainer: dropInstances[0]
previousContainer: dropInstances[0],
isPointerOverContainer: false
});
}));

Expand Down Expand Up @@ -1956,7 +2005,8 @@ describe('CdkDrag', () => {
currentIndex: 1,
item: groups[0][1],
container: dropInstances[0],
previousContainer: dropInstances[0]
previousContainer: dropInstances[0],
isPointerOverContainer: false
});
}));

Expand Down Expand Up @@ -2078,7 +2128,8 @@ describe('CdkDrag', () => {
currentIndex: 3,
item: groups[0][1],
container: dropInstances[1],
previousContainer: dropInstances[0]
previousContainer: dropInstances[0],
isPointerOverContainer: true
});
}));

Expand All @@ -2103,7 +2154,8 @@ describe('CdkDrag', () => {
currentIndex: 3,
item: groups[0][1],
container: dropInstances[1],
previousContainer: dropInstances[0]
previousContainer: dropInstances[0],
isPointerOverContainer: true
});
}));

Expand Down Expand Up @@ -2133,7 +2185,8 @@ describe('CdkDrag', () => {
currentIndex: 3,
item: groups[0][1],
container: dropInstances[1],
previousContainer: dropInstances[0]
previousContainer: dropInstances[0],
isPointerOverContainer: true
});
}));

Expand Down Expand Up @@ -2167,7 +2220,8 @@ describe('CdkDrag', () => {
currentIndex: 0,
item,
container: fixture.componentInstance.dropInstances.toArray()[1],
previousContainer: fixture.componentInstance.dropInstances.first
previousContainer: fixture.componentInstance.dropInstances.first,
isPointerOverContainer: true
});

expect(dropContainers[0].contains(item.element.nativeElement)).toBe(true,
Expand Down Expand Up @@ -2656,7 +2710,7 @@ function dragElementViaMouse(fixture: ComponentFixture<any>,
dispatchMouseEvent(document, 'mousemove', x, y);
fixture.detectChanges();

dispatchMouseEvent(document, 'mouseup');
dispatchMouseEvent(document, 'mouseup', x, y);
fixture.detectChanges();
}

Expand Down Expand Up @@ -2695,7 +2749,7 @@ function dragElementViaTouch(fixture: ComponentFixture<any>,
dispatchTouchEvent(document, 'touchmove', x, y);
fixture.detectChanges();

dispatchTouchEvent(document, 'touchend');
dispatchTouchEvent(document, 'touchend', x, y);
fixture.detectChanges();
}

Expand Down
21 changes: 12 additions & 9 deletions src/cdk/drag-drop/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
}

/** Handler that is invoked when the user lifts their pointer up, after initiating a drag. */
private _pointerUp = () => {
private _pointerUp = (event: MouseEvent | TouchEvent) => {
if (!this._isDragging()) {
return;
}
Expand All @@ -539,13 +539,13 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
}

this._animatePreviewToPlaceholder().then(() => {
this._cleanupDragArtifacts();
this._cleanupDragArtifacts(event);
this._dragDropRegistry.stopDragging(this);
});
}

/** Cleans up the DOM artifacts that were added to facilitate the element being dragged. */
private _cleanupDragArtifacts() {
private _cleanupDragArtifacts(event: MouseEvent | TouchEvent) {
// Restore the element's visibility and insert it at its old position in the DOM.
// It's important that we maintain the position, because moving the element around in the DOM
// can throw off `NgFor` which does smart diffing and re-creates elements only when necessary,
Expand All @@ -564,16 +564,19 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
// Re-enter the NgZone since we bound `document` events on the outside.
this._ngZone.run(() => {
const currentIndex = this.dropContainer.getItemIndex(this);
const {x, y} = this._getPointerPositionOnPage(event);
const isPointerOverContainer = this.dropContainer._isOverContainer(x, y);

this.ended.emit({source: this});
this.dropped.emit({
item: this,
currentIndex,
previousIndex: this._initialContainer.getItemIndex(this),
container: this.dropContainer,
previousContainer: this._initialContainer
previousContainer: this._initialContainer,
isPointerOverContainer
});
this.dropContainer.drop(this, currentIndex, this._initialContainer);
this.dropContainer.drop(this, currentIndex, this._initialContainer, isPointerOverContainer);
this.dropContainer = this._initialContainer;
});
}
Expand All @@ -587,11 +590,11 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
let newContainer = this.dropContainer._getSiblingContainerFromPosition(this, x, y);

// If we couldn't find a new container to move the item into, and the item has left it's
// initial container, check whether the it's allowed to return into its original container.
// This handles the case where two containers are connected one way and the user tries to
// undo dragging an item into a new container.
// initial container, check whether the it's over the initial container. This handles the
// case where two containers are connected one way and the user tries to undo dragging an
// item into a new container.
if (!newContainer && this.dropContainer !== this._initialContainer &&
this._initialContainer._canReturnItem(x, y)) {
this._initialContainer._isOverContainer(x, y)) {
newContainer = this._initialContainer;
}

Expand Down
7 changes: 5 additions & 2 deletions src/cdk/drag-drop/drop-list-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export interface CdkDropListContainer<T = any> {
* @param item Item being dropped into the container.
* @param currentIndex Index at which the item should be inserted.
* @param previousContainer Container from which the item got dragged in.
* @param isPointerOverContainer Whether the user's pointer was over the
* container when the item was dropped.
*/
drop(item: CdkDrag, currentIndex: number, previousContainer?: CdkDropListContainer): void;
drop(item: CdkDrag, currentIndex: number, previousContainer: CdkDropListContainer,
isPointerOverContainer: boolean): void;

/**
* Emits an event to indicate that the user moved an item into the container.
Expand All @@ -63,7 +66,7 @@ export interface CdkDropListContainer<T = any> {
_draggables: QueryList<CdkDrag>;
_getSiblingContainerFromPosition(item: CdkDrag, x: number, y: number):
CdkDropListContainer | null;
_canReturnItem(x: number, y: number): boolean;
_isOverContainer(x: number, y: number): boolean;
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/cdk/drag-drop/drop-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,19 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
* @param item Item being dropped into the container.
* @param currentIndex Index at which the item should be inserted.
* @param previousContainer Container from which the item got dragged in.
* @param isPointerOverContainer Whether the user's pointer was over the
* container when the item was dropped.
*/
drop(item: CdkDrag, currentIndex: number, previousContainer: CdkDropList): void {
drop(item: CdkDrag, currentIndex: number, previousContainer: CdkDropList,
isPointerOverContainer: boolean): void {
this._reset();
this.dropped.emit({
item,
currentIndex,
previousIndex: previousContainer.getItemIndex(item),
container: this,
// TODO(crisbeto): reconsider whether to make this null if the containers are the same.
previousContainer
previousContainer,
isPointerOverContainer
});
}

Expand Down Expand Up @@ -386,12 +389,11 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
}

/**
* Checks whether an item that started in this container can be returned to it,
* after it was moved out into another container.
* @param x Position of the item along the X axis.
* @param y Position of the item along the Y axis.
* Checks whether the user's pointer is positioned over the container.
* @param x Pointer position along the X axis.
* @param y Pointer position along the Y axis.
*/
_canReturnItem(x: number, y: number): boolean {
_isOverContainer(x: number, y: number): boolean {
return isInsideClientRect(this._positionCache.self, x, y);
}

Expand Down
9 changes: 5 additions & 4 deletions tools/public_api_guard/cdk/drag-drop.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface CdkDragConfig {
export interface CdkDragDrop<T, O = T> {
container: CdkDropListContainer<T>;
currentIndex: number;
isPointerOverContainer: boolean;
item: CdkDrag;
previousContainer: CdkDropListContainer<O>;
previousIndex: number;
Expand Down Expand Up @@ -119,13 +120,13 @@ export declare class CdkDropList<T = any> implements OnInit, OnDestroy {
orientation: 'horizontal' | 'vertical';
sorted: EventEmitter<CdkDragSortEvent<T>>;
constructor(element: ElementRef<HTMLElement>, _dragDropRegistry: DragDropRegistry<CdkDrag, CdkDropList<T>>, _changeDetectorRef: ChangeDetectorRef, _dir?: Directionality | undefined, _group?: CdkDropListGroup<CdkDropList<any>> | undefined);
_canReturnItem(x: number, y: number): boolean;
_getSiblingContainerFromPosition(item: CdkDrag, x: number, y: number): CdkDropList | null;
_isOverContainer(x: number, y: number): boolean;
_sortItem(item: CdkDrag, pointerX: number, pointerY: number, pointerDelta: {
x: number;
y: number;
}): void;
drop(item: CdkDrag, currentIndex: number, previousContainer: CdkDropList): void;
drop(item: CdkDrag, currentIndex: number, previousContainer: CdkDropList, isPointerOverContainer: boolean): void;
enter(item: CdkDrag, pointerX: number, pointerY: number): void;
exit(item: CdkDrag): void;
getItemIndex(item: CdkDrag): number;
Expand All @@ -142,13 +143,13 @@ export interface CdkDropListContainer<T = any> {
id: string;
lockAxis: 'x' | 'y';
orientation: 'horizontal' | 'vertical';
_canReturnItem(x: number, y: number): boolean;
_getSiblingContainerFromPosition(item: CdkDrag, x: number, y: number): CdkDropListContainer | null;
_isOverContainer(x: number, y: number): boolean;
_sortItem(item: CdkDrag, pointerX: number, pointerY: number, delta: {
x: number;
y: number;
}): void;
drop(item: CdkDrag, currentIndex: number, previousContainer?: CdkDropListContainer): void;
drop(item: CdkDrag, currentIndex: number, previousContainer: CdkDropListContainer, isPointerOverContainer: boolean): void;
enter(item: CdkDrag, pointerX: number, pointerY: number): void;
exit(item: CdkDrag): void;
getItemIndex(item: CdkDrag): number;
Expand Down