Use JavaScript Optional chaining

This commit is contained in:
the-djmaze 2022-09-02 11:52:07 +02:00
parent d9bc435e2c
commit 732b6eb641
55 changed files with 169 additions and 199 deletions

View file

@ -38,7 +38,7 @@ export class AccountPopupView extends AbstractViewPopup {
this.submitRequest(false);
if (iError) {
this.submitError(getNotification(iError));
this.submitErrorAdditional((data && data.ErrorMessageAdditional) || '');
this.submitErrorAdditional(data?.ErrorMessageAdditional);
} else {
rl.app.accountsAndIdentities();
this.close();
@ -54,7 +54,7 @@ export class AccountPopupView extends AbstractViewPopup {
}
onShow(account) {
if (account && account.isAdditional()) {
if (account?.isAdditional()) {
this.isNew(false);
this.email(account.email);
} else {

View file

@ -213,8 +213,8 @@ export class ComposePopupView extends AbstractViewPopup {
super('Compose');
const fEmailOutInHelper = (context, identity, name, isIn) => {
if (identity && context && identity[name]() && (isIn ? true : context[name]())) {
const identityEmail = identity[name]();
const identityEmail = context && identity?.[name]();
if (identityEmail && (isIn ? true : context[name]())) {
let list = context[name]().trim().split(',');
list = list.filter(email => {
@ -222,9 +222,7 @@ export class ComposePopupView extends AbstractViewPopup {
return email && identityEmail.trim() !== email;
});
if (isIn) {
list.push(identityEmail);
}
isIn && list.push(identityEmail);
context[name](list.join(','));
}
@ -355,7 +353,7 @@ export class ComposePopupView extends AbstractViewPopup {
},
attachmentsInProcess: () => this.attachments.filter(item => item && !item.complete()),
attachmentsInError: () => this.attachments.filter(item => item && item.error()),
attachmentsInError: () => this.attachments.filter(item => item?.error()),
attachmentsCount: () => this.attachments.length,
attachmentsInErrorCount: () => this.attachmentsInError.length,
@ -511,7 +509,7 @@ export class ComposePopupView extends AbstractViewPopup {
alternative.children.push(data);
data = alternative;
}
if (sign && !draft && sign[1]) {
if (!draft && sign?.[1]) {
if ('openpgp' == sign[0]) {
// Doesn't sign attachments
params.Html = params.Text = '';
@ -611,7 +609,7 @@ export class ComposePopupView extends AbstractViewPopup {
this.savedErrorDesc(i18n('COMPOSE/SAVED_ERROR_ON_SEND').trim());
} else {
this.sendError(true);
this.sendErrorDesc(getNotification(iError, data && data.ErrorMessage)
this.sendErrorDesc(getNotification(iError, data?.ErrorMessage)
|| getNotification(Notification.CantSendMessage));
}
} else {
@ -762,7 +760,7 @@ export class ComposePopupView extends AbstractViewPopup {
(iError, data) => {
if (!iError && isArray(data.Result)) {
fResponse(
data.Result.map(item => (item && item[0] ? (new EmailModel(item[0], item[1])).toLine() : null))
data.Result.map(item => (item?.[0] ? (new EmailModel(item[0], item[1])).toLine() : null))
.filter(v => v)
);
} else if (Notification.RequestAborted !== iError) {
@ -777,7 +775,7 @@ export class ComposePopupView extends AbstractViewPopup {
}
selectIdentity(identity) {
identity = identity && identity.item;
identity = identity?.item;
if (identity) {
this.currentIdentity(identity);
this.setSignatureFromIdentity(identity);
@ -1153,7 +1151,7 @@ export class ComposePopupView extends AbstractViewPopup {
});
} else {
this.attachments.forEach(attachment => {
if (attachment && attachment.fromMessage) {
if (attachment?.fromMessage) {
attachment
.waiting(false)
.uploading(false)
@ -1180,7 +1178,7 @@ export class ComposePopupView extends AbstractViewPopup {
if (!this.to()) {
this.to.focused(true);
} else if (!this.to.focused()) {
this.oEditor && this.oEditor.focus();
this.oEditor?.focus();
}
}, 100);
}
@ -1266,7 +1264,7 @@ export class ComposePopupView extends AbstractViewPopup {
})
.on('onComplete', (id, result, data) => {
const attachment = this.getAttachmentById(id),
response = (data && data.Result) || {},
response = data?.Result || {},
errorCode = response.ErrorCode,
attachmentJson = result && response.Attachment;
@ -1354,7 +1352,7 @@ export class ComposePopupView extends AbstractViewPopup {
prepareAttachmentsForSendOrSave() {
const result = {};
this.attachments.forEach(item => {
if (item && item.complete() && item.tempName() && item.enabled()) {
if (item?.complete() && item?.tempName() && item?.enabled()) {
result[item.tempName()] = [item.fileName(), item.isInline ? '1' : '0', item.CID, item.contentLocation];
}
});
@ -1381,7 +1379,7 @@ export class ComposePopupView extends AbstractViewPopup {
oJua || attachment.waiting(false).uploading(true);
attachment.cancel = () => {
this.attachments.remove(attachment);
oJua && oJua.cancel(attachment.id);
oJua?.cancel(attachment.id);
};
this.attachments.push(attachment);
view && this.attachmentsArea();
@ -1435,7 +1433,7 @@ export class ComposePopupView extends AbstractViewPopup {
isEmptyForm(includeAttachmentInProgress = true) {
const withoutAttachment = includeAttachmentInProgress
? !this.attachments.length
: !this.attachments.some(item => item && item.complete());
: !this.attachments.some(item => item?.complete());
return (
!this.to.length &&
@ -1491,7 +1489,7 @@ export class ComposePopupView extends AbstractViewPopup {
this.sending(false);
this.saving(false);
this.oEditor && this.oEditor.clear();
this.oEditor?.clear();
this.dropMailvelope();
}

View file

@ -108,7 +108,7 @@ export class ContactsPopupView extends AbstractViewPopup {
contactHasValidName: () => !!this.viewProperties.find(prop => propertyIsName(prop) && prop.isValid()),
contactsCheckedOrSelected: () => {
const checked = ContactUserStore.filter(item => item.checked && item.checked()),
const checked = ContactUserStore.filter(item => item.checked?.()),
selected = this.currentContact();
return selected
@ -120,7 +120,7 @@ export class ContactsPopupView extends AbstractViewPopup {
contactsSyncEnabled: () => ContactUserStore.allowSync() && ContactUserStore.syncMode(),
viewHash: () => '' + this.viewProperties.map(property => property.value && property.value()).join('')
viewHash: () => '' + this.viewProperties.map(property => property.value?.()).join('')
});
this.search.subscribe(() => this.reloadContactList());
@ -164,7 +164,7 @@ export class ContactsPopupView extends AbstractViewPopup {
const data = oItem.getNameAndEmailHelper(),
email = data ? new EmailModel(data[0], data[1]) : null;
if (email && email.validate()) {
if (email?.validate()) {
return email;
}
}
@ -344,7 +344,7 @@ export class ContactsPopupView extends AbstractViewPopup {
if (this.contactsCheckedOrSelected().length) {
Remote.request('ContactsDelete',
(iError, oData) => {
if (500 < (!iError && oData && oData.Time ? pInt(oData.Time) : 0)) {
if (500 < (!iError && oData?.Time) ? pInt(oData.Time) : 0) {
this.reloadContactList(this.bDropPageAfterDelete);
} else {
setTimeout(() => this.reloadContactList(this.bDropPageAfterDelete), 500);

View file

@ -248,7 +248,7 @@ export class DomainPopupView extends AbstractViewPopup {
if (oDomain) {
this.enableSmartPorts(false);
this.edit(true);
forEachObjectEntry(oDomain, (key, value) => this[key] && this[key](value));
forEachObjectEntry(oDomain, (key, value) => this[key]?.(value));
this.enableSmartPorts(true);
}
}

View file

@ -63,9 +63,7 @@ export class IdentityPopupView extends AbstractViewPopup {
submitForm() {
if (!this.submitRequest()) {
if (this.signature && this.signature.__fetchEditorValue) {
this.signature.__fetchEditorValue();
}
this.signature?.__fetchEditorValue?.();
if (!this.emailHasError()) {
this.emailHasError(!this.email().trim());

View file

@ -51,7 +51,7 @@ export class LanguagesPopupView extends AbstractViewPopup {
}
changeLanguage(lang) {
this.fLang && this.fLang(lang);
this.fLang?.(lang);
this.close();
}
}

View file

@ -94,7 +94,7 @@ export class OpenPgpGeneratePopupView extends AbstractViewPopup {
showError(e) {
console.log(e);
if (e && e.message) {
if (e?.message) {
this.submitError(e.message);
}
}

View file

@ -114,9 +114,9 @@ export class LoginUserView extends AbstractViewLogin {
if (Notification.InvalidInputArgument == iError) {
iError = Notification.AuthError;
}
this.submitError(getNotification(iError, (oData ? oData.ErrorMessage : ''),
this.submitError(getNotification(iError, oData?.ErrorMessage,
Notification.UnknownNotification));
this.submitErrorAdditional((oData && oData.ErrorMessageAdditional) || '');
this.submitErrorAdditional(oData?.ErrorMessageAdditional);
} else {
rl.setData(oData.Result);
}

View file

@ -82,7 +82,7 @@ export class MailFolderList extends AbstractViewLeft {
}
el = eqs(event, 'a');
if (el && el.matches('.selectable')) {
if (el?.matches('.selectable')) {
event.preventDefault();
const folder = ko.dataFor(el);
if (folder) {
@ -170,10 +170,10 @@ export class MailFolderList extends AbstractViewLeft {
AppUserStore.focusedState.subscribe(value => {
let el = qs('li a.focused');
el && el.classList.remove('focused');
el?.classList.remove('focused');
if (Scope.FolderList === value) {
el = qs('li a.selected');
el && el.classList.add('focused');
el?.classList.add('focused');
}
});
}

View file

@ -210,7 +210,7 @@ export class MailMessageList extends AbstractViewRight {
const sFolder = e.detail.Folder, iUid = e.detail.Uid;
const message = MessagelistUserStore.find(
item => item && sFolder === item.folder && iUid == item.uid
item => sFolder === item?.folder && iUid == item?.uid
);
if ('INBOX' === sFolder) {
@ -326,7 +326,7 @@ export class MailMessageList extends AbstractViewRight {
moveNewCommand(vm, event) {
if (this.newMoveToFolder && this.mobileCheckedStateShow()) {
if (vm && event && event.preventDefault) {
if (vm && event?.preventDefault) {
event.preventDefault();
event.stopPropagation();
}
@ -428,7 +428,7 @@ export class MailMessageList extends AbstractViewRight {
getDragData(event) {
const item = ko.dataFor(doc.elementFromPoint(event.clientX, event.clientY));
item && item.checked && item.checked(true);
item?.checked?.(true);
const uids = MessagelistUserStore.listCheckedOrSelectedUidsWithSubMails();
item && !uids.includes(item.uid) && uids.push(item.uid);
return uids.length ? {
@ -571,7 +571,7 @@ export class MailMessageList extends AbstractViewRight {
}
gotoThread(message) {
if (message && 0 < message.threadsLen()) {
if (0 < message?.threadsLen()) {
MessagelistUserStore.pageBeforeThread(MessagelistUserStore.page());
hasher.setHash(
@ -718,7 +718,7 @@ export class MailMessageList extends AbstractViewRight {
registerShortcut('t', '', [Scope.MessageList], () => {
let message = MessagelistUserStore.selectedMessage() || MessagelistUserStore.focusedMessage();
if (message && 0 < message.threadsLen()) {
if (0 < message?.threadsLen()) {
this.gotoThread(message);
}
return false;

View file

@ -134,8 +134,8 @@ export class MailMessageView extends AbstractViewRight {
allowAttachmentControls: () => arrayLength(attachmentsActions) && SettingsCapa('AttachmentsActions'),
downloadAsZipAllowed: () => this.attachmentsActions.includes('zip')
&& (currentMessage() ? currentMessage().attachments : [])
.filter(item => item && !item.isLinked() && item.checked() && item.download)
&& (currentMessage()?.attachments || [])
.filter(item => item?.download && !item?.isLinked() && item?.checked())
.length,
tagsAllowed: () => FolderUserStore.currentFolder() ? FolderUserStore.currentFolder().tagsAllowed() : false,
@ -286,7 +286,7 @@ export class MailMessageView extends AbstractViewRight {
el = eqs(event, '.attachmentsPlace .attachmentItem .attachmentNameParent');
if (el) {
const attachment = ko.dataFor(el);
if (attachment && attachment.linkDownload()) {
if (attachment?.linkDownload()) {
if ('message/rfc822' == attachment.mimeType) {
// TODO
rl.fetch(attachment.linkDownload()).then(response => {
@ -393,7 +393,7 @@ export class MailMessageView extends AbstractViewRight {
// toggle message blockquotes
registerShortcut('b', '', [Scope.MessageList, Scope.MessageView], () => {
const message = currentMessage();
if (message && message.body) {
if (message?.body) {
message.body.querySelectorAll('.rlBlockquoteSwitcher').forEach(node => node.click());
return false;
}
@ -411,7 +411,7 @@ export class MailMessageView extends AbstractViewRight {
// print
addShortcut('p,printscreen', 'meta', [Scope.MessageView, Scope.MessageList], () => {
currentMessage() && currentMessage().printMessage();
currentMessage()?.printMessage();
return false;
});
@ -510,7 +510,7 @@ export class MailMessageView extends AbstractViewRight {
downloadAsZip() {
const hashes = (currentMessage() ? currentMessage().attachments : [])
.map(item => (item && !item.isLinked() && item.checked() ? item.download : ''))
.map(item => item?.checked() && !item?.isLinked() ? item.download : '')
.filter(v => v);
if (hashes.length) {
Remote.post('AttachmentsActions', this.downloadAsZipLoading, {
@ -518,7 +518,7 @@ export class MailMessageView extends AbstractViewRight {
Hashes: hashes
})
.then(result => {
let hash = result && result.Result && result.Result.FileHash;
let hash = result?.Result?.FileHash;
if (hash) {
download(attachmentDownload(hash), hash+'.zip');
} else {
@ -577,7 +577,7 @@ export class MailMessageView extends AbstractViewRight {
if (result.data) {
MimeToMessage(result.data, oMessage);
oMessage.html() ? oMessage.viewHtml() : oMessage.viewPlain();
if (result.signatures && result.signatures.length) {
if (result.signatures?.length) {
oMessage.pgpSigned(true);
oMessage.pgpVerified({
signatures: result.signatures,
@ -596,7 +596,7 @@ export class MailMessageView extends AbstractViewRight {
oMessage.pgpVerified(result);
}
/*
if (result && result.success) {
if (result?.success) {
i18n('OPENPGP/GOOD_SIGNATURE', {
USER: validKey.user + ' (' + validKey.id + ')'
});
@ -604,7 +604,7 @@ export class MailMessageView extends AbstractViewRight {
} else {
const keyIds = arrayLength(signingKeyIds) ? signingKeyIds : null,
additional = keyIds
? keyIds.map(item => (item && item.toHex ? item.toHex() : null)).filter(v => v).join(', ')
? keyIds.map(item => item?.toHex?.()).filter(v => v).join(', ')
: '';
i18n('OPENPGP/ERROR', {

View file

@ -52,7 +52,7 @@ export class SystemDropDownUserView extends AbstractViewRight {
}
accountClick(account, event) {
let email = account && account.email;
let email = account?.email;
if (email && 0 === event.button && AccountUserStore.email() != email) {
AccountUserStore.loading(true);
event.preventDefault();