Merge pull request #3449 from Snuffleupagus/history-remove-beforeunload
[Browsing history] Remove 'beforeunload' to enable caching and prevent duplicate entries
This commit is contained in:
		
						commit
						5fa609584e
					
				| @ -195,7 +195,7 @@ var PDFHistory = { | |||||||
| 
 | 
 | ||||||
|     var state = window.history.state; |     var state = window.history.state; | ||||||
|     if (this._isStateObjectDefined(state)) { |     if (this._isStateObjectDefined(state)) { | ||||||
|       // This case corresponds to navigating back to the document
 |       // This corresponds to navigating back to the document
 | ||||||
|       // from another page in the browser history.
 |       // from another page in the browser history.
 | ||||||
|       if (state.target.dest) { |       if (state.target.dest) { | ||||||
|         this.initialDestination = state.target.dest; |         this.initialDestination = state.target.dest; | ||||||
| @ -206,7 +206,7 @@ var PDFHistory = { | |||||||
|       this.uid = state.uid + 1; |       this.uid = state.uid + 1; | ||||||
|       this.current = state.target; |       this.current = state.target; | ||||||
|     } else { |     } else { | ||||||
|       // This case corresponds to the loading of a new document.
 |       // This corresponds to the loading of a new document.
 | ||||||
|       if (state && state.fingerprint && |       if (state && state.fingerprint && | ||||||
|           this.fingerprint !== state.fingerprint) { |           this.fingerprint !== state.fingerprint) { | ||||||
|         // Reinitialize the browsing history when a new document
 |         // Reinitialize the browsing history when a new document
 | ||||||
| @ -230,10 +230,14 @@ var PDFHistory = { | |||||||
|       } else { |       } else { | ||||||
|         // Handle the user modifying the hash of a loaded document.
 |         // Handle the user modifying the hash of a loaded document.
 | ||||||
|         self.previousHash = window.location.hash.substring(1); |         self.previousHash = window.location.hash.substring(1); | ||||||
|  | 
 | ||||||
|  |         // If the history is empty when the hash changes,
 | ||||||
|  |         // update the previous entry in the browser history.
 | ||||||
|         if (self.uid === 0) { |         if (self.uid === 0) { | ||||||
|           var previousParams = (self.previousHash && self.currentBookmark && |           var previousParams = (self.previousHash && self.currentBookmark && | ||||||
|                                 self.previousHash !== self.currentBookmark) ? |                                 self.previousHash !== self.currentBookmark) ? | ||||||
|             { hash: self.currentBookmark } : { page: 1 }; |             { hash: self.currentBookmark, page: self.currentPage } : | ||||||
|  |             { page: 1 }; | ||||||
|           self.historyUnlocked = false; |           self.historyUnlocked = false; | ||||||
|           self.allowHashChange = false; |           self.allowHashChange = false; | ||||||
|           window.history.back(); |           window.history.back(); | ||||||
| @ -242,23 +246,29 @@ var PDFHistory = { | |||||||
|           self.historyUnlocked = true; |           self.historyUnlocked = true; | ||||||
|         } |         } | ||||||
|         self._pushToHistory({ hash: self.previousHash }, false, true); |         self._pushToHistory({ hash: self.previousHash }, false, true); | ||||||
|         if (self.currentBookmark) { |         self._updatePreviousBookmark(); | ||||||
|           self.previousBookmark = self.currentBookmark; |  | ||||||
|         } |  | ||||||
|       } |       } | ||||||
|     }, false); |     }, false); | ||||||
| 
 | 
 | ||||||
|     window.addEventListener('beforeunload', |     function pdfHistoryBeforeUnload() { | ||||||
|                             function pdfHistoryBeforeunload(evt) { |  | ||||||
|       var previousParams = self._getPreviousParams(null, true); |       var previousParams = self._getPreviousParams(null, true); | ||||||
|       if (previousParams) { |       if (previousParams) { | ||||||
|         self._pushToHistory(previousParams, false); |         var replacePrevious = (!self.current.dest && | ||||||
|       } |                                self.current.hash !== self.previousHash); | ||||||
|       if (PDFView.isPresentationMode) { |         self._pushToHistory(previousParams, false, replacePrevious); | ||||||
|         // Prevent the user from accidentally navigating away from
 |         self._updatePreviousBookmark(); | ||||||
|         // the document when presentation mode is active.
 |  | ||||||
|         evt.preventDefault(); |  | ||||||
|       } |       } | ||||||
|  |       // Remove the event listener when navigating away from the document,
 | ||||||
|  |       // since 'beforeunload' prevents Firefox from caching the document.
 | ||||||
|  |       window.removeEventListener('beforeunload', pdfHistoryBeforeUnload, false); | ||||||
|  |     } | ||||||
|  |     window.addEventListener('beforeunload', pdfHistoryBeforeUnload, false); | ||||||
|  | 
 | ||||||
|  |     window.addEventListener('pageshow', function pdfHistoryPageShow(evt) { | ||||||
|  |       // If the entire viewer (including the PDF file) is cached in the browser,
 | ||||||
|  |       // we need to reattach the 'beforeunload' event listener since
 | ||||||
|  |       // the 'DOMContentLoaded' event is not fired on 'pageshow'.
 | ||||||
|  |       window.addEventListener('beforeunload', pdfHistoryBeforeUnload, false); | ||||||
|     }, false); |     }, false); | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
| @ -287,16 +297,21 @@ var PDFHistory = { | |||||||
|     return temp; |     return temp; | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|  |   _updatePreviousBookmark: function pdfHistory_updatePreviousBookmark() { | ||||||
|  |     if (this.updatePreviousBookmark && | ||||||
|  |         this.currentBookmark && this.currentPage) { | ||||||
|  |       this.previousBookmark = this.currentBookmark; | ||||||
|  |       this.previousPage = this.currentPage; | ||||||
|  |       this.updatePreviousBookmark = false; | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  | 
 | ||||||
|   updateCurrentBookmark: function pdfHistoryUpdateCurrentBookmark(bookmark, |   updateCurrentBookmark: function pdfHistoryUpdateCurrentBookmark(bookmark, | ||||||
|                                                                   pageNum) { |                                                                   pageNum) { | ||||||
|     if (this.initialized) { |     if (this.initialized) { | ||||||
|       this.currentBookmark = bookmark.substring(1); |       this.currentBookmark = bookmark.substring(1); | ||||||
|       this.currentPage = pageNum | 0; |       this.currentPage = pageNum | 0; | ||||||
|       if (this.updatePreviousBookmark) { |       this._updatePreviousBookmark(); | ||||||
|         this.previousBookmark = this.currentBookmark; |  | ||||||
|         this.previousPage = this.currentPage; |  | ||||||
|         this.updatePreviousBookmark = false; |  | ||||||
|       } |  | ||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
| @ -319,10 +334,20 @@ var PDFHistory = { | |||||||
|     if (params.page) { |     if (params.page) { | ||||||
|       params.page |= 0; |       params.page |= 0; | ||||||
|     } |     } | ||||||
|     if (isInitialBookmark && this.uid === 0) { |     if (isInitialBookmark) { | ||||||
|       this._pushToHistory(params, false); |       var target = window.history.state.target; | ||||||
|       this.previousHash = window.location.hash.substring(1); |       if (!target) { | ||||||
|  |         // Invoked when the user specifies an initial bookmark,
 | ||||||
|  |         // thus setting PDFView.initialBookmark, when the document is loaded.
 | ||||||
|  |         this._pushToHistory(params, false); | ||||||
|  |         this.previousHash = window.location.hash.substring(1); | ||||||
|  |       } | ||||||
|       this.updatePreviousBookmark = this.nextHashParam ? false : true; |       this.updatePreviousBookmark = this.nextHashParam ? false : true; | ||||||
|  |       if (target) { | ||||||
|  |         // If the current document is reloaded,
 | ||||||
|  |         // avoid creating duplicate entries in the history.
 | ||||||
|  |         this._updatePreviousBookmark(); | ||||||
|  |       } | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     if (this.nextHashParam && this.nextHashParam === params.hash) { |     if (this.nextHashParam && this.nextHashParam === params.hash) { | ||||||
| @ -335,8 +360,11 @@ var PDFHistory = { | |||||||
|       if (this.current.hash) { |       if (this.current.hash) { | ||||||
|         if (this.current.hash !== params.hash) { |         if (this.current.hash !== params.hash) { | ||||||
|           this._pushToHistory(params, true); |           this._pushToHistory(params, true); | ||||||
|         } else if (!this.current.page && params.page) { |         } else { | ||||||
|           this._pushToHistory(params, false, true); |           if (!this.current.page && params.page) { | ||||||
|  |             this._pushToHistory(params, false, true); | ||||||
|  |           } | ||||||
|  |           this.updatePreviousBookmark = true; | ||||||
|         } |         } | ||||||
|       } else { |       } else { | ||||||
|         this._pushToHistory(params, true); |         this._pushToHistory(params, true); | ||||||
| @ -385,7 +413,8 @@ var PDFHistory = { | |||||||
|     if (addPrevious && !overwrite) { |     if (addPrevious && !overwrite) { | ||||||
|       var previousParams = this._getPreviousParams(); |       var previousParams = this._getPreviousParams(); | ||||||
|       if (previousParams) { |       if (previousParams) { | ||||||
|         this._pushToHistory(previousParams, false); |         var replacePrevious = (this.current.hash !== this.previousHash); | ||||||
|  |         this._pushToHistory(previousParams, false, replacePrevious); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     if (overwrite || this.uid === 0) { |     if (overwrite || this.uid === 0) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user