Skip to content

refactor(drag-drop): expose method for resetting the drag position #13673

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 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
26 changes: 26 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,32 @@ describe('CdkDrag', () => {
expect(dragElement.style.touchAction)
.not.toEqual('none', 'should not disable touchAction on when there is a drag handle');
});
it('should be able to reset a freely-dragged item to its initial position', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;

expect(dragElement.style.transform).toBeFalsy();
dragElementViaMouse(fixture, dragElement, 50, 100);
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');

fixture.componentInstance.dragInstance.reset();
expect(dragElement.style.transform).toBeFalsy();
}));

it('should start dragging an item from its initial position after a reset', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;

expect(dragElement.style.transform).toBeFalsy();
dragElementViaMouse(fixture, dragElement, 50, 100);
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
fixture.componentInstance.dragInstance.reset();

dragElementViaMouse(fixture, dragElement, 25, 50);
expect(dragElement.style.transform).toBe('translate3d(25px, 50px, 0px)');
}));

});

Expand Down
7 changes: 7 additions & 0 deletions src/cdk/drag-drop/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
return this._rootElement;
}

/** Resets a standalone drag item to its initial position. */
reset(): void {
this._rootElement.style.transform = '';
this._activeTransform = {x: 0, y: 0};
this._passiveTransform = {x: 0, y: 0};
}

ngAfterViewInit() {
// We need to wait for the zone to stabilize, in order for the reference
// element to be in the proper place in the DOM. This is mostly relevant
Expand Down