snappymail/dev/View/User/Login.js

424 lines
10 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
window = require('window'),
_ = require('_'),
$ = require('$'),
2014-08-25 23:49:01 +08:00
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
2014-10-06 02:37:31 +08:00
Links = require('Common/Links'),
2014-08-21 23:08:34 +08:00
Translator = require('Common/Translator'),
Plugins = require('Common/Plugins'),
LanguageStore = require('Stores/Language'),
AppStore = require('Stores/User/App'),
2015-02-05 12:05:27 +08:00
Local = require('Storage/Client'),
Settings = require('Storage/Settings'),
2014-10-18 21:43:44 +08:00
Remote = require('Storage/User/Remote'),
2014-08-21 23:08:34 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
2014-08-21 23:08:34 +08:00
;
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
2014-10-18 21:43:44 +08:00
function LoginUserView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Center', 'Login');
2014-08-21 23:08:34 +08:00
this.email = ko.observable('');
this.password = ko.observable('');
this.signMe = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.additionalCode = ko.observable('');
this.additionalCode.error = ko.observable(false);
this.additionalCode.focused = ko.observable(false);
this.additionalCode.visibility = ko.observable(false);
this.additionalCodeSignMe = ko.observable(false);
2014-08-27 23:59:44 +08:00
this.logoImg = Utils.trim(Settings.settingsGet('LoginLogo'));
2014-10-24 01:59:21 +08:00
this.logoPowered = !!Settings.settingsGet('LoginPowered');
this.loginDescription = Utils.trim(Settings.settingsGet('LoginDescription'));
this.forgotPasswordLinkUrl = Settings.settingsGet('ForgotPasswordLinkUrl');
this.registrationLinkUrl = Settings.settingsGet('RegistrationLinkUrl');
2014-08-21 23:08:34 +08:00
this.emailError = ko.observable(false);
this.passwordError = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.emailFocus = ko.observable(false);
this.submitFocus = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.email.subscribe(function () {
this.emailError(false);
this.additionalCode('');
this.additionalCode.visibility(false);
}, this);
2014-08-21 23:08:34 +08:00
this.password.subscribe(function () {
this.passwordError(false);
}, this);
2014-08-21 23:08:34 +08:00
this.additionalCode.subscribe(function () {
this.additionalCode.error(false);
}, this);
2014-08-21 23:08:34 +08:00
this.additionalCode.visibility.subscribe(function () {
this.additionalCode.error(false);
}, this);
2014-08-21 23:08:34 +08:00
this.submitRequest = ko.observable(false);
this.submitError = ko.observable('');
this.submitErrorAddidional = ko.observable('');
this.submitError.subscribe(function (sValue) {
if ('' === sValue)
{
this.submitErrorAddidional('');
}
}, this);
this.allowLanguagesOnLogin = AppStore.allowLanguagesOnLogin;
2014-08-21 23:08:34 +08:00
this.langRequest = ko.observable(false);
this.language = LanguageStore.language;
2014-08-21 23:08:34 +08:00
this.bSendLanguage = false;
this.languageFullName = ko.computed(function () {
return Utils.convertLangName(this.language());
2014-08-21 23:08:34 +08:00
}, this);
2014-07-25 06:28:10 +08:00
2014-08-21 23:08:34 +08:00
this.signMeType = ko.observable(Enums.LoginSignMeType.Unused);
2014-08-21 23:08:34 +08:00
this.signMeType.subscribe(function (iValue) {
this.signMe(Enums.LoginSignMeType.DefaultOn === iValue);
}, this);
2014-07-29 18:28:02 +08:00
2014-08-21 23:08:34 +08:00
this.signMeVisibility = ko.computed(function () {
return Enums.LoginSignMeType.Unused !== this.signMeType();
}, this);
2014-08-21 23:08:34 +08:00
this.submitCommand = Utils.createCommand(this, function () {
2014-08-21 23:08:34 +08:00
Utils.triggerAutocompleteInputChange();
2014-08-21 23:08:34 +08:00
this.emailError('' === Utils.trim(this.email()));
this.passwordError('' === Utils.trim(this.password()));
2014-08-21 23:08:34 +08:00
if (this.additionalCode.visibility())
{
this.additionalCode.error('' === Utils.trim(this.additionalCode()));
}
2014-07-25 06:28:10 +08:00
2014-08-21 23:08:34 +08:00
if (this.emailError() || this.passwordError() || this.additionalCode.error())
{
return false;
}
var
iPluginResultCode = 0,
sPluginResultMessage = '',
fSubmitResult = function (iResultCode, sResultMessage) {
iPluginResultCode = iResultCode || 0;
sPluginResultMessage = sResultMessage || '';
}
;
Plugins.runHook('user-login-submit', [fSubmitResult]);
if (0 < iPluginResultCode)
{
this.submitError(Translator.getNotification(iPluginResultCode));
return false;
}
else if ('' !== sPluginResultMessage)
{
this.submitError(sPluginResultMessage);
return false;
}
2014-08-21 23:08:34 +08:00
this.submitRequest(true);
2014-07-25 00:18:03 +08:00
2014-08-21 23:08:34 +08:00
var
sPassword = this.password(),
fLoginRequest = _.bind(function (sPassword) {
Remote.login(_.bind(function (sResult, oData) {
if (Enums.StorageResultType.Success === sResult && oData && 'Login' === oData.Action)
2014-07-25 00:18:03 +08:00
{
2014-08-21 23:08:34 +08:00
if (oData.Result)
{
if (oData.TwoFactorAuth)
{
this.additionalCode('');
this.additionalCode.visibility(true);
this.additionalCode.focused(true);
this.submitRequest(false);
}
else if (oData.Admin)
{
2014-10-18 21:43:44 +08:00
require('App/User').redirectToAdminPanel();
}
2014-08-21 23:08:34 +08:00
else
{
2014-10-18 21:43:44 +08:00
require('App/User').loginAndLogoutReload();
2014-08-21 23:08:34 +08:00
}
}
else if (oData.ErrorCode)
2014-07-25 00:18:03 +08:00
{
this.submitRequest(false);
2014-11-15 04:23:46 +08:00
if (-1 < Utils.inArray(oData.ErrorCode, [Enums.Notification.InvalidInputArgument]))
{
oData.ErrorCode = Enums.Notification.AuthError;
}
this.submitError(Translator.getNotification(oData.ErrorCode));
2014-08-21 23:08:34 +08:00
if ('' === this.submitError())
{
this.submitError(Translator.getNotification(Enums.Notification.UnknownError));
2014-08-21 23:08:34 +08:00
}
else
{
if (oData.ErrorMessageAdditional)
{
this.submitErrorAddidional(oData.ErrorMessageAdditional);
}
}
2014-07-25 00:18:03 +08:00
}
else
{
2014-08-21 23:08:34 +08:00
this.submitRequest(false);
2014-07-25 00:18:03 +08:00
}
}
2014-08-21 23:08:34 +08:00
else
2014-07-25 00:18:03 +08:00
{
this.submitRequest(false);
this.submitError(Translator.getNotification(Enums.Notification.UnknownError));
2014-07-25 00:18:03 +08:00
}
2014-08-21 23:08:34 +08:00
}, this), this.email(), '', sPassword, !!this.signMe(),
this.bSendLanguage ? this.language() : '',
2014-08-21 23:08:34 +08:00
this.additionalCode.visibility() ? this.additionalCode() : '',
this.additionalCode.visibility() ? !!this.additionalCodeSignMe() : false
);
2015-02-05 12:05:27 +08:00
Local.set(Enums.ClientSideKeyName.LastSignMe, !!this.signMe() ? '-1-' : '-0-');
2014-08-21 23:08:34 +08:00
}, this)
;
2014-09-19 21:31:14 +08:00
if (!!Settings.settingsGet('UseRsaEncryption') && Utils.rsaEncode.supported &&
Settings.settingsGet('RsaPublicKey'))
2014-08-21 23:08:34 +08:00
{
2014-09-19 21:31:14 +08:00
fLoginRequest(Utils.rsaEncode(sPassword, Settings.settingsGet('RsaPublicKey')));
2014-08-21 23:08:34 +08:00
}
else
{
fLoginRequest(sPassword);
}
2014-07-25 06:28:10 +08:00
2014-08-21 23:08:34 +08:00
return true;
2014-07-25 00:18:03 +08:00
2014-08-21 23:08:34 +08:00
}, function () {
return !this.submitRequest();
});
2014-07-25 06:28:10 +08:00
2014-08-21 23:08:34 +08:00
this.facebookLoginEnabled = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.facebookCommand = Utils.createCommand(this, function () {
2014-07-25 06:28:10 +08:00
2014-10-06 02:37:31 +08:00
window.open(Links.socialFacebook(), 'Facebook',
2014-11-13 00:46:05 +08:00
'left=200,top=100,width=650,height=450,menubar=no,status=no,resizable=yes,scrollbars=yes');
2014-11-15 04:23:46 +08:00
2014-08-21 23:08:34 +08:00
return true;
2014-08-21 23:08:34 +08:00
}, function () {
return !this.submitRequest() && this.facebookLoginEnabled();
});
2014-08-21 23:08:34 +08:00
this.googleLoginEnabled = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.googleCommand = Utils.createCommand(this, function () {
2014-07-25 06:28:10 +08:00
2014-10-06 02:37:31 +08:00
window.open(Links.socialGoogle(), 'Google',
2014-11-13 00:46:05 +08:00
'left=200,top=100,width=650,height=450,menubar=no,status=no,resizable=yes,scrollbars=yes');
2014-08-21 23:08:34 +08:00
return true;
2014-08-21 23:08:34 +08:00
}, function () {
return !this.submitRequest() && this.googleLoginEnabled();
});
2014-08-21 23:08:34 +08:00
this.twitterLoginEnabled = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.twitterCommand = Utils.createCommand(this, function () {
2014-10-06 02:37:31 +08:00
window.open(Links.socialTwitter(), 'Twitter',
2014-11-13 00:46:05 +08:00
'left=200,top=100,width=650,height=450,menubar=no,status=no,resizable=yes,scrollbars=yes');
2014-08-25 15:10:51 +08:00
2014-08-21 23:08:34 +08:00
return true;
2014-08-21 23:08:34 +08:00
}, function () {
return !this.submitRequest() && this.twitterLoginEnabled();
});
2014-08-21 23:08:34 +08:00
this.socialLoginEnabled = ko.computed(function () {
2014-07-25 06:28:10 +08:00
2014-08-21 23:08:34 +08:00
var
bF = this.facebookLoginEnabled(),
bG = this.googleLoginEnabled(),
bT = this.twitterLoginEnabled()
;
2014-08-21 23:08:34 +08:00
return bF || bG || bT;
}, this);
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
2014-10-18 21:43:44 +08:00
kn.extendAsViewModel(['View/User/Login', 'View/App/Login', 'LoginViewModel'], LoginUserView);
_.extend(LoginUserView.prototype, AbstractView.prototype);
2014-10-18 21:43:44 +08:00
LoginUserView.prototype.onShow = function ()
2014-08-21 23:08:34 +08:00
{
kn.routeOff();
2014-07-25 06:28:10 +08:00
2014-08-21 23:08:34 +08:00
_.delay(_.bind(function () {
if ('' !== this.email() && '' !== this.password())
{
this.submitFocus(true);
}
else
{
this.emailFocus(true);
}
2014-08-27 23:59:44 +08:00
if (Settings.settingsGet('UserLanguage'))
2014-08-21 23:08:34 +08:00
{
$.cookie('rllang', LanguageStore.language(), {'expires': 30});
2014-08-21 23:08:34 +08:00
}
2014-08-21 23:08:34 +08:00
}, this), 100);
};
2014-10-18 21:43:44 +08:00
LoginUserView.prototype.onHide = function ()
2014-08-21 23:08:34 +08:00
{
this.submitFocus(false);
this.emailFocus(false);
};
2014-10-18 21:43:44 +08:00
LoginUserView.prototype.onBuild = function ()
2014-08-21 23:08:34 +08:00
{
var
self = this,
2015-02-05 12:05:27 +08:00
sSignMeLocal = Local.get(Enums.ClientSideKeyName.LastSignMe),
sSignMe = (Settings.settingsGet('SignMe') || 'unused').toLowerCase(),
2014-08-27 23:59:44 +08:00
sJsHash = Settings.settingsGet('JsHash'),
2014-08-21 23:08:34 +08:00
fSocial = function (iErrorCode) {
iErrorCode = Utils.pInt(iErrorCode);
if (0 === iErrorCode)
{
self.submitRequest(true);
2014-10-18 21:43:44 +08:00
require('App/User').loginAndLogoutReload();
2014-08-21 23:08:34 +08:00
}
else
{
self.submitError(Translator.getNotification(iErrorCode));
2014-08-21 23:08:34 +08:00
}
}
;
2014-08-27 23:59:44 +08:00
this.facebookLoginEnabled(!!Settings.settingsGet('AllowFacebookSocial'));
this.twitterLoginEnabled(!!Settings.settingsGet('AllowTwitterSocial'));
this.googleLoginEnabled(!!Settings.settingsGet('AllowGoogleSocial') &&
!!Settings.settingsGet('AllowGoogleSocialAuth'));
2015-02-05 12:05:27 +08:00
switch (sSignMe)
{
2014-08-21 23:08:34 +08:00
case Enums.LoginSignMeTypeAsString.DefaultOff:
case Enums.LoginSignMeTypeAsString.DefaultOn:
2015-02-05 12:05:27 +08:00
this.signMeType(Enums.LoginSignMeTypeAsString.DefaultOn === sSignMe ?
Enums.LoginSignMeType.DefaultOn : Enums.LoginSignMeType.DefaultOff);
switch (sSignMeLocal)
{
case '-1-':
this.signMeType(Enums.LoginSignMeType.DefaultOn);
break;
case '-0-':
this.signMeType(Enums.LoginSignMeType.DefaultOff);
break;
}
2014-08-21 23:08:34 +08:00
break;
default:
case Enums.LoginSignMeTypeAsString.Unused:
this.signMeType(Enums.LoginSignMeType.Unused);
break;
}
2014-08-21 23:08:34 +08:00
2015-02-03 07:58:58 +08:00
this.email(AppStore.devEmail);
this.password(AppStore.devPassword);
2014-08-21 23:08:34 +08:00
if (this.googleLoginEnabled())
{
2014-08-21 23:08:34 +08:00
window['rl_' + sJsHash + '_google_login_service'] = fSocial;
}
2014-08-21 23:08:34 +08:00
if (this.facebookLoginEnabled())
{
2014-08-21 23:08:34 +08:00
window['rl_' + sJsHash + '_facebook_login_service'] = fSocial;
}
2014-08-21 23:08:34 +08:00
if (this.twitterLoginEnabled())
{
window['rl_' + sJsHash + '_twitter_login_service'] = fSocial;
}
2014-08-21 23:08:34 +08:00
_.delay(function () {
LanguageStore.language.subscribe(function (sValue) {
2014-08-21 23:08:34 +08:00
self.langRequest(true);
Translator.reload(sValue, function() {
self.langRequest(false);
2014-08-21 23:08:34 +08:00
self.bSendLanguage = true;
$.cookie('rllang', sValue, {'expires': 30});
}, function() {
2014-08-21 23:08:34 +08:00
self.langRequest(false);
});
2014-08-21 23:08:34 +08:00
});
}, 50);
2014-08-21 23:08:34 +08:00
Utils.triggerAutocompleteInputChange(true);
};
2014-07-25 06:28:10 +08:00
2014-10-18 21:43:44 +08:00
LoginUserView.prototype.submitForm = function ()
{
2014-08-21 23:08:34 +08:00
this.submitCommand();
};
2014-10-18 21:43:44 +08:00
LoginUserView.prototype.selectLanguage = function ()
{
kn.showScreenPopup(require('View/Popup/Languages'));
2014-08-21 23:08:34 +08:00
};
2014-10-18 21:43:44 +08:00
module.exports = LoginUserView;
2014-09-05 06:49:03 +08:00
}());