snappymail/dev/View/Popup/Identity.js

187 lines
3.7 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2016-06-30 08:02:45 +08:00
import {StorageResultType, Notification} from 'Common/Enums';
import {bMobileDevice} from 'Common/Globals';
2016-09-10 06:38:16 +08:00
import {trim, fakeMd5} from 'Common/Utils';
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
import {getApp} from 'Helper/Apps/User';
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +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'
})
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.name.focused = ko.observable(false);
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() && 0 < value.length)
{
this.showBcc(true);
}
});
2014-08-21 23:08:34 +08:00
this.replyTo.subscribe((value) => {
if (false === this.showReplyTo() && 0 < 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() {
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
if (this.signature && this.signature.__fetchEditorValue)
{
this.signature.__fetchEditorValue();
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
if (!this.email.hasError())
{
this.email.hasError('' === trim(this.email()));
}
2016-09-10 06:38:16 +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;
}
2016-09-10 06:38:16 +08:00
if (this.replyTo.hasError())
{
this.replyTo.focused(true);
return false;
}
if (this.bcc.hasError())
{
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
2016-09-10 06:38:16 +08:00
Remote.identityUpdate((result, data) => {
2016-09-10 06:38:16 +08:00
this.submitRequest(false);
if (StorageResultType.Success === result && data)
{
if (data.Result)
{
2016-09-10 06:38:16 +08:00
getApp().accountsAndIdentities();
this.cancelCommand();
2016-06-30 08:02:45 +08:00
}
2016-09-10 06:38:16 +08:00
else if (data.ErrorCode)
2016-06-30 08:02:45 +08:00
{
2016-09-10 06:38:16 +08:00
this.submitError(getNotification(data.ErrorCode));
}
2016-09-10 06:38:16 +08:00
}
else
{
this.submitError(getNotification(Notification.UnknownError));
}
2016-09-10 06:38:16 +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();
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);
}
else
{
this.id = fakeMd5();
}
2016-06-30 08:02:45 +08:00
}
onShowWithDelay() {
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
2016-09-10 06:38:16 +08:00
export {IdentityPopupView, IdentityPopupView as default};