From 109d924ffacbf1d2b88928238d70a52b760482dd Mon Sep 17 00:00:00 2001 From: Mohamed Ben Makhlouf Date: Sat, 19 Oct 2024 23:47:44 +0200 Subject: [PATCH 1/8] convert to new control flow and improve module imports --- .../virtualscroller/virtualscroller.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/app/components/virtualscroller/virtualscroller.ts b/src/app/components/virtualscroller/virtualscroller.ts index 88701de8ed4..755415b6edc 100755 --- a/src/app/components/virtualscroller/virtualscroller.ts +++ b/src/app/components/virtualscroller/virtualscroller.ts @@ -1,4 +1,4 @@ -import { CommonModule } from '@angular/common'; +import { NgClass, NgStyle, NgTemplateOutlet } from '@angular/common'; import { AfterContentInit, ChangeDetectionStrategy, @@ -36,10 +36,12 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface'; [attr.data-pc-name]="'virtualscroller'" [attr.data-pc-section]="'root'" > -
- - -
+ @if (header || headerTemplate) { +
+ + +
+ }
- + @if (footer || footerTemplate) { + + } `, changeDetection: ChangeDetectionStrategy.Default, @@ -193,7 +197,7 @@ export class VirtualScroller implements AfterContentInit, BlockableUI { } @NgModule({ - imports: [CommonModule, SharedModule, ScrollerModule], + imports: [NgClass, NgStyle, NgTemplateOutlet, SharedModule, ScrollerModule], exports: [VirtualScroller, SharedModule, ScrollerModule], declarations: [VirtualScroller], }) From 9a8a7ffa39eb2d2b2c8bbb72ba96980946f3969e Mon Sep 17 00:00:00 2001 From: Mohamed Ben Makhlouf Date: Sat, 19 Oct 2024 23:55:37 +0200 Subject: [PATCH 2/8] move to modern input --- .../virtualscroller/virtualscroller.ts | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/app/components/virtualscroller/virtualscroller.ts b/src/app/components/virtualscroller/virtualscroller.ts index 755415b6edc..835734a2f6c 100755 --- a/src/app/components/virtualscroller/virtualscroller.ts +++ b/src/app/components/virtualscroller/virtualscroller.ts @@ -8,7 +8,7 @@ import { ContentChildren, ElementRef, EventEmitter, - Input, + input, NgModule, Output, QueryList, @@ -31,8 +31,8 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface'; template: `
@@ -45,16 +45,16 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface';
-
+
- @if (header || headerTemplate) { + @if (header || headerTemplate()) {
- +
}
@@ -57,7 +58,7 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface';
@@ -65,10 +66,10 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface';
- @if (footer || footerTemplate) { + @if (footer || footerTemplate()) { }
@@ -76,7 +77,7 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface'; changeDetection: ChangeDetectionStrategy.Default, encapsulation: ViewEncapsulation.None, }) -export class VirtualScroller implements AfterContentInit, BlockableUI { +export class VirtualScroller implements BlockableUI { /** * An array of objects to display. * @group Props @@ -128,51 +129,32 @@ export class VirtualScroller implements AfterContentInit, BlockableUI { @ContentChild(Footer) footer: Footer | undefined; - @ContentChildren(PrimeTemplate) templates: Nullable>; + templates: Signal = contentChildren(PrimeTemplate); @ViewChild('scroller') scroller: Nullable; - itemTemplate: Nullable>; + itemTemplate = computed>>(() => this.mappedTemplates()['item']); - headerTemplate: Nullable>; + headerTemplate = computed>>(() => this.mappedTemplates()['header']); - footerTemplate: Nullable>; + footerTemplate = computed>>(() => this.mappedTemplates()['footer']); - loadingItemTemplate: Nullable>; + loadingItemTemplate = computed>>(() => this.mappedTemplates()['loadingItem']); virtualScrollTimeout: any; + private mappedTemplates = computed<{ [key: string]: TemplateRef }>(() => { + return (this.templates() || []).reduce((prev, item) => { + prev[item.getType()] = item.template; + return prev; + }, {}); + }); + constructor( public el: ElementRef, public cd: ChangeDetectorRef, ) {} - ngAfterContentInit() { - (this.templates as QueryList).forEach((item) => { - switch (item.getType()) { - case 'item': - this.itemTemplate = item.template; - break; - - case 'loadingItem': - this.loadingItemTemplate = item.template; - break; - - case 'header': - this.headerTemplate = item.template; - break; - - case 'footer': - this.footerTemplate = item.template; - break; - - default: - this.itemTemplate = item.template; - break; - } - }); - } - onLazyItemLoad(event: VirtualScrollerLazyLoadEvent) { if (this.virtualScrollTimeout) { clearTimeout(this.virtualScrollTimeout); From ef46734fb3671f919a5f1feb4fba08dd0067ba58 Mon Sep 17 00:00:00 2001 From: Mohamed Ben Makhlouf Date: Sun, 20 Oct 2024 00:15:14 +0200 Subject: [PATCH 5/8] move to modern contentChild and viewChild --- .../virtualscroller/virtualscroller.ts | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/app/components/virtualscroller/virtualscroller.ts b/src/app/components/virtualscroller/virtualscroller.ts index 53ceb763f1a..4815eeaa139 100755 --- a/src/app/components/virtualscroller/virtualscroller.ts +++ b/src/app/components/virtualscroller/virtualscroller.ts @@ -1,10 +1,9 @@ import { NgClass, NgStyle, NgTemplateOutlet } from '@angular/common'; import { - AfterContentInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, - ContentChild, + contentChild, contentChildren, ElementRef, OutputEmitterRef, @@ -13,11 +12,12 @@ import { output, Signal, TemplateRef, - ViewChild, + viewChild, ViewEncapsulation, booleanAttribute, numberAttribute, computed, + inject, } from '@angular/core'; import { BlockableUI, Footer, Header, PrimeTemplate, ScrollerOptions, SharedModule } from 'primeng/api'; import { Scroller, ScrollerModule } from 'primeng/scroller'; @@ -37,7 +37,7 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface'; [attr.data-pc-name]="'virtualscroller'" [attr.data-pc-section]="'root'" > - @if (header || headerTemplate()) { + @if (header() || headerTemplate()) {
@@ -66,7 +66,7 @@ import { VirtualScrollerLazyLoadEvent } from './virtualscroller.interface';
- @if (footer || footerTemplate()) { + @if (footer() || footerTemplate()) {