-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathscroll-dispatcher.ts
194 lines (169 loc) · 6.72 KB
/
scroll-dispatcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {coerceElement} from '../coercion';
import {Platform} from '../platform';
import {
DOCUMENT,
ElementRef,
Injectable,
NgZone,
OnDestroy,
RendererFactory2,
inject,
} from '@angular/core';
import {of as observableOf, Subject, Subscription, Observable, Observer} from 'rxjs';
import {auditTime, filter} from 'rxjs/operators';
import type {CdkScrollable} from './scrollable';
/** Time in ms to throttle the scrolling events by default. */
export const DEFAULT_SCROLL_TIME = 20;
/**
* Service contained all registered Scrollable references and emits an event when any one of the
* Scrollable references emit a scrolled event.
*/
@Injectable({providedIn: 'root'})
export class ScrollDispatcher implements OnDestroy {
private _ngZone = inject(NgZone);
private _platform = inject(Platform);
private _renderer = inject(RendererFactory2).createRenderer(null, null);
private _document = inject(DOCUMENT);
private _cleanupGlobalListener: (() => void) | undefined;
private _lastScrollFromDocument = false;
constructor(...args: unknown[]);
constructor() {}
/** Subject for notifying that a registered scrollable reference element has been scrolled. */
private readonly _scrolled = new Subject<CdkScrollable | void>();
/** Keeps track of the amount of subscriptions to `scrolled`. Used for cleaning up afterwards. */
private _scrolledCount = 0;
/**
* Map of all the scrollable references that are registered with the service and their
* scroll event subscriptions.
*/
scrollContainers: Map<CdkScrollable, Subscription> = new Map();
/**
* Registers a scrollable instance with the service and listens for its scrolled events. When the
* scrollable is scrolled, the service emits the event to its scrolled observable.
* @param scrollable Scrollable instance to be registered.
*/
register(scrollable: CdkScrollable): void {
if (!this.scrollContainers.has(scrollable)) {
this.scrollContainers.set(
scrollable,
scrollable.elementScrolled().subscribe(() => this._scrolled.next(scrollable)),
);
}
}
/**
* De-registers a Scrollable reference and unsubscribes from its scroll event observable.
* @param scrollable Scrollable instance to be deregistered.
*/
deregister(scrollable: CdkScrollable): void {
const scrollableReference = this.scrollContainers.get(scrollable);
if (scrollableReference) {
scrollableReference.unsubscribe();
this.scrollContainers.delete(scrollable);
}
}
/**
* Returns an observable that emits an event whenever any of the registered Scrollable
* references (or window, document, or body) fire a scrolled event. Can provide a time in ms
* to override the default "throttle" time.
*
* **Note:** in order to avoid hitting change detection for every scroll event,
* all of the events emitted from this stream will be run outside the Angular zone.
* If you need to update any data bindings as a result of a scroll event, you have
* to run the callback using `NgZone.run`.
*/
scrolled(auditTimeInMs: number = DEFAULT_SCROLL_TIME): Observable<CdkScrollable | void> {
if (!this._platform.isBrowser) {
return observableOf<void>();
}
return new Observable((observer: Observer<CdkScrollable | void>) => {
if (!this._cleanupGlobalListener) {
this._cleanupGlobalListener = this._ngZone.runOutsideAngular(() =>
this._renderer.listen(
'document',
'scroll',
(event: Event) => {
this._lastScrollFromDocument = event.target === this._document;
this._scrolled.next();
},
{capture: true},
),
);
}
// In the case of a 0ms delay, use an observable without auditTime
// since it does add a perceptible delay in processing overhead.
const subscription =
auditTimeInMs > 0
? this._scrolled.pipe(auditTime(auditTimeInMs)).subscribe(observer)
: this._scrolled.subscribe(observer);
this._scrolledCount++;
return () => {
subscription.unsubscribe();
this._scrolledCount--;
if (!this._scrolledCount) {
this._lastScrollFromDocument = false;
this._cleanupGlobalListener?.();
this._cleanupGlobalListener = undefined;
}
};
});
}
ngOnDestroy() {
this._cleanupGlobalListener?.();
this._cleanupGlobalListener = undefined;
this.scrollContainers.forEach((_, container) => this.deregister(container));
this._scrolled.complete();
}
/**
* Returns an observable that emits whenever any of the
* scrollable ancestors of an element are scrolled.
* @param elementOrElementRef Element whose ancestors to listen for.
* @param auditTimeInMs Time to throttle the scroll events.
*/
ancestorScrolled(
elementOrElementRef: ElementRef | HTMLElement,
auditTimeInMs?: number,
): Observable<CdkScrollable | void> {
const ancestors = this.getAncestorScrollContainers(elementOrElementRef);
return this.scrolled(auditTimeInMs).pipe(
filter(target => {
// The document is using capturing for its `scroll` event which means that we'll usually
// get two events here. This is what we want in most cases, but for the ancestor scrolling
// we actually want to know the exact ancestor that was scrolled.
return target ? ancestors.indexOf(target) > -1 : this._lastScrollFromDocument;
}),
);
}
/** Returns all registered Scrollables that contain the provided element. */
getAncestorScrollContainers(elementOrElementRef: ElementRef | HTMLElement): CdkScrollable[] {
const scrollingContainers: CdkScrollable[] = [];
this.scrollContainers.forEach((_subscription: Subscription, scrollable: CdkScrollable) => {
if (this._scrollableContainsElement(scrollable, elementOrElementRef)) {
scrollingContainers.push(scrollable);
}
});
return scrollingContainers;
}
/** Returns true if the element is contained within the provided Scrollable. */
private _scrollableContainsElement(
scrollable: CdkScrollable,
elementOrElementRef: ElementRef | HTMLElement,
): boolean {
let element: HTMLElement | null = coerceElement(elementOrElementRef);
let scrollableElement = scrollable.getElementRef().nativeElement;
// Traverse through the element parents until we reach null, checking if any of the elements
// are the scrollable's element.
do {
if (element == scrollableElement) {
return true;
}
} while ((element = element!.parentElement));
return false;
}
}