Skip to content

refactor: add virtual scroll repeater interface #19570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions goldens/ts-circular-deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
"src/cdk/scrolling/scroll-dispatcher.ts",
"src/cdk/scrolling/scrollable.ts"
],
[
"src/cdk/scrolling/virtual-for-of.ts",
"src/cdk/scrolling/virtual-scroll-viewport.ts"
],
[
"src/cdk/scrolling/virtual-scroll-strategy.ts",
"src/cdk/scrolling/virtual-scroll-viewport.ts"
Expand Down
1 change: 1 addition & 0 deletions src/cdk/scrolling/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './viewport-ruler';
export * from './virtual-for-of';
export * from './virtual-scroll-strategy';
export * from './virtual-scroll-viewport';
export * from './virtual-scroll-repeater';
4 changes: 3 additions & 1 deletion src/cdk/scrolling/virtual-for-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import {Observable, Subject, of as observableOf, isObservable} from 'rxjs';
import {pairwise, shareReplay, startWith, switchMap, takeUntil} from 'rxjs/operators';
import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
import {CdkVirtualScrollRepeater} from './virtual-scroll-repeater';


/** The context for an item rendered by `CdkVirtualForOf` */
Expand Down Expand Up @@ -74,7 +75,8 @@ function getSize(orientation: 'horizontal' | 'vertical', node: Node): number {
@Directive({
selector: '[cdkVirtualFor][cdkVirtualForOf]',
})
export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy {
export class CdkVirtualForOf<T> implements
CdkVirtualScrollRepeater<T>, CollectionViewer, DoCheck, OnDestroy {
/** Emits when the rendered view of the data changes. */
viewChange = new Subject<ListRange>();

Expand Down
18 changes: 18 additions & 0 deletions src/cdk/scrolling/virtual-scroll-repeater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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.io/license
*/

import {Observable} from 'rxjs';
import {ListRange} from '@angular/cdk/collections';

/**
* An item to be repeated by the VirtualScrollViewport
*/
export interface CdkVirtualScrollRepeater<T> {
dataStream: Observable<T[] | ReadonlyArray<T>>;
measureRangeSize(range: ListRange, orientation: 'horizontal' | 'vertical'): number;
}
10 changes: 5 additions & 5 deletions src/cdk/scrolling/virtual-scroll-viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import {
import {auditTime, startWith, takeUntil} from 'rxjs/operators';
import {ScrollDispatcher} from './scroll-dispatcher';
import {CdkScrollable, ExtendedScrollToOptions} from './scrollable';
import {CdkVirtualForOf} from './virtual-for-of';
import {VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy} from './virtual-scroll-strategy';
import {ViewportRuler} from './viewport-ruler';
import {CdkVirtualScrollRepeater} from './virtual-scroll-repeater';

/** Checks if the given ranges are equal. */
function rangesEqual(r1: ListRange, r2: ListRange): boolean {
Expand Down Expand Up @@ -131,8 +131,8 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
/** The size of the viewport (in pixels). */
private _viewportSize = 0;

/** the currently attached CdkVirtualForOf. */
private _forOf: CdkVirtualForOf<any> | null;
/** the currently attached CdkVirtualScrollRepeater. */
private _forOf: CdkVirtualScrollRepeater<any> | null;

/** The last rendered content offset that was set. */
private _renderedContentOffset = 0;
Expand Down Expand Up @@ -215,8 +215,8 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
super.ngOnDestroy();
}

/** Attaches a `CdkVirtualForOf` to this viewport. */
attach(forOf: CdkVirtualForOf<any>) {
/** Attaches a `CdkVirtualScrollRepeater` to this viewport. */
attach(forOf: CdkVirtualScrollRepeater<any>) {
if (this._forOf) {
throw Error('CdkVirtualScrollViewport is already attached.');
}
Expand Down
9 changes: 7 additions & 2 deletions tools/public_api_guard/cdk/scrolling.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export declare class CdkScrollableModule {
static ɵmod: i0.ɵɵNgModuleDefWithMeta<CdkScrollableModule, [typeof i1.CdkScrollable], never, [typeof i1.CdkScrollable]>;
}

export declare class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy {
export declare class CdkVirtualForOf<T> implements CdkVirtualScrollRepeater<T>, CollectionViewer, DoCheck, OnDestroy {
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined;
get cdkVirtualForOf(): DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined;
set cdkVirtualForOf(value: DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined);
Expand Down Expand Up @@ -107,6 +107,11 @@ export declare type CdkVirtualForOfContext<T> = {
odd: boolean;
};

export interface CdkVirtualScrollRepeater<T> {
dataStream: Observable<T[] | ReadonlyArray<T>>;
measureRangeSize(range: ListRange, orientation: 'horizontal' | 'vertical'): number;
}

export declare class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, OnDestroy {
_contentWrapper: ElementRef<HTMLElement>;
_totalContentHeight: string;
Expand All @@ -118,7 +123,7 @@ export declare class CdkVirtualScrollViewport extends CdkScrollable implements O
scrolledIndexChange: Observable<number>;
constructor(elementRef: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, ngZone: NgZone, _scrollStrategy: VirtualScrollStrategy, dir: Directionality, scrollDispatcher: ScrollDispatcher,
viewportRuler?: ViewportRuler);
attach(forOf: CdkVirtualForOf<any>): void;
attach(forOf: CdkVirtualScrollRepeater<any>): void;
checkViewportSize(): void;
detach(): void;
getDataLength(): number;
Expand Down