Refactors PDFHistory.

# Conflicts:
#	extensions/b2g/viewer.js
#	web/pdf_history.js
#	web/pdf_viewer.component.js
#	web/viewer.js
This commit is contained in:
Yury Delendik 2015-04-27 09:04:11 -05:00
parent 0ef6212b64
commit 5cff06e52a
4 changed files with 361 additions and 323 deletions

View File

@ -59,6 +59,17 @@ IPDFLinkService.prototype = {
cachePageRef: function (pageNum, pageRef) {},
};
/**
* @interface
*/
function IPDFHistory() {}
IPDFHistory.prototype = {
forward: function () {},
back: function () {},
push: function (params) {},
updateNextHashParam: function (hash) {},
};
/**
* @interface
*/

View File

@ -17,15 +17,21 @@
'use strict';
var PDFHistory = {
initialized: false,
initialDestination: null,
var PDFHistory = (function () {
function PDFHistory(options) {
this.linkService = options.linkService;
this.initialized = false;
this.initialDestination = null;
this.initialBookmark = null;
}
PDFHistory.prototype = {
/**
* @param {string} fingerprint
* @param {IPDFLinkService} linkService
*/
initialize: function pdfHistoryInitialize(fingerprint, linkService) {
initialize: function pdfHistoryInitialize(fingerprint) {
this.initialized = true;
this.reInitialized = false;
this.allowHashChange = true;
@ -41,7 +47,6 @@ var PDFHistory = {
this.nextHashParam = '';
this.fingerprint = fingerprint;
this.linkService = linkService;
this.currentUid = this.uid = 0;
this.current = {};
@ -52,7 +57,7 @@ var PDFHistory = {
if (state.target.dest) {
this.initialDestination = state.target.dest;
} else {
linkService.setHash(state.target.hash);
this.initialBookmark = state.target.hash;
}
this.currentUid = state.uid;
this.uid = state.uid + 1;
@ -112,14 +117,16 @@ var PDFHistory = {
}
// Remove the event listener when navigating away from the document,
// since 'beforeunload' prevents Firefox from caching the document.
window.removeEventListener('beforeunload', pdfHistoryBeforeUnload, false);
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'.
// 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);
@ -266,10 +273,12 @@ var PDFHistory = {
}
if (this.uid > 0 && !(this.previousBookmark && this.previousPage)) {
// Prevent the history from getting stuck in the current state,
// effectively preventing the user from going back/forward in the history.
// effectively preventing the user from going back/forward in
// the history.
//
// This happens if the current position in the document didn't change when
// the history was previously updated. The reasons for this are either:
// This happens if the current position in the document didn't change
// when the history was previously updated. The reasons for this are
// either:
// 1. The current zoom value is such that the document does not need to,
// or cannot, be scrolled to display the destination.
// 2. The previous destination is broken, and doesn't actally point to a
@ -380,3 +389,6 @@ var PDFHistory = {
}
}
};
return PDFHistory;
})();

View File

@ -16,8 +16,8 @@
*/
/*jshint globalstrict: false */
/* globals PDFJS, PDFViewer, PDFPageView, TextLayerBuilder, PDFLinkService,
DefaultTextLayerFactory, AnnotationsLayerBuilder,
DefaultAnnotationsLayerFactory */
DefaultTextLayerFactory, AnnotationsLayerBuilder, PDFHistory,
DefaultAnnotationsLayerFactory, getFileName */
// Initializing PDFJS global object (if still undefined)
if (typeof PDFJS === 'undefined') {
@ -30,6 +30,7 @@ if (typeof PDFJS === 'undefined') {
//#include ui_utils.js
//#include pdf_link_service.js
//#include pdf_viewer.js
//#include pdf_history.js
PDFJS.PDFViewer = PDFViewer;
PDFJS.PDFPageView = PDFPageView;
@ -38,4 +39,7 @@ if (typeof PDFJS === 'undefined') {
PDFJS.DefaultTextLayerFactory = DefaultTextLayerFactory;
PDFJS.AnnotationsLayerBuilder = AnnotationsLayerBuilder;
PDFJS.DefaultAnnotationsLayerFactory = DefaultAnnotationsLayerFactory;
PDFJS.PDFHistory = PDFHistory;
PDFJS.getFileName = getFileName;
}).call((typeof window === 'undefined') ? this : window);

View File

@ -97,6 +97,7 @@ var mozL10n = document.mozL10n || document.webL10n;
var PDFViewerApplication = {
initialBookmark: document.location.hash.substring(1),
initialDestination: null,
initialized: false,
fellback: false,
pdfDocument: null,
@ -114,6 +115,8 @@ var PDFViewerApplication = {
pdfDocumentProperties: null,
/** @type {PDFLinkService} */
pdfLinkService: null,
/** @type {PDFHistory} */
pdfHistory: null,
pageRotation: 0,
updateScaleControls: true,
isInitialViewSet: false,
@ -155,6 +158,11 @@ var PDFViewerApplication = {
Preferences.initialize();
this.pdfHistory = new PDFHistory({
linkService: pdfLinkService
});
pdfLinkService.setHistory(this.pdfHistory);
this.findController = new PDFFindController({
pdfViewer: this.pdfViewer,
integratedFind: this.supportsIntegratedFind
@ -790,7 +798,9 @@ var PDFViewerApplication = {
if (!self.preferenceShowPreviousViewOnLoad && window.history.state) {
window.history.replaceState(null, '');
}
PDFHistory.initialize(self.documentFingerprint, self.pdfLinkService);
self.pdfHistory.initialize(self.documentFingerprint);
self.initialDestination = self.pdfHistory.initialDestination;
self.initialBookmark = self.pdfHistory.initialBookmark;
}
store.initializedPromise.then(function resolved() {
@ -969,12 +979,12 @@ var PDFViewerApplication = {
document.getElementById('pageNumber').value =
this.pdfViewer.currentPageNumber = 1;
if (PDFHistory.initialDestination) {
this.pdfLinkService.navigateTo(PDFHistory.initialDestination);
PDFHistory.initialDestination = null;
if (this.initialDestination) {
this.pdfLinkService.navigateTo(this.initialDestination);
this.initialDestination = null;
} else if (this.initialBookmark) {
this.pdfLinkService.setHash(this.initialBookmark);
PDFHistory.push({ hash: this.initialBookmark }, !!this.initialBookmark);
this.pdfHistory.push({ hash: this.initialBookmark }, true);
this.initialBookmark = null;
} else if (storedHash) {
this.pdfLinkService.setHash(storedHash);
@ -1635,7 +1645,8 @@ window.addEventListener('updateviewarea', function (evt) {
document.getElementById('secondaryViewBookmark').href = href;
// Update the current bookmark in the browsing history.
PDFHistory.updateCurrentBookmark(location.pdfOpenParams, location.pageNumber);
PDFViewerApplication.pdfHistory.updateCurrentBookmark(location.pdfOpenParams,
location.pageNumber);
// Show/hide the loading indicator in the page number input element.
var pageNumberInput = document.getElementById('pageNumber');
@ -1665,7 +1676,7 @@ window.addEventListener('resize', function webViewerResize(evt) {
});
window.addEventListener('hashchange', function webViewerHashchange(evt) {
if (PDFHistory.isHashChangeUnlocked) {
if (PDFViewerApplication.pdfHistory.isHashChangeUnlocked) {
var hash = document.location.hash.substring(1);
if (!hash) {
return;
@ -2070,13 +2081,13 @@ window.addEventListener('keydown', function keydown(evt) {
switch (evt.keyCode) {
case 37: // left arrow
if (isViewerInPresentationMode) {
PDFHistory.back();
PDFViewerApplication.pdfHistory.back();
handled = true;
}
break;
case 39: // right arrow
if (isViewerInPresentationMode) {
PDFHistory.forward();
PDFViewerApplication.pdfHistory.forward();
handled = true;
}
break;