Skip to content

Commit c964ada

Browse files
committed
add autosize virtual scroll strategy
1 parent 5de20aa commit c964ada

File tree

5 files changed

+122
-12
lines changed

5 files changed

+122
-12
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Directive, forwardRef, Input, OnChanges} from '@angular/core';
10+
import {VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy} from './virtual-scroll-strategy';
11+
import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
12+
13+
14+
/** Virtual scrolling strategy for lists with items of unknown or dynamic size. */
15+
export class AutoSizeVirtualScrollStrategy implements VirtualScrollStrategy {
16+
/** The attached viewport. */
17+
private _viewport: CdkVirtualScrollViewport | null = null;
18+
19+
/** The size of the items in the virtually scrolling list. */
20+
private _minBufferPx: number;
21+
22+
/** The number of buffer items to render beyond the edge of the viewport. */
23+
private _addBufferPx: number;
24+
25+
/**
26+
* @param minBufferPx The minimum amount of buffer rendered beyond the viewport (in pixels).
27+
* If the amount of buffer dips below this number, more items will be rendered.
28+
* @param addBufferPx The number of pixels worth of buffer to shoot for when rendering new items.
29+
* If the actual amount turns out to be less it will not necessarily trigger an additional
30+
* rendering cycle (as long as the amount of buffer is still greater than `minBufferPx`).
31+
*/
32+
constructor(minBufferPx: number, addBufferPx: number) {
33+
this._minBufferPx = minBufferPx;
34+
this._addBufferPx = addBufferPx;
35+
}
36+
37+
/**
38+
* Attaches this scroll strategy to a viewport.
39+
* @param viewport The viewport to attach this strategy to.
40+
*/
41+
attach(viewport: CdkVirtualScrollViewport) {
42+
this._viewport = viewport;
43+
// TODO: kick off rendering (start with totally made up size estimate).
44+
}
45+
46+
/** Detaches this scroll strategy from the currently attached viewport. */
47+
detach() {
48+
this._viewport = null;
49+
}
50+
51+
/** Called when the viewport is scrolled (debounced using requestAnimationFrame). */
52+
onContentScrolled() {
53+
// TODO: do stuff.
54+
}
55+
56+
/** Called when the length of the data changes. */
57+
onDataLengthChanged() {
58+
// TODO: do stuff.
59+
}
60+
61+
updateBufferSize(minBufferPx, addBufferPx) {
62+
this._minBufferPx = minBufferPx;
63+
this._addBufferPx = addBufferPx;
64+
}
65+
}
66+
67+
/**
68+
* Provider factory for `AutoSizeVirtualScrollStrategy` that simply extracts the already created
69+
* `AutoSizeVirtualScrollStrategy` from the given directive.
70+
* @param autoSizeDir The instance of `CdkAutoSizeVirtualScroll` to extract the
71+
* `AutoSizeVirtualScrollStrategy` from.
72+
*/
73+
export function _autoSizeVirtualScrollStrategyFactory(autoSizeDir: CdkAutoSizeVirtualScroll) {
74+
return autoSizeDir._scrollStrategy;
75+
}
76+
77+
78+
/** A virtual scroll strategy that supports unknown or dynamic size items. */
79+
@Directive({
80+
selector: 'cdk-virtual-scroll-viewport[autosize]',
81+
providers: [{
82+
provide: VIRTUAL_SCROLL_STRATEGY,
83+
useFactory: _autoSizeVirtualScrollStrategyFactory,
84+
deps: [forwardRef(() => CdkAutoSizeVirtualScroll)],
85+
}],
86+
})
87+
export class CdkAutoSizeVirtualScroll implements OnChanges {
88+
/**
89+
* The minimum amount of buffer rendered beyond the viewport (in pixels).
90+
* If the amount of buffer dips below this number, more items will be rendered.
91+
*/
92+
@Input() minBufferPx = 20;
93+
94+
/**
95+
* The number of pixels worth of buffer to shoot for when rendering new items.
96+
* If the actual amount turns out to be less it will not necessarily trigger an additional
97+
* rendering cycle (as long as the amount of buffer is still greater than `minBufferPx`).
98+
*/
99+
@Input() addBufferPx = 5;
100+
101+
/** The scroll strategy used by this directive. */
102+
_scrollStrategy = new AutoSizeVirtualScrollStrategy(this.minBufferPx, this.addBufferPx);
103+
104+
ngOnChanges() {
105+
this._scrollStrategy.updateBufferSize(this.minBufferPx, this.addBufferPx);
106+
}
107+
}

src/cdk-experimental/scrolling/fixed-size-virtual-scroll.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class FixedSizeVirtualScrollStrategy implements VirtualScrollStrategy {
2525

2626
/**
2727
* @param itemSize The size of the items in the virtually scrolling list.
28-
* @param bufferSize he number of buffer items to render beyond the edge of the viewport.
28+
* @param bufferSize The number of buffer items to render beyond the edge of the viewport.
2929
*/
3030
constructor(itemSize: number, bufferSize: number) {
3131
this._itemSize = itemSize;
@@ -115,12 +115,12 @@ export class FixedSizeVirtualScrollStrategy implements VirtualScrollStrategy {
115115

116116

117117
/**
118-
* Provider factory for `VirtualScrollFixedSizeStrategy` that simply extracts the already created
119-
* `VirtualScrollFixedSizeStrategy` from the given directive.
120-
* @param fixedSizeDir The instance of `CdkVirtualScrollFixedSize` to extract the
121-
* `VirtualScrollFixedSizeStrategy` from.
118+
* Provider factory for `FixedSizeVirtualScrollStrategy` that simply extracts the already created
119+
* `FixedSizeVirtualScrollStrategy` from the given directive.
120+
* @param fixedSizeDir The instance of `CdkFixedSizeVirtualScroll` to extract the
121+
* `FixedSizeVirtualScrollStrategy` from.
122122
*/
123-
export function _virtualScrollFixedSizeStrategyFactory(fixedSizeDir: CdkFixedSizeVirtualScroll) {
123+
export function _fixedSizeVirtualScrollStrategyFactory(fixedSizeDir: CdkFixedSizeVirtualScroll) {
124124
return fixedSizeDir._scrollStrategy;
125125
}
126126

@@ -130,7 +130,7 @@ export function _virtualScrollFixedSizeStrategyFactory(fixedSizeDir: CdkFixedSiz
130130
selector: 'cdk-virtual-scroll-viewport[itemSize]',
131131
providers: [{
132132
provide: VIRTUAL_SCROLL_STRATEGY,
133-
useFactory: _virtualScrollFixedSizeStrategyFactory,
133+
useFactory: _fixedSizeVirtualScrollStrategyFactory,
134134
deps: [forwardRef(() => CdkFixedSizeVirtualScroll)],
135135
}],
136136
})

src/cdk-experimental/scrolling/public-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
export * from './auto-size-virtual-scroll';
10+
export * from './fixed-size-virtual-scroll';
911
export * from './scrolling-module';
1012
export * from './virtual-for-of';
11-
export * from './fixed-size-virtual-scroll';
1213
export * from './virtual-scroll-strategy';
1314
export * from './virtual-scroll-viewport';

src/cdk-experimental/scrolling/scrolling-module.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10-
import {CdkVirtualForOf} from './virtual-for-of';
10+
import {CdkAutoSizeVirtualScroll} from './auto-size-virtual-scroll';
1111
import {CdkFixedSizeVirtualScroll} from './fixed-size-virtual-scroll';
12+
import {CdkVirtualForOf} from './virtual-for-of';
1213
import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
1314

1415

1516
@NgModule({
1617
exports: [
17-
CdkVirtualForOf,
18+
CdkAutoSizeVirtualScroll,
1819
CdkFixedSizeVirtualScroll,
20+
CdkVirtualForOf,
1921
CdkVirtualScrollViewport,
2022
],
2123
declarations: [
22-
CdkVirtualForOf,
24+
CdkAutoSizeVirtualScroll,
2325
CdkFixedSizeVirtualScroll,
26+
CdkVirtualForOf,
2427
CdkVirtualScrollViewport,
2528
],
2629
})

tools/package-tools/rollup-globals.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,4 @@ export const rollupGlobals = {
9191
'rxjs/operators/take': 'Rx.operators',
9292
'rxjs/operators/takeUntil': 'Rx.operators',
9393
'rxjs/operators/tap': 'Rx.operators',
94-
'rxjs/operators/throttleTime': 'Rx.operators',
9594
};

0 commit comments

Comments
 (0)