cryptpad/customize.dist/main.js

146 lines
5.6 KiB
JavaScript
Raw Normal View History

define([
'jquery',
'/customize/application_config.js',
'/common/cryptpad-common.js',
'/common/common-interface.js',
'/common/common-realtime.js',
2017-11-21 15:46:19 +00:00
'/common/common-constants.js',
'/customize/messages.js',
2017-11-21 15:46:19 +00:00
], function ($, Config, Cryptpad, UI, Realtime, Constants, Messages) {
2017-05-04 14:16:09 +00:00
window.APP = {
Cryptpad: Cryptpad,
};
$(function () {
var $main = $('#mainBlock');
$(window).click(function () {
2017-09-04 13:09:54 +00:00
$('.cp-dropdown-content').hide();
});
// main block is hidden in case javascript is disabled
$main.removeClass('hidden');
// Make sure we don't display non-translated content (empty button)
$main.find('#data').removeClass('hidden');
if (Cryptpad.isLoggedIn()) {
2017-08-04 12:35:47 +00:00
if (window.location.pathname === '/') {
window.location = '/drive/';
2017-08-04 12:37:21 +00:00
return;
2017-08-04 12:35:47 +00:00
}
$main.find('a[href="/drive/"] div.pad-button-text h4')
.text(Messages.main_yourCryptDrive);
2017-11-21 15:46:19 +00:00
var name = localStorage[Constants.userNameKey] || sessionStorage[Constants.userNameKey];
var $loggedInBlock = $main.find('#loggedIn');
var $hello = $loggedInBlock.find('#loggedInHello');
var $logout = $loggedInBlock.find('#loggedInLogOut');
if (name) {
$hello.text(Messages._getKey('login_hello', [name]));
} else {
$hello.text(Messages.login_helloNoName);
}
$('#buttons').find('.nologin').hide();
$logout.click(function () {
Cryptpad.logout(function () {
window.location.reload();
});
});
$loggedInBlock.removeClass('hidden');
}
else {
$main.find('#userForm').removeClass('hidden');
2017-03-29 10:56:08 +00:00
$('#name').focus();
}
/* 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(); }
});
};
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 14:16:09 +00:00
$('button.login').click(function () {
2017-05-10 13:54:09 +00:00
// setTimeout 100ms to remove the keyboard on mobile devices before the loading screen pops up
window.setTimeout(function () {
UI.addLoadingScreen({loadingText: Messages.login_hashing});
2017-05-10 13:54:09 +00: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;
Realtime.whenRealtimeSyncs(result.realtime, function () {
2017-05-10 13:54:09 +00:00
Cryptpad.login(result.userHash, result.userName, function () {
document.location.href = '/drive/';
});
});
2017-05-10 13:54:09 +00:00
return;
}
switch (err) {
case 'NO_SUCH_USER':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_noSuchUser);
2017-05-10 13:54:09 +00:00
});
break;
case 'INVAL_USER':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_invalUser);
2017-05-10 13:54:09 +00:00
});
break;
case 'INVAL_PASS':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_invalPass);
2017-05-10 13:54:09 +00:00
});
break;
default: // UNHANDLED ERROR
UI.errorLoadingScreen(Messages.login_unhandledError);
2017-05-10 13:54:09 +00:00
}
});
});
2017-05-10 13:54:09 +00:00
}, 0);
}, 100);
});
/* End Log in UI */
console.log("ready");
});
});