6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { NgZone } from '@angular/core ' ;
10
- import { PortalHost , Portal } from '@angular/cdk/portal' ;
11
- import { OverlayConfig } from './overlay-config ' ;
9
+ import { Direction } from '@angular/cdk/bidi ' ;
10
+ import { ComponentPortal , Portal , PortalHost , TemplatePortal } from '@angular/cdk/portal' ;
11
+ import { ComponentRef , EmbeddedViewRef , NgZone } from '@angular/core ' ;
12
12
import { Observable } from 'rxjs/Observable' ;
13
13
import { Subject } from 'rxjs/Subject' ;
14
+ import { OverlayConfig } from './overlay-config' ;
15
+
14
16
17
+ /** An object where all of its properties cannot be written. */
18
+ export type ImmutableObject < T > = {
19
+ readonly [ P in keyof T ] : T [ P ] ;
20
+ } ;
15
21
16
22
/**
17
23
* Reference to an overlay that has been created with the Overlay service.
@@ -26,7 +32,7 @@ export class OverlayRef implements PortalHost {
26
32
constructor (
27
33
private _portalHost : PortalHost ,
28
34
private _pane : HTMLElement ,
29
- private _config : OverlayConfig ,
35
+ private _config : ImmutableObject < OverlayConfig > ,
30
36
private _ngZone : NgZone ) {
31
37
32
38
if ( _config . scrollStrategy ) {
@@ -39,8 +45,14 @@ export class OverlayRef implements PortalHost {
39
45
return this . _pane ;
40
46
}
41
47
48
+ attach < T > ( portal : ComponentPortal < T > ) : ComponentRef < T > ;
49
+ attach < T > ( portal : TemplatePortal < T > ) : EmbeddedViewRef < T > ;
50
+ attach ( portal : any ) : any ;
51
+
42
52
/**
43
- * Attaches the overlay to a portal instance and adds the backdrop.
53
+ * Attaches content, given via a Portal, to the overlay.
54
+ * If the overlay is configured to have a backdrop, it will be created.
55
+ *
44
56
* @param portal Portal instance to which to attach the overlay.
45
57
* @returns The portal attachment result.
46
58
*/
@@ -53,8 +65,8 @@ export class OverlayRef implements PortalHost {
53
65
54
66
// Update the pane element with the given configuration.
55
67
this . _updateStackingOrder ( ) ;
56
- this . updateSize ( ) ;
57
- this . updateDirection ( ) ;
68
+ this . _updateElementSize ( ) ;
69
+ this . _updateElementDirection ( ) ;
58
70
this . updatePosition ( ) ;
59
71
60
72
if ( this . _config . scrollStrategy ) {
@@ -107,9 +119,7 @@ export class OverlayRef implements PortalHost {
107
119
return detachmentResult ;
108
120
}
109
121
110
- /**
111
- * Cleans up the overlay from the DOM.
112
- */
122
+ /** Cleans up the overlay from the DOM. */
113
123
dispose ( ) : void {
114
124
if ( this . _config . positionStrategy ) {
115
125
this . _config . positionStrategy . dispose ( ) ;
@@ -127,34 +137,28 @@ export class OverlayRef implements PortalHost {
127
137
this . _detachments . complete ( ) ;
128
138
}
129
139
130
- /**
131
- * Checks whether the overlay has been attached.
132
- */
140
+ /** Whether the overlay has attached content. */
133
141
hasAttached ( ) : boolean {
134
142
return this . _portalHost . hasAttached ( ) ;
135
143
}
136
144
137
- /**
138
- * Returns an observable that emits when the backdrop has been clicked.
139
- */
145
+ /** Gets an observable that emits when the backdrop has been clicked. */
140
146
backdropClick ( ) : Observable < void > {
141
147
return this . _backdropClick . asObservable ( ) ;
142
148
}
143
149
144
- /** Returns an observable that emits when the overlay has been attached. */
150
+ /** Gets an observable that emits when the overlay has been attached. */
145
151
attachments ( ) : Observable < void > {
146
152
return this . _attachments . asObservable ( ) ;
147
153
}
148
154
149
- /** Returns an observable that emits when the overlay has been detached. */
155
+ /** Gets an observable that emits when the overlay has been detached. */
150
156
detachments ( ) : Observable < void > {
151
157
return this . _detachments . asObservable ( ) ;
152
158
}
153
159
154
- /**
155
- * Gets the current config of the overlay.
156
- */
157
- getConfig ( ) : OverlayConfig {
160
+ /** Gets the the current overlay configuration, which is immutable. */
161
+ getConfig ( ) : ImmutableObject < OverlayConfig > {
158
162
return this . _config ;
159
163
}
160
164
@@ -165,13 +169,25 @@ export class OverlayRef implements PortalHost {
165
169
}
166
170
}
167
171
172
+ /** Update the size properties of the overlay. */
173
+ updateSize ( sizeConfig : OverlaySizeConfig ) {
174
+ this . _config = { ...this . _config , ...sizeConfig } ;
175
+ this . _updateElementSize ( ) ;
176
+ }
177
+
178
+ /** Sets the LTR/RTL direction for the overlay. */
179
+ setDirection ( dir : Direction ) {
180
+ this . _config = { ...this . _config , direction : dir } ;
181
+ this . _updateElementDirection ( ) ;
182
+ }
183
+
168
184
/** Updates the text direction of the overlay panel. */
169
- private updateDirection ( ) {
185
+ private _updateElementDirection ( ) {
170
186
this . _pane . setAttribute ( 'dir' , this . _config . direction ! ) ;
171
187
}
172
188
173
- /** Updates the size of the overlay based on the overlay config. */
174
- updateSize ( ) {
189
+ /** Updates the size of the overlay element based on the overlay config. */
190
+ private _updateElementSize ( ) {
175
191
if ( this . _config . width || this . _config . width === 0 ) {
176
192
this . _pane . style . width = formatCssUnit ( this . _config . width ) ;
177
193
}
@@ -220,10 +236,12 @@ export class OverlayRef implements PortalHost {
220
236
this . _backdropElement . addEventListener ( 'click' , ( ) => this . _backdropClick . next ( null ) ) ;
221
237
222
238
// Add class to fade-in the backdrop after one frame.
223
- requestAnimationFrame ( ( ) => {
224
- if ( this . _backdropElement ) {
225
- this . _backdropElement . classList . add ( 'cdk-overlay-backdrop-showing' ) ;
226
- }
239
+ this . _ngZone . runOutsideAngular ( ( ) => {
240
+ requestAnimationFrame ( ( ) => {
241
+ if ( this . _backdropElement ) {
242
+ this . _backdropElement . classList . add ( 'cdk-overlay-backdrop-showing' ) ;
243
+ }
244
+ } ) ;
227
245
} ) ;
228
246
}
229
247
@@ -284,3 +302,14 @@ export class OverlayRef implements PortalHost {
284
302
function formatCssUnit ( value : number | string ) {
285
303
return typeof value === 'string' ? value as string : `${ value } px` ;
286
304
}
305
+
306
+
307
+ /** Size properties for an overlay. */
308
+ export interface OverlaySizeConfig {
309
+ width ?: number | string ;
310
+ height ?: number | string ;
311
+ minWidth ?: number | string ;
312
+ minHeight ?: number | string ;
313
+ maxWidth ?: number | string ;
314
+ maxHeight ?: number | string ;
315
+ }
0 commit comments