@@ -18,6 +18,7 @@ import {
18
18
EnvironmentInjector ,
19
19
inject ,
20
20
RendererFactory2 ,
21
+ Renderer2 ,
21
22
} from '@angular/core' ;
22
23
import { _IdGenerator } from '../a11y' ;
23
24
import { _CdkPrivateStyleLoader } from '../private' ;
@@ -29,6 +30,56 @@ import {OverlayRef} from './overlay-ref';
29
30
import { OverlayPositionBuilder } from './position/overlay-position-builder' ;
30
31
import { ScrollStrategyOptions } from './scroll/index' ;
31
32
33
+ /**
34
+ * Creates an overlay.
35
+ * @param injector Injector to use when resolving the overlay's dependencies.
36
+ * @param config Configuration applied to the overlay.
37
+ * @returns Reference to the created overlay.
38
+ */
39
+ export function createOverlayRef ( injector : Injector , config ?: OverlayConfig ) : OverlayRef {
40
+ // This is done in the overlay container as well, but we have it here
41
+ // since it's common to mock out the overlay container in tests.
42
+ injector . get ( _CdkPrivateStyleLoader ) . load ( _CdkOverlayStyleLoader ) ;
43
+
44
+ const overlayContainer = injector . get ( OverlayContainer ) ;
45
+ const doc = injector . get ( DOCUMENT ) ;
46
+ const idGenerator = injector . get ( _IdGenerator ) ;
47
+ const appRef = injector . get ( ApplicationRef ) ;
48
+ const directionality = injector . get ( Directionality ) ;
49
+
50
+ const host = doc . createElement ( 'div' ) ;
51
+ const pane = doc . createElement ( 'div' ) ;
52
+
53
+ pane . id = idGenerator . getId ( 'cdk-overlay-' ) ;
54
+ pane . classList . add ( 'cdk-overlay-pane' ) ;
55
+ host . appendChild ( pane ) ;
56
+ overlayContainer . getContainerElement ( ) . appendChild ( host ) ;
57
+
58
+ const portalOutlet = new DomPortalOutlet ( pane , appRef , injector ) ;
59
+ const overlayConfig = new OverlayConfig ( config ) ;
60
+ const renderer =
61
+ injector . get ( Renderer2 , null , { optional : true } ) ||
62
+ injector . get ( RendererFactory2 ) . createRenderer ( null , null ) ;
63
+
64
+ overlayConfig . direction = overlayConfig . direction || directionality . value ;
65
+
66
+ return new OverlayRef (
67
+ portalOutlet ,
68
+ host ,
69
+ pane ,
70
+ overlayConfig ,
71
+ injector . get ( NgZone ) ,
72
+ injector . get ( OverlayKeyboardDispatcher ) ,
73
+ doc ,
74
+ injector . get ( Location ) ,
75
+ injector . get ( OverlayOutsideClickDispatcher ) ,
76
+ config ?. disableAnimations ??
77
+ injector . get ( ANIMATION_MODULE_TYPE , null , { optional : true } ) === 'NoopAnimations' ,
78
+ injector . get ( EnvironmentInjector ) ,
79
+ renderer ,
80
+ ) ;
81
+ }
82
+
32
83
/**
33
84
* Service to create Overlays. Overlays are dynamically added pieces of floating UI, meant to be
34
85
* used as a low-level building block for other components. Dialogs, tooltips, menus,
@@ -40,21 +91,8 @@ import {ScrollStrategyOptions} from './scroll/index';
40
91
@Injectable ( { providedIn : 'root' } )
41
92
export class Overlay {
42
93
scrollStrategies = inject ( ScrollStrategyOptions ) ;
43
- private _overlayContainer = inject ( OverlayContainer ) ;
44
94
private _positionBuilder = inject ( OverlayPositionBuilder ) ;
45
- private _keyboardDispatcher = inject ( OverlayKeyboardDispatcher ) ;
46
95
private _injector = inject ( Injector ) ;
47
- private _ngZone = inject ( NgZone ) ;
48
- private _document = inject ( DOCUMENT ) ;
49
- private _directionality = inject ( Directionality ) ;
50
- private _location = inject ( Location ) ;
51
- private _outsideClickDispatcher = inject ( OverlayOutsideClickDispatcher ) ;
52
- private _animationsModuleType = inject ( ANIMATION_MODULE_TYPE , { optional : true } ) ;
53
- private _idGenerator = inject ( _IdGenerator ) ;
54
- private _renderer = inject ( RendererFactory2 ) . createRenderer ( null , null ) ;
55
-
56
- private _appRef : ApplicationRef ;
57
- private _styleLoader = inject ( _CdkPrivateStyleLoader ) ;
58
96
59
97
constructor ( ...args : unknown [ ] ) ;
60
98
constructor ( ) { }
@@ -65,31 +103,7 @@ export class Overlay {
65
103
* @returns Reference to the created overlay.
66
104
*/
67
105
create ( config ?: OverlayConfig ) : OverlayRef {
68
- // This is done in the overlay container as well, but we have it here
69
- // since it's common to mock out the overlay container in tests.
70
- this . _styleLoader . load ( _CdkOverlayStyleLoader ) ;
71
-
72
- const host = this . _createHostElement ( ) ;
73
- const pane = this . _createPaneElement ( host ) ;
74
- const portalOutlet = this . _createPortalOutlet ( pane ) ;
75
- const overlayConfig = new OverlayConfig ( config ) ;
76
-
77
- overlayConfig . direction = overlayConfig . direction || this . _directionality . value ;
78
-
79
- return new OverlayRef (
80
- portalOutlet ,
81
- host ,
82
- pane ,
83
- overlayConfig ,
84
- this . _ngZone ,
85
- this . _keyboardDispatcher ,
86
- this . _document ,
87
- this . _location ,
88
- this . _outsideClickDispatcher ,
89
- config ?. disableAnimations ?? this . _animationsModuleType === 'NoopAnimations' ,
90
- this . _injector . get ( EnvironmentInjector ) ,
91
- this . _renderer ,
92
- ) ;
106
+ return createOverlayRef ( this . _injector , config ) ;
93
107
}
94
108
95
109
/**
@@ -100,44 +114,4 @@ export class Overlay {
100
114
position ( ) : OverlayPositionBuilder {
101
115
return this . _positionBuilder ;
102
116
}
103
-
104
- /**
105
- * Creates the DOM element for an overlay and appends it to the overlay container.
106
- * @returns Newly-created pane element
107
- */
108
- private _createPaneElement ( host : HTMLElement ) : HTMLElement {
109
- const pane = this . _document . createElement ( 'div' ) ;
110
-
111
- pane . id = this . _idGenerator . getId ( 'cdk-overlay-' ) ;
112
- pane . classList . add ( 'cdk-overlay-pane' ) ;
113
- host . appendChild ( pane ) ;
114
-
115
- return pane ;
116
- }
117
-
118
- /**
119
- * Creates the host element that wraps around an overlay
120
- * and can be used for advanced positioning.
121
- * @returns Newly-create host element.
122
- */
123
- private _createHostElement ( ) : HTMLElement {
124
- const host = this . _document . createElement ( 'div' ) ;
125
- this . _overlayContainer . getContainerElement ( ) . appendChild ( host ) ;
126
- return host ;
127
- }
128
-
129
- /**
130
- * Create a DomPortalOutlet into which the overlay content can be loaded.
131
- * @param pane The DOM element to turn into a portal outlet.
132
- * @returns A portal outlet for the given DOM element.
133
- */
134
- private _createPortalOutlet ( pane : HTMLElement ) : DomPortalOutlet {
135
- // We have to resolve the ApplicationRef later in order to allow people
136
- // to use overlay-based providers during app initialization.
137
- if ( ! this . _appRef ) {
138
- this . _appRef = this . _injector . get < ApplicationRef > ( ApplicationRef ) ;
139
- }
140
-
141
- return new DomPortalOutlet ( pane , this . _appRef , this . _injector ) ;
142
- }
143
117
}
0 commit comments