Skip to content

fix(drag-drop): emitting incorrect index for horizontal list in rtl #13274

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
Sep 27, 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
39 changes: 39 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,45 @@ describe('CdkDrag', () => {
.toEqual(['One', 'Two', 'Zero', 'Three']);
}));

it('should dispatch the correct `dropped` event in RTL horizontal drop zone', fakeAsync(() => {
const fixture = createComponent(DraggableInHorizontalDropZone, [{
provide: Directionality,
useValue: ({value: 'rtl'})
}]);

fixture.nativeElement.setAttribute('dir', 'rtl');
fixture.detectChanges();
const dragItems = fixture.componentInstance.dragItems;

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

const firstItem = dragItems.first;
const thirdItemRect = dragItems.toArray()[2].element.nativeElement.getBoundingClientRect();

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

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

const event = fixture.componentInstance.droppedSpy.calls.mostRecent().args[0];

// Assert the event like this, rather than `toHaveBeenCalledWith`, because Jasmine will
// go into an infinite loop trying to stringify the event, if the test fails.
expect(event).toEqual({
previousIndex: 0,
currentIndex: 2,
item: firstItem,
container: fixture.componentInstance.dropInstance,
previousContainer: fixture.componentInstance.dropInstance
});

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

it('should not move items in a horizontal list if pointer is too far away', fakeAsync(() => {
const fixture = createComponent(DraggableInHorizontalDropZone);
fixture.detectChanges();
Expand Down
19 changes: 15 additions & 4 deletions src/cdk/drag-drop/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
Output,
QueryList,
ViewEncapsulation,
Optional,
} from '@angular/core';
import {Directionality} from '@angular/cdk/bidi';
import {CdkDrag} from './drag';
import {DragDropRegistry} from './drag-drop-registry';
import {CdkDragDrop, CdkDragEnter, CdkDragExit} from './drag-events';
Expand Down Expand Up @@ -103,7 +105,8 @@ export class CdkDrop<T = any> implements OnInit, OnDestroy {

constructor(
public element: ElementRef<HTMLElement>,
private _dragDropRegistry: DragDropRegistry<CdkDrag, CdkDrop<T>>) {}
private _dragDropRegistry: DragDropRegistry<CdkDrag, CdkDrop<T>>,
@Optional() private _dir?: Directionality) {}

ngOnInit() {
this._dragDropRegistry.registerDropContainer(this);
Expand Down Expand Up @@ -217,9 +220,17 @@ export class CdkDrop<T = any> implements OnInit, OnDestroy {
* @param item Item whose index should be determined.
*/
getItemIndex(item: CdkDrag): number {
return this._dragging ?
findIndex(this._positionCache.items, currentItem => currentItem.drag === item) :
this._draggables.toArray().indexOf(item);
if (!this._dragging) {
return this._draggables.toArray().indexOf(item);
}

// Items are sorted always by top/left in the cache, however they flow differently in RTL.
// The rest of the logic still stands no matter what orientation we're in, however
// we need to invert the array when determining the index.
const items = this.orientation === 'horizontal' && this._dir && this._dir.value === 'rtl' ?
this._positionCache.items.slice().reverse() : this._positionCache.items;
Copy link
Member

Choose a reason for hiding this comment

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

How often is getItemIndex called? I'm wondering if slice().reverse() might get expensive for large lists

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 called twice when an item is dropped to figure out its old index and the new one. I don't imagine it getting too expensive, unless we're dealing with a massive array.


return findIndex(items, currentItem => currentItem.drag === item);
}

/**
Expand Down