6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { NgZone } from '@angular/core' ;
10
- import { PortalOutlet , Portal } from '@angular/cdk/portal' ;
11
- import { OverlayConfig } from './overlay-config' ;
12
- import { OverlayKeyboardDispatcher } from './keyboard/overlay-keyboard-dispatcher' ;
9
+ import { Direction } from '@angular/cdk/bidi' ;
10
+ import { ComponentPortal , Portal , PortalOutlet , TemplatePortal } from '@angular/cdk/portal' ;
11
+ import { ComponentRef , EmbeddedViewRef , NgZone } from '@angular/core' ;
13
12
import { Observable } from 'rxjs/Observable' ;
14
- import { Subject } from 'rxjs/Subject' ;
15
13
import { take } from 'rxjs/operators/take' ;
14
+ import { Subject } from 'rxjs/Subject' ;
15
+ import { OverlayKeyboardDispatcher } from './keyboard/overlay-keyboard-dispatcher' ;
16
+ import { OverlayConfig } from './overlay-config' ;
17
+
16
18
19
+ /** An object where all of its properties cannot be written. */
20
+ export type ImmutableObject < T > = {
21
+ readonly [ P in keyof T ] : T [ P ] ;
22
+ } ;
17
23
18
24
/**
19
25
* Reference to an overlay that has been created with the Overlay service.
@@ -31,7 +37,7 @@ export class OverlayRef implements PortalOutlet {
31
37
constructor (
32
38
private _portalOutlet : PortalOutlet ,
33
39
private _pane : HTMLElement ,
34
- private _config : OverlayConfig ,
40
+ private _config : ImmutableObject < OverlayConfig > ,
35
41
private _ngZone : NgZone ,
36
42
private _keyboardDispatcher : OverlayKeyboardDispatcher ) {
37
43
@@ -45,8 +51,14 @@ export class OverlayRef implements PortalOutlet {
45
51
return this . _pane ;
46
52
}
47
53
54
+ attach < T > ( portal : ComponentPortal < T > ) : ComponentRef < T > ;
55
+ attach < T > ( portal : TemplatePortal < T > ) : EmbeddedViewRef < T > ;
56
+ attach ( portal : any ) : any ;
57
+
48
58
/**
49
- * Attaches the overlay to a portal instance and adds the backdrop.
59
+ * Attaches content, given via a Portal, to the overlay.
60
+ * If the overlay is configured to have a backdrop, it will be created.
61
+ *
50
62
* @param portal Portal instance to which to attach the overlay.
51
63
* @returns The portal attachment result.
52
64
*/
@@ -59,8 +71,8 @@ export class OverlayRef implements PortalOutlet {
59
71
60
72
// Update the pane element with the given configuration.
61
73
this . _updateStackingOrder ( ) ;
62
- this . updateSize ( ) ;
63
- this . updateDirection ( ) ;
74
+ this . _updateElementSize ( ) ;
75
+ this . _updateElementDirection ( ) ;
64
76
65
77
if ( this . _config . scrollStrategy ) {
66
78
this . _config . scrollStrategy . enable ( ) ;
@@ -133,9 +145,7 @@ export class OverlayRef implements PortalOutlet {
133
145
return detachmentResult ;
134
146
}
135
147
136
- /**
137
- * Cleans up the overlay from the DOM.
138
- */
148
+ /** Cleans up the overlay from the DOM. */
139
149
dispose ( ) : void {
140
150
const isAttached = this . hasAttached ( ) ;
141
151
@@ -161,16 +171,12 @@ export class OverlayRef implements PortalOutlet {
161
171
this . _detachments . complete ( ) ;
162
172
}
163
173
164
- /**
165
- * Checks whether the overlay has been attached.
166
- */
174
+ /** Whether the overlay has attached content. */
167
175
hasAttached ( ) : boolean {
168
176
return this . _portalOutlet . hasAttached ( ) ;
169
177
}
170
178
171
- /**
172
- * Gets an observable that emits when the backdrop has been clicked.
173
- */
179
+ /** Gets an observable that emits when the backdrop has been clicked. */
174
180
backdropClick ( ) : Observable < void > {
175
181
return this . _backdropClick . asObservable ( ) ;
176
182
}
@@ -190,9 +196,7 @@ export class OverlayRef implements PortalOutlet {
190
196
return this . _keydownEvents . asObservable ( ) ;
191
197
}
192
198
193
- /**
194
- * Gets the current config of the overlay.
195
- */
199
+ /** Gets the the current overlay configuration, which is immutable. */
196
200
getConfig ( ) : OverlayConfig {
197
201
return this . _config ;
198
202
}
@@ -204,13 +208,25 @@ export class OverlayRef implements PortalOutlet {
204
208
}
205
209
}
206
210
211
+ /** Update the size properties of the overlay. */
212
+ updateSize ( sizeConfig : OverlaySizeConfig ) {
213
+ this . _config = { ...this . _config , ...sizeConfig } ;
214
+ this . _updateElementSize ( ) ;
215
+ }
216
+
217
+ /** Sets the LTR/RTL direction for the overlay. */
218
+ setDirection ( dir : Direction ) {
219
+ this . _config = { ...this . _config , direction : dir } ;
220
+ this . _updateElementDirection ( ) ;
221
+ }
222
+
207
223
/** Updates the text direction of the overlay panel. */
208
- private updateDirection ( ) {
224
+ private _updateElementDirection ( ) {
209
225
this . _pane . setAttribute ( 'dir' , this . _config . direction ! ) ;
210
226
}
211
227
212
- /** Updates the size of the overlay based on the overlay config. */
213
- updateSize ( ) {
228
+ /** Updates the size of the overlay element based on the overlay config. */
229
+ private _updateElementSize ( ) {
214
230
if ( this . _config . width || this . _config . width === 0 ) {
215
231
this . _pane . style . width = formatCssUnit ( this . _config . width ) ;
216
232
}
@@ -259,10 +275,12 @@ export class OverlayRef implements PortalOutlet {
259
275
this . _backdropElement . addEventListener ( 'click' , ( ) => this . _backdropClick . next ( null ) ) ;
260
276
261
277
// Add class to fade-in the backdrop after one frame.
262
- requestAnimationFrame ( ( ) => {
263
- if ( this . _backdropElement ) {
264
- this . _backdropElement . classList . add ( 'cdk-overlay-backdrop-showing' ) ;
265
- }
278
+ this . _ngZone . runOutsideAngular ( ( ) => {
279
+ requestAnimationFrame ( ( ) => {
280
+ if ( this . _backdropElement ) {
281
+ this . _backdropElement . classList . add ( 'cdk-overlay-backdrop-showing' ) ;
282
+ }
283
+ } ) ;
266
284
} ) ;
267
285
}
268
286
@@ -323,3 +341,14 @@ export class OverlayRef implements PortalOutlet {
323
341
function formatCssUnit ( value : number | string ) {
324
342
return typeof value === 'string' ? value as string : `${ value } px` ;
325
343
}
344
+
345
+
346
+ /** Size properties for an overlay. */
347
+ export interface OverlaySizeConfig {
348
+ width ?: number | string ;
349
+ height ?: number | string ;
350
+ minWidth ?: number | string ;
351
+ minHeight ?: number | string ;
352
+ maxWidth ?: number | string ;
353
+ maxHeight ?: number | string ;
354
+ }
0 commit comments