Skip to content

Commit 26ef99c

Browse files
committed
review response
1 parent dffac63 commit 26ef99c

File tree

8 files changed

+43
-45
lines changed

8 files changed

+43
-45
lines changed

src/lib/core/core.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {PortalModule} from './portal/portal-directives';
66
import {OverlayModule} from './overlay/overlay-directives';
77
import {A11yModule, A11Y_PROVIDERS} from './a11y/index';
88
import {OVERLAY_PROVIDERS} from './overlay/overlay';
9-
import {Scroll} from './scroll/scroll';
9+
import {ScrollDispatcher} from './scroll/scroll-dispatcher';
1010
import {ScrollModule} from './scroll/scrollable';
1111

1212

@@ -48,7 +48,7 @@ export {
4848
export * from './overlay/position/connected-position-strategy';
4949
export * from './overlay/position/connected-position';
5050
export * from './scroll/scrollable';
51-
export * from './scroll/scroll';
51+
export * from './scroll/scroll-dispatcher';
5252

5353
// Gestures
5454
export {MdGestureConfig} from './gestures/MdGestureConfig';
@@ -101,8 +101,8 @@ export {coerceNumberProperty} from './coersion/number-property';
101101
export {DefaultStyleCompatibilityModeModule} from './compatibility/default-mode';
102102
export {NoConflictStyleCompatibilityMode} from './compatibility/no-conflict-mode';
103103

104-
// Scroll
105-
export {Scroll} from './scroll/scroll';
104+
// ScrollDispatcher
105+
export {ScrollDispatcher} from './scroll/scroll-dispatcher';
106106
export {Scrollable} from './scroll/scrollable';
107107

108108
@NgModule({
@@ -127,7 +127,7 @@ export class MdCoreModule {
127127
static forRoot(): ModuleWithProviders {
128128
return {
129129
ngModule: MdCoreModule,
130-
providers: [A11Y_PROVIDERS, OVERLAY_PROVIDERS, Scroll],
130+
providers: [A11Y_PROVIDERS, OVERLAY_PROVIDERS, ScrollDispatcher],
131131
};
132132
}
133133
}

src/lib/core/scroll/scroll.spec.ts renamed to src/lib/core/scroll/scroll-dispatcher.spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {inject, TestBed, async, ComponentFixture} from '@angular/core/testing';
22
import {NgModule, Component, ViewChild, ElementRef} from '@angular/core';
3-
import {Scroll} from './scroll';
3+
import {ScrollDispatcher} from './scroll-dispatcher';
44
import {ScrollModule, Scrollable} from './scrollable';
55

6-
describe('Scrollable', () => {
7-
let scroll: Scroll;
6+
describe('Scroll Dispatcher', () => {
7+
let scroll: ScrollDispatcher;
88
let fixture: ComponentFixture<ScrollingComponent>;
99

1010
beforeEach(async(() => {
@@ -15,19 +15,19 @@ describe('Scrollable', () => {
1515
TestBed.compileComponents();
1616
}));
1717

18-
beforeEach(inject([Scroll], (s: Scroll) => {
18+
beforeEach(inject([ScrollDispatcher], (s: ScrollDispatcher) => {
1919
scroll = s;
2020

2121
fixture = TestBed.createComponent(ScrollingComponent);
2222
fixture.detectChanges();
2323
}));
2424

25-
it('should register the scrollable directive with the scroll service', () => {
25+
it('should be registered with the scrollable directive with the scroll service', () => {
2626
const componentScrollable = fixture.componentInstance.scrollable;
2727
expect(scroll.scrollableReferences.has(componentScrollable)).toBe(true);
2828
});
2929

30-
it('should deregister the scrollable directive when the component is destroyed', () => {
30+
it('should have the scrollable directive deregistered when the component is destroyed', () => {
3131
const componentScrollable = fixture.componentInstance.scrollable;
3232
expect(scroll.scrollableReferences.has(componentScrollable)).toBe(true);
3333

@@ -48,7 +48,9 @@ describe('Scrollable', () => {
4848
// Emit a scroll event from the scrolling element in our component.
4949
// This event should be picked up by the scrollable directive and notify.
5050
// The notification should be picked up by the service.
51-
fixture.componentInstance.scrollingElement.nativeElement.dispatchEvent(new Event('scroll'));
51+
const scrollEvent = document.createEvent('UIEvents');
52+
scrollEvent.initUIEvent('scroll', true, true, window, 0);
53+
fixture.componentInstance.scrollingElement.nativeElement.dispatchEvent(scrollEvent);
5254

5355
expect(hasDirectiveScrollNotified).toBe(true);
5456
expect(hasServiceScrollNotified).toBe(true);
@@ -58,7 +60,7 @@ describe('Scrollable', () => {
5860

5961
/** Simple component that contains a large div and can be scrolled. */
6062
@Component({
61-
template: `<div #scrollingElement md-scrollable style="height: 9999px"></div>`
63+
template: `<div #scrollingElement cdk-scrollable style="height: 9999px"></div>`
6264
})
6365
class ScrollingComponent {
6466
@ViewChild(Scrollable) scrollable: Scrollable;
@@ -68,7 +70,7 @@ class ScrollingComponent {
6870
const TEST_COMPONENTS = [ScrollingComponent];
6971
@NgModule({
7072
imports: [ScrollModule],
71-
providers: [Scroll],
73+
providers: [ScrollDispatcher],
7274
exports: TEST_COMPONENTS,
7375
declarations: TEST_COMPONENTS,
7476
entryComponents: TEST_COMPONENTS,

src/lib/core/scroll/scroll.ts renamed to src/lib/core/scroll/scroll-dispatcher.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@ import {Scrollable} from './scrollable';
33
import {Subject} from 'rxjs/Subject';
44
import {Observable} from 'rxjs/Observable';
55
import {Subscription} from 'rxjs/Subscription';
6+
import 'rxjs/add/observable/fromEvent';
67

78

89
/**
910
* Service contained all registered Scrollable references and emits an event when any one of the
1011
* Scrollable references emit a scrolled event.
1112
*/
1213
@Injectable()
13-
export class Scroll {
14+
export class ScrollDispatcher {
1415
/** Subject for notifying that a registered scrollable reference element has been scrolled. */
15-
_scrolled: Subject<Event> = new Subject();
16+
_scrolled: Subject<void> = new Subject<void>();
1617

1718
/**
1819
* Map of all the scrollable references that are registered with the service and their
1920
* scroll event subscriptions.
2021
*/
21-
scrollableReferences: Map<Scrollable, Subscription> = new Map();
22+
scrollableReferences: WeakMap<Scrollable, Subscription> = new WeakMap();
2223

2324
constructor() {
2425
// By default, notify a scroll event when the document is scrolled or the window is resized.
25-
window.document.addEventListener('scroll', this._notify.bind(this));
26-
window.addEventListener('resize', this._notify.bind(this));
26+
Observable.fromEvent(window.document, 'scroll').subscribe(() => this._notify());
27+
Observable.fromEvent(window, 'resize').subscribe(() => this._notify());
2728
}
2829

2930
/**
3031
* Registers a Scrollable with the service and listens for its scrolled events. When the
3132
* scrollable is scrolled, the service emits the event in its scrolled observable.
3233
*/
3334
register(scrollable: Scrollable): void {
34-
const scrollSubscription = scrollable.elementScrolled().subscribe(this._notify.bind(this));
35+
const scrollSubscription = scrollable.elementScrolled().subscribe(() => this._notify());
3536
this.scrollableReferences.set(scrollable, scrollSubscription);
3637
}
3738

@@ -48,12 +49,12 @@ export class Scroll {
4849
* references (or window, document, or body) fire a scrolled event.
4950
* TODO: Add an event limiter that includes throttle with the leading and trailing events.
5051
*/
51-
scrolled(): Observable<Event> {
52+
scrolled(): Observable<void> {
5253
return this._scrolled.asObservable();
5354
}
5455

5556
/** Sends a notification that a scroll event has been fired. */
56-
_notify(e: Event) {
57-
this._scrolled.next(e);
57+
_notify() {
58+
this._scrolled.next();
5859
}
5960
}

src/lib/core/scroll/scrollable.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,33 @@ import {
22
Directive, ElementRef, OnInit, OnDestroy, ModuleWithProviders,
33
NgModule
44
} from '@angular/core';
5-
import {Subject} from 'rxjs/Subject';
65
import {Observable} from 'rxjs/Observable';
7-
import {Scroll} from './scroll';
6+
import {ScrollDispatcher} from './scroll-dispatcher';
7+
import 'rxjs/add/observable/fromEvent';
88

99

1010
/**
11-
* Sends an event when the directive's element is scrolled. Registers itself with the Scroll
11+
* Sends an event when the directive's element is scrolled. Registers itself with the ScrollDispatcher
1212
* service to include itself as part of its collection of scrolling events that it can be listened
1313
* to through the service.
1414
*/
1515
@Directive({
16-
selector: '[md-scrollable]'
16+
selector: '[cdk-scrollable]'
1717
})
1818
export class Scrollable implements OnInit, OnDestroy {
19-
/** Subject for notifying that the element has been scrolled. */
20-
private _elementScrolled: Subject<Event> = new Subject();
21-
22-
constructor(private _elementRef: ElementRef, private _scroll: Scroll) {}
19+
constructor(private _elementRef: ElementRef, private _scroll: ScrollDispatcher) {}
2320

2421
ngOnInit() {
2522
this._scroll.register(this);
26-
this._elementRef.nativeElement.addEventListener('scroll', (e: Event) => {
27-
this._elementScrolled.next(e);
28-
});
2923
}
3024

3125
ngOnDestroy() {
3226
this._scroll.deregister(this);
3327
}
3428

35-
/** Returns observable that emits an event when the scroll event is fired on the host element. */
36-
elementScrolled(): Observable<Event> {
37-
return this._elementScrolled.asObservable();
29+
/** Returns observable that emits when the scroll event is fired on the host element. */
30+
elementScrolled(): Observable<any> {
31+
return Observable.fromEvent(this._elementRef.nativeElement, 'scroll');
3832
}
3933
}
4034

@@ -47,7 +41,7 @@ export class ScrollModule {
4741
static forRoot(): ModuleWithProviders {
4842
return {
4943
ngModule: ScrollModule,
50-
providers: [Scroll]
44+
providers: [ScrollDispatcher]
5145
};
5246
}
5347
}

src/lib/sidenav/sidenav.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
<ng-content select="md-sidenav, mat-sidenav"></ng-content>
55

6-
<div class="md-sidenav-content" [ngStyle]="_getStyles()" md-scrollable>
6+
<div class="md-sidenav-content" [ngStyle]="_getStyles()" cdk-scrollable>
77
<ng-content></ng-content>
88
</div>

src/lib/sidenav/sidenav.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import {
1717
} from '@angular/core';
1818
import {CommonModule} from '@angular/common';
1919
import {Dir, MdError, coerceBooleanProperty, DefaultStyleCompatibilityModeModule} from '../core';
20-
import {A11yModule, A11Y_PROVIDERS} from '../core/a11y/index';
20+
import {A11yModule} from '../core/a11y/index';
2121
import {FocusTrap} from '../core/a11y/focus-trap';
2222
import {ESCAPE} from '../core/keyboard/keycodes';
2323
import {OverlayModule} from '../core/overlay/overlay-directives';
2424
import {ScrollModule} from '../core/scroll/scrollable';
2525
import {InteractivityChecker} from '../core/a11y/interactivity-checker';
2626
import {MdLiveAnnouncer} from '../core/a11y/live-announcer';
27-
import {Scroll} from '../core/scroll/scroll';
27+
import {ScrollDispatcher} from '../core/scroll/scroll-dispatcher';
2828

2929

3030
/** Exception thrown when two MdSidenav are matching the same side. */
@@ -522,7 +522,7 @@ export class MdSidenavModule {
522522
static forRoot(): ModuleWithProviders {
523523
return {
524524
ngModule: MdSidenavModule,
525-
providers: [MdLiveAnnouncer, InteractivityChecker, Scroll]
525+
providers: [MdLiveAnnouncer, InteractivityChecker, ScrollDispatcher]
526526
};
527527
}
528528
}

src/lib/tooltip/tooltip.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {MdTooltipInvalidPositionError} from './tooltip-errors';
2929
import {Observable} from 'rxjs/Observable';
3030
import {Subject} from 'rxjs/Subject';
3131
import {Dir} from '../core/rtl/dir';
32-
import {Scroll} from '../core/scroll/scroll';
32+
import {ScrollDispatcher} from '../core/scroll/scroll-dispatcher';
3333
import {ScrollModule} from '../core/scroll/scrollable';
3434
import {OverlayPositionBuilder} from '../core/overlay/position/overlay-position-builder';
3535
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
@@ -96,7 +96,7 @@ export class MdTooltip implements OnInit, OnDestroy {
9696
}
9797

9898
constructor(private _overlay: Overlay,
99-
private _scroll: Scroll,
99+
private _scroll: ScrollDispatcher,
100100
private _elementRef: ElementRef,
101101
private _viewContainerRef: ViewContainerRef,
102102
private _ngZone: NgZone,
@@ -378,7 +378,7 @@ export class MdTooltipModule {
378378
Overlay,
379379
OverlayPositionBuilder,
380380
ViewportRuler,
381-
Scroll
381+
ScrollDispatcher
382382
]
383383
};
384384
}

tools/gulp/tasks/components.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ task(':build:components:rollup', [':build:components:inline'], () => {
6767

6868
// Rxjs dependencies
6969
'rxjs/Subject': 'Rx',
70+
'rxjs/add/observable/fromEvent': 'Rx.Observable',
7071
'rxjs/add/observable/forkJoin': 'Rx.Observable',
7172
'rxjs/add/observable/of': 'Rx.Observable',
7273
'rxjs/add/operator/toPromise': 'Rx.Observable.prototype',

0 commit comments

Comments
 (0)