66 * found in the LICENSE file at https://angular.io/license
77 */
88
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' ;
1110import { 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' ;
1811
19- /** Time in ms to throttle the resize events by default. */
20- export const DEFAULT_RESIZE_TIME = 20 ;
2112
2213/**
2314 * Simple utility for getting the bounds of the browser viewport.
2415 * @docs -private
2516 */
2617@Injectable ( )
27- export class ViewportRuler implements OnDestroy {
18+ export class ViewportRuler {
2819
2920 /** Cached document client rectangle. */
3021 private _documentRect ?: ClientRect ;
3122
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 ) {
4324 // 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 ( ) ) ;
5226 }
5327
5428 /** Gets a ClientRect for the viewport's bounds. */
@@ -82,6 +56,7 @@ export class ViewportRuler implements OnDestroy {
8256 } ;
8357 }
8458
59+
8560 /**
8661 * Gets the (top, left) scroll position of the viewport.
8762 * @param documentRect
@@ -100,40 +75,31 @@ export class ViewportRuler implements OnDestroy {
10075 // `document.documentElement` works consistently, where the `top` and `left` values will
10176 // equal negative the scroll position.
10277 const top = - documentRect ! . top || document . body . scrollTop || window . scrollY ||
103- document . documentElement . scrollTop || 0 ;
78+ document . documentElement . scrollTop || 0 ;
10479
10580 const left = - documentRect ! . left || document . body . scrollLeft || window . scrollX ||
10681 document . documentElement . scrollLeft || 0 ;
10782
10883 return { top, left} ;
10984 }
11085
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-
11986 /** Caches the latest client rectangle of the document element. */
12087 _cacheViewportGeometry ( ) {
12188 this . _documentRect = document . documentElement . getBoundingClientRect ( ) ;
12289 }
90+
12391}
12492
12593/** @docs -private */
12694export function VIEWPORT_RULER_PROVIDER_FACTORY ( parentRuler : ViewportRuler ,
127- platform : Platform ,
128- ngZone : NgZone ,
12995 scrollDispatcher : ScrollDispatcher ) {
130- return parentRuler || new ViewportRuler ( platform , ngZone , scrollDispatcher ) ;
96+ return parentRuler || new ViewportRuler ( scrollDispatcher ) ;
13197}
13298
13399/** @docs -private */
134100export const VIEWPORT_RULER_PROVIDER = {
135101 // If there is already a ViewportRuler available, use that. Otherwise, provide a new one.
136102 provide : ViewportRuler ,
137- deps : [ [ new Optional ( ) , new SkipSelf ( ) , ViewportRuler ] , Platform , NgZone , ScrollDispatcher ] ,
103+ deps : [ [ new Optional ( ) , new SkipSelf ( ) , ViewportRuler ] , ScrollDispatcher ] ,
138104 useFactory : VIEWPORT_RULER_PROVIDER_FACTORY
139105} ;
0 commit comments