|
142 | 142 | * used if you're subsequently going to call `dataRemovedTop` or `dataRemovedBottom`
|
143 | 143 | */
|
144 | 144 | saveScrollPercentage: function() {
|
145 |
| - grid.infiniteScroll.prevScrolltopPercentage = grid.renderContainers.body.prevScrolltopPercentage; |
146 |
| - grid.infiniteScroll.previousVisibleRows = grid.renderContainers.body.visibleRowCache.length; |
| 145 | + grid.infiniteScroll.prevScrollTop = grid.renderContainers.body.prevScrollTop; |
| 146 | + grid.infiniteScroll.previousVisibleRows = grid.getVisibleRowCount(); |
147 | 147 | },
|
148 | 148 |
|
149 | 149 |
|
|
320 | 320 | // to be at approximately the row we're currently at
|
321 | 321 | grid.infiniteScroll.previousVisibleRows = grid.renderContainers.body.visibleRowCache.length;
|
322 | 322 | grid.infiniteScroll.direction = grid.scrollDirection;
|
323 |
| - delete grid.infiniteScroll.prevScrolltopPercentage; |
| 323 | + delete grid.infiniteScroll.prevScrollTop; |
324 | 324 |
|
325 | 325 | if (grid.scrollDirection === uiGridConstants.scrollDirection.UP && grid.infiniteScroll.scrollUp ) {
|
326 | 326 | grid.infiniteScroll.dataLoading = true;
|
|
356 | 356 | adjustScroll: function(grid){
|
357 | 357 | var promise = $q.defer();
|
358 | 358 | $timeout(function () {
|
359 |
| - var newPercentage; |
| 359 | + var newPercentage, viewportHeight, rowHeight, newVisibleRows, oldTop, newTop; |
| 360 | + |
| 361 | + viewportHeight = grid.getViewportHeight() + grid.headerHeight - grid.renderContainers.body.headerHeight - grid.scrollbarHeight; |
| 362 | + rowHeight = grid.options.rowHeight; |
360 | 363 |
|
361 | 364 | if ( grid.infiniteScroll.direction === undefined ){
|
362 | 365 | // called from initialize, tweak our scroll up a little
|
363 | 366 | service.adjustInfiniteScrollPosition(grid, 0);
|
364 | 367 | }
|
365 | 368 |
|
366 |
| - var newVisibleRows = grid.renderContainers.body.visibleRowCache.length; |
367 |
| - var oldPercentage, oldTopRow; |
368 |
| - var halfViewport = grid.getViewportHeight() / grid.options.rowHeight / 2; |
| 369 | + newVisibleRows = grid.getVisibleRowCount(); |
369 | 370 |
|
370 | 371 | if ( grid.infiniteScroll.direction === uiGridConstants.scrollDirection.UP ){
|
371 |
| - oldPercentage = grid.infiniteScroll.prevScrolltopPercentage || 0; |
372 |
| - oldTopRow = oldPercentage * grid.infiniteScroll.previousVisibleRows; |
373 |
| - newPercentage = ( newVisibleRows - grid.infiniteScroll.previousVisibleRows + oldTopRow + halfViewport ) / newVisibleRows; |
374 |
| - service.adjustInfiniteScrollPosition(grid, newPercentage); |
| 372 | + oldTop = grid.infiniteScroll.prevScrollTop || 0; |
| 373 | + newTop = oldTop + (newVisibleRows - grid.infiniteScroll.previousVisibleRows)*rowHeight; |
| 374 | + service.adjustInfiniteScrollPosition(grid, newTop); |
375 | 375 | $timeout( function() {
|
376 | 376 | promise.resolve();
|
377 | 377 | });
|
378 | 378 | }
|
379 | 379 |
|
380 | 380 | if ( grid.infiniteScroll.direction === uiGridConstants.scrollDirection.DOWN ){
|
381 |
| - oldPercentage = grid.infiniteScroll.prevScrolltopPercentage || 1; |
382 |
| - oldTopRow = oldPercentage * grid.infiniteScroll.previousVisibleRows; |
383 |
| - newPercentage = ( oldTopRow - halfViewport ) / newVisibleRows; |
384 |
| - service.adjustInfiniteScrollPosition(grid, newPercentage); |
| 381 | + newTop = grid.infiniteScroll.prevScrollTop || (grid.infiniteScroll.previousVisibleRows*rowHeight - viewportHeight); |
| 382 | + service.adjustInfiniteScrollPosition(grid, newTop); |
385 | 383 | $timeout( function() {
|
386 | 384 | promise.resolve();
|
387 | 385 | });
|
|
398 | 396 | * @methodOf ui.grid.infiniteScroll.service:uiGridInfiniteScrollService
|
399 | 397 | * @description This function fires 'needLoadMoreData' or 'needLoadMoreDataTop' event based on scrollDirection
|
400 | 398 | * @param {Grid} grid the grid we're working on
|
401 |
| - * @param {number} percentage the percentage through the grid that we want to scroll to |
| 399 | + * @param {number} scrollTop the position through the grid that we want to scroll to |
402 | 400 | * @returns {promise} a promise that is resolved when the scrolling finishes
|
403 | 401 | */
|
404 |
| - adjustInfiniteScrollPosition: function (grid, percentage) { |
405 |
| - var scrollEvent = new ScrollEvent(grid, null, null, 'ui.grid.adjustInfiniteScrollPosition'); |
| 402 | + adjustInfiniteScrollPosition: function (grid, scrollTop) { |
| 403 | + var scrollEvent = new ScrollEvent(grid, null, null, 'ui.grid.adjustInfiniteScrollPosition'), |
| 404 | + visibleRows = grid.getVisibleRowCount(), |
| 405 | + viewportHeight = grid.getViewportHeight() + grid.headerHeight - grid.renderContainers.body.headerHeight - grid.scrollbarHeight, |
| 406 | + rowHeight = grid.options.rowHeight, |
| 407 | + scrollHeight = visibleRows*rowHeight-viewportHeight; |
406 | 408 |
|
407 | 409 | //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
|
408 |
| - if ( percentage === 0 && grid.infiniteScroll.scrollUp ) { |
409 |
| - scrollEvent.y = {pixels: 1}; |
| 410 | + if (scrollTop === 0 && grid.infiniteScroll.scrollUp) { |
| 411 | + // using pixels results in a relative scroll, hence we have to use percentage |
| 412 | + scrollEvent.y = {percentage: 1/scrollHeight}; |
410 | 413 | }
|
411 | 414 | else {
|
412 |
| - scrollEvent.y = {percentage: percentage}; |
| 415 | + scrollEvent.y = {percentage: scrollTop/scrollHeight}; |
413 | 416 | }
|
414 | 417 | grid.scrollContainers('', scrollEvent);
|
415 | 418 | },
|
|
431 | 434 | * @returns {promise} a promise that is resolved when the scrolling finishes
|
432 | 435 | */
|
433 | 436 | dataRemovedTop: function( grid, scrollUp, scrollDown ) {
|
| 437 | + var newVisibleRows, oldTop, newTop, rowHeight; |
434 | 438 | service.setScrollDirections( grid, scrollUp, scrollDown );
|
435 | 439 |
|
436 |
| - var newVisibleRows = grid.renderContainers.body.visibleRowCache.length; |
437 |
| - var oldScrollRow = grid.infiniteScroll.prevScrolltopPercentage * grid.infiniteScroll.previousVisibleRows; |
| 440 | + newVisibleRows = grid.renderContainers.body.visibleRowCache.length; |
| 441 | + oldTop = grid.infiniteScroll.prevScrollTop; |
| 442 | + rowHeight = grid.options.rowHeight; |
438 | 443 |
|
439 | 444 | // since we removed from the top, our new scroll row will be the old scroll row less the number
|
440 | 445 | // of rows removed
|
441 |
| - var newScrollRow = oldScrollRow - ( grid.infiniteScroll.previousVisibleRows - newVisibleRows ); |
442 |
| - var newScrollPercent = newScrollRow / newVisibleRows; |
| 446 | + newTop = oldTop - ( grid.infiniteScroll.previousVisibleRows - newVisibleRows )*rowHeight; |
443 | 447 |
|
444 |
| - return service.adjustInfiniteScrollPosition( grid, newScrollPercent ); |
| 448 | + return service.adjustInfiniteScrollPosition( grid, newTop ); |
445 | 449 | },
|
446 | 450 |
|
447 | 451 | /**
|
|
459 | 463 | * fire infinite scroll events downward
|
460 | 464 | */
|
461 | 465 | dataRemovedBottom: function( grid, scrollUp, scrollDown ) {
|
| 466 | + var newTop; |
462 | 467 | service.setScrollDirections( grid, scrollUp, scrollDown );
|
463 | 468 |
|
464 |
| - var newVisibleRows = grid.renderContainers.body.visibleRowCache.length; |
465 |
| - var oldScrollRow = grid.infiniteScroll.prevScrolltopPercentage * grid.infiniteScroll.previousVisibleRows; |
466 |
| - |
467 |
| - // since we removed from the bottom, our new scroll row will be same as the old scroll row |
468 |
| - var newScrollPercent = oldScrollRow / newVisibleRows; |
| 469 | + newTop = grid.infiniteScroll.prevScrollTop; |
469 | 470 |
|
470 |
| - return service.adjustInfiniteScrollPosition( grid, newScrollPercent ); |
| 471 | + return service.adjustInfiniteScrollPosition( grid, newTop ); |
471 | 472 | }
|
472 | 473 | };
|
473 | 474 | return service;
|
|
0 commit comments