snappymail/dev/View/Popup/Identity.js

171 lines
3.6 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
import { StorageResultType, Notification } from 'Common/Enums';
import { bMobileDevice } from 'Common/Globals';
import { fakeMd5 } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { getNotification } from 'Common/Translator';
2016-06-30 08:02:45 +08:00
import Remote from 'Remote/User/Ajax';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
import { getApp } from 'Helper/Apps/User';
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
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.edit = ko.observable(false);
this.owner = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.email = ko.observable('').validateEmail();
this.email.focused = ko.observable(false);
this.name = ko.observable('');
this.replyTo = ko.observable('').validateSimpleEmail();
this.replyTo.focused = ko.observable(false);
this.bcc = ko.observable('').validateSimpleEmail();
this.bcc.focused = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.signature = ko.observable('');
this.signatureInsertBefore = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.showBcc = ko.observable(false);
this.showReplyTo = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.submitRequest = ko.observable(false);
this.submitError = ko.observable('');
2016-06-30 08:02:45 +08:00
this.bcc.subscribe((value) => {
if (false === this.showBcc() && value.length) {
this.showBcc(true);
}
});
2014-08-21 23:08:34 +08:00
this.replyTo.subscribe((value) => {
if (false === this.showReplyTo() && value.length) {
this.showReplyTo(true);
}
});
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
2019-07-05 03:19:24 +08:00
if (!this.email.hasError()) {
this.email.hasError(!this.email().trim());
2016-09-10 06:38:16 +08:00
}
2019-07-05 03:19:24 +08:00
if (this.email.hasError()) {
if (!this.owner()) {
2016-09-10 06:38:16 +08:00
this.email.focused(true);
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
return false;
}
2019-07-05 03:19:24 +08:00
if (this.replyTo.hasError()) {
2016-09-10 06:38:16 +08:00
this.replyTo.focused(true);
return false;
}
2019-07-05 03:19:24 +08:00
if (this.bcc.hasError()) {
2016-09-10 06:38:16 +08:00
this.bcc.focused(true);
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) {
getApp().accountsAndIdentities();
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);
this.email.hasError(false);
this.replyTo.hasError(false);
this.bcc.hasError(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 = fakeMd5();
}
2016-06-30 08:02:45 +08:00
}
onShowWithDelay() {
2019-07-05 03:19:24 +08:00
if (!this.owner() && !bMobileDevice) {
this.email.focused(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 };