Skip to content

fix(drag-drop): allow for element in DropListRef to be changed #15091

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
May 13, 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
2 changes: 1 addition & 1 deletion src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ export class DragRef<T = any> {
if (this._nextSibling) {
this._nextSibling.parentNode!.insertBefore(this._rootElement, this._nextSibling);
} else {
this._initialContainer.element.appendChild(this._rootElement);
coerceElement(this._initialContainer.element).appendChild(this._rootElement);
}

this._destroyPreview();
Expand Down
13 changes: 8 additions & 5 deletions src/cdk/drag-drop/drop-list-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {ElementRef} from '@angular/core';
import {DragDropRegistry} from './drag-drop-registry';
import {Direction} from '@angular/cdk/bidi';
import {coerceElement} from '@angular/cdk/coercion';
import {Subject} from 'rxjs';
import {moveItemInArray} from './drag-utils';
import {DragRefInternal as DragRef} from './drag-ref';
Expand Down Expand Up @@ -51,7 +52,7 @@ export class DropListRef<T = any> {
private _document: Document;

/** Element that the drop list is attached to. */
readonly element: HTMLElement;
element: HTMLElement | ElementRef<HTMLElement>;

/**
* Unique ID for the drop list.
Expand Down Expand Up @@ -227,7 +228,7 @@ export class DropListRef<T = any> {
element.parentElement!.insertBefore(placeholder, element);
activeDraggables.splice(newIndex, 0, item);
} else {
this.element.appendChild(placeholder);
coerceElement(this.element).appendChild(placeholder);
activeDraggables.push(item);
}

Expand Down Expand Up @@ -413,7 +414,7 @@ export class DropListRef<T = any> {

/** Caches the position of the drop list. */
private _cacheOwnPosition() {
this._clientRect = this.element.getBoundingClientRect();
this._clientRect = coerceElement(this.element).getBoundingClientRect();
}

/** Refreshes the position cache of the items and sibling containers. */
Expand Down Expand Up @@ -597,21 +598,23 @@ export class DropListRef<T = any> {
return false;
}

const elementFromPoint = this._document.elementFromPoint(x, y);
const elementFromPoint = this._document.elementFromPoint(x, y) as HTMLElement | null;

// If there's no element at the pointer position, then
// the client rect is probably scrolled out of the view.
if (!elementFromPoint) {
return false;
}

const nativeElement = coerceElement(this.element);

// The `ClientRect`, that we're using to find the container over which the user is
// hovering, doesn't give us any information on whether the element has been scrolled
// out of the view or whether it's overlapping with other containers. This means that
// we could end up transferring the item into a container that's invisible or is positioned
// below another one. We use the result from `elementFromPoint` to get the top-most element
// at the pointer position and to find whether it's one of the intersecting drop containers.
return elementFromPoint === this.element || this.element.contains(elementFromPoint);
return elementFromPoint === nativeElement || nativeElement.contains(elementFromPoint);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/cdk/drag-drop.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export declare class DropListRef<T = any> {
previousContainer: DropListRef<any>;
isPointerOverContainer: boolean;
}>;
readonly element: HTMLElement;
element: HTMLElement | ElementRef<HTMLElement>;
enterPredicate: (drag: DragRef, drop: DropListRef) => boolean;
entered: Subject<{
item: DragRef;
Expand Down