Skip to content

Commit 6095aae

Browse files
MayaKirovakdinev
authored andcommitted
fix(igxFor): Emit onChunkPreload/onChunkLoad only when startIndex or chunkSize have changed. (#2905)
1 parent bb1e44c commit 6095aae

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,40 @@ describe('IgxForOf directive -', () => {
855855
const hDirective = fix.componentInstance.childVirtDirs.toArray()[0];
856856
expect(hDirective.getItemCountInView()).toBe(2);
857857
});
858+
859+
it('should emit the onChunkPreload/onChunkLoad only when startIndex or chunkSize have changed.', async () => {
860+
const verticalDir = fix.componentInstance.parentVirtDir;
861+
const chunkLoadSpy = spyOn<any>(verticalDir.onChunkLoad, 'emit').and.callThrough();
862+
const chunkPreLoadSpy = spyOn<any>(verticalDir.onChunkPreload, 'emit').and.callThrough();
863+
// scroll so that start index does not change.
864+
fix.componentInstance.scrollTop(1);
865+
fix.detectChanges();
866+
await wait();
867+
expect(chunkLoadSpy).toHaveBeenCalledTimes(0);
868+
expect(chunkPreLoadSpy).toHaveBeenCalledTimes(0);
869+
870+
// scroll so that start index changes.
871+
fix.componentInstance.scrollTop(100);
872+
fix.detectChanges();
873+
await wait();
874+
875+
expect(chunkLoadSpy).toHaveBeenCalledTimes(1);
876+
expect(chunkPreLoadSpy).toHaveBeenCalledTimes(1);
877+
878+
// change size so that chunk size does not change
879+
fix.componentInstance.height = '399px';
880+
fix.detectChanges();
881+
await wait();
882+
883+
expect(chunkLoadSpy).toHaveBeenCalledTimes(1);
884+
885+
// change size so that chunk size changes
886+
fix.componentInstance.height = '1500px';
887+
fix.detectChanges();
888+
await wait(100);
889+
890+
expect(chunkLoadSpy).toHaveBeenCalledTimes(2);
891+
});
858892
});
859893

860894
describe('variable size component', () => {

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
597597
} else {
598598
this._bScrollInternal = false;
599599
}
600-
600+
const prevStartIndex = this.state.startIndex;
601601
const scrollOffset = this.fixedUpdateAllRows(this._virtScrollTop);
602602

603603
this.dc.instance._viewContainer.element.nativeElement.style.top = -(scrollOffset) + 'px';
@@ -607,8 +607,9 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
607607
this.recalcUpdateSizes();
608608
});
609609
this.dc.changeDetectorRef.detectChanges();
610-
611-
this.onChunkLoad.emit(this.state);
610+
if (prevStartIndex !== this.state.startIndex) {
611+
this.onChunkLoad.emit(this.state);
612+
}
612613
}
613614

614615
/**
@@ -735,13 +736,15 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
735736
return;
736737
}
737738
const curScrollLeft = event.target.scrollLeft;
738-
739+
const prevStartIndex = this.state.startIndex;
739740
// Updating horizontal chunks
740741
const scrollOffset = this.fixedUpdateAllCols(curScrollLeft);
741742
this.dc.instance._viewContainer.element.nativeElement.style.left = -scrollOffset + 'px';
742743

743744
this.dc.changeDetectorRef.detectChanges();
744-
this.onChunkLoad.emit();
745+
if (prevStartIndex !== this.state.startIndex) {
746+
this.onChunkLoad.emit(this.state);
747+
}
745748
}
746749

747750
/**
@@ -753,7 +756,10 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
753756
this.sizesCache,
754757
0
755758
);
756-
this.onChunkPreload.emit(this.state);
759+
const bUpdatedStart = this.state.startIndex !== startIndex;
760+
if (bUpdatedStart) {
761+
this.onChunkPreload.emit(this.state);
762+
}
757763
/*recalculate and apply page size.*/
758764
if (startIndex + this.state.chunkSize > this.igxForOf.length) {
759765
this.state.startIndex = this.igxForOf.length - this.state.chunkSize;
@@ -805,6 +811,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
805811
* @hidden
806812
*/
807813
protected _applyChanges(changes: IterableChanges<T>) {
814+
const prevChunkSize = this.state.chunkSize;
808815
this.applyChunkSizeChange();
809816
this._recalcScrollBarSize();
810817
if (this.igxForOf && this.igxForOf.length && this.dc) {
@@ -823,7 +830,9 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
823830
cntx.index = this.igxForOf.indexOf(input);
824831
}
825832
this.dc.changeDetectorRef.detectChanges();
826-
this.onChunkLoad.emit();
833+
if (prevChunkSize !== this.state.chunkSize) {
834+
this.onChunkLoad.emit(this.state);
835+
}
827836
if (this.igxForScrollOrientation === 'vertical') {
828837
this.recalcUpdateSizes();
829838
}
@@ -1009,8 +1018,12 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
10091018
protected _recalcOnContainerChange(changes: SimpleChanges) {
10101019
this.dc.instance._viewContainer.element.nativeElement.style.top = '0px';
10111020
this.dc.instance._viewContainer.element.nativeElement.style.left = '0px';
1021+
const prevChunkSize = this.state.chunkSize;
10121022
this.applyChunkSizeChange();
10131023
this._recalcScrollBarSize();
1024+
if (prevChunkSize !== this.state.chunkSize) {
1025+
this.onChunkLoad.emit(this.state);
1026+
}
10141027
if (this.sizesCache && this.hScroll && this.hScroll.scrollLeft !== 0) {
10151028
// Updating horizontal chunks and offsets based on the new scrollLeft
10161029
const scrollOffset = this.fixedUpdateAllCols(this.hScroll.scrollLeft);
@@ -1222,6 +1235,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
12221235
}
12231236

12241237
protected _applyChanges(changes: IterableChanges<T>) {
1238+
const prevChunkSize = this.state.chunkSize;
12251239
this.applyChunkSizeChange();
12261240
this._recalcScrollBarSize();
12271241
if (this.igxForOf && this.igxForOf.length && this.dc) {
@@ -1239,6 +1253,9 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
12391253
cntx.$implicit = input;
12401254
cntx.index = this.igxForOf.indexOf(input);
12411255
}
1256+
if (prevChunkSize !== this.state.chunkSize) {
1257+
this.onChunkLoad.emit(this.state);
1258+
}
12421259
if (this.igxForScrollOrientation === 'vertical') {
12431260
requestAnimationFrame(() => {
12441261
this.recalcUpdateSizes();

0 commit comments

Comments
 (0)