snappymail/dev/View/Popup/Identity.js

184 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-07-05 03:19:24 +08:00
import { StorageResultType, Notification } from 'Common/Enums';
import { getNotification } from 'Common/Translator';
2016-06-30 08:02:45 +08:00
import Remote from 'Remote/User/Fetch';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
import { popup, command } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
2016-06-30 08:02:45 +08:00
2020-10-27 18:09:24 +08:00
const reEmail = /^[^@\s]+@[^@\s]+$/;
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/Identity',
templateID: 'PopupsIdentity'
})
2019-07-05 03:19:24 +08:00
class IdentityPopupView extends AbstractViewNext {
constructor() {
super();
2016-06-30 08:02:45 +08:00
this.id = '';
this.addObservables({
edit: false,
owner: false,
2020-10-27 18:09:24 +08:00
email: '',
emailFocused: false,
2020-10-27 18:09:24 +08:00
emailHasError: false,
name: '',
2020-10-27 18:09:24 +08:00
replyTo: '',
replyToFocused: false,
2020-10-27 18:09:24 +08:00
replyToHasError: false,
bcc: '',
bccFocused: false,
2020-10-27 18:09:24 +08:00
bccHasError: false,
signature: '',
signatureInsertBefore: false,
showBcc: false,
showReplyTo: false,
submitRequest: false,
submitError: ''
});
2016-06-30 08:02:45 +08:00
2020-10-27 18:09:24 +08:00
this.addSubscribables({
email: value => this.emailHasError(value && !reEmail.test(value)),
replyTo: value => {
this.replyToHasError(value && !reEmail.test(value));
if (false === this.showReplyTo() && value.length) {
this.showReplyTo(true);
}
},
bcc: value => {
this.bccHasError(value && !reEmail.test(value));
if (false === this.showBcc() && value.length) {
this.showBcc(true);
}
}
});
2020-10-27 18:09:24 +08:00
/*
this.email.valueHasMutated();
this.replyTo.valueHasMutated();
this.bcc.valueHasMutated();
*/
2016-09-10 06:38:16 +08:00
}
2016-09-10 06:38:16 +08:00
@command((self) => !self.submitRequest())
addOrEditIdentityCommand() {
2019-07-05 03:19:24 +08:00
if (this.signature && this.signature.__fetchEditorValue) {
2016-09-10 06:38:16 +08:00
this.signature.__fetchEditorValue();
}
2014-08-21 23:08:34 +08:00
2020-10-27 18:09:24 +08:00
if (!this.emailHasError()) {
this.emailHasError(!this.email().trim());
2016-09-10 06:38:16 +08:00
}
2020-10-27 18:09:24 +08:00
if (this.emailHasError()) {
2019-07-05 03:19:24 +08:00
if (!this.owner()) {
this.emailFocused(true);
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
return false;
}
2020-10-27 18:09:24 +08:00
if (this.replyToHasError()) {
this.replyToFocused(true);
2016-09-10 06:38:16 +08:00
return false;
}
2020-10-27 18:09:24 +08:00
if (this.bccHasError()) {
this.bccFocused(true);
2016-09-10 06:38:16 +08:00
return false;
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.submitRequest(true);
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
Remote.identityUpdate(
(result, data) => {
this.submitRequest(false);
if (StorageResultType.Success === result && data) {
if (data.Result) {
2020-09-15 15:29:25 +08:00
rl.app.accountsAndIdentities();
2019-07-05 03:19:24 +08:00
this.cancelCommand();
} else if (data.ErrorCode) {
this.submitError(getNotification(data.ErrorCode));
}
} else {
this.submitError(getNotification(Notification.UnknownError));
2016-06-30 08:02:45 +08:00
}
2019-07-05 03:19:24 +08:00
},
this.id,
this.email(),
this.name(),
this.replyTo(),
this.bcc(),
this.signature(),
this.signatureInsertBefore()
);
2016-09-10 06:38:16 +08:00
return true;
}
clearPopup() {
this.id = '';
this.edit(false);
this.owner(false);
this.name('');
this.email('');
this.replyTo('');
this.bcc('');
this.signature('');
this.signatureInsertBefore(false);
2020-10-27 18:09:24 +08:00
this.emailHasError(false);
this.replyToHasError(false);
this.bccHasError(false);
2014-08-21 23:08:34 +08:00
this.showBcc(false);
this.showReplyTo(false);
this.submitRequest(false);
this.submitError('');
2016-06-30 08:02:45 +08:00
}
/**
* @param {?IdentityModel} oIdentity
*/
onShow(identity) {
this.clearPopup();
2019-07-05 03:19:24 +08:00
if (identity) {
this.edit(true);
this.id = identity.id() || '';
this.name(identity.name());
this.email(identity.email());
this.replyTo(identity.replyTo());
this.bcc(identity.bcc());
this.signature(identity.signature());
this.signatureInsertBefore(identity.signatureInsertBefore());
this.owner(!this.id);
2019-07-05 03:19:24 +08:00
} else {
this.id = Jua.randomId();
}
2016-06-30 08:02:45 +08:00
}
onShowWithDelay() {
if (!this.owner()/* && !rl.settings.app('mobile')*/) {
this.emailFocused(true);
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
onHideWithDelay() {
this.clearPopup();
}
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
export { IdentityPopupView, IdentityPopupView as default };