From 4656c24f784a0efa7136b7138a88666d8b2da917 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 30 May 2023 10:57:55 +0200 Subject: [PATCH] fix(cdk/overlay): remove circular dependency workarounds (#27190) Removes the `OverlayReference` interface which was introduced as a workaround to avoid circular dependencies. Fixes #25650. --- .../dispatchers/base-overlay-dispatcher.ts | 8 ++-- .../overlay-keyboard-dispatcher.ts | 4 +- .../overlay-outside-click-dispatcher.ts | 4 +- src/cdk/overlay/overlay-ref.ts | 3 +- src/cdk/overlay/overlay-reference.ts | 40 ------------------- src/cdk/overlay/overlay.spec.ts | 5 +-- .../flexible-connected-position-strategy.ts | 6 +-- .../position/global-position-strategy.ts | 6 +-- src/cdk/overlay/position/position-strategy.ts | 4 +- .../overlay/scroll/close-scroll-strategy.ts | 6 +-- .../scroll/reposition-scroll-strategy.ts | 6 +-- src/cdk/overlay/scroll/scroll-strategy.ts | 4 +- tools/public_api_guard/cdk/overlay.md | 19 +++++---- 13 files changed, 36 insertions(+), 79 deletions(-) delete mode 100644 src/cdk/overlay/overlay-reference.ts diff --git a/src/cdk/overlay/dispatchers/base-overlay-dispatcher.ts b/src/cdk/overlay/dispatchers/base-overlay-dispatcher.ts index ae1f16bac734..3e513177043b 100644 --- a/src/cdk/overlay/dispatchers/base-overlay-dispatcher.ts +++ b/src/cdk/overlay/dispatchers/base-overlay-dispatcher.ts @@ -8,7 +8,7 @@ import {DOCUMENT} from '@angular/common'; import {Inject, Injectable, OnDestroy} from '@angular/core'; -import {OverlayReference} from '../overlay-reference'; +import type {OverlayRef} from '../overlay-ref'; /** * Service for dispatching events that land on the body to appropriate overlay ref, @@ -18,7 +18,7 @@ import {OverlayReference} from '../overlay-reference'; @Injectable({providedIn: 'root'}) export abstract class BaseOverlayDispatcher implements OnDestroy { /** Currently attached overlays in the order they were attached. */ - _attachedOverlays: OverlayReference[] = []; + _attachedOverlays: OverlayRef[] = []; protected _document: Document; protected _isAttached: boolean; @@ -32,14 +32,14 @@ export abstract class BaseOverlayDispatcher implements OnDestroy { } /** Add a new overlay to the list of attached overlay refs. */ - add(overlayRef: OverlayReference): void { + add(overlayRef: OverlayRef): void { // Ensure that we don't get the same overlay multiple times. this.remove(overlayRef); this._attachedOverlays.push(overlayRef); } /** Remove an overlay from the list of attached overlay refs. */ - remove(overlayRef: OverlayReference): void { + remove(overlayRef: OverlayRef): void { const index = this._attachedOverlays.indexOf(overlayRef); if (index > -1) { diff --git a/src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.ts b/src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.ts index bc3a528f82cf..5fd90dbe58c8 100644 --- a/src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.ts +++ b/src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.ts @@ -8,8 +8,8 @@ import {DOCUMENT} from '@angular/common'; import {Inject, Injectable, NgZone, Optional} from '@angular/core'; -import {OverlayReference} from '../overlay-reference'; import {BaseOverlayDispatcher} from './base-overlay-dispatcher'; +import type {OverlayRef} from '../overlay-ref'; /** * Service for dispatching keyboard events that land on the body to appropriate overlay ref, @@ -27,7 +27,7 @@ export class OverlayKeyboardDispatcher extends BaseOverlayDispatcher { } /** Add a new overlay to the list of attached overlay refs. */ - override add(overlayRef: OverlayReference): void { + override add(overlayRef: OverlayRef): void { super.add(overlayRef); // Lazily start dispatcher once first overlay is added diff --git a/src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.ts b/src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.ts index a70512502457..2791679dadfe 100644 --- a/src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.ts +++ b/src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.ts @@ -8,9 +8,9 @@ import {DOCUMENT} from '@angular/common'; import {Inject, Injectable, NgZone, Optional} from '@angular/core'; -import {OverlayReference} from '../overlay-reference'; import {Platform, _getEventTarget} from '@angular/cdk/platform'; import {BaseOverlayDispatcher} from './base-overlay-dispatcher'; +import type {OverlayRef} from '../overlay-ref'; /** * Service for dispatching mouse click events that land on the body to appropriate overlay ref, @@ -33,7 +33,7 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher { } /** Add a new overlay to the list of attached overlay refs. */ - override add(overlayRef: OverlayReference): void { + override add(overlayRef: OverlayRef): void { super.add(overlayRef); // Safari on iOS does not generate click events for non-interactive diff --git a/src/cdk/overlay/overlay-ref.ts b/src/cdk/overlay/overlay-ref.ts index d0347057913c..da45d31d5df8 100644 --- a/src/cdk/overlay/overlay-ref.ts +++ b/src/cdk/overlay/overlay-ref.ts @@ -16,7 +16,6 @@ import {OverlayKeyboardDispatcher} from './dispatchers/overlay-keyboard-dispatch import {OverlayOutsideClickDispatcher} from './dispatchers/overlay-outside-click-dispatcher'; import {OverlayConfig} from './overlay-config'; import {coerceCssPixelValue, coerceArray} from '@angular/cdk/coercion'; -import {OverlayReference} from './overlay-reference'; import {PositionStrategy} from './position/position-strategy'; import {ScrollStrategy} from './scroll'; @@ -29,7 +28,7 @@ export type ImmutableObject = { * Reference to an overlay that has been created with the Overlay service. * Used to manipulate or dispose of said overlay. */ -export class OverlayRef implements PortalOutlet, OverlayReference { +export class OverlayRef implements PortalOutlet { private _backdropElement: HTMLElement | null = null; private _backdropTimeout: number | undefined; private readonly _backdropClick = new Subject(); diff --git a/src/cdk/overlay/overlay-reference.ts b/src/cdk/overlay/overlay-reference.ts deleted file mode 100644 index 636e2910d133..000000000000 --- a/src/cdk/overlay/overlay-reference.ts +++ /dev/null @@ -1,40 +0,0 @@ -/** - * @license - * Copyright Google LLC All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.io/license - */ - -import {Portal} from '@angular/cdk/portal'; -import {Direction, Directionality} from '@angular/cdk/bidi'; -import {Observable, Subject} from 'rxjs'; - -/** - * Basic interface for an overlay. Used to avoid circular type references between - * `OverlayRef`, `PositionStrategy` and `ScrollStrategy`, and `OverlayConfig`. - * @docs-private - */ -export interface OverlayReference { - attach: (portal: Portal) => any; - detach: () => any; - dispose: () => void; - overlayElement: HTMLElement; - hostElement: HTMLElement; - backdropElement: HTMLElement | null; - getConfig: () => any; - hasAttached: () => boolean; - updateSize: (config: any) => void; - updatePosition: () => void; - getDirection: () => Direction; - setDirection: (dir: Direction | Directionality) => void; - backdropClick: () => Observable; - attachments: () => Observable; - detachments: () => Observable; - keydownEvents: () => Observable; - outsidePointerEvents: () => Observable; - addPanelClass: (classes: string | string[]) => void; - removePanelClass: (classes: string | string[]) => void; - readonly _outsidePointerEvents: Subject; - readonly _keydownEvents: Subject; -} diff --git a/src/cdk/overlay/overlay.spec.ts b/src/cdk/overlay/overlay.spec.ts index bfa3355a45c6..153e71e4aca0 100644 --- a/src/cdk/overlay/overlay.spec.ts +++ b/src/cdk/overlay/overlay.spec.ts @@ -30,7 +30,6 @@ import { PositionStrategy, ScrollStrategy, } from './index'; -import {OverlayReference} from './overlay-reference'; import {NoopAnimationsModule} from '@angular/platform-browser/animations'; describe('Overlay', () => { @@ -1148,9 +1147,9 @@ class FakePositionStrategy implements PositionStrategy { class FakeScrollStrategy implements ScrollStrategy { isEnabled = false; - overlayRef: OverlayReference; + overlayRef: OverlayRef; - attach(overlayRef: OverlayReference) { + attach(overlayRef: OverlayRef) { this.overlayRef = overlayRef; } diff --git a/src/cdk/overlay/position/flexible-connected-position-strategy.ts b/src/cdk/overlay/position/flexible-connected-position-strategy.ts index fc9103663a26..435c0c04c870 100644 --- a/src/cdk/overlay/position/flexible-connected-position-strategy.ts +++ b/src/cdk/overlay/position/flexible-connected-position-strategy.ts @@ -17,11 +17,11 @@ import { validateVerticalPosition, } from './connected-position'; import {Observable, Subscription, Subject} from 'rxjs'; -import {OverlayReference} from '../overlay-reference'; import {isElementScrolledOutsideView, isElementClippedByScrolling} from './scroll-clip'; import {coerceCssPixelValue, coerceArray} from '@angular/cdk/coercion'; import {Platform} from '@angular/cdk/platform'; import {OverlayContainer} from '../overlay-container'; +import {OverlayRef} from '../overlay-ref'; // TODO: refactor clipping detection into a separate thing (part of scrolling module) // TODO: doesn't handle both flexible width and height when it has to scroll along both axis. @@ -53,7 +53,7 @@ type Dimensions = Omit; */ export class FlexibleConnectedPositionStrategy implements PositionStrategy { /** The overlay to which this strategy is attached. */ - private _overlayRef: OverlayReference; + private _overlayRef: OverlayRef; /** Whether we're performing the very first positioning of the overlay. */ private _isInitialRender: boolean; @@ -155,7 +155,7 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { } /** Attaches this position strategy to an overlay. */ - attach(overlayRef: OverlayReference): void { + attach(overlayRef: OverlayRef): void { if ( this._overlayRef && overlayRef !== this._overlayRef && diff --git a/src/cdk/overlay/position/global-position-strategy.ts b/src/cdk/overlay/position/global-position-strategy.ts index 3eebf2498304..88d0c87fc4c5 100644 --- a/src/cdk/overlay/position/global-position-strategy.ts +++ b/src/cdk/overlay/position/global-position-strategy.ts @@ -6,8 +6,8 @@ * found in the LICENSE file at https://angular.io/license */ +import {OverlayRef} from '../overlay-ref'; import {PositionStrategy} from './position-strategy'; -import {OverlayReference} from '../overlay-reference'; /** Class to be added to the overlay pane wrapper. */ const wrapperClass = 'cdk-global-overlay-wrapper'; @@ -20,7 +20,7 @@ const wrapperClass = 'cdk-global-overlay-wrapper'; */ export class GlobalPositionStrategy implements PositionStrategy { /** The overlay to which this strategy is attached. */ - private _overlayRef: OverlayReference; + private _overlayRef: OverlayRef; private _cssPosition = 'static'; private _topOffset = ''; private _bottomOffset = ''; @@ -31,7 +31,7 @@ export class GlobalPositionStrategy implements PositionStrategy { private _height = ''; private _isDisposed = false; - attach(overlayRef: OverlayReference): void { + attach(overlayRef: OverlayRef): void { const config = overlayRef.getConfig(); this._overlayRef = overlayRef; diff --git a/src/cdk/overlay/position/position-strategy.ts b/src/cdk/overlay/position/position-strategy.ts index a32c68872094..37b5e309421d 100644 --- a/src/cdk/overlay/position/position-strategy.ts +++ b/src/cdk/overlay/position/position-strategy.ts @@ -6,12 +6,12 @@ * found in the LICENSE file at https://angular.io/license */ -import {OverlayReference} from '../overlay-reference'; +import type {OverlayRef} from '../overlay-ref'; /** Strategy for setting the position on an overlay. */ export interface PositionStrategy { /** Attaches this position strategy to an overlay. */ - attach(overlayRef: OverlayReference): void; + attach(overlayRef: OverlayRef): void; /** Updates the position of the overlay element. */ apply(): void; diff --git a/src/cdk/overlay/scroll/close-scroll-strategy.ts b/src/cdk/overlay/scroll/close-scroll-strategy.ts index 03009268c847..dddcb123f206 100644 --- a/src/cdk/overlay/scroll/close-scroll-strategy.ts +++ b/src/cdk/overlay/scroll/close-scroll-strategy.ts @@ -7,10 +7,10 @@ */ import {NgZone} from '@angular/core'; import {ScrollStrategy, getMatScrollStrategyAlreadyAttachedError} from './scroll-strategy'; -import {OverlayReference} from '../overlay-reference'; import {Subscription} from 'rxjs'; import {ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling'; import {filter} from 'rxjs/operators'; +import type {OverlayRef} from '../overlay-ref'; /** * Config options for the CloseScrollStrategy. @@ -25,7 +25,7 @@ export interface CloseScrollStrategyConfig { */ export class CloseScrollStrategy implements ScrollStrategy { private _scrollSubscription: Subscription | null = null; - private _overlayRef: OverlayReference; + private _overlayRef: OverlayRef; private _initialScrollPosition: number; constructor( @@ -36,7 +36,7 @@ export class CloseScrollStrategy implements ScrollStrategy { ) {} /** Attaches this scroll strategy to an overlay. */ - attach(overlayRef: OverlayReference) { + attach(overlayRef: OverlayRef) { if (this._overlayRef && (typeof ngDevMode === 'undefined' || ngDevMode)) { throw getMatScrollStrategyAlreadyAttachedError(); } diff --git a/src/cdk/overlay/scroll/reposition-scroll-strategy.ts b/src/cdk/overlay/scroll/reposition-scroll-strategy.ts index ba7ac19322cd..b3d5d90e1f49 100644 --- a/src/cdk/overlay/scroll/reposition-scroll-strategy.ts +++ b/src/cdk/overlay/scroll/reposition-scroll-strategy.ts @@ -9,9 +9,9 @@ import {NgZone} from '@angular/core'; import {Subscription} from 'rxjs'; import {ScrollStrategy, getMatScrollStrategyAlreadyAttachedError} from './scroll-strategy'; -import {OverlayReference} from '../overlay-reference'; import {ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling'; import {isElementScrolledOutsideView} from '../position/scroll-clip'; +import type {OverlayRef} from '../overlay-ref'; /** * Config options for the RepositionScrollStrategy. @@ -29,7 +29,7 @@ export interface RepositionScrollStrategyConfig { */ export class RepositionScrollStrategy implements ScrollStrategy { private _scrollSubscription: Subscription | null = null; - private _overlayRef: OverlayReference; + private _overlayRef: OverlayRef; constructor( private _scrollDispatcher: ScrollDispatcher, @@ -39,7 +39,7 @@ export class RepositionScrollStrategy implements ScrollStrategy { ) {} /** Attaches this scroll strategy to an overlay. */ - attach(overlayRef: OverlayReference) { + attach(overlayRef: OverlayRef) { if (this._overlayRef && (typeof ngDevMode === 'undefined' || ngDevMode)) { throw getMatScrollStrategyAlreadyAttachedError(); } diff --git a/src/cdk/overlay/scroll/scroll-strategy.ts b/src/cdk/overlay/scroll/scroll-strategy.ts index 24b2c2944a92..ed0139b9d9f8 100644 --- a/src/cdk/overlay/scroll/scroll-strategy.ts +++ b/src/cdk/overlay/scroll/scroll-strategy.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {OverlayReference} from '../overlay-reference'; +import type {OverlayRef} from '../overlay-ref'; /** * Describes a strategy that will be used by an overlay to handle scroll events while it is open. @@ -19,7 +19,7 @@ export interface ScrollStrategy { disable: () => void; /** Attaches this `ScrollStrategy` to an overlay. */ - attach: (overlayRef: OverlayReference) => void; + attach: (overlayRef: OverlayRef) => void; /** Detaches the scroll strategy from the current overlay. */ detach?: () => void; diff --git a/tools/public_api_guard/cdk/overlay.md b/tools/public_api_guard/cdk/overlay.md index 37b73ca6af12..a5a0111018c9 100644 --- a/tools/public_api_guard/cdk/overlay.md +++ b/tools/public_api_guard/cdk/overlay.md @@ -27,7 +27,6 @@ import { Observable } from 'rxjs'; import { OnChanges } from '@angular/core'; import { OnDestroy } from '@angular/core'; import { Platform } from '@angular/cdk/platform'; -import { Portal } from '@angular/cdk/portal'; import { PortalOutlet } from '@angular/cdk/portal'; import { ScrollDispatcher } from '@angular/cdk/scrolling'; import { SimpleChanges } from '@angular/core'; @@ -110,7 +109,7 @@ export { CdkScrollable } // @public export class CloseScrollStrategy implements ScrollStrategy { constructor(_scrollDispatcher: ScrollDispatcher, _ngZone: NgZone, _viewportRuler: ViewportRuler, _config?: CloseScrollStrategyConfig | undefined); - attach(overlayRef: OverlayReference): void; + attach(overlayRef: OverlayRef): void; // (undocumented) detach(): void; disable(): void; @@ -167,7 +166,7 @@ export class ConnectionPositionPair { export class FlexibleConnectedPositionStrategy implements PositionStrategy { constructor(connectedTo: FlexibleConnectedPositionStrategyOrigin, _viewportRuler: ViewportRuler, _document: Document, _platform: Platform, _overlayContainer: OverlayContainer); apply(): void; - attach(overlayRef: OverlayReference): void; + attach(overlayRef: OverlayRef): void; // (undocumented) detach(): void; dispose(): void; @@ -213,7 +212,7 @@ export class FullscreenOverlayContainer extends OverlayContainer implements OnDe export class GlobalPositionStrategy implements PositionStrategy { apply(): void; // (undocumented) - attach(overlayRef: OverlayReference): void; + attach(overlayRef: OverlayRef): void; bottom(value?: string): this; centerHorizontally(offset?: string): this; centerVertically(offset?: string): this; @@ -309,7 +308,7 @@ export class OverlayContainer implements OnDestroy { export class OverlayKeyboardDispatcher extends BaseOverlayDispatcher { constructor(document: any, _ngZone?: NgZone | undefined); - add(overlayRef: OverlayReference): void; + add(overlayRef: OverlayRef): void; protected detach(): void; // (undocumented) static ɵfac: i0.ɵɵFactoryDeclaration; @@ -331,7 +330,7 @@ export class OverlayModule { export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher { constructor(document: any, _platform: Platform, _ngZone?: NgZone | undefined); - add(overlayRef: OverlayReference): void; + add(overlayRef: OverlayRef): void; protected detach(): void; // (undocumented) static ɵfac: i0.ɵɵFactoryDeclaration; @@ -351,7 +350,7 @@ export class OverlayPositionBuilder { } // @public -export class OverlayRef implements PortalOutlet, OverlayReference { +export class OverlayRef implements PortalOutlet { constructor(_portalOutlet: PortalOutlet, _host: HTMLElement, _pane: HTMLElement, _config: ImmutableObject, _ngZone: NgZone, _keyboardDispatcher: OverlayKeyboardDispatcher, _document: Document, _location: Location_2, _outsideClickDispatcher: OverlayOutsideClickDispatcher, _animationsDisabled?: boolean); addPanelClass(classes: string | string[]): void; // (undocumented) @@ -403,7 +402,7 @@ export interface OverlaySizeConfig { // @public export interface PositionStrategy { apply(): void; - attach(overlayRef: OverlayReference): void; + attach(overlayRef: OverlayRef): void; detach?(): void; dispose(): void; } @@ -411,7 +410,7 @@ export interface PositionStrategy { // @public export class RepositionScrollStrategy implements ScrollStrategy { constructor(_scrollDispatcher: ScrollDispatcher, _viewportRuler: ViewportRuler, _ngZone: NgZone, _config?: RepositionScrollStrategyConfig | undefined); - attach(overlayRef: OverlayReference): void; + attach(overlayRef: OverlayRef): void; // (undocumented) detach(): void; disable(): void; @@ -440,7 +439,7 @@ export class ScrollingVisibility { // @public export interface ScrollStrategy { - attach: (overlayRef: OverlayReference) => void; + attach: (overlayRef: OverlayRef) => void; detach?: () => void; disable: () => void; enable: () => void;