This commit is contained in:
parent
846fc1755c
commit
381efa8a50
@ -21,11 +21,13 @@ var Cache = function(size) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var cache = new Cache(kCacheSize);
|
var cache = new Cache(kCacheSize);
|
||||||
|
var currentPageNumber = 1;
|
||||||
|
|
||||||
var PDFView = {
|
var PDFView = {
|
||||||
pages: [],
|
pages: [],
|
||||||
thumbnails: [],
|
thumbnails: [],
|
||||||
currentScale: kDefaultScale,
|
currentScale: kDefaultScale,
|
||||||
|
initialBookmark: document.location.hash.substring(1),
|
||||||
|
|
||||||
setScale: function(val, resetAutoSettings) {
|
setScale: function(val, resetAutoSettings) {
|
||||||
var pages = this.pages;
|
var pages = this.pages;
|
||||||
@ -33,11 +35,8 @@ var PDFView = {
|
|||||||
pages[i].update(val * kCssUnits);
|
pages[i].update(val * kCssUnits);
|
||||||
this.currentScale = val;
|
this.currentScale = val;
|
||||||
|
|
||||||
if (document.location.hash == '#' + this.page)
|
this.pages[this.page - 1].scrollIntoView();
|
||||||
this.pages[this.page - 1].draw();
|
this.pages[this.page - 1].draw();
|
||||||
else
|
|
||||||
// Jump the scroll position to the correct page.
|
|
||||||
document.location.hash = this.page;
|
|
||||||
|
|
||||||
var event = document.createEvent('UIEvents');
|
var event = document.createEvent('UIEvents');
|
||||||
event.initUIEvent('scalechange', false, false, window, 0);
|
event.initUIEvent('scalechange', false, false, window, 0);
|
||||||
@ -87,18 +86,18 @@ var PDFView = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.location.hash = val;
|
currentPageNumber = val;
|
||||||
document.getElementById('previous').disabled = (val == 1);
|
document.getElementById('previous').disabled = (val == 1);
|
||||||
document.getElementById('next').disabled = (val == pages.length);
|
document.getElementById('next').disabled = (val == pages.length);
|
||||||
if (input.value == val)
|
if (input.value != val) {
|
||||||
return;
|
input.value = val;
|
||||||
|
}
|
||||||
|
|
||||||
input.value = val;
|
pages[val - 1].scrollIntoView();
|
||||||
pages[val - 1].draw();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
get page() {
|
get page() {
|
||||||
return parseInt(document.location.hash.substring(1), 10) || 1;
|
return currentPageNumber;
|
||||||
},
|
},
|
||||||
|
|
||||||
open: function(url, scale) {
|
open: function(url, scale) {
|
||||||
@ -137,6 +136,20 @@ var PDFView = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getDestinationHash: function(dest) {
|
||||||
|
if (typeof dest === 'string')
|
||||||
|
return '#' + escape(dest);
|
||||||
|
if (dest instanceof Array) {
|
||||||
|
var destRef = dest[0]; // see nevigateTo method for dest format
|
||||||
|
var pageNumber = destRef instanceof Object ?
|
||||||
|
this.pagesRefMap[destRef.num + ' ' + destRef.gen + ' R'] : (destRef + 1);
|
||||||
|
if (pageNumber) {
|
||||||
|
return '#page=' + pageNumber + '&dest=' + dest.slice(1).join(',');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
},
|
||||||
|
|
||||||
error: function() {
|
error: function() {
|
||||||
var loadingIndicator = document.getElementById('loading');
|
var loadingIndicator = document.getElementById('loading');
|
||||||
loadingIndicator.innerHTML = 'Error';
|
loadingIndicator.innerHTML = 'Error';
|
||||||
@ -181,7 +194,7 @@ var PDFView = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.setScale(scale || kDefaultScale, true);
|
this.setScale(scale || kDefaultScale, true);
|
||||||
this.page = parseInt(document.location.hash.substring(1), 10) || 1;
|
|
||||||
this.pagesRefMap = pagesRefMap;
|
this.pagesRefMap = pagesRefMap;
|
||||||
this.destinations = pdf.catalog.destinations;
|
this.destinations = pdf.catalog.destinations;
|
||||||
if (pdf.catalog.documentOutline) {
|
if (pdf.catalog.documentOutline) {
|
||||||
@ -190,6 +203,28 @@ var PDFView = {
|
|||||||
outlineSwitchButton.removeAttribute('disabled');
|
outlineSwitchButton.removeAttribute('disabled');
|
||||||
this.switchSidebarView('outline');
|
this.switchSidebarView('outline');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.initialBookmark) {
|
||||||
|
this.setHash(this.initialBookmark);
|
||||||
|
this.initialBookmark = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
this.page = 1;
|
||||||
|
},
|
||||||
|
|
||||||
|
setHash: function(hash) {
|
||||||
|
if (!hash)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (hash.indexOf('=') >= 0) {
|
||||||
|
// TODO more complex hashes, for now catching page=XX only
|
||||||
|
var m = /\bpage=(\d+)/.exec(hash);
|
||||||
|
if (m && m[1] > 0)
|
||||||
|
this.page = m[1];
|
||||||
|
} else if (/^\d+$/.test(hash)) // page number
|
||||||
|
this.page = hash;
|
||||||
|
else // named destination
|
||||||
|
PDFView.navigateTo(unescape(hash));
|
||||||
},
|
},
|
||||||
|
|
||||||
switchSidebarView: function(view) {
|
switchSidebarView: function(view) {
|
||||||
@ -274,6 +309,7 @@ var PageView = function(container, content, id, pageWidth, pageHeight,
|
|||||||
|
|
||||||
function setupLinks(content, scale) {
|
function setupLinks(content, scale) {
|
||||||
function bindLink(link, dest) {
|
function bindLink(link, dest) {
|
||||||
|
link.href = PDFView.getDestinationHash(dest);
|
||||||
link.onclick = function() {
|
link.onclick = function() {
|
||||||
if (dest)
|
if (dest)
|
||||||
PDFView.navigateTo(dest);
|
PDFView.navigateTo(dest);
|
||||||
@ -296,6 +332,11 @@ var PageView = function(container, content, id, pageWidth, pageHeight,
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.scrollIntoView = function(dest) {
|
this.scrollIntoView = function(dest) {
|
||||||
|
if (!dest) {
|
||||||
|
div.scrollIntoView(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var x = 0, y = 0;
|
var x = 0, y = 0;
|
||||||
var width = 0, height = 0, widthScale, heightScale;
|
var width = 0, height = 0, widthScale, heightScale;
|
||||||
var scale = 0;
|
var scale = 0;
|
||||||
@ -405,6 +446,10 @@ var PageView = function(container, content, id, pageWidth, pageHeight,
|
|||||||
var ThumbnailView = function(container, page, id, pageRatio) {
|
var ThumbnailView = function(container, page, id, pageRatio) {
|
||||||
var anchor = document.createElement('a');
|
var anchor = document.createElement('a');
|
||||||
anchor.href = '#' + id;
|
anchor.href = '#' + id;
|
||||||
|
anchor.onclick = function stopNivigation() {
|
||||||
|
PDFView.page = id;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
var div = document.createElement('div');
|
var div = document.createElement('div');
|
||||||
div.id = 'thumbnailContainer' + id;
|
div.id = 'thumbnailContainer' + id;
|
||||||
@ -452,7 +497,7 @@ var DocumentOutlineView = function(outline) {
|
|||||||
var outlineView = document.getElementById('outlineView');
|
var outlineView = document.getElementById('outlineView');
|
||||||
|
|
||||||
function bindItemLink(domObj, item) {
|
function bindItemLink(domObj, item) {
|
||||||
domObj.href = '';
|
domObj.href = PDFView.getDestinationHash(item.dest);
|
||||||
domObj.onclick = function(e) {
|
domObj.onclick = function(e) {
|
||||||
PDFView.navigateTo(item.dest);
|
PDFView.navigateTo(item.dest);
|
||||||
return false;
|
return false;
|
||||||
@ -543,7 +588,7 @@ window.addEventListener('resize', function onscroll(evt) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('hashchange', function(evt) {
|
window.addEventListener('hashchange', function(evt) {
|
||||||
PDFView.page = PDFView.page;
|
PDFView.setHash(document.location.hash.substring(1));
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('change', function(evt) {
|
window.addEventListener('change', function(evt) {
|
||||||
@ -569,7 +614,6 @@ window.addEventListener('change', function(evt) {
|
|||||||
fileReader.readAsBinaryString(file);
|
fileReader.readAsBinaryString(file);
|
||||||
|
|
||||||
document.title = file.name;
|
document.title = file.name;
|
||||||
document.location.hash = 1;
|
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
window.addEventListener('transitionend', function(evt) {
|
window.addEventListener('transitionend', function(evt) {
|
||||||
@ -621,7 +665,6 @@ window.addEventListener('scalechange', function scalechange(evt) {
|
|||||||
|
|
||||||
window.addEventListener('pagechange', function pagechange(evt) {
|
window.addEventListener('pagechange', function pagechange(evt) {
|
||||||
var page = evt.detail;
|
var page = evt.detail;
|
||||||
document.location.hash = page;
|
|
||||||
document.getElementById('pageNumber').value = page;
|
document.getElementById('pageNumber').value = page;
|
||||||
document.getElementById('previous').disabled = (page == 1);
|
document.getElementById('previous').disabled = (page == 1);
|
||||||
document.getElementById('next').disabled = (page == PDFView.pages.length);
|
document.getElementById('next').disabled = (page == PDFView.pages.length);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user