snappymail/dev/View/Popup/Compose.js

1619 lines
42 KiB
JavaScript
Raw Normal View History

import window from 'window';
import _ from '_';
import $ from '$';
import ko from 'ko';
import key from 'key';
import Jua from 'Jua';
import {
2019-07-05 03:19:24 +08:00
Capa,
Magics,
KeyState,
ComposeType,
StorageResultType,
EditorDefaultType,
Notification,
SetSystemFoldersNotification,
UploadErrorCode
} from 'Common/Enums';
import {
2019-07-05 03:19:24 +08:00
trim,
isArray,
isNormal,
delegateRun,
isNonEmptyArray,
clearBqSwitcher,
replySubjectAdd,
encodeHtml,
noopFalse,
inFocus,
delegateRunOnDestroy,
pInt,
isUnd
} from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { UNUSED_OPTION_VALUE } from 'Common/Consts';
import { upload } from 'Common/Links';
import { i18n, getNotification, getUploadErrorDescByCode } from 'Common/Translator';
import { format as momentorFormat } from 'Common/Momentor';
import { getMessageFlagsFromCache, setMessageFlagsToCache, setFolderHash } from 'Common/Cache';
2019-07-05 03:19:24 +08:00
import { HtmlEditor } from 'Common/HtmlEditor';
2020-03-11 21:17:52 +08:00
import { bMobileDevice } from 'Common/Globals';
import AppStore from 'Stores/User/App';
import SettingsStore from 'Stores/User/Settings';
import IdentityStore from 'Stores/User/Identity';
import AccountStore from 'Stores/User/Account';
import FolderStore from 'Stores/User/Folder';
import PgpStore from 'Stores/User/Pgp';
import MessageStore from 'Stores/User/Message';
import Remote from 'Remote/User/Ajax';
import * as Settings from 'Storage/Settings';
import * as Events from 'Common/Events';
2019-07-05 03:19:24 +08:00
import { ComposeAttachmentModel } from 'Model/ComposeAttachment';
2019-07-05 03:19:24 +08:00
import { getApp } from 'Helper/Apps/User';
2019-07-05 03:19:24 +08:00
import { popup, command, isPopupVisible, showScreenPopup, hideScreenPopup, routeOn, routeOff } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/Compose',
templateID: 'PopupsCompose'
})
2019-07-05 03:19:24 +08:00
class ComposePopupView extends AbstractViewNext {
constructor() {
super();
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
const fEmailOutInHelper = (context, identity, name, isIn) => {
if (identity && context && identity[name]() && (isIn ? true : context[name]())) {
const identityEmail = identity[name]();
let list = trim(context[name]()).split(/[,]/);
list = list.filter(email => {
2019-07-05 03:19:24 +08:00
email = trim(email);
return email && trim(identityEmail) !== email;
});
2019-07-05 03:19:24 +08:00
if (isIn) {
list.push(identityEmail);
}
2019-07-05 03:19:24 +08:00
context[name](list.join(','));
}
};
this.oLastMessage = null;
this.oEditor = null;
this.aDraftInfo = null;
this.sInReplyTo = '';
this.bFromDraft = false;
this.sReferences = '';
2014-03-20 06:39:36 +08:00
this.sLastFocusedField = 'to';
2015-02-16 05:55:59 +08:00
this.resizerTrigger = this.resizerTrigger.bind(this);
this.allowContacts = !!AppStore.contactsIsAllowed();
this.allowFolders = !!Settings.capa(Capa.Folders);
this.bSkipNextHide = false;
this.composeInEdit = AppStore.composeInEdit;
this.editorDefaultType = SettingsStore.editorDefaultType;
2014-12-28 03:48:55 +08:00
this.capaOpenPGP = PgpStore.capaOpenPGP;
this.identitiesDropdownTrigger = ko.observable(false);
2014-01-04 08:20:07 +08:00
this.to = ko.observable('');
this.to.focused = ko.observable(false);
this.cc = ko.observable('');
this.cc.focused = ko.observable(false);
this.bcc = ko.observable('');
this.bcc.focused = ko.observable(false);
this.replyTo = ko.observable('');
this.replyTo.focused = ko.observable(false);
2015-02-16 05:55:59 +08:00
2017-09-28 01:58:15 +08:00
// this.to.subscribe((v) => console.log(v));
ko.computed(() => {
2019-07-05 03:19:24 +08:00
switch (true) {
case this.to.focused():
this.sLastFocusedField = 'to';
break;
case this.cc.focused():
this.sLastFocusedField = 'cc';
break;
case this.bcc.focused():
this.sLastFocusedField = 'bcc';
break;
// no default
}
2019-07-05 03:19:24 +08:00
}).extend({ notify: 'always' });
this.subject = ko.observable('');
this.subject.focused = ko.observable(false);
this.isHtml = ko.observable(false);
this.requestDsn = ko.observable(false);
this.requestReadReceipt = ko.observable(false);
this.markAsImportant = ko.observable(false);
this.sendError = ko.observable(false);
this.sendSuccessButSaveError = ko.observable(false);
this.savedError = ko.observable(false);
2019-07-05 03:19:24 +08:00
this.sendButtonSuccess = ko.computed(() => !this.sendError() && !this.sendSuccessButSaveError());
2016-04-29 04:32:54 +08:00
this.sendErrorDesc = ko.observable('');
this.savedErrorDesc = ko.observable('');
2015-04-07 03:32:19 +08:00
this.sendError.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (!value) {
this.sendErrorDesc('');
}
});
2015-04-07 03:32:19 +08:00
this.savedError.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (!value) {
this.savedErrorDesc('');
}
});
2015-04-07 03:32:19 +08:00
this.sendSuccessButSaveError.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (!value) {
this.savedErrorDesc('');
}
});
2015-04-07 03:32:19 +08:00
this.savedTime = ko.observable(0);
2019-07-05 03:19:24 +08:00
this.savedTimeText = ko.computed(() =>
0 < this.savedTime() ? i18n('COMPOSE/SAVED_TIME', { 'TIME': momentorFormat(this.savedTime() - 1, 'LT') }) : ''
);
this.emptyToError = ko.observable(false);
2019-07-05 03:19:24 +08:00
this.emptyToErrorTooltip = ko.computed(() => (this.emptyToError() ? i18n('COMPOSE/EMPTY_TO_ERROR_DESC') : ''));
2015-04-07 03:32:19 +08:00
this.attachmentsInProcessError = ko.observable(false);
this.attachmentsInErrorError = ko.observable(false);
2013-12-13 07:23:47 +08:00
this.attachmentsErrorTooltip = ko.computed(() => {
let result = '';
2019-07-05 03:19:24 +08:00
switch (true) {
case this.attachmentsInProcessError():
result = i18n('COMPOSE/ATTACHMENTS_UPLOAD_ERROR_DESC');
break;
case this.attachmentsInErrorError():
result = i18n('COMPOSE/ATTACHMENTS_ERROR_DESC');
break;
// no default
}
return result;
});
2015-04-10 16:17:49 +08:00
this.showCc = ko.observable(false);
this.showBcc = ko.observable(false);
this.showReplyTo = ko.observable(false);
2015-04-10 16:17:49 +08:00
this.cc.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (false === this.showCc() && 0 < value.length) {
this.showCc(true);
}
});
2015-04-10 16:17:49 +08:00
this.bcc.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (false === this.showBcc() && 0 < value.length) {
this.showBcc(true);
}
});
2015-04-10 16:17:49 +08:00
this.replyTo.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (false === this.showReplyTo() && 0 < value.length) {
this.showReplyTo(true);
}
});
this.draftFolder = ko.observable('');
this.draftUid = ko.observable('');
this.sending = ko.observable(false);
this.saving = ko.observable(false);
this.attachments = ko.observableArray([]);
this.attachmentsInProcess = ko.computed(() => this.attachments().filter(item => item && !item.complete()));
this.attachmentsInReady = ko.computed(() => this.attachments().filter(item => item && item.complete()));
this.attachmentsInError = ko.computed(() => this.attachments().filter(item => item && '' !== item.error()));
this.attachmentsCount = ko.computed(() => this.attachments().length);
this.attachmentsInErrorCount = ko.computed(() => this.attachmentsInError().length);
this.attachmentsInProcessCount = ko.computed(() => this.attachmentsInProcess().length);
this.isDraftFolderMessage = ko.computed(() => '' !== this.draftFolder() && '' !== this.draftUid());
this.attachmentsPlace = ko.observable(false);
this.attachments.subscribe(this.resizerTrigger);
this.attachmentsPlace.subscribe(this.resizerTrigger);
2014-08-21 23:08:34 +08:00
this.attachmentsInErrorCount.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (0 === value) {
this.attachmentsInErrorError(false);
}
});
2014-08-21 23:08:34 +08:00
this.composeUploaderButton = ko.observable(null);
this.composeUploaderDropPlace = ko.observable(null);
this.dragAndDropEnabled = ko.observable(false);
2019-07-05 03:19:24 +08:00
this.dragAndDropOver = ko.observable(false).extend({ throttle: 1 });
this.dragAndDropVisible = ko.observable(false).extend({ throttle: 1 });
this.attacheMultipleAllowed = ko.observable(false);
this.addAttachmentEnabled = ko.observable(false);
this.composeEditorArea = ko.observable(null);
this.identities = IdentityStore.identities;
2019-07-05 03:19:24 +08:00
this.identitiesOptions = ko.computed(() =>
IdentityStore.identities().map(item => ({
'item': item,
'optValue': item.id(),
'optText': item.formattedName()
}))
);
2019-07-05 03:19:24 +08:00
this.currentIdentity = ko.observable(this.identities()[0] ? this.identities()[0] : null);
2019-07-05 03:19:24 +08:00
this.currentIdentity.extend({
toggleSubscribe: [
this,
(identity) => {
fEmailOutInHelper(this, identity, 'bcc');
fEmailOutInHelper(this, identity, 'replyTo');
},
(identity) => {
fEmailOutInHelper(this, identity, 'bcc', true);
fEmailOutInHelper(this, identity, 'replyTo', true);
}
]
});
this.currentIdentityView = ko.computed(() => {
const item = this.currentIdentity();
return item ? item.formattedName() : 'unknown';
});
this.to.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (this.emptyToError() && 0 < value.length) {
this.emptyToError(false);
}
});
this.attachmentsInProcess.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (this.attachmentsInProcessError() && isArray(value) && 0 === value.length) {
this.attachmentsInProcessError(false);
}
2016-06-30 08:02:45 +08:00
});
2019-07-05 03:19:24 +08:00
this.resizer = ko.observable(false).extend({ throttle: 50 });
this.resizer.subscribe(() => {
if (this.oEditor) {
this.oEditor.resize();
}
});
this.canBeSentOrSaved = ko.computed(() => !this.sending() && !this.saving());
this.sendMessageResponse = this.sendMessageResponse.bind(this);
this.saveMessageResponse = this.saveMessageResponse.bind(this);
2016-09-10 06:38:16 +08:00
Events.sub('interval.2m', () => {
2019-07-05 03:19:24 +08:00
if (
this.modalVisibility() &&
!FolderStore.draftFolderNotEnabled() &&
SettingsStore.allowDraftAutosave() &&
!this.isEmptyForm(false) &&
!this.saving() &&
!this.sending() &&
!this.savedError()
) {
2016-09-10 06:38:16 +08:00
this.saveCommand();
}
2016-09-10 06:38:16 +08:00
});
2016-09-10 06:38:16 +08:00
this.showCc.subscribe(this.resizerTrigger);
this.showBcc.subscribe(this.resizerTrigger);
this.showReplyTo.subscribe(this.resizerTrigger);
this.onMessageUploadAttachments = this.onMessageUploadAttachments.bind(this);
2016-09-10 06:38:16 +08:00
this.bDisabeCloseOnEsc = true;
this.sDefaultKeyScope = KeyState.Compose;
this.tryToClosePopup = _.debounce(this.tryToClosePopup.bind(this), Magics.Time200ms);
this.emailsSource = this.emailsSource.bind(this);
this.autosaveFunction = this.autosaveFunction.bind(this);
2016-09-10 06:38:16 +08:00
this.iTimer = 0;
}
2016-09-10 06:38:16 +08:00
@command((self) => self.canBeSentOrSaved())
sendCommand() {
2019-07-05 03:19:24 +08:00
const sTo = trim(this.to()),
2016-09-10 06:38:16 +08:00
sCc = trim(this.cc()),
sBcc = trim(this.bcc());
2019-07-05 03:19:24 +08:00
let sSentFolder = FolderStore.sentFolder();
2015-04-10 16:17:49 +08:00
2016-09-10 06:38:16 +08:00
this.attachmentsInProcessError(false);
this.attachmentsInErrorError(false);
this.emptyToError(false);
2019-07-05 03:19:24 +08:00
if (0 < this.attachmentsInProcess().length) {
2016-09-10 06:38:16 +08:00
this.attachmentsInProcessError(true);
this.attachmentsPlace(true);
2019-07-05 03:19:24 +08:00
} else if (0 < this.attachmentsInError().length) {
2016-09-10 06:38:16 +08:00
this.attachmentsInErrorError(true);
this.attachmentsPlace(true);
}
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
if ('' === sTo && '' === sCc && '' === sBcc) {
2016-09-10 06:38:16 +08:00
this.emptyToError(true);
}
2019-07-05 03:19:24 +08:00
if (!this.emptyToError() && !this.attachmentsInErrorError() && !this.attachmentsInProcessError()) {
if (SettingsStore.replySameFolder()) {
if (
isArray(this.aDraftInfo) &&
3 === this.aDraftInfo.length &&
isNormal(this.aDraftInfo[2]) &&
0 < this.aDraftInfo[2].length
) {
2016-09-10 06:38:16 +08:00
sSentFolder = this.aDraftInfo[2];
}
}
2019-07-05 03:19:24 +08:00
if (!this.allowFolders) {
2016-09-10 06:38:16 +08:00
sSentFolder = UNUSED_OPTION_VALUE;
}
2015-04-10 16:17:49 +08:00
2019-07-05 03:19:24 +08:00
if ('' === sSentFolder) {
2016-09-10 06:38:16 +08:00
showScreenPopup(require('View/Popup/FolderSystem'), [SetSystemFoldersNotification.Sent]);
2019-07-05 03:19:24 +08:00
} else {
2016-09-10 06:38:16 +08:00
this.sendError(false);
this.sending(true);
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (isArray(this.aDraftInfo) && 3 === this.aDraftInfo.length) {
2016-09-10 06:38:16 +08:00
const flagsCache = getMessageFlagsFromCache(this.aDraftInfo[2], this.aDraftInfo[1]);
2019-07-05 03:19:24 +08:00
if (flagsCache) {
if ('forward' === this.aDraftInfo[0]) {
2016-09-10 06:38:16 +08:00
flagsCache[3] = true;
2019-07-05 03:19:24 +08:00
} else {
2016-09-10 06:38:16 +08:00
flagsCache[2] = true;
}
2016-09-10 06:38:16 +08:00
setMessageFlagsToCache(this.aDraftInfo[2], this.aDraftInfo[1], flagsCache);
getApp().reloadFlagsCurrentMessageListAndMessageFromCache();
setFolderHash(this.aDraftInfo[2], '');
}
}
sSentFolder = UNUSED_OPTION_VALUE === sSentFolder ? '' : sSentFolder;
2016-09-10 06:38:16 +08:00
setFolderHash(this.draftFolder(), '');
setFolderHash(sSentFolder, '');
Remote.sendMessage(
this.sendMessageResponse,
this.currentIdentity() ? this.currentIdentity().id() : '',
this.draftFolder(),
this.draftUid(),
2016-09-10 06:38:16 +08:00
sSentFolder,
sTo,
this.cc(),
this.bcc(),
this.replyTo(),
this.subject(),
this.oEditor ? this.oEditor.isHtml() : false,
this.oEditor ? this.oEditor.getData(true) : '',
this.prepearAttachmentsForSendOrSave(),
this.aDraftInfo,
this.sInReplyTo,
this.sReferences,
2016-09-10 06:38:16 +08:00
this.requestDsn(),
this.requestReadReceipt(),
this.markAsImportant()
);
}
2016-09-10 06:38:16 +08:00
}
}
2016-09-10 06:38:16 +08:00
@command((self) => self.canBeSentOrSaved())
saveCommand() {
2019-07-05 03:19:24 +08:00
if (!this.allowFolders) {
2016-09-10 06:38:16 +08:00
return false;
}
2019-07-05 03:19:24 +08:00
if (FolderStore.draftFolderNotEnabled()) {
2016-09-10 06:38:16 +08:00
showScreenPopup(require('View/Popup/FolderSystem'), [SetSystemFoldersNotification.Draft]);
2019-07-05 03:19:24 +08:00
} else {
2016-09-10 06:38:16 +08:00
this.savedError(false);
this.saving(true);
2014-07-10 22:44:45 +08:00
2016-09-10 06:38:16 +08:00
this.autosaveStart();
2014-12-28 03:48:55 +08:00
2016-09-10 06:38:16 +08:00
setFolderHash(FolderStore.draftFolder(), '');
2014-08-22 23:08:56 +08:00
2016-09-10 06:38:16 +08:00
Remote.saveMessage(
this.saveMessageResponse,
this.currentIdentity() ? this.currentIdentity().id() : '',
this.draftFolder(),
this.draftUid(),
FolderStore.draftFolder(),
this.to(),
this.cc(),
this.bcc(),
this.replyTo(),
this.subject(),
this.oEditor ? this.oEditor.isHtml() : false,
this.oEditor ? this.oEditor.getData(true) : '',
this.prepearAttachmentsForSendOrSave(),
this.aDraftInfo,
this.sInReplyTo,
this.sReferences,
this.markAsImportant()
);
}
2016-09-10 06:38:16 +08:00
return true;
}
2016-09-10 06:38:16 +08:00
@command((self) => self.isDraftFolderMessage())
deleteCommand() {
const PopupsAskViewModel = require('View/Popup/Ask');
2019-07-05 03:19:24 +08:00
if (!isPopupVisible(PopupsAskViewModel) && this.modalVisibility()) {
showScreenPopup(PopupsAskViewModel, [
i18n('POPUPS_ASK/DESC_WANT_DELETE_MESSAGES'),
() => {
if (this.modalVisibility()) {
getApp().deleteMessagesFromFolderWithoutCheck(this.draftFolder(), [this.draftUid()]);
hideScreenPopup(ComposePopupView);
}
2016-09-10 06:38:16 +08:00
}
2019-07-05 03:19:24 +08:00
]);
2016-09-10 06:38:16 +08:00
}
}
2014-12-28 03:48:55 +08:00
2016-09-10 06:38:16 +08:00
@command((self) => self.canBeSentOrSaved())
skipCommand() {
this.bSkipNextHide = true;
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
if (
this.modalVisibility() &&
!this.saving() &&
!this.sending() &&
!FolderStore.draftFolderNotEnabled() &&
SettingsStore.allowDraftAutosave()
) {
2016-09-10 06:38:16 +08:00
this.saveCommand();
}
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
this.tryToClosePopup();
}
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
@command((self) => self.allowContacts)
contactsCommand() {
2019-07-05 03:19:24 +08:00
if (this.allowContacts) {
2016-09-10 06:38:16 +08:00
this.skipCommand();
setTimeout(() => {
2016-09-10 06:38:16 +08:00
showScreenPopup(require('View/Popup/Contacts'), [true, this.sLastFocusedField]);
}, Magics.Time200ms);
}
}
2014-12-28 03:48:55 +08:00
autosaveFunction() {
2019-07-05 03:19:24 +08:00
if (
this.modalVisibility() &&
!FolderStore.draftFolderNotEnabled() &&
SettingsStore.allowDraftAutosave() &&
!this.isEmptyForm(false) &&
!this.saving() &&
!this.sending() &&
!this.savedError()
) {
this.saveCommand();
}
2016-06-30 08:02:45 +08:00
this.autosaveStart();
}
2016-06-30 08:02:45 +08:00
autosaveStart() {
window.clearTimeout(this.iTimer);
this.iTimer = window.setTimeout(this.autosaveFunction, Magics.Time1m);
}
2016-06-30 08:02:45 +08:00
autosaveStop() {
window.clearTimeout(this.iTimer);
2016-06-30 08:02:45 +08:00
}
2014-08-22 23:08:56 +08:00
emailsSource(oData, fResponse) {
getApp().getAutocomplete(oData.term, (aData) => {
fResponse(aData.map(oEmailItem => oEmailItem.toLine(false)));
});
}
2016-06-30 08:02:45 +08:00
openOpenPgpPopup() {
2019-07-05 03:19:24 +08:00
if (PgpStore.capaOpenPGP() && this.oEditor && !this.oEditor.isHtml()) {
showScreenPopup(require('View/Popup/ComposeOpenPgp'), [
(result) => {
this.editor((editor) => {
editor.setPlain(result);
});
},
this.oEditor.getData(false),
this.currentIdentity(),
this.to(),
this.cc(),
this.bcc()
]);
2014-08-21 23:08:34 +08:00
}
}
reloadDraftFolder() {
const draftFolder = FolderStore.draftFolder();
2019-07-05 03:19:24 +08:00
if ('' !== draftFolder && UNUSED_OPTION_VALUE !== draftFolder) {
setFolderHash(draftFolder, '');
2019-07-05 03:19:24 +08:00
if (FolderStore.currentFolderFullNameRaw() === draftFolder) {
getApp().reloadMessageList(true);
2019-07-05 03:19:24 +08:00
} else {
getApp().folderInformation(draftFolder);
}
2014-03-20 00:18:28 +08:00
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
findIdentityByMessage(composeType, message) {
2019-07-05 03:19:24 +08:00
let resultIndex = 1000,
resultIdentity = null;
2019-07-05 03:19:24 +08:00
const identities = IdentityStore.identities(),
identitiesCache = {},
fEachHelper = (item) => {
2019-07-05 03:19:24 +08:00
if (item && item.email && identitiesCache[item.email]) {
if (!resultIdentity || resultIndex > identitiesCache[item.email][1]) {
resultIdentity = identitiesCache[item.email][0];
resultIndex = identitiesCache[item.email][1];
}
2014-08-21 23:08:34 +08:00
}
};
2014-08-21 23:08:34 +08:00
identities.forEach((item, index) => {
identitiesCache[item.email()] = [item, index];
});
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (message) {
switch (composeType) {
case ComposeType.Empty:
break;
case ComposeType.Reply:
case ComposeType.ReplyAll:
case ComposeType.Forward:
case ComposeType.ForwardAsAttachment:
_.union(message.to, message.cc, message.bcc).forEach(fEachHelper);
if (!resultIdentity) {
message.deliveredTo.forEach(fEachHelper);
}
break;
case ComposeType.Draft:
_.union(message.from, message.replyTo).forEach(fEachHelper);
break;
// no default
}
2014-08-21 23:08:34 +08:00
}
return resultIdentity || identities[0] || null;
}
selectIdentity(identity) {
2019-07-05 03:19:24 +08:00
if (identity && identity.item) {
this.currentIdentity(identity.item);
this.setSignatureFromIdentity(identity.item);
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
sendMessageResponse(statusResult, data) {
2019-07-05 03:19:24 +08:00
let result = false,
message = '';
this.sending(false);
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === statusResult && data && data.Result) {
result = true;
2019-07-05 03:19:24 +08:00
if (this.modalVisibility()) {
delegateRun(this, 'closeCommand');
}
}
2019-07-05 03:19:24 +08:00
if (this.modalVisibility() && !result) {
if (data && Notification.CantSaveMessage === data.ErrorCode) {
this.sendSuccessButSaveError(true);
this.savedErrorDesc(trim(i18n('COMPOSE/SAVED_ERROR_ON_SEND')));
2019-07-05 03:19:24 +08:00
} else {
message = getNotification(
data && data.ErrorCode ? data.ErrorCode : Notification.CantSendMessage,
data && data.ErrorMessage ? data.ErrorMessage : ''
);
this.sendError(true);
this.sendErrorDesc(message || getNotification(Notification.CantSendMessage));
}
}
this.reloadDraftFolder();
2016-06-30 08:02:45 +08:00
}
2014-03-20 00:18:28 +08:00
saveMessageResponse(statusResult, oData) {
let result = false;
this.saving(false);
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === statusResult && oData && oData.Result) {
if (oData.Result.NewFolder && oData.Result.NewUid) {
result = true;
2019-07-05 03:19:24 +08:00
if (this.bFromDraft) {
const message = MessageStore.message();
2019-07-05 03:19:24 +08:00
if (message && this.draftFolder() === message.folderFullNameRaw && this.draftUid() === message.uid) {
MessageStore.message(null);
}
}
this.draftFolder(oData.Result.NewFolder);
this.draftUid(oData.Result.NewUid);
2019-07-05 03:19:24 +08:00
this.savedTime(window.Math.round(new window.Date().getTime() / 1000));
2019-07-05 03:19:24 +08:00
if (this.bFromDraft) {
setFolderHash(this.draftFolder(), '');
}
}
}
2019-07-05 03:19:24 +08:00
if (!result) {
this.savedError(true);
this.savedErrorDesc(getNotification(Notification.CantSaveMessage));
}
this.reloadDraftFolder();
}
2014-02-04 04:19:11 +08:00
onHide() {
this.autosaveStop();
2014-12-28 03:48:55 +08:00
2019-07-05 03:19:24 +08:00
if (!this.bSkipNextHide) {
AppStore.composeInEdit(false);
this.reset();
}
2014-12-28 03:48:55 +08:00
this.bSkipNextHide = false;
2014-12-28 03:48:55 +08:00
this.to.focused(false);
2015-03-21 06:22:06 +08:00
routeOn();
}
2014-08-21 23:08:34 +08:00
editor(fOnInit) {
2019-07-05 03:19:24 +08:00
if (fOnInit) {
if (!this.oEditor && this.composeEditorArea()) {
// setTimeout(() => {
2019-07-05 03:19:24 +08:00
this.oEditor = new HtmlEditor(
this.composeEditorArea(),
null,
() => {
fOnInit(this.oEditor);
this.resizerTrigger();
},
(bHtml) => {
this.isHtml(!!bHtml);
}
);
// }, 1000);
2019-07-05 03:19:24 +08:00
} else if (this.oEditor) {
fOnInit(this.oEditor);
this.resizerTrigger();
}
2016-06-30 08:02:45 +08:00
}
}
converSignature(signature) {
2019-07-05 03:19:24 +08:00
let limit = 10,
fromLine = '';
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
const moments = [],
momentRegx = /{{MOMENT:([^}]+)}}/g;
2015-01-28 06:16:00 +08:00
signature = signature.replace(/[\r]/g, '');
2014-08-21 23:08:34 +08:00
fromLine = this.oLastMessage ? this.emailArrayToStringLineHelper(this.oLastMessage.from, true) : '';
2019-07-05 03:19:24 +08:00
if ('' !== fromLine) {
signature = signature.replace(/{{FROM-FULL}}/g, fromLine);
2014-08-21 23:08:34 +08:00
2020-07-21 03:39:00 +08:00
if (!fromLine.includes(' ') && 0 < fromLine.indexOf('@')) {
fromLine = fromLine.replace(/@[\S]+/, '');
}
2014-08-21 23:08:34 +08:00
signature = signature.replace(/{{FROM}}/g, fromLine);
}
signature = signature.replace(/[\s]{1,2}{{FROM}}/g, '{{FROM}}');
signature = signature.replace(/[\s]{1,2}{{FROM-FULL}}/g, '{{FROM-FULL}}');
2014-08-14 21:09:42 +08:00
signature = signature.replace(/{{FROM}}/g, '');
signature = signature.replace(/{{FROM-FULL}}/g, '');
2016-06-30 08:02:45 +08:00
2020-07-21 03:39:00 +08:00
if (signature.includes('{{DATE}}')) {
signature = signature.replace(/{{DATE}}/g, momentorFormat(0, 'llll'));
}
2020-07-21 03:39:00 +08:00
if (signature.includes('{{TIME}}')) {
signature = signature.replace(/{{TIME}}/g, momentorFormat(0, 'LT'));
}
2020-07-21 03:39:00 +08:00
if (signature.includes('{{MOMENT:')) {
2019-07-05 03:19:24 +08:00
try {
let match = null;
2019-07-05 03:19:24 +08:00
while (null !== (match = momentRegx.exec(signature))) {
// eslint-disable-line no-cond-assign
if (match && match[0] && match[1]) {
moments.push([match[0], match[1]]);
}
limit -= 1;
2019-07-05 03:19:24 +08:00
if (0 === limit) {
break;
}
2015-02-05 02:26:33 +08:00
}
2019-07-05 03:19:24 +08:00
if (moments && 0 < moments.length) {
moments.forEach(data => {
signature = signature.replace(data[0], momentorFormat(0, data[1]));
});
}
signature = signature.replace(/{{MOMENT:[^}]+}}/g, '');
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-02-05 02:26:33 +08:00
}
return signature;
}
2014-08-21 23:08:34 +08:00
2017-07-06 06:31:41 +08:00
setSignatureFromIdentity(identity) {
2019-07-05 03:19:24 +08:00
if (identity) {
this.editor((editor) => {
2019-07-05 03:19:24 +08:00
let isHtml = false,
2016-08-31 05:31:51 +08:00
signature = identity.signature();
2016-07-16 05:29:42 +08:00
2019-07-05 03:19:24 +08:00
if ('' !== signature) {
if (':HTML:' === signature.substr(0, 6)) {
isHtml = true;
signature = signature.substr(6);
}
}
2017-07-06 06:31:41 +08:00
editor.setSignature(this.converSignature(signature), isHtml, !!identity.signatureInsertBefore());
});
}
2016-06-30 08:02:45 +08:00
}
/**
* @param {string=} type = ComposeType.Empty
* @param {?MessageModel|Array=} oMessageOrArray = null
* @param {Array=} aToEmails = null
* @param {Array=} aCcEmails = null
* @param {Array=} aBccEmails = null
* @param {string=} sCustomSubject = null
* @param {string=} sCustomPlainText = null
*/
onShow(type, oMessageOrArray, aToEmails, aCcEmails, aBccEmails, sCustomSubject, sCustomPlainText) {
routeOff();
2014-08-14 21:09:42 +08:00
this.autosaveStart();
2014-12-28 03:48:55 +08:00
2019-07-05 03:19:24 +08:00
if (AppStore.composeInEdit()) {
type = type || ComposeType.Empty;
2019-07-05 03:19:24 +08:00
if (ComposeType.Empty !== type) {
showScreenPopup(require('View/Popup/Ask'), [
i18n('COMPOSE/DISCARD_UNSAVED_DATA'),
() => {
this.initOnShow(type, oMessageOrArray, aToEmails, aCcEmails, aBccEmails, sCustomSubject, sCustomPlainText);
},
null,
null,
null,
false
]);
} else {
this.addEmailsTo(this.to, aToEmails);
this.addEmailsTo(this.cc, aCcEmails);
this.addEmailsTo(this.bcc, aBccEmails);
2019-07-05 03:19:24 +08:00
if (isNormal(sCustomSubject) && '' !== sCustomSubject && '' === this.subject()) {
this.subject(sCustomSubject);
}
}
2019-07-05 03:19:24 +08:00
} else {
this.initOnShow(type, oMessageOrArray, aToEmails, aCcEmails, aBccEmails, sCustomSubject, sCustomPlainText);
2014-12-28 03:48:55 +08:00
}
2016-06-30 08:02:45 +08:00
}
onWarmUp() {
2019-07-05 03:19:24 +08:00
if (this.modalVisibility && !this.modalVisibility()) {
this.editor((editor) => editor.modeToggle(false));
}
2016-06-30 08:02:45 +08:00
}
/**
* @param {Function} fKoValue
* @param {Array} emails
*/
addEmailsTo(fKoValue, emails) {
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(emails)) {
const value = trim(fKoValue()),
values = _.uniq(_.compact(emails.map(item => (item ? item.toLine(false) : null))));
fKoValue(value + ('' === value ? '' : ', ') + trim(values.join(', ')));
}
2016-06-30 08:02:45 +08:00
}
/**
*
* @param {Array} aList
* @param {boolean} bFriendly
* @returns {string}
*/
emailArrayToStringLineHelper(aList, bFriendly) {
bFriendly = !!bFriendly;
return aList.map(item => item.toLine(bFriendly)).join(', ');
2016-06-30 08:02:45 +08:00
}
2014-12-28 03:48:55 +08:00
/**
* @param {string=} sType = ComposeType.Empty
* @param {?MessageModel|Array=} oMessageOrArray = null
* @param {Array=} aToEmails = null
* @param {Array=} aCcEmails = null
* @param {Array=} aBccEmails = null
* @param {string=} sCustomSubject = null
* @param {string=} sCustomPlainText = null
*/
2019-07-05 03:19:24 +08:00
initOnShow(sType, oMessageOrArray, aToEmails, aCcEmails, aBccEmails, sCustomSubject, sCustomPlainText) {
AppStore.composeInEdit(true);
2019-07-05 03:19:24 +08:00
let sFrom = '',
sTo = '',
sCc = '',
sDate = '',
sSubject = '',
sText = '',
sReplyTitle = '',
identity = null,
aDraftInfo = null,
message = null;
2019-07-05 03:19:24 +08:00
const excludeEmail = {},
mEmail = AccountStore.email(),
lineComposeType = sType || ComposeType.Empty;
oMessageOrArray = oMessageOrArray || null;
2019-07-05 03:19:24 +08:00
if (oMessageOrArray && isNormal(oMessageOrArray)) {
message =
isArray(oMessageOrArray) && 1 === oMessageOrArray.length
? oMessageOrArray[0]
: !isArray(oMessageOrArray)
? oMessageOrArray
: null;
}
2014-08-21 23:08:34 +08:00
this.oLastMessage = message;
2019-07-05 03:19:24 +08:00
if (null !== mEmail) {
excludeEmail[mEmail] = true;
}
this.reset();
identity = this.findIdentityByMessage(lineComposeType, message);
2019-07-05 03:19:24 +08:00
if (identity) {
excludeEmail[identity.email()] = true;
2014-08-21 23:08:34 +08:00
}
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(aToEmails)) {
this.to(this.emailArrayToStringLineHelper(aToEmails));
}
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(aCcEmails)) {
this.cc(this.emailArrayToStringLineHelper(aCcEmails));
}
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(aBccEmails)) {
this.bcc(this.emailArrayToStringLineHelper(aBccEmails));
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if ('' !== lineComposeType && message) {
sDate = momentorFormat(message.dateTimeStampInUTC(), 'FULL');
sSubject = message.subject();
aDraftInfo = message.aDraftInfo;
2015-04-17 21:56:29 +08:00
const clonedText = $(message.body).clone();
2019-07-05 03:19:24 +08:00
if (clonedText) {
clearBqSwitcher(clonedText);
sText = clonedText.html();
2015-04-07 03:32:19 +08:00
}
2014-08-21 23:08:34 +08:00
let resplyAllParts = null;
2019-07-05 03:19:24 +08:00
switch (lineComposeType) {
case ComposeType.Empty:
break;
case ComposeType.Reply:
this.to(this.emailArrayToStringLineHelper(message.replyEmails(excludeEmail)));
this.subject(replySubjectAdd('Re', sSubject));
this.prepearMessageAttachments(message, lineComposeType);
this.aDraftInfo = ['reply', message.uid, message.folderFullNameRaw];
this.sInReplyTo = message.sMessageId;
this.sReferences = trim(this.sInReplyTo + ' ' + message.sReferences);
break;
case ComposeType.ReplyAll:
resplyAllParts = message.replyAllEmails(excludeEmail);
this.to(this.emailArrayToStringLineHelper(resplyAllParts[0]));
this.cc(this.emailArrayToStringLineHelper(resplyAllParts[1]));
this.subject(replySubjectAdd('Re', sSubject));
this.prepearMessageAttachments(message, lineComposeType);
this.aDraftInfo = ['reply', message.uid, message.folderFullNameRaw];
this.sInReplyTo = message.sMessageId;
this.sReferences = trim(this.sInReplyTo + ' ' + message.references());
break;
case ComposeType.Forward:
this.subject(replySubjectAdd('Fwd', sSubject));
this.prepearMessageAttachments(message, lineComposeType);
this.aDraftInfo = ['forward', message.uid, message.folderFullNameRaw];
this.sInReplyTo = message.sMessageId;
this.sReferences = trim(this.sInReplyTo + ' ' + message.sReferences);
break;
case ComposeType.ForwardAsAttachment:
this.subject(replySubjectAdd('Fwd', sSubject));
this.prepearMessageAttachments(message, lineComposeType);
this.aDraftInfo = ['forward', message.uid, message.folderFullNameRaw];
this.sInReplyTo = message.sMessageId;
this.sReferences = trim(this.sInReplyTo + ' ' + message.sReferences);
break;
case ComposeType.Draft:
this.to(this.emailArrayToStringLineHelper(message.to));
this.cc(this.emailArrayToStringLineHelper(message.cc));
this.bcc(this.emailArrayToStringLineHelper(message.bcc));
this.replyTo(this.emailArrayToStringLineHelper(message.replyTo));
this.bFromDraft = true;
2014-08-12 20:32:08 +08:00
this.draftFolder(message.folderFullNameRaw);
this.draftUid(message.uid);
this.subject(sSubject);
this.prepearMessageAttachments(message, lineComposeType);
this.aDraftInfo = isNonEmptyArray(aDraftInfo) && 3 === aDraftInfo.length ? aDraftInfo : null;
this.sInReplyTo = message.sInReplyTo;
this.sReferences = message.sReferences;
break;
case ComposeType.EditAsNew:
this.to(this.emailArrayToStringLineHelper(message.to));
this.cc(this.emailArrayToStringLineHelper(message.cc));
this.bcc(this.emailArrayToStringLineHelper(message.bcc));
this.replyTo(this.emailArrayToStringLineHelper(message.replyTo));
this.subject(sSubject);
this.prepearMessageAttachments(message, lineComposeType);
this.aDraftInfo = isNonEmptyArray(aDraftInfo) && 3 === aDraftInfo.length ? aDraftInfo : null;
this.sInReplyTo = message.sInReplyTo;
this.sReferences = message.sReferences;
break;
// no default
2014-08-21 23:08:34 +08:00
}
2019-07-05 03:19:24 +08:00
switch (lineComposeType) {
case ComposeType.Reply:
case ComposeType.ReplyAll:
sFrom = message.fromToLine(false, true);
sReplyTitle = i18n('COMPOSE/REPLY_MESSAGE_TITLE', {
'DATETIME': sDate,
'EMAIL': sFrom
});
2019-07-05 03:19:24 +08:00
sText = '<br /><br />' + sReplyTitle + ':' + '<br /><br />' + '<blockquote>' + trim(sText) + '</blockquote>';
break;
case ComposeType.Forward:
sFrom = message.fromToLine(false, true);
sTo = message.toToLine(false, true);
sCc = message.ccToLine(false, true);
2019-07-05 03:19:24 +08:00
sText =
'<br /><br />' +
i18n('COMPOSE/FORWARD_MESSAGE_TOP_TITLE') +
'<br />' +
i18n('COMPOSE/FORWARD_MESSAGE_TOP_FROM') +
': ' +
sFrom +
'<br />' +
i18n('COMPOSE/FORWARD_MESSAGE_TOP_TO') +
': ' +
sTo +
(0 < sCc.length ? '<br />' + i18n('COMPOSE/FORWARD_MESSAGE_TOP_CC') + ': ' + sCc : '') +
'<br />' +
i18n('COMPOSE/FORWARD_MESSAGE_TOP_SENT') +
': ' +
encodeHtml(sDate) +
'<br />' +
i18n('COMPOSE/FORWARD_MESSAGE_TOP_SUBJECT') +
': ' +
encodeHtml(sSubject) +
'<br /><br />' +
trim(sText) +
'<br /><br />';
break;
case ComposeType.ForwardAsAttachment:
sText = '';
break;
// no default
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
this.editor((editor) => {
editor.setHtml(sText, false);
2014-08-12 21:51:34 +08:00
2019-07-05 03:19:24 +08:00
if (
EditorDefaultType.PlainForced === this.editorDefaultType() ||
(!message.isHtml() && EditorDefaultType.HtmlForced !== this.editorDefaultType())
) {
editor.modeToggle(false);
}
2019-07-05 03:19:24 +08:00
if (identity && ComposeType.Draft !== lineComposeType && ComposeType.EditAsNew !== lineComposeType) {
2017-07-06 06:31:41 +08:00
this.setSignatureFromIdentity(identity);
}
this.setFocusInPopup();
});
2019-07-05 03:19:24 +08:00
} else if (ComposeType.Empty === lineComposeType) {
this.subject(isNormal(sCustomSubject) ? '' + sCustomSubject : '');
sText = isNormal(sCustomPlainText) ? '' + sCustomPlainText : '';
this.editor((editor) => {
editor.setHtml(sText, false);
2019-07-05 03:19:24 +08:00
if (
EditorDefaultType.Html !== this.editorDefaultType() &&
EditorDefaultType.HtmlForced !== this.editorDefaultType()
) {
editor.modeToggle(false);
}
2019-07-05 03:19:24 +08:00
if (identity) {
2017-07-06 06:31:41 +08:00
this.setSignatureFromIdentity(identity);
}
this.setFocusInPopup();
2016-08-10 01:27:04 +08:00
});
2019-07-05 03:19:24 +08:00
} else if (isNonEmptyArray(oMessageOrArray)) {
oMessageOrArray.forEach(item => {
this.addMessageAsAttachment(item);
});
this.editor((editor) => {
editor.setHtml('', false);
2019-07-05 03:19:24 +08:00
if (
EditorDefaultType.Html !== this.editorDefaultType() &&
EditorDefaultType.HtmlForced !== this.editorDefaultType()
) {
editor.modeToggle(false);
}
2015-02-26 22:47:37 +08:00
2019-07-05 03:19:24 +08:00
if (identity && ComposeType.Draft !== lineComposeType && ComposeType.EditAsNew !== lineComposeType) {
2017-07-06 06:31:41 +08:00
this.setSignatureFromIdentity(identity);
}
this.setFocusInPopup();
});
2019-07-05 03:19:24 +08:00
} else {
this.setFocusInPopup();
}
const downloads = this.getAttachmentsDownloadsForUpload();
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(downloads)) {
Remote.messageUploadAttachments(this.onMessageUploadAttachments, downloads);
}
2015-02-26 22:47:37 +08:00
2019-07-05 03:19:24 +08:00
if (identity) {
this.currentIdentity(identity);
}
this.resizerTrigger();
}
onMessageUploadAttachments(sResult, oData) {
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === sResult && oData && oData.Result) {
if (!this.viewModelVisibility()) {
oData.Result.forEach((id, tempName) => {
const attachment = this.getAttachmentById(id);
2019-07-05 03:19:24 +08:00
if (attachment) {
attachment.tempName(tempName);
2019-07-05 03:19:24 +08:00
attachment
.waiting(false)
.uploading(false)
.complete(true);
}
});
}
2019-07-05 03:19:24 +08:00
} else {
this.setMessageAttachmentFailedDownloadText();
2015-03-21 06:22:06 +08:00
}
2016-06-30 08:02:45 +08:00
}
setFocusInPopup() {
2019-07-05 03:19:24 +08:00
if (!bMobileDevice) {
setTimeout(() => {
2019-07-05 03:19:24 +08:00
if ('' === this.to()) {
this.to.focused(true);
2019-07-05 03:19:24 +08:00
} else if (this.oEditor) {
if (!this.to.focused()) {
this.oEditor.focus();
}
}
}, Magics.Time100ms);
}
}
2016-06-30 08:02:45 +08:00
onShowWithDelay() {
this.resizerTrigger();
}
tryToClosePopup() {
const PopupsAskViewModel = require('View/Popup/Ask');
2019-07-05 03:19:24 +08:00
if (!isPopupVisible(PopupsAskViewModel) && this.modalVisibility()) {
if (this.bSkipNextHide || (this.isEmptyForm() && !this.draftUid())) {
delegateRun(this, 'closeCommand');
2019-07-05 03:19:24 +08:00
} else {
showScreenPopup(PopupsAskViewModel, [
i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'),
() => {
if (this.modalVisibility()) {
delegateRun(this, 'closeCommand');
}
}
2019-07-05 03:19:24 +08:00
]);
}
}
}
2016-07-01 06:50:11 +08:00
onBuild() {
this.initUploader();
2014-08-21 23:08:34 +08:00
key('ctrl+q, command+q, ctrl+w, command+w', KeyState.Compose, noopFalse);
2016-06-30 08:02:45 +08:00
key('`', KeyState.Compose, () => {
2019-07-05 03:19:24 +08:00
if (this.oEditor && !this.oEditor.hasFocus() && !inFocus()) {
this.identitiesDropdownTrigger(true);
return false;
}
return true;
});
key('ctrl+`', KeyState.Compose, () => {
this.identitiesDropdownTrigger(true);
2016-06-30 08:02:45 +08:00
return false;
});
key('esc, ctrl+down, command+down', KeyState.Compose, () => {
this.skipCommand();
2016-06-30 08:02:45 +08:00
return false;
});
2015-03-21 06:22:06 +08:00
2019-07-05 03:19:24 +08:00
if (this.allowFolders) {
key('ctrl+s, command+s', KeyState.Compose, () => {
this.saveCommand();
return false;
});
2014-08-21 23:08:34 +08:00
}
2019-07-05 03:19:24 +08:00
if (Settings.appSettingsGet('allowCtrlEnterOnCompose')) {
key('ctrl+enter, command+enter', KeyState.Compose, () => {
this.sendCommand();
return false;
});
}
key('shift+esc', KeyState.Compose, () => {
2019-07-05 03:19:24 +08:00
if (this.modalVisibility()) {
this.tryToClosePopup();
}
return false;
});
Events.sub('window.resize.real', this.resizerTrigger);
Events.sub('window.resize.real', _.debounce(this.resizerTrigger, Magics.Time50ms));
window.setInterval(() => {
2019-07-05 03:19:24 +08:00
if (this.modalVisibility() && this.oEditor) {
this.oEditor.resize();
}
}, Magics.Time5s);
}
/**
* @param {string} id
* @returns {?Object}
*/
getAttachmentById(id) {
return this.attachments().find(item => item && id === item.id);
}
2014-10-04 19:58:01 +08:00
cancelAttachmentHelper(id, oJua) {
return () => {
const attachment = this.attachments().find(item => item && item.id === id);
2019-07-05 03:19:24 +08:00
if (attachment) {
this.attachments.remove(attachment);
delegateRunOnDestroy(attachment);
2014-10-04 19:58:01 +08:00
2019-07-05 03:19:24 +08:00
if (oJua) {
oJua.cancel(id);
}
}
};
}
2014-10-04 19:58:01 +08:00
initUploader() {
2019-07-05 03:19:24 +08:00
if (this.composeUploaderButton()) {
const uploadCache = {},
attachmentSizeLimit = pInt(Settings.settingsGet('AttachmentLimit')),
oJua = new Jua({
'action': upload(),
'name': 'uploader',
'queueSize': 2,
'multipleSizeLimit': 50,
'clickElement': this.composeUploaderButton(),
'dragAndDropElement': this.composeUploaderDropPlace()
});
2014-10-04 19:58:01 +08:00
2019-07-05 03:19:24 +08:00
if (oJua) {
oJua
// .on('onLimitReached', (limit) => {
// alert(limit);
// })
.on('onDragEnter', () => {
this.dragAndDropOver(true);
})
.on('onDragLeave', () => {
this.dragAndDropOver(false);
})
.on('onBodyDragEnter', () => {
this.attachmentsPlace(true);
this.dragAndDropVisible(true);
})
.on('onBodyDragLeave', () => {
this.dragAndDropVisible(false);
})
.on('onProgress', (id, loaded, total) => {
let item = uploadCache[id];
2019-07-05 03:19:24 +08:00
if (!item) {
item = this.getAttachmentById(id);
2019-07-05 03:19:24 +08:00
if (item) {
uploadCache[id] = item;
}
}
2019-07-05 03:19:24 +08:00
if (item) {
item.progress(window.Math.floor((loaded / total) * 100));
}
})
.on('onSelect', (sId, oData) => {
this.dragAndDropOver(false);
2019-07-05 03:19:24 +08:00
const fileName = isUnd(oData.FileName) ? '' : oData.FileName.toString(),
size = isNormal(oData.Size) ? pInt(oData.Size) : null,
attachment = new ComposeAttachmentModel(sId, fileName, size);
attachment.cancel = this.cancelAttachmentHelper(sId, oJua);
this.attachments.push(attachment);
this.attachmentsPlace(true);
2019-07-05 03:19:24 +08:00
if (0 < size && 0 < attachmentSizeLimit && attachmentSizeLimit < size) {
attachment
2019-07-05 03:19:24 +08:00
.waiting(false)
.uploading(true)
.complete(true)
.error(i18n('UPLOAD/ERROR_FILE_IS_TOO_BIG'));
return false;
}
return true;
})
.on('onStart', (id) => {
let item = uploadCache[id];
2019-07-05 03:19:24 +08:00
if (!item) {
item = this.getAttachmentById(id);
2019-07-05 03:19:24 +08:00
if (item) {
uploadCache[id] = item;
}
}
2019-07-05 03:19:24 +08:00
if (item) {
item
.waiting(false)
.uploading(true)
.complete(false);
}
})
.on('onComplete', (id, result, data) => {
2019-07-05 03:19:24 +08:00
const attachment = this.getAttachmentById(id),
errorCode = data && data.Result && data.Result.ErrorCode ? data.Result.ErrorCode : null,
attachmentJson = result && data && data.Result && data.Result.Attachment ? data.Result.Attachment : null;
let error = '';
2019-07-05 03:19:24 +08:00
if (null !== errorCode) {
error = getUploadErrorDescByCode(errorCode);
2019-07-05 03:19:24 +08:00
} else if (!attachmentJson) {
error = i18n('UPLOAD/ERROR_UNKNOWN');
}
2019-07-05 03:19:24 +08:00
if (attachment) {
if ('' !== error && 0 < error.length) {
attachment
.waiting(false)
.uploading(false)
.complete(true)
.error(error);
2019-07-05 03:19:24 +08:00
} else if (attachmentJson) {
attachment
.waiting(false)
.uploading(false)
.complete(true);
attachment.initByUploadJson(attachmentJson);
}
2019-07-05 03:19:24 +08:00
if (isUnd(uploadCache[id])) {
delete uploadCache[id];
}
}
});
2019-07-05 03:19:24 +08:00
this.addAttachmentEnabled(true).dragAndDropEnabled(oJua.isDragAndDropSupported());
} else {
this.addAttachmentEnabled(false).dragAndDropEnabled(false);
}
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
}
/**
* @returns {Object}
*/
prepearAttachmentsForSendOrSave() {
const result = {};
this.attachmentsInReady().forEach(item => {
2019-07-05 03:19:24 +08:00
if (item && '' !== item.tempName() && item.enabled()) {
result[item.tempName()] = [item.fileName(), item.isInline ? '1' : '0', item.CID, item.contentLocation];
}
});
2016-06-30 08:02:45 +08:00
return result;
}
2014-08-21 23:08:34 +08:00
/**
* @param {MessageModel} message
*/
addMessageAsAttachment(message) {
2019-07-05 03:19:24 +08:00
if (message) {
let temp = message.subject();
temp = '.eml' === temp.substr(-4).toLowerCase() ? temp : temp + '.eml';
2016-08-10 03:52:30 +08:00
2019-07-05 03:19:24 +08:00
const attachment = new ComposeAttachmentModel(message.requestHash, temp, message.size());
attachment.fromMessage = true;
attachment.cancel = this.cancelAttachmentHelper(message.requestHash);
2019-07-05 03:19:24 +08:00
attachment
.waiting(false)
.uploading(true)
.complete(true);
this.attachments.push(attachment);
}
2016-06-30 08:02:45 +08:00
}
/**
* @param {string} url
* @param {string} name
* @param {number} size
* @returns {ComposeAttachmentModel}
*/
addAttachmentHelper(url, name, size) {
const attachment = new ComposeAttachmentModel(url, name, size);
attachment.fromMessage = false;
attachment.cancel = this.cancelAttachmentHelper(url);
2019-07-05 03:19:24 +08:00
attachment
.waiting(false)
.uploading(true)
.complete(false);
2014-07-29 18:28:02 +08:00
this.attachments.push(attachment);
2014-07-29 18:28:02 +08:00
this.attachmentsPlace(true);
2016-08-10 02:58:34 +08:00
return attachment;
2016-06-30 08:02:45 +08:00
}
/**
* @param {MessageModel} message
* @param {string} type
*/
prepearMessageAttachments(message, type) {
2019-07-05 03:19:24 +08:00
if (message) {
if (ComposeType.ForwardAsAttachment === type) {
this.addMessageAsAttachment(message);
2019-07-05 03:19:24 +08:00
} else {
const attachments = message.attachments();
(isNonEmptyArray(attachments) ? attachments : []).forEach(item => {
let add = false;
2019-07-05 03:19:24 +08:00
switch (type) {
case ComposeType.Reply:
case ComposeType.ReplyAll:
add = item.isLinked;
break;
2016-06-30 08:02:45 +08:00
case ComposeType.Forward:
case ComposeType.Draft:
case ComposeType.EditAsNew:
add = true;
break;
// no default
}
2019-07-05 03:19:24 +08:00
if (add) {
const attachment = new ComposeAttachmentModel(
2019-07-05 03:19:24 +08:00
item.download,
item.fileName,
item.estimatedSize,
item.isInline,
item.isLinked,
item.cid,
item.contentLocation
);
2014-08-21 23:08:34 +08:00
attachment.fromMessage = true;
attachment.cancel = this.cancelAttachmentHelper(item.download);
2019-07-05 03:19:24 +08:00
attachment
.waiting(false)
.uploading(true)
.complete(false);
2014-08-21 23:08:34 +08:00
this.attachments.push(attachment);
}
});
}
2016-06-30 08:02:45 +08:00
}
}
removeLinkedAttachments() {
const arrachment = this.attachments().find(item => item && item.isLinked);
2019-07-05 03:19:24 +08:00
if (arrachment) {
this.attachments.remove(arrachment);
delegateRunOnDestroy(arrachment);
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
setMessageAttachmentFailedDownloadText() {
this.attachments().forEach(attachment => {
2019-07-05 03:19:24 +08:00
if (attachment && attachment.fromMessage) {
attachment
.waiting(false)
.uploading(false)
.complete(true)
.error(getUploadErrorDescByCode(UploadErrorCode.FileNoUploaded));
}
});
}
2014-08-21 23:08:34 +08:00
/**
* @param {boolean=} includeAttachmentInProgress = true
* @returns {boolean}
*/
isEmptyForm(includeAttachmentInProgress = true) {
2019-07-05 03:19:24 +08:00
const withoutAttachment = includeAttachmentInProgress
? 0 === this.attachments().length
: 0 === this.attachmentsInReady().length;
2019-07-05 03:19:24 +08:00
return (
0 === this.to().length &&
0 === this.cc().length &&
0 === this.bcc().length &&
0 === this.replyTo().length &&
0 === this.subject().length &&
withoutAttachment &&
2019-07-05 03:19:24 +08:00
(!this.oEditor || '' === this.oEditor.getData())
);
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
reset() {
this.to('');
this.cc('');
this.bcc('');
this.replyTo('');
this.subject('');
2016-06-30 08:02:45 +08:00
this.requestDsn(false);
this.requestReadReceipt(false);
this.markAsImportant(false);
2016-06-30 08:02:45 +08:00
this.attachmentsPlace(false);
2016-06-30 08:02:45 +08:00
this.aDraftInfo = null;
this.sInReplyTo = '';
this.bFromDraft = false;
this.sReferences = '';
2016-06-30 08:02:45 +08:00
this.sendError(false);
this.sendSuccessButSaveError(false);
this.savedError(false);
this.savedTime(0);
this.emptyToError(false);
this.attachmentsInProcessError(false);
2016-06-30 08:02:45 +08:00
this.showCc(false);
this.showBcc(false);
this.showReplyTo(false);
2016-06-30 08:02:45 +08:00
delegateRunOnDestroy(this.attachments());
this.attachments([]);
2016-06-30 08:02:45 +08:00
this.dragAndDropOver(false);
this.dragAndDropVisible(false);
2016-06-30 08:02:45 +08:00
this.draftFolder('');
this.draftUid('');
2016-06-30 08:02:45 +08:00
this.sending(false);
this.saving(false);
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
if (this.oEditor) {
this.oEditor.clear(false);
}
2016-06-30 08:02:45 +08:00
}
/**
* @returns {Array}
*/
getAttachmentsDownloadsForUpload() {
return this.attachments().filter(item => item && '' === item.tempName()).map(
item => item.id
2019-12-25 03:05:46 +08:00
);
}
resizerTrigger() {
this.resizer(!this.resizer());
}
}
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
export { ComposePopupView, ComposePopupView as default };