Skip to content

refactor(drag-drop): expose drag ref in constrainPosition function #16465

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
Jul 9, 2019
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
8 changes: 5 additions & 3 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {of as observableOf} from 'rxjs';

import {DragDropModule} from '../drag-drop-module';
import {CdkDragDrop, CdkDragEnter} from '../drag-events';
import {DragRefConfig, Point} from '../drag-ref';
import {DragRefConfig, Point, DragRef} from '../drag-ref';
import {extendStyles} from '../drag-styling';
import {moveItemInArray} from '../drag-utils';

Expand Down Expand Up @@ -751,7 +751,8 @@ describe('CdkDrag', () => {
expect(dragElement.style.transform).toBeFalsy();
dragElementViaMouse(fixture, dragElement, 300, 300);

expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({x: 300, y: 300}));
expect(spy)
.toHaveBeenCalledWith(jasmine.objectContaining({x: 300, y: 300}), jasmine.any(DragRef));
expect(dragElement.style.transform).toBe('translate3d(50px, 50px, 0px)');
}));

Expand Down Expand Up @@ -2338,7 +2339,8 @@ describe('CdkDrag', () => {

const previewRect = preview.getBoundingClientRect();

expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({x: 200, y: 200}));
expect(spy)
.toHaveBeenCalledWith(jasmine.objectContaining({x: 200, y: 200}), jasmine.any(DragRef));
expect(Math.floor(previewRect.top)).toBe(50);
expect(Math.floor(previewRect.left)).toBe(50);
}));
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
* of the user's pointer on the page and should return a point describing where the item should
* be rendered.
*/
@Input('cdkDragConstrainPosition') constrainPosition?: (point: Point) => Point;
@Input('cdkDragConstrainPosition') constrainPosition?: (point: Point, dragRef: DragRef) => Point;

/** Emits when the user starts dragging the item. */
@Output('cdkDragStarted') started: EventEmitter<CdkDragStart> = new EventEmitter<CdkDragStart>();
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class DragRef<T = any> {
* of the user's pointer on the page and should return a point describing where the item should
* be rendered.
*/
constrainPosition?: (point: Point) => Point;
constrainPosition?: (point: Point, dragRef: DragRef) => Point;

constructor(
element: ElementRef<HTMLElement> | HTMLElement,
Expand Down Expand Up @@ -940,7 +940,7 @@ export class DragRef<T = any> {
/** Gets the pointer position on the page, accounting for any position constraints. */
private _getConstrainedPointerPosition(event: MouseEvent | TouchEvent): Point {
const point = this._getPointerPositionOnPage(event);
const constrainedPoint = this.constrainPosition ? this.constrainPosition(point) : point;
const constrainedPoint = this.constrainPosition ? this.constrainPosition(point, this) : point;
const dropContainerLock = this._dropContainer ? this._dropContainer.lockAxis : null;

if (this.lockAxis === 'x' || dropContainerLock === 'x') {
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/cdk/drag-drop.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export declare class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDes
_previewTemplate: CdkDragPreview;
boundaryElement: string | ElementRef<HTMLElement> | HTMLElement;
boundaryElementSelector: string;
constrainPosition?: (point: Point) => Point;
constrainPosition?: (point: Point, dragRef: DragRef) => Point;
data: T;
disabled: boolean;
dragStartDelay: number;
Expand Down Expand Up @@ -225,7 +225,7 @@ export declare class DragDropRegistry<I, C extends {

export declare class DragRef<T = any> {
beforeStarted: Subject<void>;
constrainPosition?: (point: Point) => Point;
constrainPosition?: (point: Point, dragRef: DragRef) => Point;
data: T;
disabled: boolean;
dragStartDelay: number;
Expand Down