@angular/cdk 12.1.0-rc.0 → 12.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -197,7 +197,7 @@ class ParentPositionTracker {
197
197
  }
198
198
  /** Handles scrolling while a drag is taking place. */
199
199
  handleScroll(event) {
200
- const target = event.target;
200
+ const target = getEventTarget(event);
201
201
  const cachedPosition = this.positions.get(target);
202
202
  if (!cachedPosition) {
203
203
  return null;
@@ -232,6 +232,10 @@ class ParentPositionTracker {
232
232
  return { top: topDifference, left: leftDifference };
233
233
  }
234
234
  }
235
+ /** Gets the target of an event while accounting for shadow dom. */
236
+ function getEventTarget(event) {
237
+ return (event.composedPath ? event.composedPath()[0] : event.target);
238
+ }
235
239
 
236
240
  /**
237
241
  * @license
@@ -391,7 +395,7 @@ class DragRef {
391
395
  // Delegate the event based on whether it started from a handle or the element itself.
392
396
  if (this._handles.length) {
393
397
  const targetHandle = this._handles.find(handle => {
394
- const target = event.target;
398
+ const target = getEventTarget(event);
395
399
  return !!target && (target === handle || handle.contains(target));
396
400
  });
397
401
  if (targetHandle && !this._disabledHandles.has(targetHandle) && !this.disabled) {
@@ -830,6 +834,7 @@ class DragRef {
830
834
  const isTouchSequence = isTouchEvent(event);
831
835
  const isAuxiliaryMouseButton = !isTouchSequence && event.button !== 0;
832
836
  const rootElement = this._rootElement;
837
+ const target = getEventTarget(event);
833
838
  const isSyntheticEvent = !isTouchSequence && this._lastTouchEventTime &&
834
839
  this._lastTouchEventTime + MOUSE_EVENT_IGNORE_TIME > Date.now();
835
840
  // If the event started from an element with the native HTML drag&drop, it'll interfere
@@ -838,7 +843,7 @@ class DragRef {
838
843
  // it's flaky and it fails if the user drags it away quickly. Also note that we only want
839
844
  // to do this for `mousedown` since doing the same for `touchstart` will stop any `click`
840
845
  // events from firing on touch devices.
841
- if (event.target && event.target.draggable && event.type === 'mousedown') {
846
+ if (target && target.draggable && event.type === 'mousedown') {
842
847
  event.preventDefault();
843
848
  }
844
849
  // Abort if the user is already dragging or is using a mouse button other than the primary one.
@@ -858,9 +863,9 @@ class DragRef {
858
863
  this._removeSubscriptions();
859
864
  this._pointerMoveSubscription = this._dragDropRegistry.pointerMove.subscribe(this._pointerMove);
860
865
  this._pointerUpSubscription = this._dragDropRegistry.pointerUp.subscribe(this._pointerUp);
861
- this._scrollSubscription = this._dragDropRegistry.scroll.subscribe(scrollEvent => {
862
- this._updateOnScroll(scrollEvent);
863
- });
866
+ this._scrollSubscription = this._dragDropRegistry
867
+ .scrolled(this._getShadowRoot())
868
+ .subscribe(scrollEvent => this._updateOnScroll(scrollEvent));
864
869
  if (this._boundaryElement) {
865
870
  this._boundaryRect = getMutableClientRect(this._boundaryElement);
866
871
  }
@@ -1033,7 +1038,8 @@ class DragRef {
1033
1038
  return this._ngZone.runOutsideAngular(() => {
1034
1039
  return new Promise(resolve => {
1035
1040
  const handler = ((event) => {
1036
- if (!event || (event.target === this._preview && event.propertyName === 'transform')) {
1041
+ if (!event || (getEventTarget(event) === this._preview &&
1042
+ event.propertyName === 'transform')) {
1037
1043
  this._preview.removeEventListener('transitionend', handler);
1038
1044
  resolve();
1039
1045
  clearTimeout(timeout);
@@ -1281,7 +1287,7 @@ class DragRef {
1281
1287
  _updateOnScroll(event) {
1282
1288
  const scrollDifference = this._parentPositions.handleScroll(event);
1283
1289
  if (scrollDifference) {
1284
- const target = event.target;
1290
+ const target = getEventTarget(event);
1285
1291
  // ClientRect dimensions are based on the scroll position of the page and its parent node so
1286
1292
  // we have to update the cached boundary ClientRect if the user has scrolled. Check for
1287
1293
  // the `document` specifically since IE doesn't support `contains` on it.
@@ -2137,7 +2143,9 @@ class DropListRef {
2137
2143
  * Used for updating the internal state of the list.
2138
2144
  */
2139
2145
  _listenToScrollEvents() {
2140
- this._viewportScrollSubscription = this._dragDropRegistry.scroll.subscribe(event => {
2146
+ this._viewportScrollSubscription = this._dragDropRegistry
2147
+ .scrolled(this._getShadowRoot())
2148
+ .subscribe(event => {
2141
2149
  if (this.isDragging()) {
2142
2150
  const scrollDifference = this._parentPositions.handleScroll(event);
2143
2151
  if (scrollDifference) {
@@ -2345,7 +2353,11 @@ class DragDropRegistry {
2345
2353
  * while the user is dragging a drag item instance.
2346
2354
  */
2347
2355
  this.pointerUp = new Subject();
2348
- /** Emits when the viewport has been scrolled while the user is dragging an item. */
2356
+ /**
2357
+ * Emits when the viewport has been scrolled while the user is dragging an item.
2358
+ * @deprecated To be turned into a private member. Use the `scrolled` method instead.
2359
+ * @breaking-change 13.0.0
2360
+ */
2349
2361
  this.scroll = new Subject();
2350
2362
  /**
2351
2363
  * Event listener that will prevent the default browser action while the user is dragging.
@@ -2466,6 +2478,36 @@ class DragDropRegistry {
2466
2478
  isDragging(drag) {
2467
2479
  return this._activeDragInstances.indexOf(drag) > -1;
2468
2480
  }
2481
+ /**
2482
+ * Gets a stream that will emit when any element on the page is scrolled while an item is being
2483
+ * dragged.
2484
+ * @param shadowRoot Optional shadow root that the current dragging sequence started from.
2485
+ * Top-level listeners won't pick up events coming from the shadow DOM so this parameter can
2486
+ * be used to include an additional top-level listener at the shadow root level.
2487
+ */
2488
+ scrolled(shadowRoot) {
2489
+ const streams = [this.scroll];
2490
+ if (shadowRoot && shadowRoot !== this._document) {
2491
+ // Note that this is basically the same as `fromEvent` from rjxs, but we do it ourselves,
2492
+ // because we want to guarantee that the event is bound outside of the `NgZone`. With
2493
+ // `fromEvent` it'll only happen if the subscription is outside the `NgZone`.
2494
+ streams.push(new Observable((observer) => {
2495
+ return this._ngZone.runOutsideAngular(() => {
2496
+ const eventOptions = true;
2497
+ const callback = (event) => {
2498
+ if (this._activeDragInstances.length) {
2499
+ observer.next(event);
2500
+ }
2501
+ };
2502
+ shadowRoot.addEventListener('scroll', callback, eventOptions);
2503
+ return () => {
2504
+ shadowRoot.removeEventListener('scroll', callback, eventOptions);
2505
+ };
2506
+ });
2507
+ }));
2508
+ }
2509
+ return merge(...streams);
2510
+ }
2469
2511
  ngOnDestroy() {
2470
2512
  this._dragInstances.forEach(instance => this.removeDragItem(instance));
2471
2513
  this._dropInstances.forEach(instance => this.removeDropContainer(instance));