@@ -30,27 +30,39 @@ import {Overlay} from './overlay';
3030import { OverlayConfig } from './overlay-config' ;
3131import { OverlayRef } from './overlay-ref' ;
3232import {
33- ConnectedOverlayPositionChange ,
34- ConnectionPositionPair ,
35- } from './position/connected-position' ;
36- import { ConnectedPositionStrategy } from './position/connected-position-strategy ' ;
33+ FlexibleConnectedPositionStrategy ,
34+ ConnectedPosition ,
35+ } from './position/flexible- connected-position-strategy ' ;
36+ import { ConnectedOverlayPositionChange } from './position/connected-position' ;
3737import { RepositionScrollStrategy , ScrollStrategy } from './scroll/index' ;
3838
3939
4040/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
41- const defaultPositionList = [
42- new ConnectionPositionPair (
43- { originX : 'start' , originY : 'bottom' } ,
44- { overlayX : 'start' , overlayY : 'top' } ) ,
45- new ConnectionPositionPair (
46- { originX : 'start' , originY : 'top' } ,
47- { overlayX : 'start' , overlayY : 'bottom' } ) ,
48- new ConnectionPositionPair (
49- { originX : 'end' , originY : 'top' } ,
50- { overlayX : 'end' , overlayY : 'bottom' } ) ,
51- new ConnectionPositionPair (
52- { originX : 'end' , originY : 'bottom' } ,
53- { overlayX : 'end' , overlayY : 'top' } ) ,
41+ const defaultPositionList : ConnectedPosition [ ] = [
42+ {
43+ originX : 'start' ,
44+ originY : 'bottom' ,
45+ overlayX : 'start' ,
46+ overlayY : 'top'
47+ } ,
48+ {
49+ originX : 'start' ,
50+ originY : 'top' ,
51+ overlayX : 'start' ,
52+ overlayY : 'bottom'
53+ } ,
54+ {
55+ originX : 'end' ,
56+ originY : 'top' ,
57+ overlayX : 'end' ,
58+ overlayY : 'bottom'
59+ } ,
60+ {
61+ originX : 'end' ,
62+ originY : 'bottom' ,
63+ overlayX : 'end' ,
64+ overlayY : 'top'
65+ }
5466] ;
5567
5668/** Injection token that determines the scroll handling while the connected overlay is open. */
@@ -101,21 +113,22 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
101113 private _positionSubscription = Subscription . EMPTY ;
102114 private _offsetX : number = 0 ;
103115 private _offsetY : number = 0 ;
104- private _position : ConnectedPositionStrategy ;
116+ private _position : FlexibleConnectedPositionStrategy ;
105117
106118 /** Origin for the connected overlay. */
107119 @Input ( 'cdkConnectedOverlayOrigin' ) origin : CdkOverlayOrigin ;
108120
109121 /** Registered connected position pairs. */
110- @Input ( 'cdkConnectedOverlayPositions' ) positions : ConnectionPositionPair [ ] ;
122+ @Input ( 'cdkConnectedOverlayPositions' ) positions : ConnectedPosition [ ] ;
111123
112124 /** The offset in pixels for the overlay connection point on the x-axis */
113125 @Input ( 'cdkConnectedOverlayOffsetX' )
114126 get offsetX ( ) : number { return this . _offsetX ; }
115127 set offsetX ( offsetX : number ) {
116128 this . _offsetX = offsetX ;
129+
117130 if ( this . _position ) {
118- this . _position . withOffsetX ( offsetX ) ;
131+ this . _setPositions ( this . _position ) ;
119132 }
120133 }
121134
@@ -124,8 +137,9 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
124137 get offsetY ( ) { return this . _offsetY ; }
125138 set offsetY ( offsetY : number ) {
126139 this . _offsetY = offsetY ;
140+
127141 if ( this . _position ) {
128- this . _position . withOffsetY ( offsetY ) ;
142+ this . _setPositions ( this . _position ) ;
129143 }
130144 }
131145
@@ -163,8 +177,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
163177
164178 /** @deprecated */
165179 @Input ( 'positions' )
166- get _deprecatedPositions ( ) : ConnectionPositionPair [ ] { return this . positions ; }
167- set _deprecatedPositions ( _positions : ConnectionPositionPair [ ] ) { this . positions = _positions ; }
180+ get _deprecatedPositions ( ) : ConnectedPosition [ ] { return this . positions ; }
181+ set _deprecatedPositions ( _positions : ConnectedPosition [ ] ) { this . positions = _positions ; }
168182
169183 /** @deprecated */
170184 @Input ( 'offsetX' )
@@ -303,31 +317,40 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
303317 }
304318
305319 /** Returns the position strategy of the overlay to be set on the overlay config */
306- private _createPositionStrategy ( ) : ConnectedPositionStrategy {
307- const pos = this . positions [ 0 ] ;
308- const originPoint = { originX : pos . originX , originY : pos . originY } ;
309- const overlayPoint = { overlayX : pos . overlayX , overlayY : pos . overlayY } ;
310-
320+ private _createPositionStrategy ( ) : FlexibleConnectedPositionStrategy {
311321 const strategy = this . _overlay . position ( )
312- . connectedTo ( this . origin . elementRef , originPoint , overlayPoint )
313- . withOffsetX ( this . offsetX )
314- . withOffsetY ( this . offsetY ) ;
315-
316- this . _handlePositionChanges ( strategy ) ;
322+ . flexibleConnectedTo ( this . origin . elementRef )
323+ // Turn off all of the flexible positioning features for now to have it behave
324+ // the same way as the old ConnectedPositionStrategy and to avoid breaking changes.
325+ // TODO(crisbeto): make these on by default and add inputs for them
326+ // next time we do breaking changes.
327+ . withFlexibleHeight ( false )
328+ . withFlexibleWidth ( false )
329+ . withPush ( false )
330+ . withGrowAfterOpen ( false ) ;
331+
332+ this . _setPositions ( strategy ) ;
333+ this . _positionSubscription =
334+ strategy . positionChange . subscribe ( p => this . positionChange . emit ( p ) ) ;
317335
318336 return strategy ;
319337 }
320338
321- private _handlePositionChanges ( strategy : ConnectedPositionStrategy ) : void {
322- for ( let i = 1 ; i < this . positions . length ; i ++ ) {
323- strategy . withFallbackPosition (
324- { originX : this . positions [ i ] . originX , originY : this . positions [ i ] . originY } ,
325- { overlayX : this . positions [ i ] . overlayX , overlayY : this . positions [ i ] . overlayY }
326- ) ;
327- }
328-
329- this . _positionSubscription =
330- strategy . onPositionChange . subscribe ( pos => this . positionChange . emit ( pos ) ) ;
339+ /**
340+ * Sets the primary and fallback positions of a positions strategy,
341+ * based on the current directive inputs.
342+ */
343+ private _setPositions ( positionStrategy : FlexibleConnectedPositionStrategy ) {
344+ const positions : ConnectedPosition [ ] = this . positions . map ( pos => ( {
345+ originX : pos . originX ,
346+ originY : pos . originY ,
347+ overlayX : pos . overlayX ,
348+ overlayY : pos . overlayY ,
349+ offsetX : this . offsetX ,
350+ offsetY : this . offsetY
351+ } ) ) ;
352+
353+ positionStrategy . withPositions ( positions ) ;
331354 }
332355
333356 /** Attaches the overlay and subscribes to backdrop clicks if backdrop exists */
@@ -342,7 +365,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
342365 } ) ;
343366 }
344367
345- this . _position . withDirection ( this . dir ) ;
346368 this . _overlayRef . setDirection ( this . dir ) ;
347369
348370 if ( ! this . _overlayRef . hasAttached ( ) ) {
0 commit comments