Marks some private methods in PDFViewer and PDFThumbnailViewer

Conflicts:
	web/pdf_viewer.js
This commit is contained in:
Yury Delendik 2014-09-20 12:15:18 -05:00
parent a1eca2084d
commit 3773972dce
4 changed files with 148 additions and 72 deletions

View File

@ -353,7 +353,8 @@ var PageView = function pageView(container, id, scale, defaultViewport,
return; return;
} }
dest = null; dest = null;
this.viewer.setScale(this.viewer.currentScaleValue, true, true); // Fixes the case when PDF has different page sizes.
this.viewer.currentScaleValue = this.viewer.currentScaleValue;
} }
if (!dest) { if (!dest) {
scrollIntoView(div); scrollIntoView(div);
@ -413,9 +414,9 @@ var PageView = function pageView(container, id, scale, defaultViewport,
} }
if (scale && scale !== this.viewer.currentScale) { if (scale && scale !== this.viewer.currentScale) {
this.viewer.setScale(scale, true, true); this.viewer.currentScaleValue = scale;
} else if (this.viewer.currentScale === UNKNOWN_SCALE) { } else if (this.viewer.currentScale === UNKNOWN_SCALE) {
this.viewer.setScale(DEFAULT_SCALE, true, true); this.viewer.currentScaleValue = DEFAULT_SCALE;
} }
if (scale === 'page-fit' && !dest[4]) { if (scale === 'page-fit' && !dest[4]) {

View File

@ -53,7 +53,7 @@ var PDFViewer = (function pdfViewer() {
this.lastScroll = 0; this.lastScroll = 0;
this.updateInProgress = false; this.updateInProgress = false;
this.presentationModeState = PresentationModeState.UNKNOWN; this.presentationModeState = PresentationModeState.UNKNOWN;
this.resetView(); this._resetView();
} }
PDFViewer.prototype = { PDFViewer.prototype = {
@ -65,28 +65,102 @@ var PDFViewer = (function pdfViewer() {
return this.pages[index]; return this.pages[index];
}, },
setCurrentPageNumber: function (val) { get currentPageNumber() {
return this._currentPageNumber;
},
set currentPageNumber(val) {
if (!this.pdfDocument) {
this._currentPageNumber = val;
return;
}
var event = document.createEvent('UIEvents'); var event = document.createEvent('UIEvents');
event.initUIEvent('pagechange', true, true, window, 0); event.initUIEvent('pagechange', true, true, window, 0);
event.updateInProgress = this.updateInProgress; event.updateInProgress = this.updateInProgress;
if (!(0 < val && val <= this.pagesCount)) { if (!(0 < val && val <= this.pagesCount)) {
this.previousPageNumber = val;
event.pageNumber = this.page; event.pageNumber = this.page;
event.previousPageNumber = val;
this.container.dispatchEvent(event); this.container.dispatchEvent(event);
return; return;
} }
this.pages[val - 1].updateStats(); this.pages[val - 1].updateStats();
this.previousPageNumber = this.currentPageNumber; event.previousPageNumber = this._currentPageNumber;
this.currentPageNumber = val; this._currentPageNumber = val;
event.pageNumber = val; event.pageNumber = val;
this.container.dispatchEvent(event); this.container.dispatchEvent(event);
}, },
/**
* @returns {number}
*/
get currentScale() {
return this._currentScale;
},
/**
* @param {number} val - Scale of the pages in percents.
*/
set currentScale(val) {
if (isNaN(val)) {
throw new Error('Invalid numeric scale');
}
if (!this.pdfDocument) {
this._currentScale = val;
this._currentScaleValue = val.toString();
return;
}
this._setScale(val, false);
},
/**
* @returns {string}
*/
get currentScaleValue() {
return this._currentScaleValue;
},
/**
* @param val - The scale of the pages (in percent or predefined value).
*/
set currentScaleValue(val) {
if (!this.pdfDocument) {
this._currentScale = isNaN(val) ? UNKNOWN_SCALE : val;
this._currentScaleValue = val;
return;
}
this._setScale(val, false);
},
/**
* @returns {number}
*/
get pagesRotation() {
return this._pagesRotation;
},
/**
* @param {number} rotation - The rotation of the pages (0, 90, 180, 270).
*/
set pagesRotation(rotation) {
this._pagesRotation = rotation;
for (var i = 0, l = this.pages.length; i < l; i++) {
var page = this.pages[i];
page.update(page.scale, rotation);
}
this._setScale(this._currentScaleValue, true);
},
/**
* @param pdfDocument {PDFDocument}
*/
setDocument: function (pdfDocument) { setDocument: function (pdfDocument) {
if (this.pdfDocument) { if (this.pdfDocument) {
this.resetView(); this._resetView();
} }
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
@ -139,7 +213,7 @@ var PDFViewer = (function pdfViewer() {
// Fetch a single page so we can get a viewport that will be the default // Fetch a single page so we can get a viewport that will be the default
// viewport for all pages // viewport for all pages
return firstPagePromise.then(function(pdfPage) { return firstPagePromise.then(function(pdfPage) {
var scale = this.currentScale || 1.0; var scale = this._currentScale || 1.0;
var viewport = pdfPage.getViewport(scale * CSS_UNITS); var viewport = pdfPage.getViewport(scale * CSS_UNITS);
for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) { for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
var pageSource = new PDFPageSource(pdfDocument, pageNum); var pageSource = new PDFPageSource(pdfDocument, pageNum);
@ -183,14 +257,14 @@ var PDFViewer = (function pdfViewer() {
}.bind(this)); }.bind(this));
}, },
resetView: function () { _resetView: function () {
this.cache = new Cache(DEFAULT_CACHE_SIZE); this.cache = new Cache(DEFAULT_CACHE_SIZE);
this.pages = []; this.pages = [];
this.currentPageNumber = 1; this._currentPageNumber = 1;
this.previousPageNumber = 1; this._currentScale = UNKNOWN_SCALE;
this.currentScale = UNKNOWN_SCALE; this._currentScaleValue = null;
this.currentScaleValue = null;
this.location = null; this.location = null;
this._pagesRotation = 0;
var container = this.viewer; var container = this.viewer;
while (container.hasChildNodes()) { while (container.hasChildNodes()) {
@ -208,18 +282,18 @@ var PDFViewer = (function pdfViewer() {
}, },
_setScaleUpdatePages: function pdfViewer_setScaleUpdatePages( _setScaleUpdatePages: function pdfViewer_setScaleUpdatePages(
newScale, newValue, resetAutoSettings, noScroll, preset) { newScale, newValue, noScroll, preset) {
this.currentScaleValue = newValue; this._currentScaleValue = newValue;
if (newScale === this.currentScale) { if (newScale === this._currentScale) {
return; return;
} }
for (var i = 0, ii = this.pages.length; i < ii; i++) { for (var i = 0, ii = this.pages.length; i < ii; i++) {
this.pages[i].update(newScale); this.pages[i].update(newScale);
} }
this.currentScale = newScale; this._currentScale = newScale;
if (!noScroll) { if (!noScroll) {
var page = this.currentPageNumber, dest; var page = this._currentPageNumber, dest;
var inPresentationMode = var inPresentationMode =
this.presentationModeState === PresentationModeState.CHANGING || this.presentationModeState === PresentationModeState.CHANGING ||
this.presentationModeState === PresentationModeState.FULLSCREEN; this.presentationModeState === PresentationModeState.FULLSCREEN;
@ -235,23 +309,22 @@ var PDFViewer = (function pdfViewer() {
var event = document.createEvent('UIEvents'); var event = document.createEvent('UIEvents');
event.initUIEvent('scalechange', true, true, window, 0); event.initUIEvent('scalechange', true, true, window, 0);
event.scale = newScale; event.scale = newScale;
event.resetAutoSettings = resetAutoSettings;
if (preset) { if (preset) {
event.presetValue = newValue; event.presetValue = newValue;
} }
this.container.dispatchEvent(event); this.container.dispatchEvent(event);
}, },
setScale: function pdfViewer_setScale(value, resetAutoSettings, noScroll) { _setScale: function pdfViewer_setScale(value, noScroll) {
if (value === 'custom') { if (value === 'custom') {
return; return;
} }
var scale = parseFloat(value); var scale = parseFloat(value);
if (scale > 0) { if (scale > 0) {
this._setScaleUpdatePages(scale, value, true, noScroll, false); this._setScaleUpdatePages(scale, value, noScroll, false);
} else { } else {
var currentPage = this.pages[this.currentPageNumber - 1]; var currentPage = this.pages[this._currentPageNumber - 1];
if (!currentPage) { if (!currentPage) {
return; return;
} }
@ -287,23 +360,13 @@ var PDFViewer = (function pdfViewer() {
'\' is an unknown zoom value.'); '\' is an unknown zoom value.');
return; return;
} }
this._setScaleUpdatePages(scale, value, resetAutoSettings, noScroll, this._setScaleUpdatePages(scale, value, noScroll, true);
true);
} }
}, },
updateRotation: function pdfViewRotatePages(rotation) { _updateLocation: function (firstPage) {
for (var i = 0, l = this.pages.length; i < l; i++) { var currentScale = this._currentScale;
var page = this.pages[i]; var currentScaleValue = this._currentScaleValue;
page.update(page.scale, rotation);
}
this.setScale(this.currentScaleValue, true, true);
},
updateLocation: function (firstPage) {
var currentScale = this.currentScale;
var currentScaleValue = this.currentScaleValue;
var normalizedScaleValue = var normalizedScaleValue =
parseFloat(currentScaleValue) === currentScale ? parseFloat(currentScaleValue) === currentScale ?
Math.round(currentScale * 10000) / 100 : currentScaleValue; Math.round(currentScale * 10000) / 100 : currentScaleValue;
@ -330,7 +393,7 @@ var PDFViewer = (function pdfViewer() {
}, },
update: function () { update: function () {
var visible = this.getVisiblePages(); var visible = this._getVisiblePages();
var visiblePages = visible.views; var visiblePages = visible.views;
if (visiblePages.length === 0) { if (visiblePages.length === 0) {
return; return;
@ -354,7 +417,7 @@ var PDFViewer = (function pdfViewer() {
if (page.percent < 100) { if (page.percent < 100) {
break; break;
} }
if (page.id === this.currentPageNumber) { if (page.id === currentId) {
stillFullyVisible = true; stillFullyVisible = true;
break; break;
} }
@ -365,10 +428,10 @@ var PDFViewer = (function pdfViewer() {
} }
if (this.presentationModeState !== PresentationModeState.FULLSCREEN) { if (this.presentationModeState !== PresentationModeState.FULLSCREEN) {
this.setCurrentPageNumber(currentId); this.currentPageNumber = currentId;
} }
this.updateLocation(firstPage); this._updateLocation(firstPage);
this.updateInProgress = false; this.updateInProgress = false;
@ -394,14 +457,14 @@ var PDFViewer = (function pdfViewer() {
false : (this.container.scrollWidth > this.container.clientWidth)); false : (this.container.scrollWidth > this.container.clientWidth));
}, },
getVisiblePages: function () { _getVisiblePages: function () {
if (this.presentationModeState !== PresentationModeState.FULLSCREEN) { if (this.presentationModeState !== PresentationModeState.FULLSCREEN) {
return getVisibleElements(this.container, this.pages, true); return getVisibleElements(this.container, this.pages, true);
} else { } else {
// The algorithm in getVisibleElements doesn't work in all browsers and // The algorithm in getVisibleElements doesn't work in all browsers and
// configurations when presentation mode is active. // configurations when presentation mode is active.
var visible = []; var visible = [];
var currentPage = this.pages[this.currentPageNumber - 1]; var currentPage = this.pages[this._currentPageNumber - 1];
visible.push({ id: currentPage.id, view: currentPage }); visible.push({ id: currentPage.id, view: currentPage });
return { first: currentPage, last: currentPage, views: visible }; return { first: currentPage, last: currentPage, views: visible };
} }
@ -417,7 +480,7 @@ var PDFViewer = (function pdfViewer() {
}, },
forceRendering: function (currentlyVisiblePages) { forceRendering: function (currentlyVisiblePages) {
var visiblePages = currentlyVisiblePages || this.getVisiblePages(); var visiblePages = currentlyVisiblePages || this._getVisiblePages();
var pageView = this.renderingQueue.getHighestPriority(visiblePages, var pageView = this.renderingQueue.getHighestPriority(visiblePages,
this.pages, this.pages,
this.scroll.down); this.scroll.down);

View File

@ -247,13 +247,12 @@ var PDFThumbnailViewer = (function pdfThumbnailViewer() {
this.renderingQueue = options.renderingQueue; this.renderingQueue = options.renderingQueue;
this.linkService = options.linkService; this.linkService = options.linkService;
this.scroll = watchScroll(this.container, this.scrollUpdated.bind(this)); this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this));
this.thumbnails = []; this._resetView();
this.currentPage = -1;
} }
PDFThumbnailViewer.prototype = { PDFThumbnailViewer.prototype = {
scrollUpdated: function PDFThumbnailViewer_scrollUpdated() { _scrollUpdated: function PDFThumbnailViewer_scrollUpdated() {
this.renderingQueue.renderHighestPriority(); this.renderingQueue.renderHighestPriority();
}, },
@ -261,18 +260,18 @@ var PDFThumbnailViewer = (function pdfThumbnailViewer() {
return this.thumbnails[index]; return this.thumbnails[index];
}, },
getVisibleThumbs: function PDFThumbnailViewer_getVisibleThumbs() { _getVisibleThumbs: function PDFThumbnailViewer_getVisibleThumbs() {
return getVisibleElements(this.container, this.thumbnails); return getVisibleElements(this.container, this.thumbnails);
}, },
updatePage: function (page) { scrollThumbnailIntoView: function (page) {
var selected = document.querySelector('.thumbnail.selected'); var selected = document.querySelector('.thumbnail.selected');
if (selected) { if (selected) {
selected.classList.remove('selected'); selected.classList.remove('selected');
} }
var thumbnail = document.getElementById('thumbnailContainer' + page); var thumbnail = document.getElementById('thumbnailContainer' + page);
thumbnail.classList.add('selected'); thumbnail.classList.add('selected');
var visibleThumbs = this.getVisibleThumbs(); var visibleThumbs = this._getVisibleThumbs();
var numVisibleThumbs = visibleThumbs.views.length; var numVisibleThumbs = visibleThumbs.views.length;
// If the thumbnail isn't currently visible, scroll it into view. // If the thumbnail isn't currently visible, scroll it into view.
@ -284,13 +283,17 @@ var PDFThumbnailViewer = (function pdfThumbnailViewer() {
scrollIntoView(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN }); scrollIntoView(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN });
} }
} }
this.currentPage = page;
}, },
updateRotation: function (pageRotation) { get pagesRotation() {
return this._pagesRotation;
},
set pagesRotation(rotation) {
this._pagesRotation = rotation;
for (var i = 0, l = this.thumbnails.length; i < l; i++) { for (var i = 0, l = this.thumbnails.length; i < l; i++) {
var thumb = this.thumbnails[i]; var thumb = this.thumbnails[i];
thumb.update(pageRotation); thumb.update(rotation);
} }
}, },
@ -298,6 +301,11 @@ var PDFThumbnailViewer = (function pdfThumbnailViewer() {
ThumbnailView.tempImageCache = null; ThumbnailView.tempImageCache = null;
}, },
_resetView: function () {
this.thumbnails = [];
this._pagesRotation = 0;
},
setDocument: function (pdfDocument) { setDocument: function (pdfDocument) {
if (this.pdfDocument) { if (this.pdfDocument) {
// cleanup of the elements and views // cleanup of the elements and views
@ -305,7 +313,7 @@ var PDFThumbnailViewer = (function pdfThumbnailViewer() {
while (thumbsView.hasChildNodes()) { while (thumbsView.hasChildNodes()) {
thumbsView.removeChild(thumbsView.lastChild); thumbsView.removeChild(thumbsView.lastChild);
} }
this.thumbnails = []; this._resetView();
} }
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
@ -334,7 +342,7 @@ var PDFThumbnailViewer = (function pdfThumbnailViewer() {
}, },
forceRendering: function () { forceRendering: function () {
var visibleThumbs = this.getVisibleThumbs(); var visibleThumbs = this._getVisibleThumbs();
var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs,
this.thumbnails, this.thumbnails,
this.scroll.down); this.scroll.down);

View File

@ -96,6 +96,7 @@ var PDFView = {
pdfThumbnailViewer: null, pdfThumbnailViewer: null,
pdfRenderingQueue: null, pdfRenderingQueue: null,
pageRotation: 0, pageRotation: 0,
updateScaleControls: true,
mouseScrollTimeStamp: 0, mouseScrollTimeStamp: 0,
mouseScrollDelta: 0, mouseScrollDelta: 0,
isViewerEmbedded: (window.parent !== window), isViewerEmbedded: (window.parent !== window),
@ -258,7 +259,7 @@ var PDFView = {
newScale = Math.ceil(newScale * 10) / 10; newScale = Math.ceil(newScale * 10) / 10;
newScale = Math.min(MAX_SCALE, newScale); newScale = Math.min(MAX_SCALE, newScale);
} while (--ticks && newScale < MAX_SCALE); } while (--ticks && newScale < MAX_SCALE);
this.pdfViewer.setScale(newScale, true); this.setScale(newScale, true);
}, },
zoomOut: function pdfViewZoomOut(ticks) { zoomOut: function pdfViewZoomOut(ticks) {
@ -268,7 +269,7 @@ var PDFView = {
newScale = Math.floor(newScale * 10) / 10; newScale = Math.floor(newScale * 10) / 10;
newScale = Math.max(MIN_SCALE, newScale); newScale = Math.max(MIN_SCALE, newScale);
} while (--ticks && newScale > MIN_SCALE); } while (--ticks && newScale > MIN_SCALE);
this.pdfViewer.setScale(newScale, true); this.setScale(newScale, true);
}, },
get currentScaleValue() { get currentScaleValue() {
@ -280,7 +281,7 @@ var PDFView = {
}, },
set page(val) { set page(val) {
this.pdfViewer.setCurrentPageNumber(val); this.pdfViewer.currentPageNumber = val;
}, },
get page() { get page() {
@ -825,6 +826,7 @@ var PDFView = {
load: function pdfViewLoad(pdfDocument, scale) { load: function pdfViewLoad(pdfDocument, scale) {
var self = this; var self = this;
scale = scale || UNKNOWN_SCALE;
PDFView.findController.reset(); PDFView.findController.reset();
@ -1061,14 +1063,14 @@ var PDFView = {
} else if (storedHash) { } else if (storedHash) {
this.setHash(storedHash); this.setHash(storedHash);
} else if (scale) { } else if (scale) {
this.pdfViewer.setScale(scale, true); this.setScale(scale, true);
this.page = 1; this.page = 1;
} }
if (PDFView.pdfViewer.currentScale === UNKNOWN_SCALE) { if (PDFView.pdfViewer.currentScale === UNKNOWN_SCALE) {
// Scale was not initialized: invalid bookmark or scale was not specified. // Scale was not initialized: invalid bookmark or scale was not specified.
// Setting the default one. // Setting the default one.
this.pdfViewer.setScale(DEFAULT_SCALE, true); this.setScale(DEFAULT_SCALE, true);
} }
}, },
@ -1270,16 +1272,18 @@ var PDFView = {
this.forceRendering(); this.forceRendering();
}, },
setScale: function (value, resetAutoSettings, noScroll) { setScale: function (value, resetAutoSettings) {
this.pdfViewer.setScale(value, resetAutoSettings, noScroll); this.updateScaleControls = !!resetAutoSettings;
this.pdfViewer.currentScaleValue = value;
this.updateScaleControls = true;
}, },
rotatePages: function pdfViewRotatePages(delta) { rotatePages: function pdfViewRotatePages(delta) {
var currentPage = this.getPageView(this.page - 1); var currentPage = this.getPageView(this.page - 1);
this.pageRotation = (this.pageRotation + 360 + delta) % 360; this.pageRotation = (this.pageRotation + 360 + delta) % 360;
this.pdfViewer.updateRotation(this.pageRotation); this.pdfViewer.pagesRotation = this.pageRotation;
this.pdfThumbnailViewer.updateRotation(this.pageRotation); this.pdfThumbnailViewer.pagesRotation = this.pageRotation;
this.forceRendering(); this.forceRendering();
@ -1590,7 +1594,7 @@ function webViewerInitialized() {
document.getElementById('scaleSelect').addEventListener('change', document.getElementById('scaleSelect').addEventListener('change',
function() { function() {
PDFView.pdfViewer.setScale(this.value); PDFView.setScale(this.value, false);
}); });
document.getElementById('presentationMode').addEventListener('click', document.getElementById('presentationMode').addEventListener('click',
@ -1730,7 +1734,7 @@ window.addEventListener('resize', function webViewerResize(evt) {
(document.getElementById('pageWidthOption').selected || (document.getElementById('pageWidthOption').selected ||
document.getElementById('pageFitOption').selected || document.getElementById('pageFitOption').selected ||
document.getElementById('pageAutoOption').selected)) { document.getElementById('pageAutoOption').selected)) {
PDFView.pdfViewer.setScale(document.getElementById('scaleSelect').value); PDFView.setScale(document.getElementById('scaleSelect').value, false);
} }
updateViewarea(); updateViewarea();
@ -1825,7 +1829,7 @@ window.addEventListener('scalechange', function scalechange(evt) {
var customScaleOption = document.getElementById('customScaleOption'); var customScaleOption = document.getElementById('customScaleOption');
customScaleOption.selected = false; customScaleOption.selected = false;
if (!evt.resetAutoSettings && if (!PDFView.updateScaleControls &&
(document.getElementById('pageWidthOption').selected || (document.getElementById('pageWidthOption').selected ||
document.getElementById('pageFitOption').selected || document.getElementById('pageFitOption').selected ||
document.getElementById('pageAutoOption').selected)) { document.getElementById('pageAutoOption').selected)) {
@ -1849,9 +1853,9 @@ window.addEventListener('scalechange', function scalechange(evt) {
window.addEventListener('pagechange', function pagechange(evt) { window.addEventListener('pagechange', function pagechange(evt) {
var page = evt.pageNumber; var page = evt.pageNumber;
if (PDFView.pdfViewer.previousPageNumber !== page) { if (evt.previousPageNumber !== page) {
document.getElementById('pageNumber').value = page; document.getElementById('pageNumber').value = page;
PDFView.pdfThumbnailViewer.updatePage(page); PDFView.pdfThumbnailViewer.scrollThumbnailIntoView(page);
} }
var numPages = PDFView.pagesCount; var numPages = PDFView.pagesCount;
@ -1948,7 +1952,7 @@ window.addEventListener('keydown', function keydown(evt) {
// keeping it unhandled (to restore page zoom to 100%) // keeping it unhandled (to restore page zoom to 100%)
setTimeout(function () { setTimeout(function () {
// ... and resetting the scale after browser adjusts its scale // ... and resetting the scale after browser adjusts its scale
PDFView.pdfViewer.setScale(DEFAULT_SCALE, true); PDFView.setScale(DEFAULT_SCALE, true);
}); });
handled = false; handled = false;
break; break;