@jase7/ng-mat-select-infinite-scroll
Version:
Adds missing infinite scroll functionality for the angular material select component
114 lines (108 loc) • 5.42 kB
JavaScript
import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, Output, NgModule } from '@angular/core';
import { takeUntil, debounceTime, tap } from 'rxjs/operators';
import { Subject, fromEvent } from 'rxjs';
import * as i1 from '@angular/material/legacy-select';
import { MatSelectModule } from '@angular/material/select';
/** The height of the select items in `em` units. */
const SELECT_ITEM_HEIGHT_EM = 3;
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: "15.2.9", ngImport: i0, type: MatSelectInfiniteScrollDirective, deps: [{ token: i1.MatLegacySelect }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive });
MatSelectInfiniteScrollDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.9", type: MatSelectInfiniteScrollDirective, selector: "[msInfiniteScroll]", inputs: { threshold: "threshold", debounceTime: "debounceTime", complete: "complete" }, outputs: { infiniteScroll: "infiniteScroll" }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: MatSelectInfiniteScrollDirective, decorators: [{
type: Directive,
args: [{
selector: '[msInfiniteScroll]'
}]
}], ctorParameters: function () { return [{ type: i1.MatLegacySelect }, { type: i0.NgZone }]; }, propDecorators: { threshold: [{
type: Input
}], debounceTime: [{
type: Input
}], complete: [{
type: Input
}], infiniteScroll: [{
type: Output
}] } });
class MatSelectInfiniteScrollModule {
}
MatSelectInfiniteScrollModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: MatSelectInfiniteScrollModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
MatSelectInfiniteScrollModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.9", ngImport: i0, type: MatSelectInfiniteScrollModule, declarations: [MatSelectInfiniteScrollDirective], imports: [MatSelectModule], exports: [MatSelectInfiniteScrollDirective] });
MatSelectInfiniteScrollModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: MatSelectInfiniteScrollModule, imports: [MatSelectModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: MatSelectInfiniteScrollModule, decorators: [{
type: NgModule,
args: [{
declarations: [MatSelectInfiniteScrollDirective],
imports: [
MatSelectModule
],
exports: [MatSelectInfiniteScrollDirective]
}]
}] });
/*
* Public API Surface of ng-mat-select-infinite-scroll
*/
/**
* Generated bundle index. Do not edit.
*/
export { MatSelectInfiniteScrollDirective, MatSelectInfiniteScrollModule };
//# sourceMappingURL=jase7-ng-mat-select-infinite-scroll.mjs.map