Skip to content

Commit

Permalink
fix(cdk/overlay): remove circular dependency workarounds (#27190)
Browse files Browse the repository at this point in the history
Removes the `OverlayReference` interface which was introduced as a workaround to avoid circular dependencies.

Fixes #25650.
  • Loading branch information
crisbeto committed May 30, 2023
1 parent 87dd8fd commit 4656c24
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 79 deletions.
8 changes: 4 additions & 4 deletions src/cdk/overlay/dispatchers/base-overlay-dispatcher.ts
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.ts
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Expand Up @@ -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,
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/cdk/overlay/overlay-ref.ts
Expand Up @@ -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';

Expand All @@ -29,7 +28,7 @@ export type ImmutableObject<T> = {
* 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<MouseEvent>();
Expand Down
40 changes: 0 additions & 40 deletions src/cdk/overlay/overlay-reference.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/cdk/overlay/overlay.spec.ts
Expand Up @@ -30,7 +30,6 @@ import {
PositionStrategy,
ScrollStrategy,
} from './index';
import {OverlayReference} from './overlay-reference';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';

describe('Overlay', () => {
Expand Down Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -53,7 +53,7 @@ type Dimensions = Omit<ClientRect, 'x' | 'y' | 'toJSON'>;
*/
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;
Expand Down Expand Up @@ -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 &&
Expand Down
6 changes: 3 additions & 3 deletions src/cdk/overlay/position/global-position-strategy.ts
Expand Up @@ -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';
Expand All @@ -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 = '';
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/overlay/position/position-strategy.ts
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/cdk/overlay/scroll/close-scroll-strategy.ts
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/cdk/overlay/scroll/reposition-scroll-strategy.ts
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/overlay/scroll/scroll-strategy.ts
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
19 changes: 9 additions & 10 deletions tools/public_api_guard/cdk/overlay.md
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<OverlayKeyboardDispatcher, [null, { optional: true; }]>;
Expand All @@ -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<OverlayOutsideClickDispatcher, [null, null, { optional: true; }]>;
Expand All @@ -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<OverlayConfig>, _ngZone: NgZone, _keyboardDispatcher: OverlayKeyboardDispatcher, _document: Document, _location: Location_2, _outsideClickDispatcher: OverlayOutsideClickDispatcher, _animationsDisabled?: boolean);
addPanelClass(classes: string | string[]): void;
// (undocumented)
Expand Down Expand Up @@ -403,15 +402,15 @@ export interface OverlaySizeConfig {
// @public
export interface PositionStrategy {
apply(): void;
attach(overlayRef: OverlayReference): void;
attach(overlayRef: OverlayRef): void;
detach?(): void;
dispose(): void;
}

// @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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4656c24

Please sign in to comment.