20
20
angular
21
21
. module ( 'material.components.backdrop' , [ 'material.core' ] )
22
22
. directive ( 'mdBackdrop' , function BackdropDirective ( $mdTheming , $mdUtil , $animate , $rootElement , $window , $log , $$rAF , $document ) {
23
- var ERROR_CSS_POSITION = " <md-backdrop> may not work properly in a scrolled, static-positioned parent container." ;
23
+ var ERROR_CSS_POSITION = ' <md-backdrop> may not work properly in a scrolled, static-positioned parent container.' ;
24
24
25
25
return {
26
26
restrict : 'E' ,
@@ -31,48 +31,53 @@ angular
31
31
// backdrop may be outside the $rootElement, tell ngAnimate to animate regardless
32
32
if ( $animate . pin ) $animate . pin ( element , $rootElement ) ;
33
33
34
- var bodyRect ;
35
- $$rAF ( function ( ) {
34
+ var bodyStyles ;
35
+
36
+ $$rAF ( function ( ) {
36
37
// If body scrolling has been disabled using mdUtil.disableBodyScroll(),
37
38
// adjust the 'backdrop' height to account for the fixed 'body' top offset.
38
39
// Note that this can be pretty expensive and is better done inside the $$rAF.
39
- bodyRect = $window . getComputedStyle ( $document [ 0 ] . body ) ;
40
- if ( bodyRect . position == 'fixed' ) {
41
- var hViewport = parseInt ( bodyRect . height , 10 ) + Math . abs ( parseInt ( bodyRect . top , 10 ) ) ;
42
- element . css ( {
43
- height : hViewport + 'px'
40
+ bodyStyles = $window . getComputedStyle ( $document [ 0 ] . body ) ;
41
+
42
+ if ( bodyStyles . position === 'fixed' ) {
43
+ var resizeHandler = $mdUtil . debounce ( function ( ) {
44
+ bodyStyles = $window . getComputedStyle ( $document [ 0 ] . body ) ;
45
+ resize ( ) ;
46
+ } , 60 , null , false ) ;
47
+
48
+ resize ( ) ;
49
+ angular . element ( $window ) . on ( 'resize' , resizeHandler ) ;
50
+
51
+ scope . $on ( '$destroy' , function ( ) {
52
+ angular . element ( $window ) . off ( 'resize' , resizeHandler ) ;
44
53
} ) ;
45
54
}
46
55
47
56
// Often $animate.enter() is used to append the backDrop element
48
57
// so let's wait until $animate is done...
49
- var parent = element . parent ( ) [ 0 ] ;
50
- if ( parent ) {
58
+ var parent = element . parent ( ) ;
51
59
52
- if ( parent . nodeName == 'BODY' ) {
53
- element . css ( { position : 'fixed' } ) ;
60
+ if ( parent . length ) {
61
+ if ( parent [ 0 ] . nodeName === 'BODY' ) {
62
+ element . css ( 'position' , 'fixed' ) ;
54
63
}
55
64
56
- var styles = $window . getComputedStyle ( parent ) ;
57
- if ( styles . position == 'static' ) {
65
+ var styles = $window . getComputedStyle ( parent [ 0 ] ) ;
66
+
67
+ if ( styles . position === 'static' ) {
58
68
// backdrop uses position:absolute and will not work properly with parent position:static (default)
59
69
$log . warn ( ERROR_CSS_POSITION ) ;
60
70
}
61
- }
62
71
63
- // Only inherit the parent if the backdrop has a parent.
64
- if ( element . parent ( ) . length ) {
65
- $mdTheming . inherit ( element , element . parent ( ) ) ;
72
+ // Only inherit the parent if the backdrop has a parent.
73
+ $mdTheming . inherit ( element , parent ) ;
66
74
}
67
75
} ) ;
68
76
69
77
function resize ( ) {
70
- var hViewport = parseInt ( bodyRect . height , 10 ) + Math . abs ( parseInt ( bodyRect . top , 10 ) ) ;
71
- element . css ( {
72
- height : hViewport + 'px'
73
- } ) ;
78
+ var viewportHeight = parseInt ( bodyStyles . height , 10 ) + Math . abs ( parseInt ( bodyStyles . top , 10 ) ) ;
79
+ element . css ( 'height' , viewportHeight + 'px' ) ;
74
80
}
75
-
76
81
}
77
82
78
83
} ) ;
0 commit comments