ngx-lazy-mat-select
Version:
Lazy load for mat-select
117 lines (111 loc) • 5.18 kB
JavaScript
import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, Output, NgModule } from '@angular/core';
import { Subject, takeUntil, fromEvent, debounceTime, tap } from 'rxjs';
import * as i1 from '@angular/material/select';
import { MatSelectModule } from '@angular/material/select';
/** The height of the select items in `em` units. */
const SELECT_ITEM_HEIGHT_EM = 3;
class NgxLazyMatSelectDirective {
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;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.9", ngImport: i0, type: NgxLazyMatSelectDirective, deps: [{ token: i1.MatSelect }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive }); }
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.9", type: NgxLazyMatSelectDirective, selector: "[ngxLazyMatSelect]", inputs: { threshold: "threshold", debounceTime: "debounceTime", complete: "complete" }, outputs: { infiniteScroll: "infiniteScroll" }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.9", ngImport: i0, type: NgxLazyMatSelectDirective, decorators: [{
type: Directive,
args: [{
selector: '[ngxLazyMatSelect]'
}]
}], ctorParameters: function () { return [{ type: i1.MatSelect }, { type: i0.NgZone }]; }, propDecorators: { threshold: [{
type: Input
}], debounceTime: [{
type: Input
}], complete: [{
type: Input
}], infiniteScroll: [{
type: Output
}] } });
class NgxLazyMatSelectModule {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.9", ngImport: i0, type: NgxLazyMatSelectModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.9", ngImport: i0, type: NgxLazyMatSelectModule, declarations: [NgxLazyMatSelectDirective], imports: [MatSelectModule], exports: [NgxLazyMatSelectDirective] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.9", ngImport: i0, type: NgxLazyMatSelectModule, imports: [MatSelectModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.9", ngImport: i0, type: NgxLazyMatSelectModule, decorators: [{
type: NgModule,
args: [{
declarations: [
NgxLazyMatSelectDirective
],
imports: [
MatSelectModule
],
exports: [
NgxLazyMatSelectDirective
]
}]
}] });
/*
* Public API Surface of ngx-lazy-mat-select
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxLazyMatSelectDirective, NgxLazyMatSelectModule };
//# sourceMappingURL=ngx-lazy-mat-select.mjs.map