From e99a69a9aa16c659839c218fe287bb5461e1e114 Mon Sep 17 00:00:00 2001 From: djmaze Date: Tue, 15 Sep 2020 15:08:08 +0200 Subject: [PATCH] Attachments array to new AttachmentCollectionModel --- dev/Model/AttachmentCollection.js | 49 ++++++++++ dev/Model/EmailCollection.js | 12 +-- dev/Model/Message.js | 95 +++---------------- dev/View/Popup/Compose.js | 13 +-- .../templates/Views/User/MailMessageView.html | 6 +- 5 files changed, 70 insertions(+), 105 deletions(-) create mode 100644 dev/Model/AttachmentCollection.js diff --git a/dev/Model/AttachmentCollection.js b/dev/Model/AttachmentCollection.js new file mode 100644 index 000000000..2f06d3fa3 --- /dev/null +++ b/dev/Model/AttachmentCollection.js @@ -0,0 +1,49 @@ +import { AttachmentModel } from 'Model/Attachment'; + +'use strict'; + +class AttachmentCollectionModel extends Array +{ + constructor() { + super(); + } + + /** + * @param {?Array} json + * @returns {AttachmentCollectionModel} + */ + static reviveFromJson(items, foundedCIDs) { + let result = new AttachmentCollectionModel; + if (items && 'Collection/AttachmentCollection' === items['@Object']) { + items = items['@Collection']; + } + Array.isArray(items) && items.forEach(attachment => { + attachment = AttachmentModel.newInstanceFromJson(attachment); + if (attachment) { + if (attachment.cidWithOutTags && foundedCIDs.includes(attachment.cidWithOutTags)) { + attachment.isLinked = true; + } + result.push(attachment); + } + }); + return result; + } + + /** + * @returns {boolean} + */ + hasVisible() { + return !!this.find(item => !item.isLinked); + } + + /** + * @param {string} cid + * @returns {*} + */ + findByCid(cid) { + cid = cid.replace(/^<+|>+$/, ''); + return this.find(item => cid === item.cidWithOutTags); + } +} + +export { AttachmentCollectionModel, AttachmentCollectionModel as default }; diff --git a/dev/Model/EmailCollection.js b/dev/Model/EmailCollection.js index 2a6254223..7689ef5a1 100644 --- a/dev/Model/EmailCollection.js +++ b/dev/Model/EmailCollection.js @@ -10,16 +10,14 @@ class EmailCollectionModel extends Array /** * @param {?Array} json - * @returns {Array.} + * @returns {EmailCollectionModel} */ static reviveFromJson(items) { let result = new EmailCollectionModel; - if (Array.isArray(items)) { - items.forEach(email => { - email = EmailModel.newInstanceFromJson(email); - email && result.push(email); - }); - } + Array.isArray(items) && items.forEach(email => { + email = EmailModel.newInstanceFromJson(email); + email && result.push(email); + }); return result; } diff --git a/dev/Model/Message.js b/dev/Model/Message.js index 3adf8dc7d..f16a06a14 100644 --- a/dev/Model/Message.js +++ b/dev/Model/Message.js @@ -15,7 +15,8 @@ import { messageViewLink, messageDownloadLink } from 'Common/Links'; import FolderStore from 'Stores/User/Folder'; import PgpStore from 'Stores/User/Pgp'; -import { AttachmentModel, staticCombinedIconClass } from 'Model/Attachment'; +import { staticCombinedIconClass } from 'Model/Attachment'; +import { AttachmentCollectionModel } from 'Model/AttachmentCollection'; import { EmailCollectionModel } from 'Model/EmailCollection'; import { AbstractModel } from 'Knoin/AbstractModel'; @@ -73,7 +74,7 @@ class MessageModel extends AbstractModel { this.isHtml = ko.observable(false); this.hasImages = ko.observable(false); - this.attachments = ko.observableArray([]); + this.attachments = new AttachmentCollectionModel; this.isPgpSigned = ko.observable(false); this.isPgpEncrypted = ko.observable(false); @@ -158,7 +159,7 @@ class MessageModel extends AbstractModel { this.isHtml(false); this.hasImages(false); - this.attachments([]); + this.attachments = new AttachmentCollectionModel; this.isPgpSigned(false); this.isPgpEncrypted(false); @@ -299,7 +300,7 @@ class MessageModel extends AbstractModel { this.attachmentsSpecData(isArray(json.AttachmentsSpecData) ? json.AttachmentsSpecData : []); this.foundedCIDs = isArray(json.FoundedCIDs) ? json.FoundedCIDs : []; - this.attachments(this.initAttachmentsFromJson(json.Attachments)); + this.attachments = AttachmentCollectionModel.reviveFromJson(json.Attachments, this.foundedCIDs); this.readReceipt(json.ReadReceipt || ''); @@ -311,35 +312,6 @@ class MessageModel extends AbstractModel { return result; } - /** - * @param {(AjaxJsonAttachment|null)} oJsonAttachments - * @returns {Array} - */ - initAttachmentsFromJson(json) { - let index = 0, - len = 0, - attachment = null; - const result = []; - - if (json && 'Collection/AttachmentCollection' === json['@Object'] && Array.isNotEmpty(json['@Collection'])) { - for (index = 0, len = json['@Collection'].length; index < len; index++) { - attachment = AttachmentModel.newInstanceFromJson(json['@Collection'][index]); - if (attachment) { - if ( - attachment.cidWithOutTags && - this.foundedCIDs.includes(attachment.cidWithOutTags) - ) { - attachment.isLinked = true; - } - - result.push(attachment); - } - } - } - - return result; - } - /** * @returns {boolean} */ @@ -457,44 +429,6 @@ class MessageModel extends AbstractModel { return classes.join(' '); } - /** - * @returns {boolean} - */ - hasVisibleAttachments() { - return !!this.attachments().find(item => !item.isLinked); - } - - /** - * @param {string} cid - * @returns {*} - */ - findAttachmentByCid(cid) { - let result = null; - const attachments = this.attachments(); - - if (Array.isNotEmpty(attachments)) { - cid = cid.replace(/^<+/, '').replace(/>+$/, ''); - result = attachments.find(item => cid === item.cidWithOutTags); - } - - return result || null; - } - - /** - * @param {string} contentLocation - * @returns {*} - */ - findAttachmentByContentLocation(contentLocation) { - let result = null; - const attachments = this.attachments(); - - if (Array.isNotEmpty(attachments)) { - result = attachments.find(item => contentLocation === item.contentLocation); - } - - return result || null; - } - /** * @returns {string} */ @@ -585,14 +519,6 @@ class MessageModel extends AbstractModel { return [toResult, ccResult]; } - /** - * @returns {string} - */ - attachmentsToStringLine() { - const attachLines = this.attachments().map(item => item.fileName + ' (' + item.friendlySize + ')'); - return attachLines && attachLines.length ? attachLines.join(', ') : ''; - } - /** * @param {boolean=} print = false */ @@ -732,18 +658,19 @@ class MessageModel extends AbstractModel { if (this.body && !this.body.rlInitInternalImages) { this.body.rlInitInternalImages = true; - const body = this.body; + const body = this.body, + findAttachmentByCid = cid => this.attachments.findByCid(cid); body.querySelectorAll('[data-x-src-cid]').forEach(node => { - const attachment = this.findAttachmentByCid(node.dataset.xSrcCid); + const attachment = findAttachmentByCid(node.dataset.xSrcCid); if (attachment && attachment.download) { node.src = attachment.linkPreview(); } }); body.querySelectorAll('[data-x-src-location]').forEach(node => { - const attachment = this.findAttachmentByContentLocation(node.dataset.xSrcLocation) - || this.findAttachmentByCid(node.dataset.xSrcLocation); + const attachment = this.attachments.find(item => node.dataset.xSrcLocation === item.contentLocation) + || findAttachmentByCid(node.dataset.xSrcLocation); if (attachment && attachment.download) { if (node.matches('img')) { node.loading = 'lazy'; @@ -754,7 +681,7 @@ class MessageModel extends AbstractModel { body.querySelectorAll('[style-cid]').forEach(node => { const name = node.dataset.xStyleCidName, - attachment = this.findAttachmentByCid(node.dataset.xStyleCid); + attachment = findAttachmentByCid(node.dataset.xStyleCid); if (attachment && attachment.linkPreview && name) { node.setAttribute('style', ((node.getAttribute('style')||'') + ';' + name + ": url('" + attachment.linkPreview() + "')") diff --git a/dev/View/Popup/Compose.js b/dev/View/Popup/Compose.js index f22e8d616..6ef9a0cfe 100644 --- a/dev/View/Popup/Compose.js +++ b/dev/View/Popup/Compose.js @@ -1162,7 +1162,7 @@ class ComposePopupView extends AbstractViewNext { cancelAttachmentHelper(id, oJua) { return () => { - const attachment = this.attachments().find(item => item && item.id === id); + const attachment = this.getAttachmentById(id); if (attachment) { this.attachments.remove(attachment); delegateRunOnDestroy(attachment); @@ -1367,8 +1367,7 @@ class ComposePopupView extends AbstractViewNext { if (ComposeType.ForwardAsAttachment === type) { this.addMessageAsAttachment(message); } else { - const attachments = message.attachments(); - (Array.isNotEmpty(attachments) ? attachments : []).forEach(item => { + message.attachments.forEach(item => { let add = false; switch (type) { case ComposeType.Reply: @@ -1409,14 +1408,6 @@ class ComposePopupView extends AbstractViewNext { } } - removeLinkedAttachments() { - const arrachment = this.attachments().find(item => item && item.isLinked); - if (arrachment) { - this.attachments.remove(arrachment); - delegateRunOnDestroy(arrachment); - } - } - setMessageAttachmentFailedDownloadText() { this.attachments().forEach(attachment => { if (attachment && attachment.fromMessage) { diff --git a/rainloop/v/0.0.0/app/templates/Views/User/MailMessageView.html b/rainloop/v/0.0.0/app/templates/Views/User/MailMessageView.html index 488ffd38a..0e128a55f 100644 --- a/rainloop/v/0.0.0/app/templates/Views/User/MailMessageView.html +++ b/rainloop/v/0.0.0/app/templates/Views/User/MailMessageView.html @@ -341,9 +341,9 @@    -
-
    +
    • @@ -393,7 +393,7 @@
      + data-bind="visible: showAttachmnetControls() && message() && message().attachments.hasVisible()">