snappymail/dev/ViewModels/LoginViewModel.js

370 lines
9.2 KiB
JavaScript
Raw Normal View History

2014-08-25 23:49:01 +08:00
(function (module, require) {
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-08-25 23:49:01 +08:00
Enums = require('Enums'),
Utils = require('Utils'),
2014-08-25 23:49:01 +08:00
LinkBuilder = require('LinkBuilder'),
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
Settings = require('Storage:Settings'),
Data = require('Storage:RainLoop:Data'),
Remote = require('Storage:RainLoop:Remote'),
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
kn = require('App:Knoin'),
KnoinAbstractViewModel = require('Knoin:AbstractViewModel')
2014-08-21 23:08:34 +08:00
;
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
function LoginViewModel()
{
KnoinAbstractViewModel.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'));
this.loginDescription = Utils.trim(Settings.settingsGet('LoginDescription'));
this.logoCss = Utils.trim(Settings.settingsGet('LoginCss'));
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('');
2014-08-22 23:08:56 +08:00
this.allowLanguagesOnLogin = Data.allowLanguagesOnLogin;
2014-08-21 23:08:34 +08:00
this.langRequest = ko.observable(false);
2014-08-22 23:08:56 +08:00
this.mainLanguage = Data.mainLanguage;
2014-08-21 23:08:34 +08:00
this.bSendLanguage = false;
2014-08-21 23:08:34 +08:00
this.mainLanguageFullName = ko.computed(function () {
return Utils.convertLangName(this.mainLanguage());
}, 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;
}
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
{
2014-08-27 23:59:44 +08:00
require('App:RainLoop').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-08-21 23:08:34 +08:00
this.submitError(Utils.getNotification(oData.ErrorCode));
if ('' === this.submitError())
{
this.submitError(Utils.getNotification(Enums.Notification.UnknownError));
}
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);
2014-08-21 23:08:34 +08:00
this.submitError(Utils.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.mainLanguage() : '',
this.additionalCode.visibility() ? this.additionalCode() : '',
this.additionalCode.visibility() ? !!this.additionalCodeSignMe() : false
);
}, this)
;
2014-08-27 23:59:44 +08:00
if (!!Settings.settingsGet('UseRsaEncryption') && Utils.rsaEncode.supported)
2014-08-21 23:08:34 +08:00
{
Remote.getPublicKey(_.bind(function (sResult, oData) {
var bRequest = false;
if (Enums.StorageResultType.Success === sResult && oData && oData.Result &&
Utils.isArray(oData.Result) && oData.Result[0] && oData.Result[1] && oData.Result[2])
{
var sEncryptedPassword = Utils.rsaEncode(sPassword, oData.Result[0], oData.Result[1], oData.Result[2]);
if (sEncryptedPassword)
2014-07-25 00:18:03 +08:00
{
2014-08-21 23:08:34 +08:00
fLoginRequest(sEncryptedPassword);
bRequest = true;
2014-07-25 00:18:03 +08:00
}
}
2014-08-21 23:08:34 +08:00
if (!bRequest)
{
2014-07-25 00:18:03 +08:00
this.submitRequest(false);
this.submitError(Utils.getNotification(Enums.Notification.UnknownError));
}
2014-08-21 23:08:34 +08:00
}, this));
}
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-08-22 23:08:56 +08:00
window.open(LinkBuilder.socialFacebook(), 'Facebook',
'left=200,top=100,width=650,height=335,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.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-08-22 23:08:56 +08:00
window.open(LinkBuilder.socialGoogle(), 'Google',
'left=200,top=100,width=650,height=335,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-08-22 23:08:56 +08:00
window.open(LinkBuilder.socialTwitter(), 'Twitter',
'left=200,top=100,width=650,height=335,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);
}
kn.extendAsViewModel(['View:RainLoop:Login', 'LoginViewModel'], LoginViewModel);
_.extend(LoginViewModel.prototype, KnoinAbstractViewModel.prototype);
2014-08-21 23:08:34 +08:00
LoginViewModel.prototype.onShow = function ()
{
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
{
2014-08-22 23:08:56 +08:00
$.cookie('rllang', Data.language(), {'expires': 30});
2014-08-21 23:08:34 +08:00
}
2014-08-21 23:08:34 +08:00
}, this), 100);
};
2014-08-21 23:08:34 +08:00
LoginViewModel.prototype.onHide = function ()
{
this.submitFocus(false);
this.emailFocus(false);
};
2014-08-21 23:08:34 +08:00
LoginViewModel.prototype.onBuild = function ()
{
var
self = this,
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-08-27 23:59:44 +08:00
require('App:RainLoop').loginAndLogoutReload();
2014-08-21 23:08:34 +08:00
}
else
{
self.submitError(Utils.getNotification(iErrorCode));
}
}
;
2014-08-27 23:59:44 +08:00
this.facebookLoginEnabled(!!Settings.settingsGet('AllowFacebookSocial'));
this.twitterLoginEnabled(!!Settings.settingsGet('AllowTwitterSocial'));
this.googleLoginEnabled(!!Settings.settingsGet('AllowGoogleSocial'));
2014-08-27 23:59:44 +08:00
switch ((Settings.settingsGet('SignMe') || 'unused').toLowerCase())
{
2014-08-21 23:08:34 +08:00
case Enums.LoginSignMeTypeAsString.DefaultOff:
this.signMeType(Enums.LoginSignMeType.DefaultOff);
break;
case Enums.LoginSignMeTypeAsString.DefaultOn:
this.signMeType(Enums.LoginSignMeType.DefaultOn);
break;
default:
case Enums.LoginSignMeTypeAsString.Unused:
this.signMeType(Enums.LoginSignMeType.Unused);
break;
}
2014-08-21 23:08:34 +08:00
2014-08-22 23:08:56 +08:00
this.email(Data.devEmail);
this.password(Data.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 () {
2014-08-22 23:08:56 +08:00
Data.language.subscribe(function (sValue) {
2014-08-21 23:08:34 +08:00
self.langRequest(true);
$.ajax({
'url': LinkBuilder.langLink(sValue),
'dataType': 'script',
'cache': true
}).done(function() {
self.bSendLanguage = true;
Utils.i18nReload();
2014-08-22 23:08:56 +08:00
$.cookie('rllang', Data.language(), {'expires': 30});
2014-08-21 23:08:34 +08:00
}).always(function() {
self.langRequest(false);
});
});
}, 50);
2014-08-21 23:08:34 +08:00
Utils.triggerAutocompleteInputChange(true);
};
2014-07-25 06:28:10 +08:00
2014-08-21 23:08:34 +08:00
LoginViewModel.prototype.submitForm = function ()
{
2014-08-21 23:08:34 +08:00
this.submitCommand();
};
2014-08-21 23:08:34 +08:00
LoginViewModel.prototype.selectLanguage = function ()
{
2014-08-27 23:59:44 +08:00
kn.showScreenPopup(require('View:Popup:Languages'));
2014-08-21 23:08:34 +08:00
};
2014-08-22 23:08:56 +08:00
module.exports = LoginViewModel;
2014-08-25 23:49:01 +08:00
}(module, require));