Skip to content

Commit 7ed11ec

Browse files
Portugal, Marcelomportuga
Portugal, Marcelo
authored andcommitted
fix(Grid.js): ScrollIfNecessary will not scroll if percentage has not changed.
Doing this will prevent us from getting stuck in an infinite loop of triggering the scrollEnd event. fix #6653, fix #4221
1 parent 4c63239 commit 7ed11ec

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

Diff for: src/js/core/factories/Grid.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,29 @@ angular.module('ui.grid')
23472347
return percentage <= 1 ? percentage : 1;
23482348
}
23492349

2350+
// Only returns the scroll Y position if the percentage is different from the previous
2351+
function getScrollY(scrollPixels, scrollLength, prevScrolltopPercentage) {
2352+
var scrollPercentage = getScrollPercentage(scrollPixels, scrollLength);
2353+
2354+
if (scrollPercentage !== prevScrolltopPercentage) {
2355+
return { percentage: getScrollPercentage(scrollPixels, scrollLength) };
2356+
}
2357+
2358+
return undefined;
2359+
}
2360+
2361+
// Only returns the scroll X position if the percentage is different from the previous
2362+
function getScrollX(horizScrollPixels, horizScrollLength, prevScrollleftPercentage) {
2363+
var horizPercentage = horizScrollPixels / horizScrollLength;
2364+
horizPercentage = (horizPercentage > 1) ? 1 : horizPercentage;
2365+
2366+
if (horizPercentage !== prevScrollleftPercentage) {
2367+
return { percentage: horizPercentage };
2368+
}
2369+
2370+
return undefined;
2371+
}
2372+
23502373
/**
23512374
* @ngdoc method
23522375
* @methodOf ui.grid.class:Grid
@@ -2421,15 +2444,15 @@ angular.module('ui.grid')
24212444
// to get the full position we need
24222445
scrollPixels = self.renderContainers.body.prevScrollTop - (topBound - pixelsToSeeRow);
24232446

2424-
scrollEvent.y = { percentage: getScrollPercentage(scrollPixels, scrollLength) };
2447+
scrollEvent.y = getScrollY(scrollPixels, scrollLength, self.renderContainers.body.prevScrolltopPercentage);
24252448
}
24262449
// Otherwise if the scroll position we need to see the row is MORE than the bottom boundary, i.e. obscured below the bottom of the self...
24272450
else if (pixelsToSeeRow > bottomBound) {
24282451
// Get the different between the bottom boundary and the required scroll position and add it to the current scroll position
24292452
// to get the full position we need
24302453
scrollPixels = pixelsToSeeRow - bottomBound + self.renderContainers.body.prevScrollTop;
24312454

2432-
scrollEvent.y = { percentage: getScrollPercentage(scrollPixels, scrollLength) };
2455+
scrollEvent.y = getScrollY(scrollPixels, scrollLength,self.renderContainers.body.prevScrolltopPercentage);
24332456
}
24342457
}
24352458

@@ -2454,7 +2477,7 @@ angular.module('ui.grid')
24542477
// Don't let the pixels required to see the column be less than zero
24552478
columnRightEdge = (columnRightEdge < 0) ? 0 : columnRightEdge;
24562479

2457-
var horizScrollPixels, horizPercentage;
2480+
var horizScrollPixels;
24582481

24592482
// If the scroll position we need to see the column is LESS than the left boundary, i.e. obscured before the left of the self...
24602483
if (columnLeftEdge < leftBound) {
@@ -2463,9 +2486,7 @@ angular.module('ui.grid')
24632486
horizScrollPixels = self.renderContainers.body.prevScrollLeft - (leftBound - columnLeftEdge);
24642487

24652488
// Turn the scroll position into a percentage and make it an argument for a scroll event
2466-
horizPercentage = horizScrollPixels / horizScrollLength;
2467-
horizPercentage = (horizPercentage > 1) ? 1 : horizPercentage;
2468-
scrollEvent.x = { percentage: horizPercentage };
2489+
scrollEvent.x = getScrollX(horizScrollPixels, horizScrollLength, self.renderContainers.body.prevScrollleftPercentage);
24692490
}
24702491
// Otherwise if the scroll position we need to see the column is MORE than the right boundary, i.e. obscured after the right of the self...
24712492
else if (columnRightEdge > rightBound) {
@@ -2474,9 +2495,7 @@ angular.module('ui.grid')
24742495
horizScrollPixels = columnRightEdge - rightBound + self.renderContainers.body.prevScrollLeft;
24752496

24762497
// Turn the scroll position into a percentage and make it an argument for a scroll event
2477-
horizPercentage = horizScrollPixels / horizScrollLength;
2478-
horizPercentage = (horizPercentage > 1) ? 1 : horizPercentage;
2479-
scrollEvent.x = { percentage: horizPercentage };
2498+
scrollEvent.x = getScrollX(horizScrollPixels, horizScrollLength, self.renderContainers.body.prevScrollleftPercentage);
24802499
}
24812500
}
24822501

0 commit comments

Comments
 (0)