2014-04-25 04:29:05 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-03-28 17:15:02 +09:00
|
|
|
import {
|
|
|
|
createObjectURL, createPromiseCapability, getFilenameFromUrl, PDFJS,
|
|
|
|
removeNullCharacters
|
2017-05-31 10:38:22 +09:00
|
|
|
} from 'pdfjs-lib';
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2015-01-28 06:22:42 +09:00
|
|
|
/**
|
2016-02-21 21:36:24 +09:00
|
|
|
* @typedef {Object} PDFAttachmentViewerOptions
|
2015-01-28 06:22:42 +09:00
|
|
|
* @property {HTMLDivElement} container - The viewer element.
|
2016-04-26 07:57:15 +09:00
|
|
|
* @property {EventBus} eventBus - The application event bus.
|
2015-01-28 06:22:42 +09:00
|
|
|
* @property {DownloadManager} downloadManager - The download manager.
|
|
|
|
*/
|
|
|
|
|
2016-02-21 21:36:24 +09:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PDFAttachmentViewerRenderParameters
|
2017-06-30 19:55:22 +09:00
|
|
|
* @property {Object|null} attachments - A lookup table of attachment objects.
|
2016-02-21 21:36:24 +09:00
|
|
|
*/
|
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
class PDFAttachmentViewer {
|
2015-01-28 06:22:42 +09:00
|
|
|
/**
|
2016-02-21 21:36:24 +09:00
|
|
|
* @param {PDFAttachmentViewerOptions} options
|
2015-01-28 06:22:42 +09:00
|
|
|
*/
|
2017-06-30 19:55:22 +09:00
|
|
|
constructor({ container, eventBus, downloadManager, }) {
|
|
|
|
this.container = container;
|
|
|
|
this.eventBus = eventBus;
|
|
|
|
this.downloadManager = downloadManager;
|
2017-01-16 02:12:24 +09:00
|
|
|
|
2017-08-14 19:34:24 +09:00
|
|
|
this.reset();
|
|
|
|
|
2017-01-16 02:12:24 +09:00
|
|
|
this.eventBus.on('fileattachmentannotation',
|
|
|
|
this._appendAttachment.bind(this));
|
2014-04-25 04:29:05 +09:00
|
|
|
}
|
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
reset(keepRenderedCapability = false) {
|
|
|
|
this.attachments = null;
|
2016-02-21 21:36:24 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
// Remove the attachments from the DOM.
|
|
|
|
this.container.textContent = '';
|
2017-01-16 02:12:24 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
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,
|
|
|
|
});
|
2017-01-16 02:12:24 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
this._renderedCapability.resolve();
|
|
|
|
}
|
2015-03-21 21:07:01 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_bindPdfLink(button, content, filename) {
|
|
|
|
if (PDFJS.disableCreateObjectURL) {
|
|
|
|
throw new Error('bindPdfLink: ' +
|
|
|
|
'Unsupported "PDFJS.disableCreateObjectURL" value.');
|
|
|
|
}
|
2017-06-30 19:55:22 +09:00
|
|
|
let blobUrl;
|
2017-04-18 00:13:48 +09:00
|
|
|
button.onclick = function() {
|
|
|
|
if (!blobUrl) {
|
|
|
|
blobUrl = createObjectURL(content, 'application/pdf');
|
2017-04-16 03:49:16 +09:00
|
|
|
}
|
2017-06-30 19:55:22 +09:00
|
|
|
let viewerUrl;
|
2017-04-18 00:13:48 +09:00
|
|
|
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);
|
2016-02-21 21:36:24 +09:00
|
|
|
}
|
2017-04-18 00:13:48 +09:00
|
|
|
window.open(viewerUrl);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
2015-01-28 06:06:19 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_bindLink(button, content, filename) {
|
|
|
|
button.onclick = () => {
|
|
|
|
this.downloadManager.downloadData(content, filename, '');
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
2015-01-28 06:06:19 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
/**
|
|
|
|
* @param {PDFAttachmentViewerRenderParameters} params
|
|
|
|
*/
|
2017-06-30 19:55:22 +09:00
|
|
|
render({ attachments, keepRenderedCapability = false, }) {
|
|
|
|
let attachmentsCount = 0;
|
2017-02-06 08:56:13 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
if (this.attachments) {
|
2017-06-30 19:55:22 +09:00
|
|
|
this.reset(keepRenderedCapability === true);
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
2017-06-30 19:55:22 +09:00
|
|
|
this.attachments = attachments || null;
|
2015-03-21 21:07:01 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
if (!attachments) {
|
2015-03-21 21:07:01 +09:00
|
|
|
this._dispatchEvent(attachmentsCount);
|
2017-04-18 00:13:48 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-30 19:55:22 +09:00
|
|
|
let names = Object.keys(attachments).sort(function(a, b) {
|
2017-04-18 00:13:48 +09:00
|
|
|
return a.toLowerCase().localeCompare(b.toLowerCase());
|
|
|
|
});
|
|
|
|
attachmentsCount = names.length;
|
|
|
|
|
2017-06-30 19:55:22 +09:00
|
|
|
for (let i = 0; i < attachmentsCount; i++) {
|
|
|
|
let item = attachments[names[i]];
|
|
|
|
let filename = removeNullCharacters(getFilenameFromUrl(item.filename));
|
2017-04-18 00:13:48 +09:00
|
|
|
|
2017-06-30 19:55:22 +09:00
|
|
|
let div = document.createElement('div');
|
2017-04-18 00:13:48 +09:00
|
|
|
div.className = 'attachmentsItem';
|
2017-06-30 19:55:22 +09:00
|
|
|
let button = document.createElement('button');
|
2017-04-18 00:13:48 +09:00
|
|
|
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
|
|
|
|
*/
|
2017-05-05 00:09:50 +09:00
|
|
|
_appendAttachment({ id, filename, content, }) {
|
|
|
|
this._renderedCapability.promise.then(() => {
|
2017-06-30 19:55:22 +09:00
|
|
|
let attachments = this.attachments;
|
2017-04-18 00:13:48 +09:00
|
|
|
|
|
|
|
if (!attachments) {
|
|
|
|
attachments = Object.create(null);
|
|
|
|
} else {
|
2017-06-30 19:55:22 +09:00
|
|
|
for (let name in attachments) {
|
2017-04-18 00:13:48 +09:00
|
|
|
if (id === name) {
|
|
|
|
return; // Ignore the new attachment if it already exists.
|
2017-01-16 02:12:24 +09:00
|
|
|
}
|
|
|
|
}
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
|
|
|
attachments[id] = {
|
|
|
|
filename,
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
this.render({
|
|
|
|
attachments,
|
|
|
|
keepRenderedCapability: true,
|
|
|
|
});
|
2017-05-05 00:09:50 +09:00
|
|
|
});
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export {
|
|
|
|
PDFAttachmentViewer,
|
|
|
|
};
|