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