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

This commit is contained in:
Tim van der Meij 2015-01-27 22:06:19 +01:00
parent 609e2a30e9
commit 733882ac25
2 changed files with 52 additions and 34 deletions

View File

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

View File

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