2016-07-04 21:21:23 +08:00
|
|
|
define([
|
2017-04-18 18:14:32 +08:00
|
|
|
'jquery',
|
2016-09-13 17:01:10 +08:00
|
|
|
'/customize/application_config.js',
|
2017-05-31 02:11:15 +08:00
|
|
|
'/common/cryptpad-common.js',
|
2017-11-13 17:23:18 +08:00
|
|
|
'/common/common-interface.js',
|
2017-11-14 00:01:09 +08:00
|
|
|
'/common/common-realtime.js',
|
2017-11-21 23:46:19 +08:00
|
|
|
'/common/common-constants.js',
|
2017-11-14 00:01:09 +08:00
|
|
|
'/customize/messages.js',
|
2017-11-21 23:46:19 +08:00
|
|
|
], function ($, Config, Cryptpad, UI, Realtime, Constants, Messages) {
|
2017-01-03 19:17:17 +08:00
|
|
|
|
2017-05-04 22:16:09 +08:00
|
|
|
window.APP = {
|
2016-08-31 00:15:43 +08:00
|
|
|
Cryptpad: Cryptpad,
|
|
|
|
};
|
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
$(function () {
|
|
|
|
var $main = $('#mainBlock');
|
2017-02-03 01:09:27 +08:00
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
$(window).click(function () {
|
2017-09-04 21:09:54 +08:00
|
|
|
$('.cp-dropdown-content').hide();
|
2017-02-10 01:53:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// main block is hidden in case javascript is disabled
|
|
|
|
$main.removeClass('hidden');
|
2017-02-03 01:09:27 +08:00
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
// Make sure we don't display non-translated content (empty button)
|
|
|
|
$main.find('#data').removeClass('hidden');
|
2017-01-31 19:43:28 +08:00
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
if (Cryptpad.isLoggedIn()) {
|
2017-08-04 20:35:47 +08:00
|
|
|
if (window.location.pathname === '/') {
|
|
|
|
window.location = '/drive/';
|
2017-08-04 20:37:21 +08:00
|
|
|
return;
|
2017-08-04 20:35:47 +08:00
|
|
|
}
|
|
|
|
|
2017-08-11 17:32:21 +08:00
|
|
|
$main.find('a[href="/drive/"] div.pad-button-text h4')
|
|
|
|
.text(Messages.main_yourCryptDrive);
|
|
|
|
|
2017-11-21 23:46:19 +08:00
|
|
|
var name = localStorage[Constants.userNameKey] || sessionStorage[Constants.userNameKey];
|
2017-02-10 01:53:04 +08:00
|
|
|
var $loggedInBlock = $main.find('#loggedIn');
|
|
|
|
var $hello = $loggedInBlock.find('#loggedInHello');
|
|
|
|
var $logout = $loggedInBlock.find('#loggedInLogOut');
|
2017-02-03 01:09:27 +08:00
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
if (name) {
|
|
|
|
$hello.text(Messages._getKey('login_hello', [name]));
|
|
|
|
} else {
|
|
|
|
$hello.text(Messages.login_helloNoName);
|
|
|
|
}
|
|
|
|
$('#buttons').find('.nologin').hide();
|
2016-07-04 21:21:23 +08:00
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
$logout.click(function () {
|
|
|
|
Cryptpad.logout(function () {
|
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$loggedInBlock.removeClass('hidden');
|
2017-06-06 17:37:54 +08:00
|
|
|
}
|
|
|
|
else {
|
2017-02-10 01:53:04 +08:00
|
|
|
$main.find('#userForm').removeClass('hidden');
|
2017-03-29 18:56:08 +08:00
|
|
|
$('#name').focus();
|
2017-02-03 17:48:15 +08:00
|
|
|
}
|
2017-02-03 01:09:27 +08:00
|
|
|
|
2017-02-10 23:49:17 +08:00
|
|
|
/* Log in UI */
|
|
|
|
var Login;
|
|
|
|
// deferred execution to avoid unnecessary asset loading
|
|
|
|
var loginReady = function (cb) {
|
|
|
|
if (Login) {
|
|
|
|
if (typeof(cb) === 'function') { cb(); }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
require([
|
|
|
|
'/common/login.js',
|
|
|
|
], function (_Login) {
|
|
|
|
Login = Login || _Login;
|
|
|
|
if (typeof(cb) === 'function') { cb(); }
|
2017-02-10 01:53:04 +08:00
|
|
|
});
|
2017-02-10 23:49:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var $uname = $('#name').on('focus', loginReady);
|
|
|
|
|
|
|
|
var $passwd = $('#password')
|
|
|
|
// background loading of login assets
|
|
|
|
.on('focus', loginReady)
|
|
|
|
// enter key while on password field clicks signup
|
|
|
|
.on('keyup', function (e) {
|
|
|
|
if (e.which !== 13) { return; } // enter
|
|
|
|
$('button.login').click();
|
|
|
|
});
|
|
|
|
|
2017-05-04 22:16:09 +08:00
|
|
|
$('button.login').click(function () {
|
2017-05-10 21:54:09 +08:00
|
|
|
// setTimeout 100ms to remove the keyboard on mobile devices before the loading screen pops up
|
2017-02-13 18:09:30 +08:00
|
|
|
window.setTimeout(function () {
|
2017-11-13 19:00:15 +08:00
|
|
|
UI.addLoadingScreen({loadingText: Messages.login_hashing});
|
2017-05-10 21:54:09 +08:00
|
|
|
// We need a setTimeout(cb, 0) otherwise the loading screen is only displayed after hashing the password
|
|
|
|
window.setTimeout(function () {
|
|
|
|
loginReady(function () {
|
|
|
|
var uname = $uname.val();
|
|
|
|
var passwd = $passwd.val();
|
|
|
|
Login.loginOrRegister(uname, passwd, false, function (err, result) {
|
|
|
|
if (!err) {
|
|
|
|
var proxy = result.proxy;
|
|
|
|
|
|
|
|
// successful validation and user already exists
|
|
|
|
// set user hash in localStorage and redirect to drive
|
|
|
|
if (proxy && !proxy.login_name) {
|
|
|
|
proxy.login_name = result.userName;
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy.edPrivate = result.edPrivate;
|
|
|
|
proxy.edPublic = result.edPublic;
|
|
|
|
|
2017-11-14 00:01:09 +08:00
|
|
|
Realtime.whenRealtimeSyncs(result.realtime, function () {
|
2017-05-10 21:54:09 +08:00
|
|
|
Cryptpad.login(result.userHash, result.userName, function () {
|
|
|
|
document.location.href = '/drive/';
|
|
|
|
});
|
2017-02-13 18:09:30 +08:00
|
|
|
});
|
2017-05-10 21:54:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (err) {
|
|
|
|
case 'NO_SUCH_USER':
|
2017-11-13 19:00:15 +08:00
|
|
|
UI.removeLoadingScreen(function () {
|
2017-11-13 17:23:18 +08:00
|
|
|
UI.alert(Messages.login_noSuchUser);
|
2017-05-10 21:54:09 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'INVAL_USER':
|
2017-11-13 19:00:15 +08:00
|
|
|
UI.removeLoadingScreen(function () {
|
2017-11-13 17:23:18 +08:00
|
|
|
UI.alert(Messages.login_invalUser);
|
2017-05-10 21:54:09 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'INVAL_PASS':
|
2017-11-13 19:00:15 +08:00
|
|
|
UI.removeLoadingScreen(function () {
|
2017-11-13 17:23:18 +08:00
|
|
|
UI.alert(Messages.login_invalPass);
|
2017-05-10 21:54:09 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
default: // UNHANDLED ERROR
|
2017-11-13 19:00:15 +08:00
|
|
|
UI.errorLoadingScreen(Messages.login_unhandledError);
|
2017-05-10 21:54:09 +08:00
|
|
|
}
|
|
|
|
});
|
2017-02-13 18:09:30 +08:00
|
|
|
});
|
2017-05-10 21:54:09 +08:00
|
|
|
}, 0);
|
|
|
|
}, 100);
|
2017-02-10 23:49:17 +08:00
|
|
|
});
|
|
|
|
/* End Log in UI */
|
2017-02-10 01:53:04 +08:00
|
|
|
console.log("ready");
|
|
|
|
});
|
2016-07-04 21:21:23 +08:00
|
|
|
});
|