All files / components/virtual-scroller flexible-virtual-scroll-strategy.ts

75% Statements 15/20
50% Branches 1/2
55.55% Functions 5/9
72.22% Lines 13/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 611x         1x 1x     1x 1x             1x 12x             12x     12x         12x       11x       3x       10x                              
import {
    CdkVirtualScrollViewport,
    FixedSizeVirtualScrollStrategy,
    VirtualScrollStrategy,
} from '@angular/cdk/scrolling';
import { AutoSizeVirtualScrollStrategy } from '@angular/cdk-experimental/scrolling';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
 
const MIN_BUFFER_PX = 200;
const MAX_BUFFER_PX = 400;
 
/**
 * Delegating scroll strategy that switches between fixed-size and autosize
 * based on configuration. Must be configured before the viewport's ngOnInit.
 */
@Injectable()
export class FlexibleVirtualScrollStrategy implements VirtualScrollStrategy {
    private delegate: VirtualScrollStrategy = new AutoSizeVirtualScrollStrategy(MIN_BUFFER_PX, MAX_BUFFER_PX);
 
    get scrolledIndexChange(): Observable<number> {
        return this.delegate.scrolledIndexChange;
    }
 
    configure(itemSize: number | undefined): void {
        Iif (itemSize !== undefined) {
            this.delegate = new FixedSizeVirtualScrollStrategy(itemSize, MIN_BUFFER_PX, MAX_BUFFER_PX);
        } else {
            this.delegate = new AutoSizeVirtualScrollStrategy(MIN_BUFFER_PX, MAX_BUFFER_PX);
        }
    }
 
    attach(viewport: CdkVirtualScrollViewport): void {
        this.delegate.attach(viewport);
    }
 
    detach(): void {
        this.delegate.detach();
    }
 
    onContentScrolled(): void {
        this.delegate.onContentScrolled();
    }
 
    onDataLengthChanged(): void {
        this.delegate.onDataLengthChanged();
    }
 
    onContentRendered(): void {
        this.delegate.onContentRendered();
    }
 
    onRenderedOffsetChanged(): void {
        this.delegate.onRenderedOffsetChanged();
    }
 
    scrollToIndex(index: number, behavior: ScrollBehavior): void {
        this.delegate.scrollToIndex(index, behavior);
    }
}