Skip to content

Commit 43ffebb

Browse files
rdkleinemportuga
authored andcommitted
fix(scrollEvent): Prevent scrollEvent when scroll percentage is greater then 1. (#6241)
* Prevent scrollEvent when scroll percentage is greater then 1. * Unit tests * Small adjustment to prevscrolltop
1 parent 02a6a68 commit 43ffebb

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/js/core/factories/Grid.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,9 @@ angular.module('ui.grid')
23942394

23952395
// Turn the scroll position into a percentage and make it an argument for a scroll event
23962396
percentage = scrollPixels / scrollLength;
2397-
scrollEvent.y = { percentage: percentage };
2397+
if (percentage <= 1) {
2398+
scrollEvent.y = { percentage: percentage };
2399+
}
23982400
}
23992401
// 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...
24002402
else if (pixelsToSeeRow > bottomBound) {
@@ -2404,7 +2406,9 @@ angular.module('ui.grid')
24042406

24052407
// Turn the scroll position into a percentage and make it an argument for a scroll event
24062408
percentage = scrollPixels / scrollLength;
2407-
scrollEvent.y = { percentage: percentage };
2409+
if (percentage <= 1) {
2410+
scrollEvent.y = { percentage: percentage };
2411+
}
24082412
}
24092413
}
24102414

test/unit/core/factories/Grid.spec.js

+64
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,70 @@ describe('Grid factory', function () {
3838
$scope.$digest();
3939
}
4040

41+
describe('scrollToIfNecessary', function() {
42+
var renderContainers;
43+
var prevScrollTop = 100;
44+
var viewportWidth = 100;
45+
var viewportHeight = 100;
46+
var canvasHeight = 360;
47+
var canvasWidth = 100;
48+
49+
beforeEach(function() {
50+
renderContainers = {
51+
body: {
52+
visibleRowCache: null,
53+
visibleColumnCache: null,
54+
prevScrollTop: prevScrollTop,
55+
headerHeight: 30,
56+
getViewportWidth: jasmine.createSpy('getViewportWidth').and.callFake(function() { return viewportWidth;}),
57+
getViewportHeight: jasmine.createSpy('getViewportWidth').and.callFake(function() { return viewportHeight;}),
58+
getCanvasHeight: jasmine.createSpy('getCanvasHeight').and.callFake(function() { return canvasHeight; }),
59+
getCanvasWidth: jasmine.createSpy('getCanvasHeight').and.callFake(function() { return canvasWidth; })
60+
}
61+
};
62+
});
63+
64+
it('should not scroll.y when scrollpercentage > 100% when row is less then top boundry', function() {
65+
// row is less then the top boundary
66+
var rowCache = [];
67+
for ( var i = 0; i < 9; i++ ){
68+
rowCache.push(i);
69+
}
70+
rowCache.push(rows[1]);
71+
renderContainers.body.prevScrollTop = 100;
72+
renderContainers.body.visibleRowCache = rowCache;
73+
renderContainers.body.visibleColumnCache = [column];
74+
grid.renderContainers = renderContainers;
75+
76+
// try to scroll to row 10
77+
grid.scrollToIfNecessary(rowCache[9], column).then(function(scrollEvent){
78+
expect(scrollEvent).toBeUndefined();
79+
});
80+
81+
$scope.$apply();
82+
});
83+
84+
it('should not scroll.y when scrollpercentage > 100% when row is more then top boundry', function() {
85+
// row is more then the top boundary
86+
var rowCache = [];
87+
for ( var i = 0; i < 9; i++ ){
88+
rowCache.push(i);
89+
}
90+
rowCache.push(rows[1]);
91+
renderContainers.body.prevScrollTop = 300;
92+
renderContainers.body.visibleRowCache = rowCache;
93+
renderContainers.body.visibleColumnCache = [column];
94+
grid.renderContainers = renderContainers;
95+
96+
// try to scroll to row 10
97+
grid.scrollToIfNecessary(rowCache[9], column).then(function(scrollEvent){
98+
expect(scrollEvent).toBeUndefined();
99+
});
100+
101+
$scope.$apply();
102+
});
103+
});
104+
41105
describe('constructor', function() {
42106
it('should throw an exception if no id is provided', function() {
43107
expect(function() {

0 commit comments

Comments
 (0)