@@ -2156,6 +2156,12 @@ MdPanelPosition.absPosition = {
2156
2156
LEFT : 'left'
2157
2157
} ;
2158
2158
2159
+ /**
2160
+ * Margin between the edges of a panel and the viewport.
2161
+ * @const {number}
2162
+ */
2163
+ MdPanelPosition . viewportMargin = 8 ;
2164
+
2159
2165
2160
2166
/**
2161
2167
* Sets absolute positioning for the panel.
@@ -2525,6 +2531,9 @@ MdPanelPosition.prototype._reduceTranslateValues =
2525
2531
* @private
2526
2532
*/
2527
2533
MdPanelPosition . prototype . _setPanelPosition = function ( panelEl ) {
2534
+ // Remove the class in case it has been added before.
2535
+ panelEl . removeClass ( '_md-panel-position-adjusted' ) ;
2536
+
2528
2537
// Only calculate the position if necessary.
2529
2538
if ( this . _absolute ) {
2530
2539
return ;
@@ -2539,12 +2548,49 @@ MdPanelPosition.prototype._setPanelPosition = function(panelEl) {
2539
2548
this . _actualPosition = this . _positions [ i ] ;
2540
2549
this . _calculatePanelPosition ( panelEl , this . _actualPosition ) ;
2541
2550
if ( this . _isOnscreen ( panelEl ) ) {
2542
- break ;
2551
+ return ;
2543
2552
}
2544
2553
}
2554
+
2555
+ // Class that can be used to re-style the panel if it was repositioned.
2556
+ panelEl . addClass ( '_md-panel-position-adjusted' ) ;
2557
+ this . _constrainToViewport ( panelEl ) ;
2545
2558
} ;
2546
2559
2547
2560
2561
+ /**
2562
+ * Constrains a panel's position to the viewport.
2563
+ * @param {!angular.JQLite } panelEl
2564
+ * @private
2565
+ */
2566
+ MdPanelPosition . prototype . _constrainToViewport = function ( panelEl ) {
2567
+ var margin = MdPanelPosition . viewportMargin ;
2568
+
2569
+ if ( this . getTop ( ) ) {
2570
+ var top = parseInt ( this . getTop ( ) ) ;
2571
+ var bottom = panelEl [ 0 ] . offsetHeight + top ;
2572
+ var viewportHeight = this . _$window . innerHeight ;
2573
+
2574
+ if ( top < margin ) {
2575
+ this . _top = margin + 'px' ;
2576
+ } else if ( bottom > viewportHeight ) {
2577
+ this . _top = top - ( bottom - viewportHeight + margin ) + 'px' ;
2578
+ }
2579
+ }
2580
+
2581
+ if ( this . getLeft ( ) ) {
2582
+ var left = parseInt ( this . getLeft ( ) ) ;
2583
+ var right = panelEl [ 0 ] . offsetWidth + left ;
2584
+ var viewportWidth = this . _$window . innerWidth ;
2585
+
2586
+ if ( left < margin ) {
2587
+ this . _left = margin + 'px' ;
2588
+ } else if ( right > viewportWidth ) {
2589
+ this . _left = left - ( right - viewportWidth + margin ) + 'px' ;
2590
+ }
2591
+ }
2592
+ } ;
2593
+
2548
2594
/**
2549
2595
* Switches between 'start' and 'end'.
2550
2596
* @param {string } position Horizontal position of the panel
0 commit comments