diff --git a/dev/Model/ComposeAttachment.js b/dev/Model/ComposeAttachment.js index 99f3c87b3..8c3d898c9 100644 --- a/dev/Model/ComposeAttachment.js +++ b/dev/Model/ComposeAttachment.js @@ -1,4 +1,3 @@ -import { pInt } from 'Common/Utils'; import { FileInfo } from 'Common/File'; import { AbstractModel } from 'Knoin/AbstractModel'; @@ -59,39 +58,6 @@ export class ComposeAttachmentModel extends AbstractModel { }); } - static fromAttachment(item) - { - const attachment = new ComposeAttachmentModel( - item.download, - item.fileName, - item.estimatedSize, - item.isInline(), - item.isLinked(), - item.cid, - item.contentLocation - ); - attachment.fromMessage = true; - return attachment; - } - - /** - * @param {FetchJsonComposeAttachment} json - * @returns {boolean} - */ - initByUploadJson(json) { - let bResult = false; - if (json) { - this.fileName(json.Name); - this.size(undefined === json.Size ? 0 : pInt(json.Size)); - this.tempName(undefined === json.TempName ? '' : json.TempName); - this.isInline = false; - - bResult = true; - } - - return bResult; - } - /** * @returns {string} */ diff --git a/dev/View/Popup/Compose.js b/dev/View/Popup/Compose.js index 42dc915fd..4836e0fc5 100644 --- a/dev/View/Popup/Compose.js +++ b/dev/View/Popup/Compose.js @@ -1203,7 +1203,6 @@ export class ComposePopupView extends AbstractViewPopup { clickElement: dom.querySelector('#composeUploadButton'), dragAndDropElement: dom.querySelector('.b-attachment-place') }), - uploadCache = {}, attachmentSizeLimit = pInt(SettingsGet('AttachmentLimit')); oJua @@ -1224,14 +1223,7 @@ export class ComposePopupView extends AbstractViewPopup { this.dragAndDropVisible(false); }) .on('onProgress', (id, loaded, total) => { - let item = uploadCache[id]; - if (!item) { - item = this.getAttachmentById(id); - if (item) { - uploadCache[id] = item; - } - } - + let item = this.getAttachmentById(id); if (item) { item.progress(Math.floor((loaded / total) * 100)); } @@ -1239,15 +1231,15 @@ export class ComposePopupView extends AbstractViewPopup { .on('onSelect', (sId, oData) => { this.dragAndDropOver(false); - const fileName = undefined === oData.FileName ? '' : oData.FileName.toString(), + const size = pInt(oData.Size, null), - attachment = new ComposeAttachmentModel(sId, fileName, size); + attachment = new ComposeAttachmentModel( + sId, + oData.FileName ? oData.FileName.toString() : '', + size + ); - attachment.cancel = this.cancelAttachmentHelper(sId, oJua); - - this.attachments.push(attachment); - - this.attachmentsArea(); + this.addAttachment(attachment, 1, oJua); if (0 < size && 0 < attachmentSizeLimit && attachmentSizeLimit < size) { attachment @@ -1261,15 +1253,8 @@ export class ComposePopupView extends AbstractViewPopup { return true; }) - .on('onStart', (id) => { - let item = uploadCache[id]; - if (!item) { - item = this.getAttachmentById(id); - if (item) { - uploadCache[id] = item; - } - } - + .on('onStart', id => { + let item = this.getAttachmentById(id); if (item) { item .waiting(false) @@ -1302,12 +1287,10 @@ export class ComposePopupView extends AbstractViewPopup { .waiting(false) .uploading(false) .complete(true); - - attachment.initByUploadJson(attachmentJson); - } - - if (undefined === uploadCache[id]) { - delete uploadCache[id]; + attachment.fileName(attachmentJson.Name); + attachment.size(attachmentJson.Size ? pInt(attachmentJson.Size) : 0); + attachment.tempName(attachmentJson.TempName ? attachmentJson.TempName : ''); + attachment.isInline = false; } } }); @@ -1363,16 +1346,6 @@ export class ComposePopupView extends AbstractViewPopup { return this.attachments.find(item => item && id === item.id); } - cancelAttachmentHelper(id, oJua) { - return () => { - const attachment = this.getAttachmentById(id); - if (attachment) { - this.attachments.remove(attachment); - oJua && oJua.cancel(id); - } - }; - } - /** * @returns {Object} */ @@ -1396,18 +1369,22 @@ export class ComposePopupView extends AbstractViewPopup { temp = '.eml' === temp.slice(-4).toLowerCase() ? temp : temp + '.eml'; const attachment = new ComposeAttachmentModel(message.requestHash, temp, message.size()); - attachment.fromMessage = true; - attachment.cancel = this.cancelAttachmentHelper(message.requestHash); - attachment - .waiting(false) - .uploading(true) - .complete(true); - - this.attachments.push(attachment); + attachment.complete(true); + this.addAttachment(attachment); } } + addAttachment(attachment, view, oJua) { + oJua || attachment.waiting(false).uploading(true); + attachment.cancel = () => { + this.attachments.remove(attachment); + oJua && oJua.cancel(attachment.id); + }; + this.attachments.push(attachment); + view && this.attachmentsArea(); + } + /** * @param {string} url * @param {string} name @@ -1416,18 +1393,7 @@ export class ComposePopupView extends AbstractViewPopup { */ addAttachmentHelper(url, name, size) { const attachment = new ComposeAttachmentModel(url, name, size); - - attachment.fromMessage = false; - attachment.cancel = this.cancelAttachmentHelper(url); - attachment - .waiting(false) - .uploading(true) - .complete(false); - - this.attachments.push(attachment); - - this.attachmentsArea(); - + this.addAttachment(attachment, 1); return attachment; } @@ -1456,14 +1422,17 @@ export class ComposePopupView extends AbstractViewPopup { } if (add) { - const attachment = ComposeAttachmentModel.fromAttachment(item); - attachment.cancel = this.cancelAttachmentHelper(item.download); - attachment - .waiting(false) - .uploading(true) - .complete(false); - - this.attachments.push(attachment); + const attachment = new ComposeAttachmentModel( + item.download, + item.fileName, + item.estimatedSize, + item.isInline(), + item.isLinked(), + item.cid, + item.contentLocation + ); + attachment.fromMessage = true; + this.addAttachment(attachment); } }); } diff --git a/vendors/jua/jua.js b/vendors/jua/jua.js index 84c0d1c87..b30dd7ae2 100644 --- a/vendors/jua/jua.js +++ b/vendors/jua/jua.js @@ -242,7 +242,7 @@ oEvent.files || oEvent.dataTransfer.files, oFile => { if (oFile) { - self.addNewFile(oFile); + self.addFile(oFile); timerStop(); } }, @@ -286,18 +286,10 @@ /** * @param {Object} oFileInfo */ - addNewFile(oFileInfo) + addFile(oFileInfo) { - this.addFile('jua-uid-' + Jua.randomId(16) + '-' + (Date.now().toString()), oFileInfo); - } - - /** - * @param {string} sUid - * @param {Object} oFileInfo - */ - addFile(sUid, oFileInfo) - { - const fOnSelect = this.getEvent('onSelect'); + const sUid = 'jua-uid-' + Jua.randomId(16) + '-' + (Date.now().toString()), + fOnSelect = this.getEvent('onSelect'); if (oFileInfo && (!fOnSelect || (false !== fOnSelect(sUid, oFileInfo)))) { this.oUids[sUid] = true; @@ -404,7 +396,7 @@ oInput.addEventListener('input', () => { const fFileCallback = oFile => { - self.addNewFile(oFile); + self.addFile(oFile); setTimeout(() => { oInput.remove(); oClickElement.removeEventListener('click', onClick);