Skip to content

Commit

Permalink
Merge pull request mozilla#8302 from timvandermeij/es6-attachments-ou…
Browse files Browse the repository at this point in the history
…tline

Convert the attachments/outline view to ES6 syntax
  • Loading branch information
Snuffleupagus authored Apr 23, 2017
2 parents 14e9747 + 95400a7 commit 49f14a8
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 306 deletions.
268 changes: 129 additions & 139 deletions web/pdf_attachment_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ import {
* @property {Array|null} attachments - An array of attachment objects.
*/

/**
* @class
*/
var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
class PDFAttachmentViewer {
/**
* @constructs PDFAttachmentViewer
* @param {PDFAttachmentViewerOptions} options
*/
function PDFAttachmentViewer(options) {
constructor(options) {
this.attachments = null;

this.container = options.container;
this.eventBus = options.eventBus;
this.downloadManager = options.downloadManager;
Expand All @@ -49,152 +46,145 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
this._appendAttachment.bind(this));
}

PDFAttachmentViewer.prototype = {
reset: function PDFAttachmentViewer_reset(keepRenderedCapability) {
this.attachments = null;
reset(keepRenderedCapability = false) {
this.attachments = null;

// Remove the attachments from the DOM.
this.container.textContent = '';
// Remove the attachments from the DOM.
this.container.textContent = '';

if (!keepRenderedCapability) {
// NOTE: The *only* situation in which the `_renderedCapability` should
// not be replaced is when appending file attachment annotations.
this._renderedCapability = createPromiseCapability();
}
},

/**
* @private
*/
_dispatchEvent:
function PDFAttachmentViewer_dispatchEvent(attachmentsCount) {
this.eventBus.dispatch('attachmentsloaded', {
source: this,
attachmentsCount: attachmentsCount,
});
if (!keepRenderedCapability) {
// NOTE: The *only* situation in which the `_renderedCapability` should
// not be replaced is when appending file attachment annotations.
this._renderedCapability = createPromiseCapability();
}
}

/**
* @private
*/
_dispatchEvent(attachmentsCount) {
this.eventBus.dispatch('attachmentsloaded', {
source: this,
attachmentsCount,
});

this._renderedCapability.resolve();
},
this._renderedCapability.resolve();
}

/**
* @private
*/
_bindPdfLink(button, content, filename) {
if (PDFJS.disableCreateObjectURL) {
throw new Error('bindPdfLink: ' +
'Unsupported "PDFJS.disableCreateObjectURL" value.');
/**
* @private
*/
_bindPdfLink(button, content, filename) {
if (PDFJS.disableCreateObjectURL) {
throw new Error('bindPdfLink: ' +
'Unsupported "PDFJS.disableCreateObjectURL" value.');
}
var blobUrl;
button.onclick = function() {
if (!blobUrl) {
blobUrl = createObjectURL(content, 'application/pdf');
}
var blobUrl;
button.onclick = function() {
if (!blobUrl) {
blobUrl = createObjectURL(content, 'application/pdf');
}
var viewerUrl;
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
// The current URL is the viewer, let's use it and append the file.
viewerUrl = '?file=' + encodeURIComponent(blobUrl + '#' + filename);
} else if (PDFJSDev.test('CHROME')) {
// In the Chrome extension, the URL is rewritten using the history API
// in viewer.js, so an absolute URL must be generated.
// eslint-disable-next-line no-undef
viewerUrl = chrome.runtime.getURL('/content/web/viewer.html') +
'?file=' + encodeURIComponent(blobUrl + '#' + filename);
} else if (PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
// Let Firefox's content handler catch the URL and display the PDF.
viewerUrl = blobUrl + '?' + encodeURIComponent(filename);
}
window.open(viewerUrl);
return false;
};
},

/**
* @private
*/
_bindLink:
function PDFAttachmentViewer_bindLink(button, content, filename) {
button.onclick = function downloadFile(e) {
this.downloadManager.downloadData(content, filename, '');
return false;
}.bind(this);
},

/**
* @param {PDFAttachmentViewerRenderParameters} params
*/
render: function PDFAttachmentViewer_render(params) {
params = params || {};
var attachments = params.attachments || null;
var attachmentsCount = 0;

if (this.attachments) {
var keepRenderedCapability = params.keepRenderedCapability === true;
this.reset(keepRenderedCapability);
var viewerUrl;
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
// The current URL is the viewer, let's use it and append the file.
viewerUrl = '?file=' + encodeURIComponent(blobUrl + '#' + filename);
} else if (PDFJSDev.test('CHROME')) {
// In the Chrome extension, the URL is rewritten using the history API
// in viewer.js, so an absolute URL must be generated.
// eslint-disable-next-line no-undef
viewerUrl = chrome.runtime.getURL('/content/web/viewer.html') +
'?file=' + encodeURIComponent(blobUrl + '#' + filename);
} else if (PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
// Let Firefox's content handler catch the URL and display the PDF.
viewerUrl = blobUrl + '?' + encodeURIComponent(filename);
}
this.attachments = attachments;
window.open(viewerUrl);
return false;
};
}

if (!attachments) {
this._dispatchEvent(attachmentsCount);
return;
}
/**
* @private
*/
_bindLink(button, content, filename) {
button.onclick = () => {
this.downloadManager.downloadData(content, filename, '');
return false;
};
}

var names = Object.keys(attachments).sort(function(a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
attachmentsCount = names.length;

for (var i = 0; i < attachmentsCount; i++) {
var item = attachments[names[i]];
var filename = removeNullCharacters(getFilenameFromUrl(item.filename));

var div = document.createElement('div');
div.className = 'attachmentsItem';
var button = document.createElement('button');
button.textContent = filename;
if (/\.pdf$/i.test(filename) && !PDFJS.disableCreateObjectURL) {
this._bindPdfLink(button, item.content, filename);
} else {
this._bindLink(button, item.content, filename);
}
/**
* @param {PDFAttachmentViewerRenderParameters} params
*/
render(params = {}) {
var attachments = params.attachments || null;
var attachmentsCount = 0;

div.appendChild(button);
this.container.appendChild(div);
}
if (this.attachments) {
var keepRenderedCapability = params.keepRenderedCapability === true;
this.reset(keepRenderedCapability);
}
this.attachments = attachments;

if (!attachments) {
this._dispatchEvent(attachmentsCount);
},

/**
* Used to append FileAttachment annotations to the sidebar.
* @private
*/
_appendAttachment: function PDFAttachmentViewer_appendAttachment(item) {
this._renderedCapability.promise.then(function (id, filename, content) {
var attachments = this.attachments;

if (!attachments) {
attachments = Object.create(null);
} else {
for (var name in attachments) {
if (id === name) {
return; // Ignore the new attachment if it already exists.
}
return;
}

var names = Object.keys(attachments).sort(function(a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
attachmentsCount = names.length;

for (var i = 0; i < attachmentsCount; i++) {
var item = attachments[names[i]];
var filename = removeNullCharacters(getFilenameFromUrl(item.filename));

var div = document.createElement('div');
div.className = 'attachmentsItem';
var button = document.createElement('button');
button.textContent = filename;
if (/\.pdf$/i.test(filename) && !PDFJS.disableCreateObjectURL) {
this._bindPdfLink(button, item.content, filename);
} else {
this._bindLink(button, item.content, filename);
}

div.appendChild(button);
this.container.appendChild(div);
}

this._dispatchEvent(attachmentsCount);
}

/**
* Used to append FileAttachment annotations to the sidebar.
* @private
*/
_appendAttachment(item) {
this._renderedCapability.promise.then(function (id, filename, content) {
var attachments = this.attachments;

if (!attachments) {
attachments = Object.create(null);
} else {
for (var name in attachments) {
if (id === name) {
return; // Ignore the new attachment if it already exists.
}
}
attachments[id] = {
filename: filename,
content: content,
};
this.render({
attachments: attachments,
keepRenderedCapability: true,
});
}.bind(this, item.id, item.filename, item.content));
},
};

return PDFAttachmentViewer;
})();
}
attachments[id] = {
filename,
content,
};
this.render({
attachments,
keepRenderedCapability: true,
});
}.bind(this, item.id, item.filename, item.content));
}
}

export {
PDFAttachmentViewer,
Expand Down
Loading

0 comments on commit 49f14a8

Please sign in to comment.