From 8704064c078bfb3458b743f69ecc9aafaf8ba1b7 Mon Sep 17 00:00:00 2001 From: Benjamin Kindle Date: Sun, 20 Jan 2019 16:32:54 -0500 Subject: [PATCH] refactor: add virtual scroll adapter --- src/cdk/scrolling/public-api.ts | 1 + src/cdk/scrolling/virtual-for-of.ts | 4 +++- src/cdk/scrolling/virtual-scroll-repeater.ts | 18 ++++++++++++++++++ src/cdk/scrolling/virtual-scroll-viewport.ts | 6 +++--- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/cdk/scrolling/virtual-scroll-repeater.ts diff --git a/src/cdk/scrolling/public-api.ts b/src/cdk/scrolling/public-api.ts index 2c1cf815d1a0..f4a89ab4032c 100644 --- a/src/cdk/scrolling/public-api.ts +++ b/src/cdk/scrolling/public-api.ts @@ -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'; diff --git a/src/cdk/scrolling/virtual-for-of.ts b/src/cdk/scrolling/virtual-for-of.ts index 82c00a8bb3a3..68531be6585c 100644 --- a/src/cdk/scrolling/virtual-for-of.ts +++ b/src/cdk/scrolling/virtual-for-of.ts @@ -27,6 +27,7 @@ import { import {Observable, Subject} 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` */ @@ -68,7 +69,8 @@ function getSize(orientation: 'horizontal' | 'vertical', node: Node): number { @Directive({ selector: '[cdkVirtualFor][cdkVirtualForOf]', }) -export class CdkVirtualForOf implements CollectionViewer, DoCheck, OnDestroy { +export class CdkVirtualForOf implements + CdkVirtualScrollRepeater, CollectionViewer, DoCheck, OnDestroy { /** Emits when the rendered view of the data changes. */ viewChange = new Subject(); diff --git a/src/cdk/scrolling/virtual-scroll-repeater.ts b/src/cdk/scrolling/virtual-scroll-repeater.ts new file mode 100644 index 000000000000..1c82eaeb5a10 --- /dev/null +++ b/src/cdk/scrolling/virtual-scroll-repeater.ts @@ -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 '../collections'; + +/** + * An item to be repeated by the VirtualScrollViewport + */ +export interface CdkVirtualScrollRepeater { + dataStream: Observable>; + measureRangeSize(range: ListRange, orientation: 'horizontal' | 'vertical'): number; +} diff --git a/src/cdk/scrolling/virtual-scroll-viewport.ts b/src/cdk/scrolling/virtual-scroll-viewport.ts index 2c71c3ddf83d..02c5908974d9 100644 --- a/src/cdk/scrolling/virtual-scroll-viewport.ts +++ b/src/cdk/scrolling/virtual-scroll-viewport.ts @@ -27,8 +27,8 @@ import {animationFrameScheduler, Observable, Subject, Observer} from 'rxjs'; 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 {CdkVirtualScrollRepeater} from './virtual-scroll-repeater'; /** Checks if the given ranges are equal. */ @@ -108,7 +108,7 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O private _viewportSize = 0; /** the currently attached CdkVirtualForOf. */ - private _forOf: CdkVirtualForOf | null; + private _forOf: CdkVirtualScrollRepeater | null; /** The last rendered content offset that was set. */ private _renderedContentOffset = 0; @@ -176,7 +176,7 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O } /** Attaches a `CdkVirtualForOf` to this viewport. */ - attach(forOf: CdkVirtualForOf) { + attach(forOf: CdkVirtualScrollRepeater) { if (this._forOf) { throw Error('CdkVirtualScrollViewport is already attached.'); }