6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Injectable , Optional , SkipSelf , NgZone , OnDestroy } from '@angular/core' ;
10
- import { Platform } from '@angular/cdk/platform' ;
9
+ import { Injectable , Optional , SkipSelf } from '@angular/core' ;
11
10
import { ScrollDispatcher } from './scroll-dispatcher' ;
12
- import { Observable } from 'rxjs/Observable' ;
13
- import { fromEvent } from 'rxjs/observable/fromEvent' ;
14
- import { merge } from 'rxjs/observable/merge' ;
15
- import { auditTime } from 'rxjs/operator/auditTime' ;
16
- import { Subscription } from 'rxjs/Subscription' ;
17
- import { of as observableOf } from 'rxjs/observable/of' ;
18
11
19
- /** Time in ms to throttle the resize events by default. */
20
- export const DEFAULT_RESIZE_TIME = 20 ;
21
12
22
13
/**
23
14
* Simple utility for getting the bounds of the browser viewport.
24
15
* @docs -private
25
16
*/
26
17
@Injectable ( )
27
- export class ViewportRuler implements OnDestroy {
18
+ export class ViewportRuler {
28
19
29
20
/** Cached document client rectangle. */
30
21
private _documentRect ?: ClientRect ;
31
22
32
- /** Stream of viewport change events. */
33
- private _change : Observable < Event > ;
34
-
35
- /** Subscriptions to streams that invalidate the cached viewport dimensions. */
36
- private _invalidateCacheSubscriptions : Subscription [ ] ;
37
-
38
- constructor ( platform : Platform , ngZone : NgZone , scrollDispatcher : ScrollDispatcher ) {
39
- this . _change = platform . isBrowser ? ngZone . runOutsideAngular ( ( ) => {
40
- return merge < Event > ( fromEvent ( window , 'resize' ) , fromEvent ( window , 'orientationchange' ) ) ;
41
- } ) : observableOf ( ) ;
42
-
23
+ constructor ( scrollDispatcher : ScrollDispatcher ) {
43
24
// Subscribe to scroll and resize events and update the document rectangle on changes.
44
- this . _invalidateCacheSubscriptions = [
45
- scrollDispatcher . scrolled ( 0 , ( ) => this . _cacheViewportGeometry ( ) ) ,
46
- this . change ( ) . subscribe ( ( ) => this . _cacheViewportGeometry ( ) )
47
- ] ;
48
- }
49
-
50
- ngOnDestroy ( ) {
51
- this . _invalidateCacheSubscriptions . forEach ( subscription => subscription . unsubscribe ( ) ) ;
25
+ scrollDispatcher . scrolled ( 0 , ( ) => this . _cacheViewportGeometry ( ) ) ;
52
26
}
53
27
54
28
/** Gets a ClientRect for the viewport's bounds. */
@@ -82,6 +56,7 @@ export class ViewportRuler implements OnDestroy {
82
56
} ;
83
57
}
84
58
59
+
85
60
/**
86
61
* Gets the (top, left) scroll position of the viewport.
87
62
* @param documentRect
@@ -100,40 +75,31 @@ export class ViewportRuler implements OnDestroy {
100
75
// `document.documentElement` works consistently, where the `top` and `left` values will
101
76
// equal negative the scroll position.
102
77
const top = - documentRect ! . top || document . body . scrollTop || window . scrollY ||
103
- document . documentElement . scrollTop || 0 ;
78
+ document . documentElement . scrollTop || 0 ;
104
79
105
80
const left = - documentRect ! . left || document . body . scrollLeft || window . scrollX ||
106
81
document . documentElement . scrollLeft || 0 ;
107
82
108
83
return { top, left} ;
109
84
}
110
85
111
- /**
112
- * Returns a stream that emits whenever the size of the viewport changes.
113
- * @param throttle Time in milliseconds to throttle the stream.
114
- */
115
- change ( throttleTime : number = DEFAULT_RESIZE_TIME ) : Observable < string > {
116
- return throttleTime > 0 ? auditTime . call ( this . _change , throttleTime ) : this . _change ;
117
- }
118
-
119
86
/** Caches the latest client rectangle of the document element. */
120
87
_cacheViewportGeometry ( ) {
121
88
this . _documentRect = document . documentElement . getBoundingClientRect ( ) ;
122
89
}
90
+
123
91
}
124
92
125
93
/** @docs -private */
126
94
export function VIEWPORT_RULER_PROVIDER_FACTORY ( parentRuler : ViewportRuler ,
127
- platform : Platform ,
128
- ngZone : NgZone ,
129
95
scrollDispatcher : ScrollDispatcher ) {
130
- return parentRuler || new ViewportRuler ( platform , ngZone , scrollDispatcher ) ;
96
+ return parentRuler || new ViewportRuler ( scrollDispatcher ) ;
131
97
}
132
98
133
99
/** @docs -private */
134
100
export const VIEWPORT_RULER_PROVIDER = {
135
101
// If there is already a ViewportRuler available, use that. Otherwise, provide a new one.
136
102
provide : ViewportRuler ,
137
- deps : [ [ new Optional ( ) , new SkipSelf ( ) , ViewportRuler ] , Platform , NgZone , ScrollDispatcher ] ,
103
+ deps : [ [ new Optional ( ) , new SkipSelf ( ) , ViewportRuler ] , ScrollDispatcher ] ,
138
104
useFactory : VIEWPORT_RULER_PROVIDER_FACTORY
139
105
} ;
0 commit comments