|
| 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 | +} |
0 commit comments