Skip to content

refactor(drag-drop): add sorted event #13887

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 10, 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
12 changes: 12 additions & 0 deletions src/cdk/drag-drop/drag-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ export interface CdkDragMove<T = any> {
*/
delta: {x: -1 | 0 | 1, y: -1 | 0 | 1};
}

/** Event emitted when the user swaps the position of two drag items. */
export interface CdkDragSortEvent<T = any, I = T> {
/** Index from which the item was sorted previously. */
previousIndex: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be the index since the last time a sort event or since the beginning of the drag?

Should be provide the previousContainer and currentContainer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's since the last sort. I don't know how useful the previousContainer will be in this case and it'll add some complexity since we don't have it easily available in _sortItem.

/** Index that the item is currently in. */
currentIndex: number;
/** Container that the item belongs to. */
container: CdkDropListContainer<T>;
/** Item that is being sorted. */
item: CdkDrag<I>;
}
32 changes: 32 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,36 @@ describe('CdkDrag', () => {
.toEqual(['One', 'Two', 'Zero', 'Three']);
}));

it('should dispatch the `sorted` event as an item is being sorted', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();

const items = fixture.componentInstance.dragItems.map(item => item.element.nativeElement);
const draggedItem = items[0];
const {top, left} = draggedItem.getBoundingClientRect();

startDraggingViaMouse(fixture, draggedItem, left, top);

// Drag over each item one-by-one going downwards.
for (let i = 1; i < items.length; i++) {
const elementRect = items[i].getBoundingClientRect();

dispatchMouseEvent(document, 'mousemove', elementRect.left, elementRect.top + 5);
fixture.detectChanges();

expect(fixture.componentInstance.sortedSpy.calls.mostRecent().args[0]).toEqual({
previousIndex: i - 1,
currentIndex: i,
item: fixture.componentInstance.dragItems.first,
container: fixture.componentInstance.dropInstance
});
}

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

it('should not move items in a vertical list if the pointer is too far away', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
Expand Down Expand Up @@ -2075,6 +2105,7 @@ const DROP_ZONE_FIXTURE_TEMPLATE = `
style="width: 100px; background: pink;"
[id]="dropZoneId"
[cdkDropListData]="items"
(cdkDropListSorted)="sortedSpy($event)"
(cdkDropListDropped)="droppedSpy($event)">
<div
*ngFor="let item of items"
Expand All @@ -2097,6 +2128,7 @@ class DraggableInDropZone {
{value: 'Three', height: ITEM_HEIGHT, margin: 0}
];
dropZoneId = 'items';
sortedSpy = jasmine.createSpy('sorted spy');
droppedSpy = jasmine.createSpy('dropped spy').and.callFake((event: CdkDragDrop<string[]>) => {
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
});
Expand Down
13 changes: 12 additions & 1 deletion src/cdk/drag-drop/drop-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import {Directionality} from '@angular/cdk/bidi';
import {CdkDrag} from './drag';
import {DragDropRegistry} from './drag-drop-registry';
import {CdkDragDrop, CdkDragEnter, CdkDragExit} from './drag-events';
import {CdkDragDrop, CdkDragEnter, CdkDragExit, CdkDragSortEvent} from './drag-events';
import {moveItemInArray} from './drag-utils';
import {CDK_DROP_LIST_CONTAINER} from './drop-list-container';

Expand Down Expand Up @@ -139,6 +139,10 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
@Output('cdkDropListExited')
exited: EventEmitter<CdkDragExit<T>> = new EventEmitter<CdkDragExit<T>>();

/** Emits as the user is swapping items while actively dragging. */
@Output('cdkDropListSorted')
sorted: EventEmitter<CdkDragSortEvent<T>> = new EventEmitter<CdkDragSortEvent<T>>();

constructor(
public element: ElementRef<HTMLElement>,
private _dragDropRegistry: DragDropRegistry<CdkDrag, CdkDropList<T>>,
Expand Down Expand Up @@ -311,6 +315,13 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
// Shuffle the array in place.
moveItemInArray(siblings, currentIndex, newIndex);

this.sorted.emit({
previousIndex: currentIndex,
currentIndex: newIndex,
container: this,
item
});

siblings.forEach((sibling, index) => {
// Don't do anything if the position hasn't changed.
if (oldOrder[index] === sibling) {
Expand Down