Browse Source

Merge pull request #8302 from timvandermeij/es6-attachments-outline

Convert the attachments/outline view to ES6 syntax
Jonas Jenwald 8 years ago committed by GitHub
parent
commit
ece24dfe61
  1. 52
      web/pdf_attachment_viewer.js
  2. 69
      web/pdf_outline_viewer.js

52
web/pdf_attachment_viewer.js

@ -30,16 +30,13 @@ import {
* @property {Array|null} attachments - An array of attachment objects. * @property {Array|null} attachments - An array of attachment objects.
*/ */
class PDFAttachmentViewer {
/** /**
* @class
*/
var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
/**
* @constructs PDFAttachmentViewer
* @param {PDFAttachmentViewerOptions} options * @param {PDFAttachmentViewerOptions} options
*/ */
function PDFAttachmentViewer(options) { constructor(options) {
this.attachments = null; this.attachments = null;
this.container = options.container; this.container = options.container;
this.eventBus = options.eventBus; this.eventBus = options.eventBus;
this.downloadManager = options.downloadManager; this.downloadManager = options.downloadManager;
@ -49,8 +46,7 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
this._appendAttachment.bind(this)); this._appendAttachment.bind(this));
} }
PDFAttachmentViewer.prototype = { reset(keepRenderedCapability = false) {
reset: function PDFAttachmentViewer_reset(keepRenderedCapability) {
this.attachments = null; this.attachments = null;
// Remove the attachments from the DOM. // Remove the attachments from the DOM.
@ -61,20 +57,19 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
// not be replaced is when appending file attachment annotations. // not be replaced is when appending file attachment annotations.
this._renderedCapability = createPromiseCapability(); this._renderedCapability = createPromiseCapability();
} }
}, }
/** /**
* @private * @private
*/ */
_dispatchEvent: _dispatchEvent(attachmentsCount) {
function PDFAttachmentViewer_dispatchEvent(attachmentsCount) {
this.eventBus.dispatch('attachmentsloaded', { this.eventBus.dispatch('attachmentsloaded', {
source: this, source: this,
attachmentsCount: attachmentsCount, attachmentsCount,
}); });
this._renderedCapability.resolve(); this._renderedCapability.resolve();
}, }
/** /**
* @private * @private
@ -106,24 +101,22 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
window.open(viewerUrl); window.open(viewerUrl);
return false; return false;
}; };
}, }
/** /**
* @private * @private
*/ */
_bindLink: _bindLink(button, content, filename) {
function PDFAttachmentViewer_bindLink(button, content, filename) { button.onclick = () => {
button.onclick = function downloadFile(e) {
this.downloadManager.downloadData(content, filename, ''); this.downloadManager.downloadData(content, filename, '');
return false; return false;
}.bind(this); };
}, }
/** /**
* @param {PDFAttachmentViewerRenderParameters} params * @param {PDFAttachmentViewerRenderParameters} params
*/ */
render: function PDFAttachmentViewer_render(params) { render(params = {}) {
params = params || {};
var attachments = params.attachments || null; var attachments = params.attachments || null;
var attachmentsCount = 0; var attachmentsCount = 0;
@ -162,13 +155,13 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
} }
this._dispatchEvent(attachmentsCount); this._dispatchEvent(attachmentsCount);
}, }
/** /**
* Used to append FileAttachment annotations to the sidebar. * Used to append FileAttachment annotations to the sidebar.
* @private * @private
*/ */
_appendAttachment: function PDFAttachmentViewer_appendAttachment(item) { _appendAttachment(item) {
this._renderedCapability.promise.then(function (id, filename, content) { this._renderedCapability.promise.then(function (id, filename, content) {
var attachments = this.attachments; var attachments = this.attachments;
@ -182,19 +175,16 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
} }
} }
attachments[id] = { attachments[id] = {
filename: filename, filename,
content: content, content,
}; };
this.render({ this.render({
attachments: attachments, attachments,
keepRenderedCapability: true, keepRenderedCapability: true,
}); });
}.bind(this, item.id, item.filename, item.content)); }.bind(this, item.id, item.filename, item.content));
}, }
}; }
return PDFAttachmentViewer;
})();
export { export {
PDFAttachmentViewer, PDFAttachmentViewer,

69
web/pdf_outline_viewer.js

@ -17,7 +17,7 @@ import {
addLinkAttributes, PDFJS, removeNullCharacters addLinkAttributes, PDFJS, removeNullCharacters
} from './pdfjs'; } from './pdfjs';
var DEFAULT_TITLE = '\u2013'; const DEFAULT_TITLE = '\u2013';
/** /**
* @typedef {Object} PDFOutlineViewerOptions * @typedef {Object} PDFOutlineViewerOptions
@ -31,48 +31,45 @@ var DEFAULT_TITLE = '\u2013';
* @property {Array|null} outline - An array of outline objects. * @property {Array|null} outline - An array of outline objects.
*/ */
class PDFOutlineViewer {
/** /**
* @class
*/
var PDFOutlineViewer = (function PDFOutlineViewerClosure() {
/**
* @constructs PDFOutlineViewer
* @param {PDFOutlineViewerOptions} options * @param {PDFOutlineViewerOptions} options
*/ */
function PDFOutlineViewer(options) { constructor(options) {
this.outline = null; this.outline = null;
this.lastToggleIsShow = true; this.lastToggleIsShow = true;
this.container = options.container; this.container = options.container;
this.linkService = options.linkService; this.linkService = options.linkService;
this.eventBus = options.eventBus; this.eventBus = options.eventBus;
} }
PDFOutlineViewer.prototype = { reset() {
reset: function PDFOutlineViewer_reset() {
this.outline = null; this.outline = null;
this.lastToggleIsShow = true; this.lastToggleIsShow = true;
// Remove the outline from the DOM. // Remove the outline from the DOM.
this.container.textContent = ''; this.container.textContent = '';
// Ensure that the left (right in RTL locales) margin is always reset, // Ensure that the left (right in RTL locales) margin is always reset,
// to prevent incorrect outline alignment if a new document is opened. // to prevent incorrect outline alignment if a new document is opened.
this.container.classList.remove('outlineWithDeepNesting'); this.container.classList.remove('outlineWithDeepNesting');
}, }
/** /**
* @private * @private
*/ */
_dispatchEvent: function PDFOutlineViewer_dispatchEvent(outlineCount) { _dispatchEvent(outlineCount) {
this.eventBus.dispatch('outlineloaded', { this.eventBus.dispatch('outlineloaded', {
source: this, source: this,
outlineCount: outlineCount outlineCount,
}); });
}, }
/** /**
* @private * @private
*/ */
_bindLink: function PDFOutlineViewer_bindLink(element, item) { _bindLink(element, item) {
if (item.url) { if (item.url) {
addLinkAttributes(element, { addLinkAttributes(element, {
url: item.url, url: item.url,
@ -80,21 +77,21 @@ var PDFOutlineViewer = (function PDFOutlineViewerClosure() {
}); });
return; return;
} }
var self = this, destination = item.dest; var destination = item.dest;
element.href = self.linkService.getDestinationHash(destination); element.href = this.linkService.getDestinationHash(destination);
element.onclick = function () { element.onclick = () => {
if (destination) { if (destination) {
self.linkService.navigateTo(destination); this.linkService.navigateTo(destination);
} }
return false; return false;
}; };
}, }
/** /**
* @private * @private
*/ */
_setStyles: function PDFOutlineViewer_setStyles(element, item) { _setStyles(element, item) {
var styleStr = ''; var styleStr = '';
if (item.bold) { if (item.bold) {
styleStr += 'font-weight: bold;'; styleStr += 'font-weight: bold;';
@ -106,7 +103,7 @@ var PDFOutlineViewer = (function PDFOutlineViewerClosure() {
if (styleStr) { if (styleStr) {
element.setAttribute('style', styleStr); element.setAttribute('style', styleStr);
} }
}, }
/** /**
* Prepend a button before an outline item which allows the user to toggle * Prepend a button before an outline item which allows the user to toggle
@ -114,20 +111,20 @@ var PDFOutlineViewer = (function PDFOutlineViewerClosure() {
* *
* @private * @private
*/ */
_addToggleButton: function PDFOutlineViewer_addToggleButton(div) { _addToggleButton(div) {
var toggler = document.createElement('div'); var toggler = document.createElement('div');
toggler.className = 'outlineItemToggler'; toggler.className = 'outlineItemToggler';
toggler.onclick = function(event) { toggler.onclick = (evt) => {
event.stopPropagation(); evt.stopPropagation();
toggler.classList.toggle('outlineItemsHidden'); toggler.classList.toggle('outlineItemsHidden');
if (event.shiftKey) { if (evt.shiftKey) {
var shouldShowAll = !toggler.classList.contains('outlineItemsHidden'); var shouldShowAll = !toggler.classList.contains('outlineItemsHidden');
this._toggleOutlineItem(div, shouldShowAll); this._toggleOutlineItem(div, shouldShowAll);
} }
}.bind(this); };
div.insertBefore(toggler, div.firstChild); div.insertBefore(toggler, div.firstChild);
}, }
/** /**
* Toggle the visibility of the subtree of an outline item. * Toggle the visibility of the subtree of an outline item.
@ -138,30 +135,29 @@ var PDFOutlineViewer = (function PDFOutlineViewerClosure() {
* *
* @private * @private
*/ */
_toggleOutlineItem: _toggleOutlineItem(root, show) {
function PDFOutlineViewer_toggleOutlineItem(root, show) {
this.lastToggleIsShow = show; this.lastToggleIsShow = show;
var togglers = root.querySelectorAll('.outlineItemToggler'); var togglers = root.querySelectorAll('.outlineItemToggler');
for (var i = 0, ii = togglers.length; i < ii; ++i) { for (var i = 0, ii = togglers.length; i < ii; ++i) {
togglers[i].classList[show ? 'remove' : 'add']('outlineItemsHidden'); togglers[i].classList[show ? 'remove' : 'add']('outlineItemsHidden');
} }
}, }
/** /**
* Collapse or expand all subtrees of the outline. * Collapse or expand all subtrees of the outline.
*/ */
toggleOutlineTree: function PDFOutlineViewer_toggleOutlineTree() { toggleOutlineTree() {
if (!this.outline) { if (!this.outline) {
return; return;
} }
this._toggleOutlineItem(this.container, !this.lastToggleIsShow); this._toggleOutlineItem(this.container, !this.lastToggleIsShow);
}, }
/** /**
* @param {PDFOutlineViewerRenderParameters} params * @param {PDFOutlineViewerRenderParameters} params
*/ */
render: function PDFOutlineViewer_render(params) { render(params = {}) {
var outline = (params && params.outline) || null; var outline = params.outline || null;
var outlineCount = 0; var outlineCount = 0;
if (this.outline) { if (this.outline) {
@ -215,10 +211,7 @@ var PDFOutlineViewer = (function PDFOutlineViewerClosure() {
this._dispatchEvent(outlineCount); this._dispatchEvent(outlineCount);
} }
}; }
return PDFOutlineViewer;
})();
export { export {
PDFOutlineViewer, PDFOutlineViewer,

Loading…
Cancel
Save