license import { ScrollStrategy } from './scroll-strategy' ; import { ViewportRuler } from '@angular-mdc/web/scrolling' ; import { coerceCssPixelValue } from '@angular/cdk/coercion' ; docs-private type ScrollBehaviorCSSStyleDeclaration = CSSStyleDeclaration & { scrollBehavior : string ; } ; /**
 * Strategy that will prevent the user from scrolling while the overlay is visible.
 */ export class BlockScrollStrategy implements ScrollStrategy { private _previousHTMLStyles = { top : '' , left : '' } ; private _previousScrollPosition : { top : number ; left : number ; } ; private _isEnabled = false ; private _document : Document ; constructor ( private _viewportRuler , document ) { this . _document = document ; } /** Attaches this scroll strategy to an overlay. */ attach ( ) { } /** Blocks page-level scroll while the attached overlay is open. */ enable ( ) { if ( this . _canBeEnabled ( ) ) { const root = this . _document . documentElement ! ; this . _previousScrollPosition = this . _viewportRuler . getViewportScrollPosition ( ) ; this . _previousHTMLStyles . left = root . style . left || '' ; this . _previousHTMLStyles . top = root . style . top || '' ; root . style . left = coerceCssPixelValue ( - this . _previousScrollPosition . left ) ; root . style . top = coerceCssPixelValue ( - this . _previousScrollPosition . top ) ; root . classList . add ( 'cdk-global-scrollblock' ) ; this . _isEnabled = true ; } } /** Unblocks page-level scroll while the attached overlay is open. */ disable ( ) { if ( this . _isEnabled ) { const html = this . _document . documentElement ! ; const body = this . _document . body ! ; const htmlStyle = html . style as ScrollBehaviorCSSStyleDeclaration ; const bodyStyle = body . style as ScrollBehaviorCSSStyleDeclaration ; const previousHtmlScrollBehavior = htmlStyle . scrollBehavior || '' ; const previousBodyScrollBehavior = bodyStyle . scrollBehavior || '' ; this . _isEnabled = false ; htmlStyle . left = this . _previousHTMLStyles . left ; htmlStyle . top = this . _previousHTMLStyles . top ; html . classList . remove ( 'cdk-global-scrollblock' ) ; htmlStyle . scrollBehavior = bodyStyle . scrollBehavior = 'auto' ; window . scroll ( this . _previousScrollPosition . left , this . _previousScrollPosition . top ) ; htmlStyle . scrollBehavior = previousHtmlScrollBehavior ; bodyStyle . scrollBehavior = previousBodyScrollBehavior ; } } private _canBeEnabled ( ) { const html = this . _document . documentElement ! ; if ( html . classList . contains ( 'cdk-global-scrollblock' ) || this . _isEnabled ) { return false ; } const body = this . _document . body ; const viewport = this . _viewportRuler . getViewportSize ( ) ; return body . scrollHeight > viewport . height || body . scrollWidth > viewport . width ; } }