Fix regressions caused by the recent implementation of browsing history - address comments v3

This commit is contained in:
Jonas 2013-05-18 18:18:44 +02:00
parent 59d0ccf64b
commit d3c9a9d856

View File

@ -29,6 +29,7 @@ var VERTICAL_PADDING = 5;
var MIN_SCALE = 0.25; var MIN_SCALE = 0.25;
var MAX_SCALE = 4.0; var MAX_SCALE = 4.0;
var SETTINGS_MEMORY = 20; var SETTINGS_MEMORY = 20;
var HISTORY_DISABLED = false;
var RenderingStates = { var RenderingStates = {
INITIAL: 0, INITIAL: 0,
RUNNING: 1, RUNNING: 1,
@ -690,7 +691,7 @@ var PDFHistory = {
initialDestination: null, initialDestination: null,
initialize: function pdfHistoryInitialize(params, fingerprint) { initialize: function pdfHistoryInitialize(params, fingerprint) {
if (window.parent !== window) { if (HISTORY_DISABLED || window.parent !== window) {
// The browsing history is only enabled when the viewer is standalone, // The browsing history is only enabled when the viewer is standalone,
// i.e. not when it is embedded in a page. // i.e. not when it is embedded in a page.
return; return;
@ -699,10 +700,12 @@ var PDFHistory = {
this.reInitialized = false; this.reInitialized = false;
this.historyUnlocked = true; this.historyUnlocked = true;
this.previousHash = '';
this.currentBookmark = '';
this.currentPage = 1;
this.fingerprint = fingerprint; this.fingerprint = fingerprint;
this.currentUid = this.uid = 0; this.currentUid = this.uid = 0;
this.currentPage = 1;
this.previousHash = '';
this.current = {}; this.current = {};
var state = window.history.state; var state = window.history.state;
@ -737,15 +740,17 @@ var PDFHistory = {
self._goTo(evt.state); self._goTo(evt.state);
} else { } else {
self.previousHash = window.location.hash.substring(1); self.previousHash = window.location.hash.substring(1);
self._pushToHistory({ hash: self.previousHash }, self._pushToHistory({ hash: self.previousHash }, false, true);
false, true, !!self.previousHash);
} }
}, false); }, false);
window.addEventListener('beforeunload', window.addEventListener('beforeunload',
function pdfHistoryBeforeunload(evt) { function pdfHistoryBeforeunload(evt) {
if (self.current.page && self.current.page !== self.currentPage) { if (self._shouldPreviousPositionBeAddedToHistory(true)) {
self._pushToHistory({ page: self.currentPage }, false); var previousParams = self._getPreviousParams();
if (previousParams) {
self._pushToHistory(previousParams, false);
}
} }
if (PDFView.isPresentationMode) { if (PDFView.isPresentationMode) {
// Prevent the user from accidentally navigating away from // Prevent the user from accidentally navigating away from
@ -777,9 +782,11 @@ var PDFHistory = {
return temp; return temp;
}, },
updateCurrentPageNumber: function pdfHistoryUpdateCurrentPageNumber(page) { updateCurrentBookmark: function pdfHistoryUpdateCurrentBookmark(bookmark,
pageNum) {
if (this.initialized) { if (this.initialized) {
this.currentPage = page | 0; this.currentBookmark = bookmark.substring(1);
this.currentPage = pageNum | 0;
} }
}, },
@ -787,7 +794,6 @@ var PDFHistory = {
if (!(this.initialized && this.historyUnlocked)) { if (!(this.initialized && this.historyUnlocked)) {
return; return;
} }
if (params.dest && !params.hash) { if (params.dest && !params.hash) {
params.hash = (this.current.dest === params.dest && this.current.hash) ? params.hash = (this.current.dest === params.dest && this.current.hash) ?
this.current.hash : this.current.hash :
@ -796,7 +802,6 @@ var PDFHistory = {
if (params.page) { if (params.page) {
params.page |= 0; params.page |= 0;
} }
if (params.hash) { if (params.hash) {
if (this.current.hash) { if (this.current.hash) {
if (this.current.hash !== params.hash) { if (this.current.hash !== params.hash) {
@ -812,29 +817,61 @@ var PDFHistory = {
} }
}, },
_stateObj: function PDFHistory_stateObj(target) { _stateObj: function pdfHistory_stateObj(params) {
return { fingerprint: this.fingerprint, uid: this.uid, target: target }; return { fingerprint: this.fingerprint, uid: this.uid, target: params };
}, },
_pushToHistory: function pdfHistory_pushToHistory(params, addPrevious, _shouldPreviousPositionBeAddedToHistory:
overwrite, addUrl) { function pdfHistory_shouldPreviousPositionBeAddedToHistory(onUnload) {
if (!(this.currentBookmark && this.currentPage)) {
return false;
}
if (this.previousHash && this.previousHash === this.current.hash) {
return false;
}
if (!this.current.dest) {
if (this.current.hash === this.currentBookmark) {
return false;
} else if (onUnload) {
return true;
}
} else if (this.current.page && this.current.page === this.currentPage) {
return false;
}
return true;
},
_getPreviousParams: function pdfHistory_getPreviousParams() {
var previousParams = { hash: this.currentBookmark,
page: this.currentPage };
if (PDFView.isPresentationMode) {
if (this.current.page && this.current.page !== this.currentPage) {
previousParams.hash = null;
} else {
previousParams = null;
}
}
return previousParams;
},
_pushToHistory: function pdfHistory_pushToHistory(params,
addPrevious, overwrite) {
if (!this.initialized) { if (!this.initialized) {
return; return;
} }
if (!params.hash && params.page) { if (!params.hash && params.page) {
params.hash = ('page=' + params.page); params.hash = ('page=' + params.page);
} }
var hash = addUrl ? ('#' + this.previousHash) : '';
if (overwrite) { if (overwrite) {
window.history.replaceState(this._stateObj(params), '', hash); window.history.replaceState(this._stateObj(params), '', '');
} else { } else {
if (addPrevious && if (addPrevious && this._shouldPreviousPositionBeAddedToHistory()) {
this.current.page && this.current.page !== this.currentPage) { var previousParams = this._getPreviousParams();
this._pushToHistory({ page: this.currentPage }, false); if (previousParams) {
this._pushToHistory(previousParams, false);
}
} }
window.history.pushState(this._stateObj(params), '', hash); window.history.pushState(this._stateObj(params), '', '');
} }
this.currentUid = this.uid++; this.currentUid = this.uid++;
this.current = params; this.current = params;
@ -845,16 +882,18 @@ var PDFHistory = {
this._isStateObjectDefined(state))) { this._isStateObjectDefined(state))) {
return; return;
} }
if (!this.reInitialized && state.uid < this.currentUid && if (!this.reInitialized && state.uid < this.currentUid &&
this.currentBookmark && this.currentPage &&
this.current.page && this.current.page !== this.currentPage) { this.current.page && this.current.page !== this.currentPage) {
this._pushToHistory(this.current, false, false, !!this.previousHash); var previousParams = this._getPreviousParams();
this._pushToHistory({ page: this.currentPage }, false); if (previousParams) {
this._pushToHistory(this.current, false, false);
this._pushToHistory(previousParams, false);
window.history.back(); window.history.back();
return; return;
}
} }
this.historyUnlocked = false; this.historyUnlocked = false;
if (state.target.dest) { if (state.target.dest) {
@ -3576,8 +3615,8 @@ function updateViewarea() {
var href = PDFView.getAnchorUrl(pdfOpenParams); var href = PDFView.getAnchorUrl(pdfOpenParams);
document.getElementById('viewBookmark').href = href; document.getElementById('viewBookmark').href = href;
// Update the current page number in the browsing history. // Update the current bookmark in the browsing history.
PDFHistory.updateCurrentPageNumber(pageNumber); PDFHistory.updateCurrentBookmark(pdfOpenParams, pageNumber);
} }
window.addEventListener('resize', function webViewerResize(evt) { window.addEventListener('resize', function webViewerResize(evt) {