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.
|
|
|
|
*/
|
2016-02-15 22:46:41 +09:00
|
|
|
/* globals PDFJS */
|
2014-04-25 04:29:05 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
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.
|
|
|
|
* @property {DownloadManager} downloadManager - The download manager.
|
|
|
|
*/
|
|
|
|
|
2016-02-21 21:36:24 +09:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PDFAttachmentViewerRenderParameters
|
|
|
|
* @property {Array|null} attachments - An array of attachment objects.
|
|
|
|
*/
|
|
|
|
|
2015-01-28 06:22:42 +09:00
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*/
|
2016-02-21 21:36:24 +09:00
|
|
|
var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
|
2015-01-28 06:22:42 +09:00
|
|
|
/**
|
2016-02-21 21:36:24 +09:00
|
|
|
* @constructs PDFAttachmentViewer
|
|
|
|
* @param {PDFAttachmentViewerOptions} options
|
2015-01-28 06:22:42 +09:00
|
|
|
*/
|
2016-02-21 21:36:24 +09:00
|
|
|
function PDFAttachmentViewer(options) {
|
|
|
|
this.attachments = null;
|
2015-01-28 06:06:19 +09:00
|
|
|
this.container = options.container;
|
2015-01-28 06:11:04 +09:00
|
|
|
this.downloadManager = options.downloadManager;
|
2014-04-25 04:29:05 +09:00
|
|
|
}
|
|
|
|
|
2016-02-21 21:36:24 +09:00
|
|
|
PDFAttachmentViewer.prototype = {
|
|
|
|
reset: function PDFAttachmentViewer_reset() {
|
|
|
|
this.attachments = null;
|
|
|
|
|
2015-01-28 06:06:19 +09:00
|
|
|
var container = this.container;
|
|
|
|
while (container.firstChild) {
|
|
|
|
container.removeChild(container.firstChild);
|
|
|
|
}
|
|
|
|
},
|
2014-04-25 04:29:05 +09:00
|
|
|
|
2015-03-21 21:07:01 +09:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-02-21 21:36:24 +09:00
|
|
|
_dispatchEvent:
|
|
|
|
function PDFAttachmentViewer_dispatchEvent(attachmentsCount) {
|
2015-03-21 21:07:01 +09:00
|
|
|
var event = document.createEvent('CustomEvent');
|
|
|
|
event.initCustomEvent('attachmentsloaded', true, true, {
|
|
|
|
attachmentsCount: attachmentsCount
|
|
|
|
});
|
|
|
|
this.container.dispatchEvent(event);
|
|
|
|
},
|
|
|
|
|
2015-01-28 06:22:42 +09:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-02-21 21:36:24 +09:00
|
|
|
_bindLink:
|
|
|
|
function PDFAttachmentViewer_bindLink(button, content, filename) {
|
2015-01-28 06:06:19 +09:00
|
|
|
button.onclick = function downloadFile(e) {
|
2015-01-28 07:16:05 +09:00
|
|
|
this.downloadManager.downloadData(content, filename, '');
|
2015-01-28 06:06:19 +09:00
|
|
|
return false;
|
2015-01-28 06:11:04 +09:00
|
|
|
}.bind(this);
|
2015-01-28 06:06:19 +09:00
|
|
|
},
|
2014-04-25 04:29:05 +09:00
|
|
|
|
2016-02-21 21:36:24 +09:00
|
|
|
/**
|
|
|
|
* @param {PDFAttachmentViewerRenderParameters} params
|
|
|
|
*/
|
|
|
|
render: function PDFAttachmentViewer_render(params) {
|
|
|
|
var attachments = (params && params.attachments) || null;
|
2015-03-21 21:07:01 +09:00
|
|
|
var attachmentsCount = 0;
|
2015-01-28 06:06:19 +09:00
|
|
|
|
2016-02-21 21:36:24 +09:00
|
|
|
if (this.attachments) {
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
this.attachments = attachments;
|
2015-01-28 06:06:19 +09:00
|
|
|
|
|
|
|
if (!attachments) {
|
2015-03-21 21:07:01 +09:00
|
|
|
this._dispatchEvent(attachmentsCount);
|
2015-01-28 06:06:19 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var names = Object.keys(attachments).sort(function(a, b) {
|
|
|
|
return a.toLowerCase().localeCompare(b.toLowerCase());
|
|
|
|
});
|
2015-03-21 21:07:01 +09:00
|
|
|
attachmentsCount = names.length;
|
|
|
|
|
|
|
|
for (var i = 0; i < attachmentsCount; i++) {
|
2015-01-28 06:06:19 +09:00
|
|
|
var item = attachments[names[i]];
|
2016-02-15 22:46:41 +09:00
|
|
|
var filename = PDFJS.getFilenameFromUrl(item.filename);
|
2015-01-28 06:06:19 +09:00
|
|
|
var div = document.createElement('div');
|
|
|
|
div.className = 'attachmentsItem';
|
|
|
|
var button = document.createElement('button');
|
2015-01-28 07:16:05 +09:00
|
|
|
this._bindLink(button, item.content, filename);
|
2016-01-05 05:33:41 +09:00
|
|
|
button.textContent = PDFJS.removeNullCharacters(filename);
|
2015-01-28 06:06:19 +09:00
|
|
|
div.appendChild(button);
|
|
|
|
this.container.appendChild(div);
|
|
|
|
}
|
2015-03-21 21:07:01 +09:00
|
|
|
|
|
|
|
this._dispatchEvent(attachmentsCount);
|
2015-01-28 06:06:19 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-21 21:36:24 +09:00
|
|
|
return PDFAttachmentViewer;
|
2015-01-28 06:06:19 +09:00
|
|
|
})();
|