Commits

Anton Afanasyev committed 3752295

when loading previous page, current data stays on screen in the same position

  • Participants
  • Parent commits 98215a6
  • Branches backwards-load

Comments (0)

Files changed (1)

File autopagerize.user.js

     if( data.finalUrl )
         this.requestURL = data.finalURL;
 
-    var urlPrev = getNextURL( this.info.nextLink, data.htmlDoc, loadedUrl );
+    var url = getNextURL( this.info.nextLink, data.htmlDoc, loadedUrl );
 
     if ( !data.page || data.page.length < 1 ) {
         debug('pageElement not found.' , this.info.pageElement);
     this.loadedURLs[this.requestURL] = true;
     var page = this.addPage( data.htmlDoc, data.page, this.requestURL, 'next' );
     AutoPager.filters.forEach(function(i) { i(page); });
-    this.requestURL = urlPrev;
+    this.requestURL = url;
     this.showLoading(false);
-    if (!urlPrev) {
+    if (!url) {
         debug('nextLink not found.', this.info.nextLink, data.htmlDoc);
         this.forceForce = true;
         this.terminate('next');
         this.addPageSeparatorIfNeeded( insertBeboreThisNode, insertParent, page, loadedUrl );
         mapRetVals = this.addPageItems_( insertBeboreThisNode, insertParent, page, loadedUrl );
     } else if ( direction == 'prev' ) {
+        // must scroll back to where we were after adding the items
+        var wsx = window.scrollX;
+        var oldPgElems = this.getPageElements( document );
+        var oldPos = getElementPosition(oldPgElems[0]).top - window.scrollY;
+
         mapRetVals = this.addPageItems_( insertBeboreThisNode, insertParent, page, loadedUrl );
         this.addPageSeparatorIfNeeded( insertBeboreThisNode, insertParent, page, loadedUrl );
+
+        var newPos = getElementPosition(oldPgElems[0]).top - window.scrollY;
+        window.scrollTo( wsx, window.scrollY + (newPos - oldPos) );
     }
 
     if( !this.updateInsertPoint( direction ) ) {
     // if we've scrolled past the beginning of the data
     var tbrTop = getElementPosition(ap.toBeRemoved[0]).top;
     var tbrBottom = getElementBottom(ap.toBeRemoved[ap.toBeRemoved.length-1]);
+    var wsx = window.scrollX;
     var wsy = window.scrollY;
     var scrollHeight = getScrollHeight( document );
 
     //document.title = wsy.toString() + " | " + tbrTop.toString() + ' | ' + tbrBottom.toString() + ' | ' + scrollHeight.toString() + ' | ';
 
     if(tbrBottom+ap.remainHeight<wsy || force) {
-        window.scrollTo(0, wsy-(tbrBottom-tbrTop));
+        window.scrollTo(wsx, wsy-(tbrBottom-tbrTop));
         
         for(var cnt=0; cnt<ap.toBeRemoved.length; cnt++) {
             if(ap.toBeRemoved[cnt].parentNode) {