Skip to content

Commit 378281d

Browse files
committed
feat(scroll): provide directive and service to listen to scrolling
1 parent 86123a3 commit 378281d

File tree

7 files changed

+254
-13
lines changed

7 files changed

+254
-13
lines changed

src/lib/core/core.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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';
10+
import {ScrollModule} from './scroll/scrollable';
911

1012

1113
// RTL
@@ -45,6 +47,8 @@ export {
4547
} from './overlay/overlay-directives';
4648
export * from './overlay/position/connected-position-strategy';
4749
export * from './overlay/position/connected-position';
50+
export * from './scroll/scrollable';
51+
export * from './scroll/scroll';
4852

4953
// Gestures
5054
export {MdGestureConfig} from './gestures/MdGestureConfig';
@@ -97,16 +101,33 @@ export {coerceNumberProperty} from './coersion/number-property';
97101
export {DefaultStyleCompatibilityModeModule} from './compatibility/default-mode';
98102
export {NoConflictStyleCompatibilityMode} from './compatibility/no-conflict-mode';
99103

104+
// Scroll
105+
export {Scroll} from './scroll/scroll';
106+
export {Scrollable} from './scroll/scrollable';
100107

101108
@NgModule({
102-
imports: [MdLineModule, RtlModule, MdRippleModule, PortalModule, OverlayModule, A11yModule],
103-
exports: [MdLineModule, RtlModule, MdRippleModule, PortalModule, OverlayModule, A11yModule],
109+
imports: [MdLineModule,
110+
RtlModule,
111+
MdRippleModule,
112+
PortalModule,
113+
OverlayModule,
114+
A11yModule,
115+
ScrollModule
116+
],
117+
exports: [MdLineModule,
118+
RtlModule,
119+
MdRippleModule,
120+
PortalModule,
121+
OverlayModule,
122+
A11yModule,
123+
ScrollModule
124+
],
104125
})
105126
export class MdCoreModule {
106127
static forRoot(): ModuleWithProviders {
107128
return {
108129
ngModule: MdCoreModule,
109-
providers: [A11Y_PROVIDERS, OVERLAY_PROVIDERS],
130+
providers: [A11Y_PROVIDERS, OVERLAY_PROVIDERS, Scroll],
110131
};
111132
}
112133
}

src/lib/core/scroll/scroll.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {inject, TestBed, async, ComponentFixture} from '@angular/core/testing';
2+
import {NgModule, Component, ViewChild, ElementRef} from '@angular/core';
3+
import {Scroll} from './scroll';
4+
import {ScrollModule, Scrollable} from './scrollable';
5+
6+
describe('Scrollable', () => {
7+
let scroll: Scroll;
8+
let fixture: ComponentFixture<ScrollingComponent>;
9+
10+
beforeEach(async(() => {
11+
TestBed.configureTestingModule({
12+
imports: [ScrollModule.forRoot(), ScrollTestModule],
13+
});
14+
15+
TestBed.compileComponents();
16+
}));
17+
18+
beforeEach(inject([Scroll], (s: Scroll) => {
19+
scroll = s;
20+
21+
fixture = TestBed.createComponent(ScrollingComponent);
22+
fixture.detectChanges();
23+
}));
24+
25+
it('should register the scrollable directive with the scroll service', () => {
26+
const componentScrollable = fixture.componentInstance.scrollable;
27+
expect(scroll.scrollableReferences.has(componentScrollable)).toBe(true);
28+
});
29+
30+
it('should deregister the scrollable directive when the component is destroyed', () => {
31+
const componentScrollable = fixture.componentInstance.scrollable;
32+
expect(scroll.scrollableReferences.has(componentScrollable)).toBe(true);
33+
34+
fixture.destroy();
35+
expect(scroll.scrollableReferences.has(componentScrollable)).toBe(false);
36+
});
37+
38+
it('should notify through the directive and service that a scroll event occurred', () => {
39+
let hasDirectiveScrollNotified = false;
40+
// Listen for notifications from scroll directive
41+
let scrollable = fixture.componentInstance.scrollable;
42+
scrollable.elementScrolled().subscribe(() => { hasDirectiveScrollNotified = true; });
43+
44+
// Listen for notifications from scroll service
45+
let hasServiceScrollNotified = false;
46+
scroll.scrolled().subscribe(() => { hasServiceScrollNotified = true; });
47+
48+
// Emit a scroll event from the scrolling element in our component.
49+
// This event should be picked up by the scrollable directive and notify.
50+
// The notification should be picked up by the service.
51+
fixture.componentInstance.scrollingElement.nativeElement.dispatchEvent(new Event('scroll'));
52+
53+
expect(hasDirectiveScrollNotified).toBe(true);
54+
expect(hasServiceScrollNotified).toBe(true);
55+
});
56+
});
57+
58+
59+
/** Simple component that contains a large div and can be scrolled. */
60+
@Component({
61+
template: `<div #scrollingElement md-scrollable style="height: 9999px"></div>`
62+
})
63+
class ScrollingComponent {
64+
@ViewChild(Scrollable) scrollable: Scrollable;
65+
@ViewChild('scrollingElement') scrollingElement: ElementRef;
66+
}
67+
68+
const TEST_COMPONENTS = [ScrollingComponent];
69+
@NgModule({
70+
imports: [ScrollModule],
71+
providers: [Scroll],
72+
exports: TEST_COMPONENTS,
73+
declarations: TEST_COMPONENTS,
74+
entryComponents: TEST_COMPONENTS,
75+
})
76+
class ScrollTestModule { }

src/lib/core/scroll/scroll.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {Injectable} from '@angular/core';
2+
import {Scrollable} from './scrollable';
3+
import {Subject} from 'rxjs/Subject';
4+
import {Observable} from 'rxjs/Observable';
5+
import {Subscription} from 'rxjs/Subscription';
6+
7+
8+
/**
9+
* Service contained all registered Scrollable references and emits an event when any one of the
10+
* Scrollable references emit a scrolled event.
11+
*/
12+
@Injectable()
13+
export class Scroll {
14+
/** Subject for notifying that a registered scrollable reference element has been scrolled. */
15+
_scrolled: Subject<Event> = new Subject();
16+
17+
/**
18+
* Map of all the scrollable references that are registered with the service and their
19+
* scroll event subscriptions.
20+
*/
21+
scrollableReferences: Map<Scrollable, Subscription> = new Map();
22+
23+
constructor() {
24+
// 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));
27+
}
28+
29+
/**
30+
* Registers a Scrollable with the service and listens for its scrolled events. When the
31+
* scrollable is scrolled, the service emits the event in its scrolled observable.
32+
*/
33+
register(scrollable: Scrollable): void {
34+
const scrollSubscription = scrollable.elementScrolled().subscribe(this._notify.bind(this));
35+
this.scrollableReferences.set(scrollable, scrollSubscription);
36+
}
37+
38+
/**
39+
* Deregisters a Scrollable reference and unsubscribes from its scroll event observable.
40+
*/
41+
deregister(scrollable: Scrollable): void {
42+
this.scrollableReferences.get(scrollable).unsubscribe();
43+
this.scrollableReferences.delete(scrollable);
44+
}
45+
46+
/**
47+
* Returns an observable that emits an event whenever any of the registered Scrollable
48+
* references (or window, document, or body) fire a scrolled event.
49+
* TODO: Add an event limiter that includes throttle with the leading and trailing events.
50+
*/
51+
scrolled(): Observable<Event> {
52+
return this._scrolled.asObservable();
53+
}
54+
55+
/** Sends a notification that a scroll event has been fired. */
56+
_notify(e: Event) {
57+
this._scrolled.next(e);
58+
}
59+
}

src/lib/core/scroll/scrollable.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
Directive, ElementRef, OnInit, OnDestroy, ModuleWithProviders,
3+
NgModule
4+
} from '@angular/core';
5+
import {Subject} from 'rxjs/Subject';
6+
import {Observable} from 'rxjs/Observable';
7+
import {Scroll} from './scroll';
8+
9+
10+
/**
11+
* Sends an event when the directive's element is scrolled. Registers itself with the Scroll
12+
* service to include itself as part of its collection of scrolling events that it can be listened
13+
* to through the service.
14+
*/
15+
@Directive({
16+
selector: '[md-scrollable]'
17+
})
18+
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) {}
23+
24+
ngOnInit() {
25+
this._scroll.register(this);
26+
this._elementRef.nativeElement.addEventListener('scroll', (e: Event) => {
27+
this._elementScrolled.next(e);
28+
});
29+
}
30+
31+
ngOnDestroy() {
32+
this._scroll.deregister(this);
33+
}
34+
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();
38+
}
39+
}
40+
41+
42+
@NgModule({
43+
exports: [Scrollable],
44+
declarations: [Scrollable],
45+
})
46+
export class ScrollModule {
47+
static forRoot(): ModuleWithProviders {
48+
return {
49+
ngModule: ScrollModule,
50+
providers: [Scroll]
51+
};
52+
}
53+
}

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()">
6+
<div class="md-sidenav-content" [ngStyle]="_getStyles()" md-scrollable>
77
<ng-content></ng-content>
88
</div>

src/lib/sidenav/sidenav.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ import {Dir, MdError, coerceBooleanProperty, DefaultStyleCompatibilityModeModule
2020
import {A11yModule, A11Y_PROVIDERS} from '../core/a11y/index';
2121
import {FocusTrap} from '../core/a11y/focus-trap';
2222
import {ESCAPE} from '../core/keyboard/keycodes';
23+
import {OverlayModule} from '../core/overlay/overlay-directives';
24+
import {ScrollModule} from '../core/scroll/scrollable';
25+
import {InteractivityChecker} from '../core/a11y/interactivity-checker';
26+
import {MdLiveAnnouncer} from '../core/a11y/live-announcer';
27+
import {Scroll} from '../core/scroll/scroll';
2328

2429

2530
/** Exception thrown when two MdSidenav are matching the same side. */
@@ -503,15 +508,21 @@ export class MdSidenavLayout implements AfterContentInit {
503508

504509

505510
@NgModule({
506-
imports: [CommonModule, DefaultStyleCompatibilityModeModule, A11yModule],
511+
imports: [
512+
CommonModule,
513+
DefaultStyleCompatibilityModeModule,
514+
A11yModule,
515+
OverlayModule,
516+
ScrollModule
517+
],
507518
exports: [MdSidenavLayout, MdSidenav, DefaultStyleCompatibilityModeModule],
508519
declarations: [MdSidenavLayout, MdSidenav],
509520
})
510521
export class MdSidenavModule {
511522
static forRoot(): ModuleWithProviders {
512523
return {
513524
ngModule: MdSidenavModule,
514-
providers: [A11Y_PROVIDERS]
525+
providers: [MdLiveAnnouncer, InteractivityChecker, Scroll]
515526
};
516527
}
517528
}

src/lib/tooltip/tooltip.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
animate,
1414
AnimationTransitionEvent,
1515
NgZone,
16-
Optional,
16+
Optional, OnDestroy, OnInit,
1717
} from '@angular/core';
1818
import {
1919
Overlay,
@@ -23,13 +23,16 @@ import {
2323
ComponentPortal,
2424
OverlayConnectionPosition,
2525
OriginConnectionPosition,
26-
OVERLAY_PROVIDERS,
2726
DefaultStyleCompatibilityModeModule,
2827
} from '../core';
2928
import {MdTooltipInvalidPositionError} from './tooltip-errors';
3029
import {Observable} from 'rxjs/Observable';
3130
import {Subject} from 'rxjs/Subject';
3231
import {Dir} from '../core/rtl/dir';
32+
import {Scroll} from '../core/scroll/scroll';
33+
import {ScrollModule} from '../core/scroll/scrollable';
34+
import {OverlayPositionBuilder} from '../core/overlay/position/overlay-position-builder';
35+
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
3336

3437
export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | 'after';
3538

@@ -52,7 +55,7 @@ export const TOUCHEND_HIDE_DELAY = 1500;
5255
},
5356
exportAs: 'mdTooltip',
5457
})
55-
export class MdTooltip {
58+
export class MdTooltip implements OnInit, OnDestroy {
5659
_overlayRef: OverlayRef;
5760
_tooltipInstance: TooltipComponent;
5861

@@ -92,10 +95,23 @@ export class MdTooltip {
9295
}
9396
}
9497

95-
constructor(private _overlay: Overlay, private _elementRef: ElementRef,
96-
private _viewContainerRef: ViewContainerRef, private _ngZone: NgZone,
98+
constructor(private _overlay: Overlay,
99+
private _scroll: Scroll,
100+
private _elementRef: ElementRef,
101+
private _viewContainerRef: ViewContainerRef,
102+
private _ngZone: NgZone,
97103
@Optional() private _dir: Dir) {}
98104

105+
ngOnInit() {
106+
// When a scroll on the page occurs, update the position in case this tooltip needs
107+
// to be repositioned.
108+
this._scroll.scrolled().subscribe(() => {
109+
if (this._overlayRef) {
110+
this._overlayRef.updatePosition();
111+
}
112+
});
113+
}
114+
99115
/** Dispose the tooltip when destroyed */
100116
ngOnDestroy() {
101117
if (this._tooltipInstance) {
@@ -349,7 +365,7 @@ export class TooltipComponent {
349365

350366

351367
@NgModule({
352-
imports: [OverlayModule, DefaultStyleCompatibilityModeModule],
368+
imports: [OverlayModule, DefaultStyleCompatibilityModeModule, ScrollModule],
353369
exports: [MdTooltip, TooltipComponent, DefaultStyleCompatibilityModeModule],
354370
declarations: [MdTooltip, TooltipComponent],
355371
entryComponents: [TooltipComponent],
@@ -358,7 +374,12 @@ export class MdTooltipModule {
358374
static forRoot(): ModuleWithProviders {
359375
return {
360376
ngModule: MdTooltipModule,
361-
providers: OVERLAY_PROVIDERS,
377+
providers: [
378+
Overlay,
379+
OverlayPositionBuilder,
380+
ViewportRuler,
381+
Scroll
382+
]
362383
};
363384
}
364385
}

0 commit comments

Comments
 (0)