Merge pull request #3302 from Snuffleupagus/history-improvements
Fix remaining issues in the browsing history
This commit is contained in:
commit
297266f1d6
180
web/viewer.js
180
web/viewer.js
@ -687,10 +687,9 @@ var PDFFindBar = {
|
|||||||
|
|
||||||
var PDFHistory = {
|
var PDFHistory = {
|
||||||
initialized: false,
|
initialized: false,
|
||||||
allowHashChange: true,
|
|
||||||
initialDestination: null,
|
initialDestination: null,
|
||||||
|
|
||||||
initialize: function pdfHistoryInitialize(params, fingerprint) {
|
initialize: function pdfHistoryInitialize(fingerprint) {
|
||||||
if (HISTORY_DISABLED || 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.
|
||||||
@ -698,11 +697,15 @@ var PDFHistory = {
|
|||||||
}
|
}
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
this.reInitialized = false;
|
this.reInitialized = false;
|
||||||
|
this.allowHashChange = true;
|
||||||
this.historyUnlocked = true;
|
this.historyUnlocked = true;
|
||||||
|
|
||||||
this.previousHash = '';
|
this.previousHash = window.location.hash.substring(1);
|
||||||
this.currentBookmark = '';
|
this.currentBookmark = '';
|
||||||
this.currentPage = 1;
|
this.currentPage = 0;
|
||||||
|
this.updatePreviousBookmark = false;
|
||||||
|
this.previousBookmark = '';
|
||||||
|
this.nextHashParam = '';
|
||||||
|
|
||||||
this.fingerprint = fingerprint;
|
this.fingerprint = fingerprint;
|
||||||
this.currentUid = this.uid = 0;
|
this.currentUid = this.uid = 0;
|
||||||
@ -728,7 +731,7 @@ var PDFHistory = {
|
|||||||
// is opened in the web viewer.
|
// is opened in the web viewer.
|
||||||
this.reInitialized = true;
|
this.reInitialized = true;
|
||||||
}
|
}
|
||||||
this._pushToHistory(params, false, true);
|
window.history.replaceState({ fingerprint: this.fingerprint }, '', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -736,21 +739,38 @@ var PDFHistory = {
|
|||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
evt.stopPropagation();
|
evt.stopPropagation();
|
||||||
|
|
||||||
|
if (!self.historyUnlocked) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (evt.state) {
|
if (evt.state) {
|
||||||
|
// Move back/forward in the history.
|
||||||
self._goTo(evt.state);
|
self._goTo(evt.state);
|
||||||
} else {
|
} else {
|
||||||
|
// 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 (self.uid === 0) {
|
||||||
|
var previousParams = (self.previousHash && self.currentBookmark &&
|
||||||
|
self.previousHash !== self.currentBookmark) ?
|
||||||
|
{ hash: self.currentBookmark } : { page: 1 };
|
||||||
|
self.historyUnlocked = false;
|
||||||
|
self.allowHashChange = false;
|
||||||
|
window.history.back();
|
||||||
|
self._pushToHistory(previousParams, false, true);
|
||||||
|
window.history.forward();
|
||||||
|
self.historyUnlocked = true;
|
||||||
|
}
|
||||||
self._pushToHistory({ hash: self.previousHash }, false, true);
|
self._pushToHistory({ hash: self.previousHash }, false, true);
|
||||||
|
if (self.currentBookmark) {
|
||||||
|
self.previousBookmark = self.currentBookmark;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
window.addEventListener('beforeunload',
|
window.addEventListener('beforeunload',
|
||||||
function pdfHistoryBeforeunload(evt) {
|
function pdfHistoryBeforeunload(evt) {
|
||||||
if (self._shouldPreviousPositionBeAddedToHistory(true)) {
|
var previousParams = self._getPreviousParams(null, true);
|
||||||
var previousParams = self._getPreviousParams();
|
if (previousParams) {
|
||||||
if (previousParams) {
|
self._pushToHistory(previousParams, false);
|
||||||
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
|
||||||
@ -767,11 +787,14 @@ var PDFHistory = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get isHashChangeUnlocked() {
|
get isHashChangeUnlocked() {
|
||||||
|
if (!this.initialized) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
// If the current hash changes when moving back/forward in the history,
|
// If the current hash changes when moving back/forward in the history,
|
||||||
// this will trigger a 'popstate' event *as well* as a 'hashchange' event.
|
// this will trigger a 'popstate' event *as well* as a 'hashchange' event.
|
||||||
// Since the hash generally won't correspond to the exact the position
|
// Since the hash generally won't correspond to the exact the position
|
||||||
// stored in the history's state object, triggering the 'hashchange' event
|
// stored in the history's state object, triggering the 'hashchange' event
|
||||||
// would corrupt the browser history.
|
// can thus corrupt the browser history.
|
||||||
//
|
//
|
||||||
// When the hash changes during a 'popstate' event, we *only* prevent the
|
// When the hash changes during a 'popstate' event, we *only* prevent the
|
||||||
// first 'hashchange' event and immediately reset allowHashChange.
|
// first 'hashchange' event and immediately reset allowHashChange.
|
||||||
@ -787,73 +810,87 @@ var PDFHistory = {
|
|||||||
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.previousBookmark = this.currentBookmark;
|
||||||
|
this.updatePreviousBookmark = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
push: function pdfHistoryPush(params) {
|
updateNextHashParam: function pdfHistoryUpdateNextHashParam(param) {
|
||||||
|
if (this.initialized) {
|
||||||
|
this.nextHashParam = param;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
push: function pdfHistoryPush(params, isInitialBookmark) {
|
||||||
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.hash && this.current.dest &&
|
||||||
|
this.current.dest === params.dest) ?
|
||||||
this.current.hash :
|
this.current.hash :
|
||||||
PDFView.getDestinationHash(params.dest).split('#')[1];
|
PDFView.getDestinationHash(params.dest).split('#')[1];
|
||||||
}
|
}
|
||||||
if (params.page) {
|
if (params.page) {
|
||||||
params.page |= 0;
|
params.page |= 0;
|
||||||
}
|
}
|
||||||
|
if (isInitialBookmark && this.uid === 0) {
|
||||||
|
this._pushToHistory(params, false);
|
||||||
|
this.previousHash = window.location.hash.substring(1);
|
||||||
|
this.updatePreviousBookmark = this.nextHashParam ? false : true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.nextHashParam && this.nextHashParam === params.hash) {
|
||||||
|
this.nextHashParam = null;
|
||||||
|
this.updatePreviousBookmark = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
||||||
this._pushToHistory(params, true);
|
this._pushToHistory(params, true);
|
||||||
} else if (!this.current.page && params.page) {
|
} else if (!this.current.page && params.page) {
|
||||||
this._pushToHistory(params, false, true, true);
|
this._pushToHistory(params, false, true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this._pushToHistory(params, true);
|
this._pushToHistory(params, true);
|
||||||
}
|
}
|
||||||
} else if (this.current.page && this.current.page !== params.page) {
|
} else if (this.current.page && params.page &&
|
||||||
|
this.current.page !== params.page) {
|
||||||
this._pushToHistory(params, true);
|
this._pushToHistory(params, true);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_getPreviousParams: function pdfHistory_getPreviousParams(onlyCheckPage,
|
||||||
|
beforeUnload) {
|
||||||
|
if (!(this.currentBookmark && this.currentPage)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ((!this.current.dest && !onlyCheckPage) || beforeUnload) {
|
||||||
|
if (this.previousBookmark === this.currentBookmark) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else if (this.current.page) {
|
||||||
|
if (this.current.page === this.currentPage) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
var params = { hash: this.currentBookmark, page: this.currentPage };
|
||||||
|
if (PDFView.isPresentationMode) {
|
||||||
|
params.hash = null;
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
},
|
||||||
|
|
||||||
_stateObj: function pdfHistory_stateObj(params) {
|
_stateObj: function pdfHistory_stateObj(params) {
|
||||||
return { fingerprint: this.fingerprint, uid: this.uid, target: params };
|
return { fingerprint: this.fingerprint, uid: this.uid, target: params };
|
||||||
},
|
},
|
||||||
|
|
||||||
_shouldPreviousPositionBeAddedToHistory:
|
|
||||||
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,
|
_pushToHistory: function pdfHistory_pushToHistory(params,
|
||||||
addPrevious, overwrite) {
|
addPrevious, overwrite) {
|
||||||
if (!this.initialized) {
|
if (!this.initialized) {
|
||||||
@ -862,19 +899,20 @@ var PDFHistory = {
|
|||||||
if (!params.hash && params.page) {
|
if (!params.hash && params.page) {
|
||||||
params.hash = ('page=' + params.page);
|
params.hash = ('page=' + params.page);
|
||||||
}
|
}
|
||||||
if (overwrite) {
|
if (addPrevious && !overwrite) {
|
||||||
|
var previousParams = this._getPreviousParams();
|
||||||
|
if (previousParams) {
|
||||||
|
this._pushToHistory(previousParams, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (overwrite || this.uid === 0) {
|
||||||
window.history.replaceState(this._stateObj(params), '', '');
|
window.history.replaceState(this._stateObj(params), '', '');
|
||||||
} else {
|
} else {
|
||||||
if (addPrevious && this._shouldPreviousPositionBeAddedToHistory()) {
|
|
||||||
var previousParams = this._getPreviousParams();
|
|
||||||
if (previousParams) {
|
|
||||||
this._pushToHistory(previousParams, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window.history.pushState(this._stateObj(params), '', '');
|
window.history.pushState(this._stateObj(params), '', '');
|
||||||
}
|
}
|
||||||
this.currentUid = this.uid++;
|
this.currentUid = this.uid++;
|
||||||
this.current = params;
|
this.current = params;
|
||||||
|
this.updatePreviousBookmark = true;
|
||||||
},
|
},
|
||||||
|
|
||||||
_goTo: function pdfHistory_goTo(state) {
|
_goTo: function pdfHistory_goTo(state) {
|
||||||
@ -882,14 +920,12 @@ 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 &&
|
var previousParams = this._getPreviousParams(true);
|
||||||
this.current.page && this.current.page !== this.currentPage) {
|
|
||||||
var previousParams = this._getPreviousParams();
|
|
||||||
if (previousParams) {
|
if (previousParams) {
|
||||||
this._pushToHistory(this.current, false, false);
|
this._pushToHistory(this.current, false);
|
||||||
this._pushToHistory(previousParams, false);
|
this._pushToHistory(previousParams, false);
|
||||||
|
this.currentUid = state.uid;
|
||||||
window.history.back();
|
window.history.back();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -906,6 +942,7 @@ var PDFHistory = {
|
|||||||
this.uid = state.uid;
|
this.uid = state.uid;
|
||||||
}
|
}
|
||||||
this.current = state.target;
|
this.current = state.target;
|
||||||
|
this.updatePreviousBookmark = true;
|
||||||
|
|
||||||
var currentHash = window.location.hash.substring(1);
|
var currentHash = window.location.hash.substring(1);
|
||||||
if (this.previousHash !== currentHash) {
|
if (this.previousHash !== currentHash) {
|
||||||
@ -1717,9 +1754,9 @@ var PDFView = {
|
|||||||
|
|
||||||
var storePromise = store.initializedPromise;
|
var storePromise = store.initializedPromise;
|
||||||
PDFJS.Promise.all([firstPagePromise, storePromise]).then(function() {
|
PDFJS.Promise.all([firstPagePromise, storePromise]).then(function() {
|
||||||
var storedHash = null, pageNum;
|
var storedHash = null;
|
||||||
if (store.get('exists', false)) {
|
if (store.get('exists', false)) {
|
||||||
pageNum = store.get('page', '1');
|
var pageNum = store.get('page', '1');
|
||||||
var zoom = store.get('zoom', PDFView.currentScale);
|
var zoom = store.get('zoom', PDFView.currentScale);
|
||||||
var left = store.get('scrollLeft', '0');
|
var left = store.get('scrollLeft', '0');
|
||||||
var top = store.get('scrollTop', '0');
|
var top = store.get('scrollTop', '0');
|
||||||
@ -1728,8 +1765,7 @@ var PDFView = {
|
|||||||
left + ',' + top;
|
left + ',' + top;
|
||||||
}
|
}
|
||||||
// Initialize the browsing history.
|
// Initialize the browsing history.
|
||||||
PDFHistory.initialize({ hash: storedHash, page: (pageNum || 1) },
|
PDFHistory.initialize(self.documentFingerprint);
|
||||||
PDFView.documentFingerprint);
|
|
||||||
|
|
||||||
self.setInitialView(storedHash, scale);
|
self.setInitialView(storedHash, scale);
|
||||||
|
|
||||||
@ -1819,6 +1855,7 @@ var PDFView = {
|
|||||||
PDFHistory.initialDestination = null;
|
PDFHistory.initialDestination = null;
|
||||||
} else if (this.initialBookmark) {
|
} else if (this.initialBookmark) {
|
||||||
this.setHash(this.initialBookmark);
|
this.setHash(this.initialBookmark);
|
||||||
|
PDFHistory.push({ hash: this.initialBookmark }, !!this.initialBookmark);
|
||||||
this.initialBookmark = null;
|
this.initialBookmark = null;
|
||||||
} else if (storedHash) {
|
} else if (storedHash) {
|
||||||
this.setHash(storedHash);
|
this.setHash(storedHash);
|
||||||
@ -1920,16 +1957,16 @@ var PDFView = {
|
|||||||
if (!hash)
|
if (!hash)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var pageNumber;
|
|
||||||
if (hash.indexOf('=') >= 0) {
|
if (hash.indexOf('=') >= 0) {
|
||||||
var params = PDFView.parseQueryString(hash);
|
var params = PDFView.parseQueryString(hash);
|
||||||
// borrowing syntax from "Parameters for Opening PDF Files"
|
// borrowing syntax from "Parameters for Opening PDF Files"
|
||||||
if ('nameddest' in params) {
|
if ('nameddest' in params) {
|
||||||
|
PDFHistory.updateNextHashParam(params.nameddest);
|
||||||
PDFView.navigateTo(params.nameddest);
|
PDFView.navigateTo(params.nameddest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ('page' in params) {
|
if ('page' in params) {
|
||||||
pageNumber = (params.page | 0) || 1;
|
var pageNumber = (params.page | 0) || 1;
|
||||||
if ('zoom' in params) {
|
if ('zoom' in params) {
|
||||||
var zoomArgs = params.zoom.split(','); // scale,left,top
|
var zoomArgs = params.zoom.split(','); // scale,left,top
|
||||||
// building destination array
|
// building destination array
|
||||||
@ -1964,12 +2001,11 @@ var PDFView = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (/^\d+$/.test(hash)) { // page number
|
} else if (/^\d+$/.test(hash)) { // page number
|
||||||
this.page = pageNumber = hash;
|
this.page = hash;
|
||||||
} else // named destination
|
} else { // named destination
|
||||||
|
PDFHistory.updateNextHashParam(unescape(hash));
|
||||||
PDFView.navigateTo(unescape(hash));
|
PDFView.navigateTo(unescape(hash));
|
||||||
|
}
|
||||||
// Update the browsing history.
|
|
||||||
PDFHistory.push({ hash: hash, page: pageNumber });
|
|
||||||
},
|
},
|
||||||
|
|
||||||
switchSidebarView: function pdfViewSwitchSidebarView(view) {
|
switchSidebarView: function pdfViewSwitchSidebarView(view) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user