Skip to content

fixed scrollPercent calculation for GridRenderContainer and InfiniteScroll Feature #5395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/features/infinite-scroll/js/infinite-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
*/
module.service('uiGridInfiniteScrollService', ['gridUtil', '$compile', '$timeout', 'uiGridConstants', 'ScrollEvent', '$q', function (gridUtil, $compile, $timeout, uiGridConstants, ScrollEvent, $q) {


// not sure where to put this util function @vance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this to the GridRenderContainer class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vance Are you planning on taking @swalters's suggestion?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ehh. splitting time between a fortune 500 a startup and marketing my own side-app at the moment. I don't know the next time I will have the headspace to compile this again, christmas time?

var calculateHeightFromRows = function(rows){
var canvasHeight = 0;
rows.forEach(function(r){
canvasHeight+= r.height;
});
return canvasHeight;
};

var service = {

/**
Expand All @@ -40,7 +50,7 @@

grid.infiniteScroll = { dataLoading: false };
service.setScrollDirections( grid, grid.options.infiniteScrollUp, grid.options.infiniteScrollDown );
grid.api.core.on.scrollEnd($scope, service.handleScroll);
grid.api.core.on.scrollEnd($scope, service.handleScroll);

/**
* @ngdoc object
Expand Down Expand Up @@ -144,9 +154,11 @@
saveScrollPercentage: function() {
grid.infiniteScroll.prevScrollTop = grid.renderContainers.body.prevScrollTop;
grid.infiniteScroll.previousVisibleRows = grid.getVisibleRowCount();
grid.infiniteScroll.previousVisibleHeight = calculateHeightFromRows(grid.getVisibleRows());
},



/**
* @ngdoc function
* @name dataRemovedTop
Expand Down Expand Up @@ -332,6 +344,8 @@
},




/**
* @ngdoc function
* @name adjustScroll
Expand All @@ -356,35 +370,36 @@
adjustScroll: function(grid){
var promise = $q.defer();
$timeout(function () {
var newPercentage, viewportHeight, rowHeight, newVisibleRows, oldTop, newTop;
var viewportHeight, oldTop, newTop;

viewportHeight = grid.getViewportHeight() + grid.headerHeight - grid.renderContainers.body.headerHeight - grid.scrollbarHeight;
rowHeight = grid.options.rowHeight;

if ( grid.infiniteScroll.direction === undefined ){
// called from initialize, tweak our scroll up a little
service.adjustInfiniteScrollPosition(grid, 0);
}

newVisibleRows = grid.getVisibleRowCount();
var rows = grid.getVisibleRows();
var canvasHeight = calculateHeightFromRows(rows);

// in case not enough data is loaded to enable scroller - load more data
var canvasHeight = rowHeight * newVisibleRows;

if (grid.infiniteScroll.scrollDown && (viewportHeight > canvasHeight)) {
grid.api.infiniteScroll.raise.needLoadMoreData();
}

if ( grid.infiniteScroll.direction === uiGridConstants.scrollDirection.UP ){
oldTop = grid.infiniteScroll.prevScrollTop || 0;
newTop = oldTop + (newVisibleRows - grid.infiniteScroll.previousVisibleRows)*rowHeight;
newTop = oldTop + canvasHeight - grid.infiniteScroll.previousVisibleHeight;

service.adjustInfiniteScrollPosition(grid, newTop);
$timeout( function() {
promise.resolve();
});
}

if ( grid.infiniteScroll.direction === uiGridConstants.scrollDirection.DOWN ){
newTop = grid.infiniteScroll.prevScrollTop || (grid.infiniteScroll.previousVisibleRows*rowHeight - viewportHeight);
newTop = grid.infiniteScroll.prevScrollTop || ( grid.infiniteScroll.previousVisibleHeight - viewportHeight );
service.adjustInfiniteScrollPosition(grid, newTop);
$timeout( function() {
promise.resolve();
Expand All @@ -406,11 +421,10 @@
* @returns {promise} a promise that is resolved when the scrolling finishes
*/
adjustInfiniteScrollPosition: function (grid, scrollTop) {
var scrollEvent = new ScrollEvent(grid, null, null, 'ui.grid.adjustInfiniteScrollPosition'),
visibleRows = grid.getVisibleRowCount(),
viewportHeight = grid.getViewportHeight() + grid.headerHeight - grid.renderContainers.body.headerHeight - grid.scrollbarHeight,
rowHeight = grid.options.rowHeight,
scrollHeight = visibleRows*rowHeight-viewportHeight;
var scrollEvent = new ScrollEvent(grid, null, null, 'ui.grid.adjustInfiniteScrollPosition');

var rows = grid.getVisibleRows();
var scrollHeight = calculateHeightFromRows(rows);

//for infinite scroll, if there are pages upwards then never allow it to be at the zero position so the up button can be active
if (scrollTop === 0 && grid.infiniteScroll.scrollUp) {
Expand Down
4 changes: 2 additions & 2 deletions src/js/core/factories/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -1813,9 +1813,9 @@ angular.module('ui.grid')
return this.renderContainers.body.visibleRowCache.length;
};

Grid.prototype.getVisibleRows = function getVisibleRows() {
Grid.prototype.getVisibleRows = function getVisibleRows(){
return this.renderContainers.body.visibleRowCache;
};
};

Grid.prototype.getVisibleColumnCount = function getVisibleColumnCount() {
// var count = 0;
Expand Down
Loading