primeng
Version:
PrimeNG is an open source UI library for Angular featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeB
102 lines (98 loc) • 4.1 kB
JavaScript
import { isPlatformBrowser } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, inject, ViewContainerRef, TemplateRef, ContentChild, Output, Directive, NgModule } from '@angular/core';
import { BaseComponent } from 'primeng/basecomponent';
/**
* Defer postpones the loading the content that is initially not in the viewport until it becomes visible on scroll.
* @deprecated use Angular defer block instead.
* @group Components
*/
class Defer extends BaseComponent {
/**
* Callback to invoke when deferred content is loaded.
* @param {Event} event - Browser event.
* @group Emits
*/
onLoad = new EventEmitter();
template;
documentScrollListener;
view;
viewContainer = inject(ViewContainerRef);
ngOnInit() {
super.ngOnInit();
console.log('Defer is deprecated as of v18, use Angular defer block instead.');
}
ngAfterViewInit() {
super.ngAfterViewInit();
if (isPlatformBrowser(this.platformId)) {
if (this.shouldLoad()) {
this.load();
}
if (!this.isLoaded()) {
this.documentScrollListener = this.renderer.listen(this.document.defaultView, 'scroll', () => {
if (this.shouldLoad()) {
this.load();
this.documentScrollListener && this.documentScrollListener();
this.documentScrollListener = null;
}
});
}
}
}
shouldLoad() {
if (this.isLoaded()) {
return false;
}
else {
let rect = this.el.nativeElement.getBoundingClientRect();
let docElement = this.document.documentElement;
let winHeight = docElement.clientHeight;
return winHeight >= rect.top;
}
}
load() {
this.view = this.viewContainer.createEmbeddedView(this.template);
this.onLoad.emit();
this.cd.detectChanges();
}
isLoaded() {
return this.view != null && isPlatformBrowser(this.platformId);
}
ngOnDestroy() {
this.view = null;
if (this.documentScrollListener) {
this.documentScrollListener();
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: Defer, deps: null, target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.5", type: Defer, isStandalone: true, selector: "[pDefer]", outputs: { onLoad: "onLoad" }, queries: [{ propertyName: "template", first: true, predicate: TemplateRef, descendants: true }], usesInheritance: true, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: Defer, decorators: [{
type: Directive,
args: [{
selector: '[pDefer]',
standalone: true
}]
}], propDecorators: { onLoad: [{
type: Output
}], template: [{
type: ContentChild,
args: [TemplateRef]
}] } });
class DeferModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: DeferModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.5", ngImport: i0, type: DeferModule, imports: [Defer], exports: [Defer] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: DeferModule });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: DeferModule, decorators: [{
type: NgModule,
args: [{
imports: [Defer],
exports: [Defer]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { Defer, DeferModule };
//# sourceMappingURL=primeng-defer.mjs.map