6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
+ import { coerceElement } from '@angular/cdk/coercion' ;
9
10
import { Platform } from '@angular/cdk/platform' ;
10
11
import { ElementRef , Injectable , NgZone , OnDestroy , Optional , Inject } from '@angular/core' ;
11
12
import { fromEvent , of as observableOf , Subject , Subscription , Observable , Observer } from 'rxjs' ;
@@ -119,23 +120,25 @@ export class ScrollDispatcher implements OnDestroy {
119
120
/**
120
121
* Returns an observable that emits whenever any of the
121
122
* scrollable ancestors of an element are scrolled.
122
- * @param elementRef Element whose ancestors to listen for.
123
+ * @param elementOrElementRef Element whose ancestors to listen for.
123
124
* @param auditTimeInMs Time to throttle the scroll events.
124
125
*/
125
- ancestorScrolled ( elementRef : ElementRef , auditTimeInMs ?: number ) : Observable < CdkScrollable | void > {
126
- const ancestors = this . getAncestorScrollContainers ( elementRef ) ;
126
+ ancestorScrolled (
127
+ elementOrElementRef : ElementRef | HTMLElement ,
128
+ auditTimeInMs ?: number ) : Observable < CdkScrollable | void > {
129
+ const ancestors = this . getAncestorScrollContainers ( elementOrElementRef ) ;
127
130
128
131
return this . scrolled ( auditTimeInMs ) . pipe ( filter ( target => {
129
132
return ! target || ancestors . indexOf ( target ) > - 1 ;
130
133
} ) ) ;
131
134
}
132
135
133
136
/** Returns all registered Scrollables that contain the provided element. */
134
- getAncestorScrollContainers ( elementRef : ElementRef ) : CdkScrollable [ ] {
137
+ getAncestorScrollContainers ( elementOrElementRef : ElementRef | HTMLElement ) : CdkScrollable [ ] {
135
138
const scrollingContainers : CdkScrollable [ ] = [ ] ;
136
139
137
140
this . scrollContainers . forEach ( ( _subscription : Subscription , scrollable : CdkScrollable ) => {
138
- if ( this . _scrollableContainsElement ( scrollable , elementRef ) ) {
141
+ if ( this . _scrollableContainsElement ( scrollable , elementOrElementRef ) ) {
139
142
scrollingContainers . push ( scrollable ) ;
140
143
}
141
144
} ) ;
@@ -149,8 +152,10 @@ export class ScrollDispatcher implements OnDestroy {
149
152
}
150
153
151
154
/** Returns true if the element is contained within the provided Scrollable. */
152
- private _scrollableContainsElement ( scrollable : CdkScrollable , elementRef : ElementRef ) : boolean {
153
- let element : HTMLElement | null = elementRef . nativeElement ;
155
+ private _scrollableContainsElement (
156
+ scrollable : CdkScrollable ,
157
+ elementOrElementRef : ElementRef | HTMLElement ) : boolean {
158
+ let element : HTMLElement | null = coerceElement ( elementOrElementRef ) ;
154
159
let scrollableElement = scrollable . getElementRef ( ) . nativeElement ;
155
160
156
161
// Traverse through the element parents until we reach null, checking if any of the elements
0 commit comments