Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(backdrop): re-introduce resize handler, general cleanup
Browse files Browse the repository at this point in the history
* Re-introduces the resize handler that was accidentally removed in 4803b49. This fixes issues where the backdrop wouldn't adjust when the body resizes.
* Cleans up a few redundancies.

Closes #9249
  • Loading branch information
crisbeto authored and ThomasBurleson committed Aug 11, 2016
1 parent e85a115 commit c4c7c33
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/components/backdrop/backdrop.js
Expand Up @@ -20,7 +20,7 @@
angular
.module('material.components.backdrop', ['material.core'])
.directive('mdBackdrop', function BackdropDirective($mdTheming, $mdUtil, $animate, $rootElement, $window, $log, $$rAF, $document) {
var ERROR_CSS_POSITION = "<md-backdrop> may not work properly in a scrolled, static-positioned parent container.";
var ERROR_CSS_POSITION = '<md-backdrop> may not work properly in a scrolled, static-positioned parent container.';

return {
restrict: 'E',
Expand All @@ -31,48 +31,53 @@ angular
// backdrop may be outside the $rootElement, tell ngAnimate to animate regardless
if ($animate.pin) $animate.pin(element, $rootElement);

var bodyRect;
$$rAF(function () {
var bodyStyles;

$$rAF(function() {
// If body scrolling has been disabled using mdUtil.disableBodyScroll(),
// adjust the 'backdrop' height to account for the fixed 'body' top offset.
// Note that this can be pretty expensive and is better done inside the $$rAF.
bodyRect = $window.getComputedStyle($document[0].body);
if (bodyRect.position == 'fixed') {
var hViewport = parseInt(bodyRect.height, 10) + Math.abs(parseInt(bodyRect.top, 10));
element.css({
height: hViewport + 'px'
bodyStyles = $window.getComputedStyle($document[0].body);

if (bodyStyles.position === 'fixed') {
var resizeHandler = $mdUtil.debounce(function(){
bodyStyles = $window.getComputedStyle($document[0].body);
resize();
}, 60, null, false);

resize();
angular.element($window).on('resize', resizeHandler);

scope.$on('$destroy', function() {
angular.element($window).off('resize', resizeHandler);
});
}

// Often $animate.enter() is used to append the backDrop element
// so let's wait until $animate is done...
var parent = element.parent()[0];
if (parent) {
var parent = element.parent();

if ( parent.nodeName == 'BODY' ) {
element.css({position : 'fixed'});
if (parent.length) {
if (parent[0].nodeName === 'BODY') {
element.css('position', 'fixed');
}

var styles = $window.getComputedStyle(parent);
if (styles.position == 'static') {
var styles = $window.getComputedStyle(parent[0]);

if (styles.position === 'static') {
// backdrop uses position:absolute and will not work properly with parent position:static (default)
$log.warn(ERROR_CSS_POSITION);
}
}

// Only inherit the parent if the backdrop has a parent.
if (element.parent().length) {
$mdTheming.inherit(element, element.parent());
// Only inherit the parent if the backdrop has a parent.
$mdTheming.inherit(element, parent);
}
});

function resize() {
var hViewport = parseInt(bodyRect.height, 10) + Math.abs(parseInt(bodyRect.top, 10));
element.css({
height: hViewport + 'px'
});
var viewportHeight = parseInt(bodyStyles.height, 10) + Math.abs(parseInt(bodyStyles.top, 10));
element.css('height', viewportHeight + 'px');
}

}

});

0 comments on commit c4c7c33

Please sign in to comment.