@@ -5,13 +5,19 @@ import {
55 Injector ,
66 NgZone ,
77 Provider ,
8+ ReflectiveInjector ,
89} from '@angular/core' ;
910import { OverlayState } from './overlay-state' ;
1011import { DomPortalHost } from '../portal/dom-portal-host' ;
1112import { OverlayRef } from './overlay-ref' ;
1213import { OverlayPositionBuilder } from './position/overlay-position-builder' ;
1314import { VIEWPORT_RULER_PROVIDER } from './position/viewport-ruler' ;
1415import { OverlayContainer , OVERLAY_CONTAINER_PROVIDER } from './overlay-container' ;
16+ import { ScrollStrategy } from './scroll/scroll-strategy' ;
17+ import { RepositionScrollStrategy } from './scroll/reposition-scroll-strategy' ;
18+ import { BlockScrollStrategy } from './scroll/block-scroll-strategy' ;
19+ import { CloseScrollStrategy } from './scroll/close-scroll-strategy' ;
20+ import { NoopScrollStrategy } from './scroll/noop-scroll-strategy' ;
1521
1622
1723/** Next overlay unique ID. */
@@ -31,12 +37,22 @@ let defaultState = new OverlayState();
3137 */
3238@Injectable ( )
3339export class Overlay {
40+ // Create a child ReflectiveInjector, allowing us to instantiate scroll
41+ // strategies without going throught the injector cache.
42+ private _reflectiveInjector = ReflectiveInjector . resolveAndCreate ( [ ] , this . _injector ) ;
43+ private _scrollStrategies = {
44+ reposition : RepositionScrollStrategy ,
45+ block : BlockScrollStrategy ,
46+ close : CloseScrollStrategy ,
47+ noop : NoopScrollStrategy
48+ } ;
49+
3450 constructor ( private _overlayContainer : OverlayContainer ,
3551 private _componentFactoryResolver : ComponentFactoryResolver ,
3652 private _positionBuilder : OverlayPositionBuilder ,
3753 private _appRef : ApplicationRef ,
3854 private _injector : Injector ,
39- private _ngZone : NgZone ) { }
55+ private _ngZone : NgZone ) { }
4056
4157 /**
4258 * Creates an overlay.
@@ -55,15 +71,26 @@ export class Overlay {
5571 return this . _positionBuilder ;
5672 }
5773
74+ /**
75+ * Registers a scroll strategy to be available for use when creating an overlay.
76+ * @param name Name of the scroll strategy.
77+ * @param constructor Class to be used to instantiate the scroll strategy.
78+ */
79+ registerScrollStrategy ( name : string , constructor : Function ) : void {
80+ if ( name && constructor ) {
81+ this . _scrollStrategies [ name ] = constructor ;
82+ }
83+ }
84+
5885 /**
5986 * Creates the DOM element for an overlay and appends it to the overlay container.
6087 * @returns Newly-created pane element
6188 */
6289 private _createPaneElement ( ) : HTMLElement {
6390 let pane = document . createElement ( 'div' ) ;
91+
6492 pane . id = `cdk-overlay-${ nextUniqueId ++ } ` ;
6593 pane . classList . add ( 'cdk-overlay-pane' ) ;
66-
6794 this . _overlayContainer . getContainerElement ( ) . appendChild ( pane ) ;
6895
6996 return pane ;
@@ -84,7 +111,28 @@ export class Overlay {
84111 * @param state
85112 */
86113 private _createOverlayRef ( pane : HTMLElement , state : OverlayState ) : OverlayRef {
87- return new OverlayRef ( this . _createPortalHost ( pane ) , pane , state , this . _ngZone ) ;
114+ let portalHost = this . _createPortalHost ( pane ) ;
115+ let scrollStrategy = this . _createScrollStrategy ( state ) ;
116+ return new OverlayRef ( portalHost , pane , state , scrollStrategy , this . _ngZone ) ;
117+ }
118+
119+ /**
120+ * Resolves the scroll strategy of an overlay state.
121+ * @param state State for which to resolve the scroll strategy.
122+ */
123+ private _createScrollStrategy ( state : OverlayState ) : ScrollStrategy {
124+ let strategyName = typeof state . scrollStrategy === 'string' ?
125+ state . scrollStrategy :
126+ state . scrollStrategy . name ;
127+
128+ if ( ! this . _scrollStrategies . hasOwnProperty ( strategyName ) ) {
129+ throw new Error ( `Unsupported scroll strategy "${ strategyName } ". The available scroll ` +
130+ `strategies are ${ Object . keys ( this . _scrollStrategies ) . join ( ', ' ) } .` ) ;
131+ }
132+
133+ // Note that we use `resolveAndInstantiate` which will instantiate
134+ // the scroll strategy without putting it in the injector cache.
135+ return this . _reflectiveInjector . resolveAndInstantiate ( this . _scrollStrategies [ strategyName ] ) ;
88136 }
89137}
90138
0 commit comments