@@ -128,7 +128,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
128
128
this . _offsetX = offsetX ;
129
129
130
130
if ( this . _position ) {
131
- this . _setPositions ( this . _position ) ;
131
+ this . _updatePositionStrategy ( this . _position ) ;
132
132
}
133
133
}
134
134
@@ -139,7 +139,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
139
139
this . _offsetY = offsetY ;
140
140
141
141
if ( this . _position ) {
142
- this . _setPositions ( this . _position ) ;
142
+ this . _updatePositionStrategy ( this . _position ) ;
143
143
}
144
144
}
145
145
@@ -240,20 +240,10 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
240
240
241
241
ngOnChanges ( changes : SimpleChanges ) {
242
242
if ( this . _position ) {
243
- if ( changes [ 'positions' ] ) {
244
- this . _position . withPositions ( this . positions ) ;
245
- }
246
-
247
- if ( changes [ 'lockPosition' ] ) {
248
- this . _position . withLockedPosition ( this . lockPosition ) ;
249
- }
243
+ this . _updatePositionStrategy ( this . _position ) ;
250
244
251
- if ( changes [ 'origin' ] ) {
252
- this . _position . setOrigin ( this . origin . elementRef ) ;
253
-
254
- if ( this . open ) {
255
- this . _position . apply ( ) ;
256
- }
245
+ if ( changes [ 'origin' ] && this . open ) {
246
+ this . _position . apply ( ) ;
257
247
}
258
248
}
259
249
@@ -269,6 +259,14 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
269
259
}
270
260
271
261
this . _overlayRef = this . _overlay . create ( this . _buildConfig ( ) ) ;
262
+
263
+ this . _overlayRef . keydownEvents ( ) . subscribe ( ( event : KeyboardEvent ) => {
264
+ this . overlayKeydown . next ( event ) ;
265
+
266
+ if ( event . keyCode === ESCAPE ) {
267
+ this . _detachOverlay ( ) ;
268
+ }
269
+ } ) ;
272
270
}
273
271
274
272
/** Builds the overlay config based on the directive's inputs */
@@ -308,51 +306,41 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
308
306
return overlayConfig ;
309
307
}
310
308
311
- /** Returns the position strategy of the overlay to be set on the overlay config */
312
- private _createPositionStrategy ( ) : FlexibleConnectedPositionStrategy {
313
- const strategy = this . _overlay . position ( )
314
- . flexibleConnectedTo ( this . origin . elementRef )
309
+ /** Updates the state of a position strategy, based on the values of the directive inputs. */
310
+ private _updatePositionStrategy ( positionStrategy : FlexibleConnectedPositionStrategy ) {
311
+ const positions : ConnectedPosition [ ] = this . positions . map ( currentPosition => ( {
312
+ originX : currentPosition . originX ,
313
+ originY : currentPosition . originY ,
314
+ overlayX : currentPosition . overlayX ,
315
+ overlayY : currentPosition . overlayY ,
316
+ offsetX : currentPosition . offsetX || this . offsetX ,
317
+ offsetY : currentPosition . offsetY || this . offsetY
318
+ } ) ) ;
319
+
320
+ return positionStrategy
321
+ . setOrigin ( this . origin . elementRef )
322
+ . withPositions ( positions )
315
323
. withFlexibleDimensions ( this . flexibleDimensions )
316
324
. withPush ( this . push )
317
325
. withGrowAfterOpen ( this . growAfterOpen )
318
326
. withViewportMargin ( this . viewportMargin )
319
327
. withLockedPosition ( this . lockPosition ) ;
328
+ }
329
+
330
+ /** Returns the position strategy of the overlay to be set on the overlay config */
331
+ private _createPositionStrategy ( ) : FlexibleConnectedPositionStrategy {
332
+ const strategy = this . _overlay . position ( ) . flexibleConnectedTo ( this . origin . elementRef ) ;
320
333
321
- this . _setPositions ( strategy ) ;
334
+ this . _updatePositionStrategy ( strategy ) ;
322
335
strategy . positionChanges . subscribe ( p => this . positionChange . emit ( p ) ) ;
323
336
324
337
return strategy ;
325
338
}
326
339
327
- /**
328
- * Sets the primary and fallback positions of a positions strategy,
329
- * based on the current directive inputs.
330
- */
331
- private _setPositions ( positionStrategy : FlexibleConnectedPositionStrategy ) {
332
- const positions : ConnectedPosition [ ] = this . positions . map ( pos => ( {
333
- originX : pos . originX ,
334
- originY : pos . originY ,
335
- overlayX : pos . overlayX ,
336
- overlayY : pos . overlayY ,
337
- offsetX : pos . offsetX || this . offsetX ,
338
- offsetY : pos . offsetY || this . offsetY
339
- } ) ) ;
340
-
341
- positionStrategy . withPositions ( positions ) ;
342
- }
343
-
344
340
/** Attaches the overlay and subscribes to backdrop clicks if backdrop exists */
345
341
private _attachOverlay ( ) {
346
342
if ( ! this . _overlayRef ) {
347
343
this . _createOverlay ( ) ;
348
-
349
- this . _overlayRef ! . keydownEvents ( ) . subscribe ( ( event : KeyboardEvent ) => {
350
- this . overlayKeydown . next ( event ) ;
351
-
352
- if ( event . keyCode === ESCAPE ) {
353
- this . _detachOverlay ( ) ;
354
- }
355
- } ) ;
356
344
} else {
357
345
// Update the overlay size, in case the directive's inputs have changed
358
346
this . _overlayRef . updateSize ( {
0 commit comments