Skip to content

Commit 2b4871d

Browse files
committed
feat(viewport-ruler): add common window resize handler
Adds the `change` method to the `ViewportRuler`, allowing for components to hook up to a common window resize handler. BREAKING CHANGE: Previously the `ScrollDispatcher.scrolled` subscription would react both on scroll events and on window resize events. Now it only reacts to scroll events. To react to resize events, subscribe to the `ViewportRuler.change()` stream.
1 parent 1b6b270 commit 2b4871d

9 files changed

+100
-45
lines changed

src/cdk/scrolling/scroll-dispatcher.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ describe('Scroll Dispatcher', () => {
7272

7373
scroll.scrolled(0, () => {});
7474
dispatchFakeEvent(document, 'scroll');
75-
dispatchFakeEvent(window, 'resize');
7675

7776
expect(spy).not.toHaveBeenCalled();
7877
subscription.unsubscribe();

src/cdk/scrolling/scroll-dispatcher.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {Platform} from '@angular/cdk/platform';
1111
import {Subject} from 'rxjs/Subject';
1212
import {Subscription} from 'rxjs/Subscription';
1313
import {fromEvent} from 'rxjs/observable/fromEvent';
14-
import {merge} from 'rxjs/observable/merge';
1514
import {auditTime} from 'rxjs/operator/auditTime';
1615
import {Scrollable} from './scrollable';
1716

@@ -87,10 +86,7 @@ export class ScrollDispatcher {
8786

8887
if (!this._globalSubscription) {
8988
this._globalSubscription = this._ngZone.runOutsideAngular(() => {
90-
return merge(
91-
fromEvent(window.document, 'scroll'),
92-
fromEvent(window, 'resize')
93-
).subscribe(() => this._notify());
89+
return fromEvent(window.document, 'scroll').subscribe(() => this._notify());
9490
});
9591
}
9692

src/cdk/scrolling/viewport-ruler.spec.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {TestBed, inject} from '@angular/core/testing';
1+
import {TestBed, inject, fakeAsync, tick} from '@angular/core/testing';
22
import {ScrollDispatchModule} from './public_api';
33
import {ViewportRuler, VIEWPORT_RULER_PROVIDER} from './viewport-ruler';
4+
import {dispatchFakeEvent} from '@angular/cdk/testing';
45

56

67
// For all tests, we assume the browser window is 1024x786 (outerWidth x outerHeight).
@@ -32,6 +33,10 @@ describe('ViewportRuler', () => {
3233
scrollTo(0, 0);
3334
}));
3435

36+
afterEach(() => {
37+
ruler.ngOnDestroy();
38+
});
39+
3540
it('should get the viewport bounds when the page is not scrolled', () => {
3641
let bounds = ruler.getViewportRect();
3742
expect(bounds.top).toBe(0);
@@ -101,4 +106,37 @@ describe('ViewportRuler', () => {
101106

102107
document.body.removeChild(veryLargeElement);
103108
});
109+
110+
describe('changed event', () => {
111+
it('should dispatch an event when the window is resized', () => {
112+
const spy = jasmine.createSpy('viewport changed spy');
113+
const subscription = ruler.change(0).subscribe(spy);
114+
115+
dispatchFakeEvent(window, 'resize');
116+
expect(spy).toHaveBeenCalled();
117+
subscription.unsubscribe();
118+
});
119+
120+
it('should dispatch an event when the orientation is changed', () => {
121+
const spy = jasmine.createSpy('viewport changed spy');
122+
const subscription = ruler.change(0).subscribe(spy);
123+
124+
dispatchFakeEvent(window, 'orientationchange');
125+
expect(spy).toHaveBeenCalled();
126+
subscription.unsubscribe();
127+
});
128+
129+
it('should be able to throttle the callback', fakeAsync(() => {
130+
const spy = jasmine.createSpy('viewport changed spy');
131+
const subscription = ruler.change(1337).subscribe(spy);
132+
133+
dispatchFakeEvent(window, 'resize');
134+
expect(spy).not.toHaveBeenCalled();
135+
136+
tick(1337);
137+
138+
expect(spy).toHaveBeenCalledTimes(1);
139+
subscription.unsubscribe();
140+
}));
141+
});
104142
});

src/cdk/scrolling/viewport-ruler.ts

+44-9
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,50 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable, Optional, SkipSelf} from '@angular/core';
9+
import {Injectable, Optional, SkipSelf, NgZone, OnDestroy} from '@angular/core';
10+
import {Platform} from '@angular/cdk/platform';
1011
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';
1117

18+
/** Time in ms to throttle the resize events by default. */
19+
export const DEFAULT_RESIZE_TIME = 20;
1220

1321
/**
1422
* Simple utility for getting the bounds of the browser viewport.
1523
* @docs-private
1624
*/
1725
@Injectable()
18-
export class ViewportRuler {
26+
export class ViewportRuler implements OnDestroy {
1927

2028
/** Cached document client rectangle. */
2129
private _documentRect?: ClientRect;
2230

23-
constructor(scrollDispatcher: ScrollDispatcher) {
31+
/** Stream of viewport change events. */
32+
private _change: Observable<Event>;
33+
34+
/** Subscriptions to streams that invalidate the cached viewport dimensions. */
35+
private _invalidateCacheSubscriptions: Subscription[];
36+
37+
constructor(platform: Platform, ngZone: NgZone, scrollDispatcher: ScrollDispatcher) {
38+
if (platform.isBrowser) {
39+
this._change = ngZone.runOutsideAngular(() => {
40+
return merge<Event>(fromEvent(window, 'resize'), fromEvent(window, 'orientationchange'));
41+
});
42+
}
43+
2444
// Subscribe to scroll and resize events and update the document rectangle on changes.
25-
scrollDispatcher.scrolled(0, () => this._cacheViewportGeometry());
45+
this._invalidateCacheSubscriptions = [
46+
scrollDispatcher.scrolled(0, () => this._cacheViewportGeometry()),
47+
this.change().subscribe(() => this._cacheViewportGeometry())
48+
];
49+
}
50+
51+
ngOnDestroy() {
52+
this._invalidateCacheSubscriptions.forEach(subscription => subscription.unsubscribe());
2653
}
2754

2855
/** Gets a ClientRect for the viewport's bounds. */
@@ -56,7 +83,6 @@ export class ViewportRuler {
5683
};
5784
}
5885

59-
6086
/**
6187
* Gets the (top, left) scroll position of the viewport.
6288
* @param documentRect
@@ -75,31 +101,40 @@ export class ViewportRuler {
75101
// `document.documentElement` works consistently, where the `top` and `left` values will
76102
// equal negative the scroll position.
77103
const top = -documentRect!.top || document.body.scrollTop || window.scrollY ||
78-
document.documentElement.scrollTop || 0;
104+
document.documentElement.scrollTop || 0;
79105

80106
const left = -documentRect!.left || document.body.scrollLeft || window.scrollX ||
81107
document.documentElement.scrollLeft || 0;
82108

83109
return {top, left};
84110
}
85111

112+
/**
113+
* Returns a stream that emits whenever the size of the viewport changes.
114+
* @param throttle Time in milliseconds to throttle the stream.
115+
*/
116+
change(throttleTime: number = DEFAULT_RESIZE_TIME): Observable<string> {
117+
return throttleTime > 0 ? auditTime.call(this._change, throttleTime) : this._change;
118+
}
119+
86120
/** Caches the latest client rectangle of the document element. */
87121
_cacheViewportGeometry() {
88122
this._documentRect = document.documentElement.getBoundingClientRect();
89123
}
90-
91124
}
92125

93126
/** @docs-private */
94127
export function VIEWPORT_RULER_PROVIDER_FACTORY(parentRuler: ViewportRuler,
128+
platform: Platform,
129+
ngZone: NgZone,
95130
scrollDispatcher: ScrollDispatcher) {
96-
return parentRuler || new ViewportRuler(scrollDispatcher);
131+
return parentRuler || new ViewportRuler(platform, ngZone, scrollDispatcher);
97132
}
98133

99134
/** @docs-private */
100135
export const VIEWPORT_RULER_PROVIDER = {
101136
// If there is already a ViewportRuler available, use that. Otherwise, provide a new one.
102137
provide: ViewportRuler,
103-
deps: [[new Optional(), new SkipSelf(), ViewportRuler], ScrollDispatcher],
138+
deps: [[new Optional(), new SkipSelf(), ViewportRuler], Platform, NgZone, ScrollDispatcher],
104139
useFactory: VIEWPORT_RULER_PROVIDER_FACTORY
105140
};

src/lib/tabs/tab-group.spec.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/t
22
import {Component, QueryList, ViewChild, ViewChildren} from '@angular/core';
33
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
44
import {By} from '@angular/platform-browser';
5-
import {ViewportRuler} from '@angular/cdk/scrolling';
6-
import {dispatchFakeEvent, FakeViewportRuler} from '@angular/cdk/testing';
5+
import {dispatchFakeEvent} from '@angular/cdk/testing';
76
import {Observable} from 'rxjs/Observable';
87
import {MdTab, MdTabGroup, MdTabHeaderPosition, MdTabsModule} from './index';
98

@@ -19,9 +18,6 @@ describe('MdTabGroup', () => {
1918
AsyncTabsTestApp,
2019
DisabledTabsTestApp,
2120
TabGroupWithSimpleApi,
22-
],
23-
providers: [
24-
{provide: ViewportRuler, useClass: FakeViewportRuler},
2521
]
2622
});
2723

src/lib/tabs/tab-header.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {CommonModule} from '@angular/common';
66
import {By} from '@angular/platform-browser';
77
import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
88
import {PortalModule} from '@angular/cdk/portal';
9-
import {ViewportRuler} from '@angular/cdk/scrolling';
109
import {Direction, Directionality} from '@angular/cdk/bidi';
11-
import {dispatchFakeEvent, dispatchKeyboardEvent, FakeViewportRuler} from '@angular/cdk/testing';
10+
import {dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
1211
import {MdTabHeader} from './tab-header';
1312
import {MdRippleModule} from '../core/ripple/index';
1413
import {MdInkBar} from './ink-bar';
@@ -35,7 +34,6 @@ describe('MdTabHeader', () => {
3534
],
3635
providers: [
3736
{provide: Directionality, useFactory: () => ({value: dir, change: change.asObservable()})},
38-
{provide: ViewportRuler, useClass: FakeViewportRuler},
3937
]
4038
});
4139

src/lib/tabs/tab-header.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import {
2626
} from '@angular/core';
2727
import {Directionality, Direction} from '@angular/cdk/bidi';
2828
import {RIGHT_ARROW, LEFT_ARROW, ENTER, SPACE} from '@angular/cdk/keycodes';
29-
import {auditTime, startWith} from '@angular/cdk/rxjs';
29+
import {startWith} from '@angular/cdk/rxjs';
3030
import {Subscription} from 'rxjs/Subscription';
3131
import {of as observableOf} from 'rxjs/observable/of';
3232
import {merge} from 'rxjs/observable/merge';
33-
import {fromEvent} from 'rxjs/observable/fromEvent';
3433
import {MdTabLabelWrapper} from './tab-label-wrapper';
3534
import {MdInkBar} from './ink-bar';
3635
import {CanDisableRipple, mixinDisableRipple} from '../core/common-behaviors/disable-ripple';
36+
import {ViewportRuler} from '@angular/cdk/scrolling';
3737

3838
/**
3939
* The directions that scrolling can go in when the header's tabs exceed the header width. 'After'
@@ -132,6 +132,7 @@ export class MdTabHeader extends _MdTabHeaderMixinBase
132132
constructor(private _elementRef: ElementRef,
133133
private _renderer: Renderer2,
134134
private _changeDetectorRef: ChangeDetectorRef,
135+
private _viewportRuler: ViewportRuler,
135136
@Optional() private _dir: Directionality) {
136137
super();
137138
}
@@ -184,9 +185,7 @@ export class MdTabHeader extends _MdTabHeaderMixinBase
184185
*/
185186
ngAfterContentInit() {
186187
const dirChange = this._dir ? this._dir.change : observableOf(null);
187-
const resize = typeof window !== 'undefined' ?
188-
auditTime.call(fromEvent(window, 'resize'), 150) :
189-
observableOf(null);
188+
const resize = this._viewportRuler.change(150);
190189

191190
this._realignInkBar = startWith.call(merge(dirChange, resize), null).subscribe(() => {
192191
this._updatePagination();

src/lib/tabs/tab-nav-bar/tab-nav-bar.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
22
import {Component, ViewChild} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4-
import {ViewportRuler} from '@angular/cdk/scrolling';
5-
import {dispatchFakeEvent, dispatchMouseEvent, FakeViewportRuler} from '@angular/cdk/testing';
4+
import {dispatchFakeEvent, dispatchMouseEvent} from '@angular/cdk/testing';
65
import {Direction, Directionality} from '@angular/cdk/bidi';
76
import {Subject} from 'rxjs/Subject';
87
import {MdTabNav, MdTabsModule, MdTabLink} from '../index';
@@ -23,8 +22,7 @@ describe('MdTabNavBar', () => {
2322
{provide: Directionality, useFactory: () => ({
2423
value: dir,
2524
change: dirChange.asObservable()
26-
})},
27-
{provide: ViewportRuler, useClass: FakeViewportRuler},
25+
})}
2826
]
2927
});
3028

@@ -173,7 +171,7 @@ describe('MdTabNavBar', () => {
173171
spyOn(inkBar, 'alignToElement');
174172

175173
dispatchFakeEvent(window, 'resize');
176-
tick(10);
174+
tick(150);
177175
fixture.detectChanges();
178176

179177
expect(inkBar.alignToElement).toHaveBeenCalled();

src/lib/tabs/tab-nav-bar/tab-nav-bar.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ import {
2828
import {ViewportRuler} from '@angular/cdk/scrolling';
2929
import {Directionality} from '@angular/cdk/bidi';
3030
import {Platform} from '@angular/cdk/platform';
31-
import {auditTime, takeUntil} from '@angular/cdk/rxjs';
31+
import {takeUntil} from '@angular/cdk/rxjs';
3232
import {Subject} from 'rxjs/Subject';
3333
import {of as observableOf} from 'rxjs/observable/of';
3434
import {merge} from 'rxjs/observable/merge';
35-
import {fromEvent} from 'rxjs/observable/fromEvent';
3635
import {CanDisableRipple, mixinDisableRipple} from '../../core/common-behaviors/disable-ripple';
3736
import {coerceBooleanProperty} from '@angular/cdk/coercion';
3837
import {CanDisable, mixinDisabled} from '../../core/common-behaviors/disabled';
@@ -105,7 +104,8 @@ export class MdTabNav extends _MdTabNavMixinBase implements AfterContentInit, Ca
105104
elementRef: ElementRef,
106105
@Optional() private _dir: Directionality,
107106
private _ngZone: NgZone,
108-
private _changeDetectorRef: ChangeDetectorRef) {
107+
private _changeDetectorRef: ChangeDetectorRef,
108+
private _viewportRuler: ViewportRuler) {
109109
super(renderer, elementRef);
110110
}
111111

@@ -121,14 +121,10 @@ export class MdTabNav extends _MdTabNavMixinBase implements AfterContentInit, Ca
121121

122122
ngAfterContentInit(): void {
123123
this._ngZone.runOutsideAngular(() => {
124-
let dirChange = this._dir ? this._dir.change : observableOf(null);
125-
let resize = typeof window !== 'undefined' ?
126-
auditTime.call(fromEvent(window, 'resize'), 10) :
127-
observableOf(null);
128-
129-
return takeUntil.call(merge(dirChange, resize), this._onDestroy).subscribe(() => {
130-
this._alignInkBar();
131-
});
124+
const dirChange = this._dir ? this._dir.change : observableOf(null);
125+
126+
return takeUntil.call(merge(dirChange, this._viewportRuler.change(10)), this._onDestroy)
127+
.subscribe(() => this._alignInkBar());
132128
});
133129

134130
this._setLinkDisableRipple();

0 commit comments

Comments
 (0)