Skip to content

Commit

Permalink
Fixed #14731 - Update scrollToIndex method
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincakiroglu committed Feb 6, 2024
1 parent 77d0b3c commit f0d4e23
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/app/components/scroller/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,35 +660,41 @@ export class Scroller implements OnInit, AfterContentInit, AfterViewChecked, OnD
}

scrollTo(options: ScrollToOptions) {
this.lastScrollPos = this.both ? { top: 0, left: 0 } : 0;
// this.lastScrollPos = this.both ? { top: 0, left: 0 } : 0;
this.elementViewChild?.nativeElement?.scrollTo(options);
}

scrollToIndex(index: number, behavior: ScrollBehavior = 'auto') {
const { numToleratedItems } = this.calculateNumItems();
const contentPos = this.getContentPosition();
const calculateFirst = (_index = 0, _numT: number) => (_index <= _numT ? 0 : _index);
const calculateCoord = (_first: number, _size: number, _cpos: number) => _first * _size + _cpos;
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
let newFirst: any = 0;
scrollToIndex(index: number | number[], behavior: ScrollBehavior = 'auto') {
const valid = this.both ? (index as number[]).every((i) => i > -1) : (index as number) > -1;

if (this.both) {
newFirst = { rows: calculateFirst((<any>index)[0], numToleratedItems[0]), cols: calculateFirst((<any>index)[1], numToleratedItems[1]) };
scrollTo(calculateCoord(newFirst.cols, (<number[]>this._itemSize)[1], contentPos.left), calculateCoord(newFirst.rows, (<number[]>this._itemSize)[0], contentPos.top));
} else {
newFirst = calculateFirst(index, numToleratedItems);
if (valid) {
const first = this.first;
const { scrollTop = 0, scrollLeft = 0 } = this.elementViewChild?.nativeElement;
const { numToleratedItems } = this.calculateNumItems();
const contentPos = this.getContentPosition();
const itemSize = this.itemSize;
const calculateFirst = (_index = 0, _numT) => (_index <= _numT ? 0 : _index);
const calculateCoord = (_first, _size, _cpos) => _first * _size + _cpos;
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
let newFirst = this.both ? { rows: 0, cols: 0 } : 0;
let isRangeChanged = false,
isScrollChanged = false;

if (this.horizontal) {
scrollTo(calculateCoord(newFirst, <number>this._itemSize, contentPos.left), 0);
}
if (this.vertical) {
const currentScrollLeft = this.elementViewChild?.nativeElement.scrollLeft;
scrollTo(currentScrollLeft, calculateCoord(newFirst, <number>this._itemSize, contentPos.top));
if (this.both) {
newFirst = { rows: calculateFirst(index[0], numToleratedItems[0]), cols: calculateFirst(index[1], numToleratedItems[1]) };
scrollTo(calculateCoord(newFirst.cols, itemSize[1], contentPos.left), calculateCoord(newFirst.rows, itemSize[0], contentPos.top));
isScrollChanged = this.lastScrollPos.top !== scrollTop || this.lastScrollPos.left !== scrollLeft;
isRangeChanged = newFirst.rows !== first.rows || newFirst.cols !== first.cols;
} else {
newFirst = calculateFirst(index as number, numToleratedItems);
this.horizontal ? scrollTo(calculateCoord(newFirst, itemSize, contentPos.left), scrollTop) : scrollTo(scrollLeft, calculateCoord(newFirst, itemSize, contentPos.top));
isScrollChanged = this.lastScrollPos !== (this.horizontal ? scrollLeft : scrollTop);
isRangeChanged = newFirst !== first;
}
}

this.isRangeChanged = this.first !== newFirst;
this.first = newFirst;
this.isRangeChanged = isRangeChanged;
isScrollChanged && (this.first = newFirst);
}
}

scrollInView(index: number, to: ScrollerToType, behavior: ScrollBehavior = 'auto') {
Expand Down

0 comments on commit f0d4e23

Please sign in to comment.