import { Directive, EventEmitter, Input, Output } from '@angular/core'; import { debounceTime, takeUntil, tap } from 'rxjs/operators'; import { fromEvent, Subject } from 'rxjs'; import * as i0 from "@angular/core"; import * as i1 from "@angular/material/select"; /** The height of the select items in `em` units. */ const SELECT_ITEM_HEIGHT_EM = 3; export class MatSelectInfiniteScrollDirective { constructor(matSelect, ngZone) { this.matSelect = matSelect; this.ngZone = ngZone; this.threshold = '15%'; this.debounceTime = 150; this.infiniteScroll = new EventEmitter(); this.thrPx = 0; this.thrPc = 0; this.singleOptionHeight = SELECT_ITEM_HEIGHT_EM; this.destroyed$ = new Subject(); } ngOnInit() { this.evaluateThreshold(); } ngAfterViewInit() { this.matSelect.openedChange.pipe(takeUntil(this.destroyed$)).subscribe((opened) => { if (opened) { this.panel = this.matSelect.panel.nativeElement; this.singleOptionHeight = this.getSelectItemHeightPx(); this.registerScrollListener(); } }); } ngOnDestroy() { this.destroyed$.next(true); this.destroyed$.complete(); } evaluateThreshold() { if (this.threshold.lastIndexOf('%') > -1) { this.thrPx = 0; this.thrPc = (parseFloat(this.threshold) / 100); } else { this.thrPx = parseFloat(this.threshold); this.thrPc = 0; } } registerScrollListener() { fromEvent(this.panel, 'scroll').pipe(takeUntil(this.destroyed$), debounceTime(this.debounceTime), tap((event) => { this.handleScrollEvent(event); })).subscribe(); } handleScrollEvent(event) { this.ngZone.runOutsideAngular(() => { if (this.complete) { return; } const countOfRenderedOptions = this.matSelect.options.length; const infiniteScrollDistance = this.singleOptionHeight * countOfRenderedOptions; const threshold = this.thrPc !== 0 ? (infiniteScrollDistance * this.thrPc) : this.thrPx; const scrolledDistance = this.panel.clientHeight + event.target.scrollTop; if ((scrolledDistance + threshold) >= infiniteScrollDistance) { this.ngZone.run(() => this.infiniteScroll.emit()); } }); } getSelectItemHeightPx() { return parseFloat(getComputedStyle(this.panel).fontSize) * SELECT_ITEM_HEIGHT_EM; } } MatSelectInfiniteScrollDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: MatSelectInfiniteScrollDirective, deps: [{ token: i1.MatSelect }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive }); MatSelectInfiniteScrollDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.1.3", type: MatSelectInfiniteScrollDirective, selector: "[msInfiniteScroll]", inputs: { threshold: "threshold", debounceTime: "debounceTime", complete: "complete" }, outputs: { infiniteScroll: "infiniteScroll" }, ngImport: i0 }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: MatSelectInfiniteScrollDirective, decorators: [{ type: Directive, args: [{ selector: '[msInfiniteScroll]' }] }], ctorParameters: function () { return [{ type: i1.MatSelect }, { type: i0.NgZone }]; }, propDecorators: { threshold: [{ type: Input }], debounceTime: [{ type: Input }], complete: [{ type: Input }], infiniteScroll: [{ type: Output }] } }); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWF0LXNlbGVjdC1pbmZpbml0ZS1zY3JvbGwuZGlyZWN0aXZlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvbmctbWF0LXNlbGVjdC1pbmZpbml0ZS1zY3JvbGwvc3JjL2xpYi9tYXQtc2VsZWN0LWluZmluaXRlLXNjcm9sbC5kaXJlY3RpdmUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFnQixTQUFTLEVBQUUsWUFBWSxFQUFFLEtBQUssRUFBNkIsTUFBTSxFQUFDLE1BQU0sZUFBZSxDQUFDO0FBRS9HLE9BQU8sRUFBQyxZQUFZLEVBQUUsU0FBUyxFQUFFLEdBQUcsRUFBQyxNQUFNLGdCQUFnQixDQUFDO0FBQzVELE9BQU8sRUFBQyxTQUFTLEVBQUUsT0FBTyxFQUFDLE1BQU0sTUFBTSxDQUFDOzs7QUFFeEMsb0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