Browse Source

Convert the thumbnail viewer to ES6 syntax

Tim van der Meij 8 years ago
parent
commit
a682d77e71
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
  1. 124
      web/pdf_thumbnail_viewer.js

124
web/pdf_thumbnail_viewer.js

@ -18,7 +18,7 @@ import {
} from './ui_utils'; } from './ui_utils';
import { PDFThumbnailView } from './pdf_thumbnail_view'; import { PDFThumbnailView } from './pdf_thumbnail_view';
var THUMBNAIL_SCROLL_MARGIN = -19; const THUMBNAIL_SCROLL_MARGIN = -19;
/** /**
* @typedef {Object} PDFThumbnailViewerOptions * @typedef {Object} PDFThumbnailViewerOptions
@ -30,99 +30,95 @@ var THUMBNAIL_SCROLL_MARGIN = -19;
*/ */
/** /**
* Simple viewer control to display thumbnails for pages. * Viewer control to display thumbnails for pages in a PDF document.
* @class *
* @implements {IRenderableView} * @implements {IRenderableView}
*/ */
var PDFThumbnailViewer = (function PDFThumbnailViewerClosure() { class PDFThumbnailViewer {
/** /**
* @constructs PDFThumbnailViewer
* @param {PDFThumbnailViewerOptions} options * @param {PDFThumbnailViewerOptions} options
*/ */
function PDFThumbnailViewer(options) { constructor({ container, linkService, renderingQueue, l10n = NullL10n, }) {
this.container = options.container; this.container = container;
this.renderingQueue = options.renderingQueue; this.linkService = linkService;
this.linkService = options.linkService; this.renderingQueue = renderingQueue;
this.l10n = options.l10n || NullL10n; this.l10n = l10n;
this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this)); this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this));
this._resetView(); this._resetView();
} }
PDFThumbnailViewer.prototype = {
/** /**
* @private * @private
*/ */
_scrollUpdated: function PDFThumbnailViewer_scrollUpdated() { _scrollUpdated() {
this.renderingQueue.renderHighestPriority(); this.renderingQueue.renderHighestPriority();
}, }
getThumbnail: function PDFThumbnailViewer_getThumbnail(index) { getThumbnail(index) {
return this.thumbnails[index]; return this._thumbnails[index];
}, }
/** /**
* @private * @private
*/ */
_getVisibleThumbs: function PDFThumbnailViewer_getVisibleThumbs() { _getVisibleThumbs() {
return getVisibleElements(this.container, this.thumbnails); return getVisibleElements(this.container, this._thumbnails);
}, }
scrollThumbnailIntoView: scrollThumbnailIntoView(page) {
function PDFThumbnailViewer_scrollThumbnailIntoView(page) { let 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.querySelector( let thumbnail = document.querySelector(
'div.thumbnail[data-page-number="' + page + '"]'); 'div.thumbnail[data-page-number="' + page + '"]');
if (thumbnail) { if (thumbnail) {
thumbnail.classList.add('selected'); thumbnail.classList.add('selected');
} }
var visibleThumbs = this._getVisibleThumbs(); let visibleThumbs = this._getVisibleThumbs();
var numVisibleThumbs = visibleThumbs.views.length; let 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.
if (numVisibleThumbs > 0) { if (numVisibleThumbs > 0) {
var first = visibleThumbs.first.id; let first = visibleThumbs.first.id;
// Account for only one thumbnail being visible. // Account for only one thumbnail being visible.
var last = (numVisibleThumbs > 1 ? visibleThumbs.last.id : first); let last = (numVisibleThumbs > 1 ? visibleThumbs.last.id : first);
if (page <= first || page >= last) { if (page <= first || page >= last) {
scrollIntoView(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN, }); scrollIntoView(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN, });
} }
} }
}, }
get pagesRotation() { get pagesRotation() {
return this._pagesRotation; return this._pagesRotation;
}, }
set pagesRotation(rotation) { set pagesRotation(rotation) {
this._pagesRotation = rotation; this._pagesRotation = rotation;
for (var i = 0, l = this.thumbnails.length; i < l; i++) { for (let i = 0, l = this._thumbnails.length; i < l; i++) {
var thumb = this.thumbnails[i]; this._thumbnails[i].update(rotation);
thumb.update(rotation); }
} }
},
cleanup: function PDFThumbnailViewer_cleanup() { cleanup() {
PDFThumbnailView.cleanup(); PDFThumbnailView.cleanup();
}, }
/** /**
* @private * @private
*/ */
_resetView: function PDFThumbnailViewer_resetView() { _resetView() {
this.thumbnails = []; this._thumbnails = [];
this._pageLabels = null; this._pageLabels = null;
this._pagesRotation = 0; this._pagesRotation = 0;
this._pagesRequests = []; this._pagesRequests = [];
// Remove the thumbnails from the DOM. // Remove the thumbnails from the DOM.
this.container.textContent = ''; this.container.textContent = '';
}, }
setDocument: function PDFThumbnailViewer_setDocument(pdfDocument) { setDocument(pdfDocument) {
if (this.pdfDocument) { if (this.pdfDocument) {
this._cancelRendering(); this._cancelRendering();
this._resetView(); this._resetView();
@ -134,10 +130,10 @@ var PDFThumbnailViewer = (function PDFThumbnailViewerClosure() {
} }
return pdfDocument.getPage(1).then((firstPage) => { return pdfDocument.getPage(1).then((firstPage) => {
var pagesCount = pdfDocument.numPages; let pagesCount = pdfDocument.numPages;
var viewport = firstPage.getViewport(1.0); let viewport = firstPage.getViewport(1.0);
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) { for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
var thumbnail = new PDFThumbnailView({ let thumbnail = new PDFThumbnailView({
container: this.container, container: this.container,
id: pageNum, id: pageNum,
defaultViewport: viewport.clone(), defaultViewport: viewport.clone(),
@ -146,26 +142,26 @@ var PDFThumbnailViewer = (function PDFThumbnailViewerClosure() {
disableCanvasToImageConversion: false, disableCanvasToImageConversion: false,
l10n: this.l10n, l10n: this.l10n,
}); });
this.thumbnails.push(thumbnail); this._thumbnails.push(thumbnail);
} }
}); });
}, }
/** /**
* @private * @private
*/ */
_cancelRendering: function PDFThumbnailViewer_cancelRendering() { _cancelRendering() {
for (var i = 0, ii = this.thumbnails.length; i < ii; i++) { for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
if (this.thumbnails[i]) { if (this._thumbnails[i]) {
this.thumbnails[i].cancelRendering(); this._thumbnails[i].cancelRendering();
}
} }
} }
},
/** /**
* @param {Array|null} labels * @param {Array|null} labels
*/ */
setPageLabels: function PDFThumbnailViewer_setPageLabels(labels) { setPageLabels(labels) {
if (!this.pdfDocument) { if (!this.pdfDocument) {
return; return;
} }
@ -179,12 +175,11 @@ var PDFThumbnailViewer = (function PDFThumbnailViewerClosure() {
this._pageLabels = labels; this._pageLabels = labels;
} }
// Update all the `PDFThumbnailView` instances. // Update all the `PDFThumbnailView` instances.
for (var i = 0, ii = this.thumbnails.length; i < ii; i++) { for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
var thumbnailView = this.thumbnails[i]; let label = this._pageLabels && this._pageLabels[i];
var label = this._pageLabels && this._pageLabels[i]; this._thumbnails[i].setPageLabel(label);
thumbnailView.setPageLabel(label); }
} }
},
/** /**
* @param {PDFThumbnailView} thumbView * @param {PDFThumbnailView} thumbView
@ -195,23 +190,23 @@ var PDFThumbnailViewer = (function PDFThumbnailViewerClosure() {
if (thumbView.pdfPage) { if (thumbView.pdfPage) {
return Promise.resolve(thumbView.pdfPage); return Promise.resolve(thumbView.pdfPage);
} }
var pageNumber = thumbView.id; let pageNumber = thumbView.id;
if (this._pagesRequests[pageNumber]) { if (this._pagesRequests[pageNumber]) {
return this._pagesRequests[pageNumber]; return this._pagesRequests[pageNumber];
} }
var promise = this.pdfDocument.getPage(pageNumber).then((pdfPage) => { let promise = this.pdfDocument.getPage(pageNumber).then((pdfPage) => {
thumbView.setPdfPage(pdfPage); thumbView.setPdfPage(pdfPage);
this._pagesRequests[pageNumber] = null; this._pagesRequests[pageNumber] = null;
return pdfPage; return pdfPage;
}); });
this._pagesRequests[pageNumber] = promise; this._pagesRequests[pageNumber] = promise;
return promise; return promise;
}, }
forceRendering() { forceRendering() {
var visibleThumbs = this._getVisibleThumbs(); let visibleThumbs = this._getVisibleThumbs();
var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, let thumbView = this.renderingQueue.getHighestPriority(visibleThumbs,
this.thumbnails, this._thumbnails,
this.scroll.down); this.scroll.down);
if (thumbView) { if (thumbView) {
this._ensurePdfPageLoaded(thumbView).then(() => { this._ensurePdfPageLoaded(thumbView).then(() => {
@ -220,11 +215,8 @@ var PDFThumbnailViewer = (function PDFThumbnailViewerClosure() {
return true; return true;
} }
return false; return false;
}, }
}; }
return PDFThumbnailViewer;
})();
export { export {
PDFThumbnailViewer, PDFThumbnailViewer,

Loading…
Cancel
Save