Browse Source

Refactors PDFHistory.

# Conflicts:
#	extensions/b2g/viewer.js
#	web/pdf_history.js
#	web/pdf_viewer.component.js
#	web/viewer.js
Yury Delendik 10 years ago
parent
commit
5cff06e52a
  1. 11
      web/interfaces.js
  2. 52
      web/pdf_history.js
  3. 8
      web/pdf_viewer.component.js
  4. 29
      web/viewer.js

11
web/interfaces.js

@ -59,6 +59,17 @@ IPDFLinkService.prototype = { @@ -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
*/

52
web/pdf_history.js

@ -17,15 +17,21 @@ @@ -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 = { @@ -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 = { @@ -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;
@ -65,7 +70,7 @@ var PDFHistory = { @@ -65,7 +70,7 @@ var PDFHistory = {
// is opened in the web viewer.
this.reInitialized = true;
}
this._pushOrReplaceState({ fingerprint: this.fingerprint }, true);
this._pushOrReplaceState({fingerprint: this.fingerprint}, true);
}
var self = this;
@ -88,8 +93,8 @@ var PDFHistory = { @@ -88,8 +93,8 @@ var PDFHistory = {
if (self.uid === 0) {
var previousParams = (self.previousHash && self.currentBookmark &&
self.previousHash !== self.currentBookmark) ?
{ hash: self.currentBookmark, page: self.currentPage } :
{ page: 1 };
{hash: self.currentBookmark, page: self.currentPage} :
{page: 1};
self.historyUnlocked = false;
self.allowHashChange = false;
window.history.back();
@ -97,7 +102,7 @@ var PDFHistory = { @@ -97,7 +102,7 @@ var PDFHistory = {
window.history.forward();
self.historyUnlocked = true;
}
self._pushToHistory({ hash: self.previousHash }, false, true);
self._pushToHistory({hash: self.previousHash}, false, true);
self._updatePreviousBookmark();
}
}, false);
@ -112,14 +117,16 @@ var PDFHistory = { @@ -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 = { @@ -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
@ -289,7 +298,7 @@ var PDFHistory = { @@ -289,7 +298,7 @@ var PDFHistory = {
} else {
return null;
}
var params = { hash: this.currentBookmark, page: this.currentPage };
var params = {hash: this.currentBookmark, page: this.currentPage};
if (this.isViewerInPresentationMode) {
params.hash = null;
}
@ -297,7 +306,7 @@ var PDFHistory = { @@ -297,7 +306,7 @@ var PDFHistory = {
},
_stateObj: function pdfHistory_stateObj(params) {
return { fingerprint: this.fingerprint, uid: this.uid, target: params };
return {fingerprint: this.fingerprint, uid: this.uid, target: params};
},
_pushToHistory: function pdfHistory_pushToHistory(params,
@ -379,4 +388,7 @@ var PDFHistory = { @@ -379,4 +388,7 @@ var PDFHistory = {
}
}
}
};
};
return PDFHistory;
})();

8
web/pdf_viewer.component.js

@ -16,8 +16,8 @@ @@ -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') { @@ -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') { @@ -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);

29
web/viewer.js

@ -97,6 +97,7 @@ var mozL10n = document.mozL10n || document.webL10n; @@ -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 = { @@ -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 = { @@ -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 = { @@ -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 = { @@ -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) { @@ -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) { @@ -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) { @@ -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;

Loading…
Cancel
Save