@@ -10,7 +10,8 @@ import {
1010 Output ,
1111 ViewChild ,
1212 ViewContainerRef ,
13- ViewEncapsulation
13+ ViewEncapsulation ,
14+ NgZone ,
1415} from '@angular/core' ;
1516import { Overlay } from '../core/overlay/overlay' ;
1617import { OverlayRef } from '../core/overlay/overlay-ref' ;
@@ -20,18 +21,15 @@ import {Dir} from '../core/rtl/dir';
2021import { MdDialog } from '../dialog/dialog' ;
2122import { MdDialogRef } from '../dialog/dialog-ref' ;
2223import { PositionStrategy } from '../core/overlay/position/position-strategy' ;
23- import {
24- OriginConnectionPosition ,
25- OverlayConnectionPosition
26- } from '../core/overlay/position/connected-position' ;
24+ import { RepositionScrollStrategy , ScrollDispatcher } from '../core/overlay/index' ;
2725import { MdDatepickerInput } from './datepicker-input' ;
28- import 'rxjs/add/operator/first' ;
2926import { Subscription } from 'rxjs/Subscription' ;
3027import { MdDialogConfig } from '../dialog/dialog-config' ;
3128import { DateAdapter } from '../core/datetime/index' ;
3229import { createMissingDateImplError } from './datepicker-errors' ;
3330import { ESCAPE } from '../core/keyboard/keycodes' ;
3431import { MdCalendar } from './calendar' ;
32+ import 'rxjs/add/operator/first' ;
3533
3634
3735/** Used to generate a unique ID for each datepicker instance. */
@@ -155,8 +153,11 @@ export class MdDatepicker<D> implements OnDestroy {
155153
156154 private _inputSubscription : Subscription ;
157155
158- constructor ( private _dialog : MdDialog , private _overlay : Overlay ,
156+ constructor ( private _dialog : MdDialog ,
157+ private _overlay : Overlay ,
158+ private _ngZone : NgZone ,
159159 private _viewContainerRef : ViewContainerRef ,
160+ private _scrollDispatcher : ScrollDispatcher ,
160161 @Optional ( ) private _dateAdapter : DateAdapter < D > ,
161162 @Optional ( ) private _dir : Dir ) {
162163 if ( ! this . _dateAdapter ) {
@@ -253,6 +254,9 @@ export class MdDatepicker<D> implements OnDestroy {
253254 let componentRef : ComponentRef < MdDatepickerContent < D > > =
254255 this . _popupRef . attach ( this . _calendarPortal ) ;
255256 componentRef . instance . datepicker = this ;
257+
258+ // Update the position once the calendar has rendered.
259+ this . _ngZone . onStable . first ( ) . subscribe ( ( ) => this . _popupRef . updatePosition ( ) ) ;
256260 }
257261
258262 this . _popupRef . backdropClick ( ) . first ( ) . subscribe ( ( ) => this . close ( ) ) ;
@@ -265,15 +269,29 @@ export class MdDatepicker<D> implements OnDestroy {
265269 overlayState . hasBackdrop = true ;
266270 overlayState . backdropClass = 'md-overlay-transparent-backdrop' ;
267271 overlayState . direction = this . _dir ? this . _dir . value : 'ltr' ;
272+ overlayState . scrollStrategy = new RepositionScrollStrategy ( this . _scrollDispatcher ) ;
268273
269274 this . _popupRef = this . _overlay . create ( overlayState ) ;
270275 }
271276
272277 /** Create the popup PositionStrategy. */
273278 private _createPopupPositionStrategy ( ) : PositionStrategy {
274- let origin = { originX : 'start' , originY : 'bottom' } as OriginConnectionPosition ;
275- let overlay = { overlayX : 'start' , overlayY : 'top' } as OverlayConnectionPosition ;
276- return this . _overlay . position ( ) . connectedTo (
277- this . _datepickerInput . getPopupConnectionElementRef ( ) , origin , overlay ) ;
279+ return this . _overlay . position ( )
280+ . connectedTo ( this . _datepickerInput . getPopupConnectionElementRef ( ) ,
281+ { originX : 'start' , originY : 'bottom' } ,
282+ { overlayX : 'start' , overlayY : 'top' }
283+ )
284+ . withFallbackPosition (
285+ { originX : 'start' , originY : 'top' } ,
286+ { overlayX : 'start' , overlayY : 'bottom' }
287+ )
288+ . withFallbackPosition (
289+ { originX : 'end' , originY : 'bottom' } ,
290+ { overlayX : 'end' , overlayY : 'top' }
291+ )
292+ . withFallbackPosition (
293+ { originX : 'end' , originY : 'top' } ,
294+ { overlayX : 'end' , overlayY : 'bottom' }
295+ ) ;
278296 }
279297}
0 commit comments