blob: d1c1d401d40b69c501330cb974b8aa2d9b820620 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/* tslint:disable:array-type member-access variable-name typedef
only-arrow-functions directive-class-suffix component-class-suffix
component-selector*/
import {ViewportRuler} from '../position/viewport-ruler';
import {ScrollStrategy} from './scroll-strategy';
/**
* 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;
constructor(private _viewportRuler: ViewportRuler) {}
attach() {
//
}
enable() {
if (this._canBeEnabled()) {
const 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;
}
}
disable() {
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);
}
}
private _canBeEnabled(): boolean {
// 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;
}
const body = document.body;
const viewport = this._viewportRuler.getViewportRect();
return body.scrollHeight > viewport.height ||
body.scrollWidth > viewport.width;
}
}
|