snappymail/dev/View/User/Login.js

175 lines
4 KiB
JavaScript
Raw Normal View History

2021-03-24 21:22:25 +08:00
import { Notification } from 'Common/Enums';
2022-03-14 22:42:05 +08:00
import { ClientSideKeyNameLastSignMe } from 'Common/EnumsUser';
2022-02-11 21:21:41 +08:00
import { SettingsGet, fireEvent } from 'Common/Globals';
import { getNotification, translatorReload, convertLangName } from 'Common/Translator';
import { addObservablesTo, addComputablesTo, addSubscribablesTo } from 'External/ko';
import { LanguageStore } from 'Stores/Language';
import * as Local from 'Storage/Client';
import Remote from 'Remote/User/Fetch';
import { decorateKoCommands, showScreenPopup } from 'Knoin/Knoin';
import { AbstractViewLogin } from 'Knoin/AbstractViews';
import { LanguagesPopupView } from 'View/Popup/Languages';
2022-03-14 22:42:05 +08:00
const
SignMeOff = 0,
2021-07-23 17:57:45 +08:00
SignMeOn = 1,
SignMeUnused = 2;
2021-01-25 05:58:06 +08:00
export class LoginUserView extends AbstractViewLogin {
constructor() {
2022-03-08 19:28:16 +08:00
super();
2014-08-21 23:08:34 +08:00
addObservablesTo(this, {
2021-03-10 18:44:48 +08:00
loadingDesc: SettingsGet('LoadingDescription'),
email: SettingsGet('DevEmail'),
password: SettingsGet('DevPassword'),
signMe: false,
emailError: false,
passwordError: false,
submitRequest: false,
submitError: '',
2022-04-29 05:10:21 +08:00
submitErrorAdditional: '',
langRequest: false,
2021-07-23 17:57:45 +08:00
signMeType: SignMeUnused
});
2021-03-10 18:44:48 +08:00
this.allowLanguagesOnLogin = !!SettingsGet('AllowLanguagesOnLogin');
this.language = LanguageStore.language;
this.languages = LanguageStore.languages;
this.bSendLanguage = false;
addComputablesTo(this, {
languageFullName: () => convertLangName(this.language()),
2015-04-14 02:45:09 +08:00
2021-07-23 17:57:45 +08:00
signMeVisibility: () => SignMeUnused !== this.signMeType()
});
2015-04-14 02:45:09 +08:00
addSubscribablesTo(this, {
2021-07-23 17:57:45 +08:00
email: () => this.emailError(false),
password: () => this.passwordError(false),
2022-04-29 05:10:21 +08:00
submitError: value => value || this.submitErrorAdditional(''),
2015-04-14 02:45:09 +08:00
2021-07-23 17:57:45 +08:00
signMeType: iValue => this.signMe(SignMeOn === iValue),
2021-03-23 21:48:34 +08:00
language: value => {
this.langRequest(true);
2022-12-23 19:22:57 +08:00
translatorReload(value).then(
2021-03-23 21:48:34 +08:00
() => {
this.langRequest(false);
this.bSendLanguage = true;
},
() => this.langRequest(false)
);
}
});
2021-03-10 18:44:48 +08:00
if (SettingsGet('AdditionalLoginError') && !this.submitError()) {
this.submitError(SettingsGet('AdditionalLoginError'));
}
decorateKoCommands(this, {
submitCommand: self => !self.submitRequest()
});
}
hideError() {
this.submitError('');
}
toggleSignMe() {
this.signMe(!this.signMe());
}
submitCommand(self, event) {
2022-06-14 02:54:36 +08:00
const email = this.email().trim();
this.email(email);
let form = event.target.form,
data = new FormData(form),
valid = form.reportValidity() && fireEvent('sm-user-login', data, 1);
2016-12-15 05:56:17 +08:00
2022-06-14 02:54:36 +08:00
this.emailError(!email);
this.passwordError(!this.password());
2021-03-24 21:22:25 +08:00
this.formError(!valid);
if (valid) {
this.submitRequest(true);
data.set('Language', this.bSendLanguage ? this.language() : '');
data.set('SignMe', this.signMe() ? 1 : 0);
Remote.request('Login',
(iError, oData) => {
2022-02-11 21:21:41 +08:00
fireEvent('sm-user-login-response', {
error: iError,
data: oData
});
if (iError) {
this.submitRequest(false);
if (Notification.InvalidInputArgument == iError) {
iError = Notification.AuthError;
2019-07-05 03:19:24 +08:00
}
2022-09-02 17:52:07 +08:00
this.submitError(getNotification(iError, oData?.ErrorMessage,
Notification.UnknownNotification));
2022-09-02 17:52:07 +08:00
this.submitErrorAdditional(oData?.ErrorMessageAdditional);
2019-07-05 03:19:24 +08:00
} else {
rl.setData(oData.Result);
2019-07-05 03:19:24 +08:00
}
},
data
2019-07-05 03:19:24 +08:00
);
2022-03-14 22:42:05 +08:00
Local.set(ClientSideKeyNameLastSignMe, this.signMe() ? '-1-' : '-0-');
}
return valid;
2016-06-30 08:02:45 +08:00
}
onBuild(dom) {
super.onBuild(dom);
2016-06-30 08:02:45 +08:00
2022-03-14 22:42:05 +08:00
const signMe = (SettingsGet('SignMe') || '').toLowerCase();
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
switch (signMe) {
2021-07-23 17:57:45 +08:00
case 'defaultoff':
case 'defaulton':
2019-07-05 03:19:24 +08:00
this.signMeType(
2021-07-23 17:57:45 +08:00
'defaulton' === signMe ? SignMeOn : SignMeOff
2019-07-05 03:19:24 +08:00
);
2022-03-14 22:42:05 +08:00
switch (Local.get(ClientSideKeyNameLastSignMe)) {
case '-1-':
2021-07-23 17:57:45 +08:00
this.signMeType(SignMeOn);
break;
case '-0-':
2021-07-23 17:57:45 +08:00
this.signMeType(SignMeOff);
break;
// no default
}
break;
default:
2021-07-23 17:57:45 +08:00
this.signMeType(SignMeUnused);
break;
}
}
2014-07-25 06:28:10 +08:00
selectLanguage() {
showScreenPopup(LanguagesPopupView, [this.language, this.languages(), LanguageStore.userLanguage()]);
}
}