md2
Version:
Angular2 based Material Design components, directives and services are Accordion, Autocomplete, Chips(Tags), Collapse, Colorpicker, Data Table, Datepicker, Dialog(Modal), Menu, Multiselect, Select, Tabs, Tags(Chips), Toast and Tooltip.
49 lines • 2.48 kB
JavaScript
/**
* Strategy that will prevent the user from scrolling while the overlay is visible.
*/
var BlockScrollStrategy = (function () {
function BlockScrollStrategy(_viewportRuler) {
this._viewportRuler = _viewportRuler;
this._previousHTMLStyles = { top: '', left: '' };
this._isEnabled = false;
}
BlockScrollStrategy.prototype.attach = function () { };
BlockScrollStrategy.prototype.enable = function () {
if (this._canBeEnabled()) {
var root = document.documentElement;
this._previousScrollPosition = this._viewportRuler.getViewportScrollPosition();
// Cache the previous inline styles in case the user had set them.
this._previousHTMLStyles.left = root.style.left;
this._previousHTMLStyles.top = root.style.top;
// Note: we're using the `html` node, instead of the `body`, because the `body` may
// have the user agent margin, whereas the `html` is guaranteed not to have one.
root.style.left = -this._previousScrollPosition.left + "px";
root.style.top = -this._previousScrollPosition.top + "px";
root.classList.add('cdk-global-scrollblock');
this._isEnabled = true;
}
};
BlockScrollStrategy.prototype.disable = function () {
if (this._isEnabled) {
this._isEnabled = false;
document.documentElement.style.left = this._previousHTMLStyles.left;
document.documentElement.style.top = this._previousHTMLStyles.top;
document.documentElement.classList.remove('cdk-global-scrollblock');
window.scroll(this._previousScrollPosition.left, this._previousScrollPosition.top);
}
};
BlockScrollStrategy.prototype._canBeEnabled = function () {
// Since the scroll strategies can't be singletons, we have to use a global CSS class
// (`cdk-global-scrollblock`) to make sure that we don't try to disable global
// scrolling multiple times.
if (document.documentElement.classList.contains('cdk-global-scrollblock') || this._isEnabled) {
return false;
}
var body = document.body;
var viewport = this._viewportRuler.getViewportRect();
return body.scrollHeight > viewport.height || body.scrollWidth > viewport.width;
};
return BlockScrollStrategy;
}());
export { BlockScrollStrategy };
//# sourceMappingURL=block-scroll-strategy.js.map