From 3bcbe72de0f296eedbeeca28f09600c0721824ba Mon Sep 17 00:00:00 2001 From: Brian Hann Date: Wed, 8 Jul 2015 15:13:20 -0500 Subject: [PATCH] fix(Grid): Force scroll to 100% when necessary Containers that have overflow: hidden set are not able to naturally scroll to 100%, because they cannot handle native scroll events; only mousewheel. It's a long discussion; see #3772 for notes. This change catches a mousewheel event that should put the container at the max scroll position (basically scrollHeight - offsetHeight) and manually sets the scrollTop to that amount. Fixes #3772 --- misc/demo/pinning.html | 8 +++++--- src/js/core/directives/ui-grid-render-container.js | 7 +++++++ src/js/core/directives/ui-grid-viewport.js | 4 ++-- src/js/core/factories/GridRenderContainer.js | 10 ++++++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/misc/demo/pinning.html b/misc/demo/pinning.html index f1db42c7e9..9aecca4e51 100644 --- a/misc/demo/pinning.html +++ b/misc/demo/pinning.html @@ -9,7 +9,7 @@ - + @@ -45,7 +45,9 @@

Grid

diff --git a/src/js/core/directives/ui-grid-render-container.js b/src/js/core/directives/ui-grid-render-container.js index 15cb30b951..2affeaf0f0 100644 --- a/src/js/core/directives/ui-grid-render-container.js +++ b/src/js/core/directives/ui-grid-render-container.js @@ -74,10 +74,17 @@ var scrollYAmount = event.deltaY * -1 * event.deltaFactor; scrollTop = containerCtrl.viewport[0].scrollTop; + // Get the scroll percentage scrollEvent.verticalScrollLength = rowContainer.getVerticalScrollLength(); var scrollYPercentage = (scrollTop + scrollYAmount) / scrollEvent.verticalScrollLength; + // If we should be scrolled 100%, make sure the scrollTop matches the maximum scroll length + // Viewports that have "overflow: hidden" don't let the mousewheel scroll all the way to the bottom without this check + if (scrollYPercentage >= 1 && scrollTop < scrollEvent.verticalScrollLength) { + containerCtrl.viewport[0].scrollTop = scrollEvent.verticalScrollLength; + } + // Keep scrollPercentage within the range 0-1. if (scrollYPercentage < 0) { scrollYPercentage = 0; } else if (scrollYPercentage > 1) { scrollYPercentage = 1; } diff --git a/src/js/core/directives/ui-grid-viewport.js b/src/js/core/directives/ui-grid-viewport.js index 04b4ba8c71..712cd8c425 100644 --- a/src/js/core/directives/ui-grid-viewport.js +++ b/src/js/core/directives/ui-grid-viewport.js @@ -28,7 +28,7 @@ $scope.rowContainer = containerCtrl.rowContainer; $scope.colContainer = containerCtrl.colContainer; - // Register this viewport with its container + // Register this viewport with its container containerCtrl.viewport = $elm; @@ -138,4 +138,4 @@ } ]); -})(); \ No newline at end of file +})(); diff --git a/src/js/core/factories/GridRenderContainer.js b/src/js/core/factories/GridRenderContainer.js index a171d3ba72..6f5410d834 100644 --- a/src/js/core/factories/GridRenderContainer.js +++ b/src/js/core/factories/GridRenderContainer.js @@ -300,7 +300,7 @@ angular.module('ui.grid') }; GridRenderContainer.prototype.getVerticalScrollLength = function getVerticalScrollLength() { - return this.getCanvasHeight() - this.getViewportHeight(); + return this.getCanvasHeight() - this.getViewportHeight() + this.grid.scrollbarHeight; }; GridRenderContainer.prototype.getCanvasWidth = function getCanvasWidth() { @@ -354,6 +354,8 @@ angular.module('ui.grid') vertScrollPercentage = newScrollTop / vertScrollLength; + // console.log('vert', vertScrollPercentage, newScrollTop, vertScrollLength); + if (vertScrollPercentage > 1) { vertScrollPercentage = 1; } if (vertScrollPercentage < 0) { vertScrollPercentage = 0; } @@ -429,6 +431,8 @@ angular.module('ui.grid') var maxRowIndex = rowCache.length - minRows; + // console.log('scroll%1', scrollPercentage); + // Calculate the scroll percentage according to the scrollTop location, if no percentage was provided if ((typeof(scrollPercentage) === 'undefined' || scrollPercentage === null) && scrollTop) { scrollPercentage = scrollTop / self.getVerticalScrollLength(); @@ -436,6 +440,8 @@ angular.module('ui.grid') var rowIndex = Math.ceil(Math.min(maxRowIndex, maxRowIndex * scrollPercentage)); + // console.log('maxRowIndex / scroll%', maxRowIndex, scrollPercentage, rowIndex); + // Define a max row index that we can't scroll past if (rowIndex > maxRowIndex) { rowIndex = maxRowIndex; @@ -738,7 +744,7 @@ angular.module('ui.grid') GridRenderContainer.prototype.getViewportStyle = function () { var self = this; var styles = {}; - + self.hasHScrollbar = false; self.hasVScrollbar = false;