From 43ffebbad73d9d4b4c4bc982d15872fb2a378f6f Mon Sep 17 00:00:00 2001 From: Ralf de Kleine Date: Sun, 18 Jun 2017 23:01:53 +0200 Subject: [PATCH] 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 --- src/js/core/factories/Grid.js | 8 +++- test/unit/core/factories/Grid.spec.js | 64 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/js/core/factories/Grid.js b/src/js/core/factories/Grid.js index 837956ae68..bc99d26d5b 100644 --- a/src/js/core/factories/Grid.js +++ b/src/js/core/factories/Grid.js @@ -2394,7 +2394,9 @@ angular.module('ui.grid') // Turn the scroll position into a percentage and make it an argument for a scroll event percentage = scrollPixels / scrollLength; - scrollEvent.y = { percentage: percentage }; + if (percentage <= 1) { + scrollEvent.y = { percentage: percentage }; + } } // 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... else if (pixelsToSeeRow > bottomBound) { @@ -2404,7 +2406,9 @@ angular.module('ui.grid') // Turn the scroll position into a percentage and make it an argument for a scroll event percentage = scrollPixels / scrollLength; - scrollEvent.y = { percentage: percentage }; + if (percentage <= 1) { + scrollEvent.y = { percentage: percentage }; + } } } diff --git a/test/unit/core/factories/Grid.spec.js b/test/unit/core/factories/Grid.spec.js index 58bfa3b1cc..6454cc12c2 100644 --- a/test/unit/core/factories/Grid.spec.js +++ b/test/unit/core/factories/Grid.spec.js @@ -38,6 +38,70 @@ describe('Grid factory', function () { $scope.$digest(); } + describe('scrollToIfNecessary', function() { + var renderContainers; + var prevScrollTop = 100; + var viewportWidth = 100; + var viewportHeight = 100; + var canvasHeight = 360; + var canvasWidth = 100; + + beforeEach(function() { + renderContainers = { + body: { + visibleRowCache: null, + visibleColumnCache: null, + prevScrollTop: prevScrollTop, + headerHeight: 30, + getViewportWidth: jasmine.createSpy('getViewportWidth').and.callFake(function() { return viewportWidth;}), + getViewportHeight: jasmine.createSpy('getViewportWidth').and.callFake(function() { return viewportHeight;}), + getCanvasHeight: jasmine.createSpy('getCanvasHeight').and.callFake(function() { return canvasHeight; }), + getCanvasWidth: jasmine.createSpy('getCanvasHeight').and.callFake(function() { return canvasWidth; }) + } + }; + }); + + it('should not scroll.y when scrollpercentage > 100% when row is less then top boundry', function() { + // row is less then the top boundary + var rowCache = []; + for ( var i = 0; i < 9; i++ ){ + rowCache.push(i); + } + rowCache.push(rows[1]); + renderContainers.body.prevScrollTop = 100; + renderContainers.body.visibleRowCache = rowCache; + renderContainers.body.visibleColumnCache = [column]; + grid.renderContainers = renderContainers; + + // try to scroll to row 10 + grid.scrollToIfNecessary(rowCache[9], column).then(function(scrollEvent){ + expect(scrollEvent).toBeUndefined(); + }); + + $scope.$apply(); + }); + + it('should not scroll.y when scrollpercentage > 100% when row is more then top boundry', function() { + // row is more then the top boundary + var rowCache = []; + for ( var i = 0; i < 9; i++ ){ + rowCache.push(i); + } + rowCache.push(rows[1]); + renderContainers.body.prevScrollTop = 300; + renderContainers.body.visibleRowCache = rowCache; + renderContainers.body.visibleColumnCache = [column]; + grid.renderContainers = renderContainers; + + // try to scroll to row 10 + grid.scrollToIfNecessary(rowCache[9], column).then(function(scrollEvent){ + expect(scrollEvent).toBeUndefined(); + }); + + $scope.$apply(); + }); + }); + describe('constructor', function() { it('should throw an exception if no id is provided', function() { expect(function() {