Browse Source

Refactor PDFAttachmentView to be more class-like and to separate functionality into methods

Tim van der Meij 10 years ago
parent
commit
733882ac25
  1. 77
      web/pdf_attachment_view.js
  2. 9
      web/viewer.js

77
web/pdf_attachment_view.js

@ -18,37 +18,54 @@
'use strict'; 'use strict';
var PDFAttachmentView = function documentAttachmentsView(options) { var PDFAttachmentView = (function PDFAttachmentViewClosure() {
var attachments = options.attachments; function PDFAttachmentView(options) {
var attachmentsView = options.attachmentsView; this.container = options.container;
while (attachmentsView.firstChild) { this.attachments = options.attachments;
attachmentsView.removeChild(attachmentsView.firstChild);
} }
if (!attachments) { PDFAttachmentView.prototype = {
return; reset: function PDFAttachmentView_reset() {
} var container = this.container;
while (container.firstChild) {
container.removeChild(container.firstChild);
}
},
function bindItemLink(domObj, item) { _bindLink: function PDFAttachmentView_bindLink(button, item) {
domObj.onclick = function documentAttachmentsViewOnclick(e) { button.onclick = function downloadFile(e) {
var downloadManager = new DownloadManager(); var downloadManager = new DownloadManager();
downloadManager.downloadData(item.content, getFileName(item.filename), var content = item.content;
''); var filename = item.filename;
return false; downloadManager.downloadData(content, getFileName(filename), '');
}; return false;
} };
},
var names = Object.keys(attachments).sort(function(a,b) { render: function PDFAttachmentView_render() {
return a.toLowerCase().localeCompare(b.toLowerCase()); var attachments = this.attachments;
});
for (var i = 0, ii = names.length; i < ii; i++) { this.reset();
var item = attachments[names[i]];
var div = document.createElement('div'); if (!attachments) {
div.className = 'attachmentsItem'; return;
var button = document.createElement('button'); }
bindItemLink(button, item);
button.textContent = getFileName(item.filename); var names = Object.keys(attachments).sort(function(a, b) {
div.appendChild(button); return a.toLowerCase().localeCompare(b.toLowerCase());
attachmentsView.appendChild(div); });
} for (var i = 0, len = names.length; i < len; i++) {
}; var item = attachments[names[i]];
var div = document.createElement('div');
div.className = 'attachmentsItem';
var button = document.createElement('button');
this._bindLink(button, item);
button.textContent = getFileName(item.filename);
div.appendChild(button);
this.container.appendChild(div);
}
}
};
return PDFAttachmentView;
})();

9
web/viewer.js

@ -985,14 +985,15 @@ var PDFViewerApplication = {
} }
}); });
pdfDocument.getAttachments().then(function(attachments) { pdfDocument.getAttachments().then(function(attachments) {
var attachmentsView = document.getElementById('attachmentsView'); var container = document.getElementById('attachmentsView');
self.attachments = new PDFAttachmentView({ self.attachments = new PDFAttachmentView({
attachments: attachments, container: container,
attachmentsView: attachmentsView attachments: attachments
}); });
self.attachments.render();
document.getElementById('viewAttachments').disabled = !attachments; document.getElementById('viewAttachments').disabled = !attachments;
if (!attachments && !attachmentsView.classList.contains('hidden')) { if (!attachments && !container.classList.contains('hidden')) {
self.switchSidebarView('thumbs'); self.switchSidebarView('thumbs');
} }
if (attachments && if (attachments &&

Loading…
Cancel
Save