snappymail/dev/View/Popup/Account.js

121 lines
2.4 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
import {StorageResultType, Notification} from 'Common/Enums';
2016-09-10 06:38:16 +08:00
import {trim} from 'Common/Utils';
import {getNotification} from 'Common/Translator';
import Remote from 'Remote/User/Ajax';
2014-08-25 15:10:51 +08:00
import {getApp} from 'Helper/Apps/User';
2016-09-10 06:38:16 +08:00
import {popup, command} from 'Knoin/Knoin';
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/Account',
templateID: 'PopupsAccount'
})
class AccountPopupView extends AbstractViewNext
2016-06-30 08:02:45 +08:00
{
constructor() {
super();
2014-08-22 23:08:56 +08:00
this.isNew = ko.observable(true);
this.email = ko.observable('');
this.password = ko.observable('');
2014-10-22 00:49:15 +08:00
this.emailError = ko.observable(false);
this.passwordError = ko.observable(false);
this.email.subscribe(() => {
this.emailError(false);
});
this.password.subscribe(() => {
this.passwordError(false);
});
this.submitRequest = ko.observable(false);
this.submitError = ko.observable('');
this.submitErrorAdditional = ko.observable('');
this.emailFocus = ko.observable(false);
2016-09-10 06:38:16 +08:00
}
2016-09-10 06:38:16 +08:00
@command((self) => !self.submitRequest())
addAccountCommand() {
2016-09-10 06:38:16 +08:00
this.emailError('' === trim(this.email()));
this.passwordError('' === trim(this.password()));
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
if (this.emailError() || this.passwordError())
{
return false;
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.submitRequest(true);
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
Remote.accountSetup((result, data) => {
2014-08-21 23:08:34 +08:00
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();
}
2014-08-21 23:08:34 +08:00
else
{
2016-09-10 06:38:16 +08:00
this.submitError(data.ErrorCode ? getNotification(data.ErrorCode) :
getNotification(Notification.UnknownError));
2016-09-10 06:38:16 +08:00
if (data.ErrorMessageAdditional)
{
this.submitErrorAdditional(data.ErrorMessageAdditional);
}
}
}
else
{
this.submitError(getNotification(Notification.UnknownError));
this.submitErrorAdditional('');
}
2016-09-10 06:38:16 +08:00
}, this.email(), this.password(), this.isNew());
2016-09-10 06:38:16 +08:00
return true;
}
clearPopup() {
this.isNew(true);
2014-10-22 00:49:15 +08:00
this.email('');
this.password('');
2014-08-21 23:08:34 +08:00
this.emailError(false);
this.passwordError(false);
this.submitRequest(false);
this.submitError('');
this.submitErrorAdditional('');
}
onShow(account) {
this.clearPopup();
if (account && account.canBeEdit())
{
this.isNew(false);
this.email(account.email);
}
2016-06-30 08:02:45 +08:00
}
onShowWithDelay() {
this.emailFocus(true);
}
}
2013-12-29 04:42:07 +08:00
2016-09-10 06:38:16 +08:00
export {AccountPopupView, AccountPopupView as default};