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
108 lines (104 loc) • 4.19 kB
JavaScript
import { isPlatformBrowser } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, inject, ViewContainerRef, TemplateRef, Directive, Output, ContentChild, 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 = /*@__PURE__*/ (() => { let ɵDefer_BaseFactory; return function Defer_Factory(__ngFactoryType__) { return (ɵDefer_BaseFactory || (ɵDefer_BaseFactory = i0.ɵɵgetInheritedFactory(Defer)))(__ngFactoryType__ || Defer); }; })();
static ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: Defer, selectors: [["", "pDefer", ""]], contentQueries: function Defer_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) {
i0.ɵɵcontentQuery(dirIndex, TemplateRef, 5);
} if (rf & 2) {
let _t;
i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.template = _t.first);
} }, outputs: { onLoad: "onLoad" }, features: [i0.ɵɵInheritDefinitionFeature] });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(Defer, [{
type: Directive,
args: [{
selector: '[pDefer]',
standalone: true
}]
}], null, { onLoad: [{
type: Output
}], template: [{
type: ContentChild,
args: [TemplateRef]
}] }); })();
class DeferModule {
static ɵfac = function DeferModule_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || DeferModule)(); };
static ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: DeferModule });
static ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({});
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(DeferModule, [{
type: NgModule,
args: [{
imports: [Defer],
exports: [Defer]
}]
}], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(DeferModule, { imports: [Defer], exports: [Defer] }); })();
/**
* Generated bundle index. Do not edit.
*/
export { Defer, DeferModule };
//# sourceMappingURL=primeng-defer.mjs.map