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-04-18 18:14:32 +08:00
|
|
|
'/common/cryptpad-common.js'
|
|
|
|
], function ($, Config, Cryptpad) {
|
2017-01-03 19:17:17 +08:00
|
|
|
|
2016-08-31 00:15:43 +08:00
|
|
|
var APP = window.APP = {
|
|
|
|
Cryptpad: Cryptpad,
|
|
|
|
};
|
|
|
|
|
2017-02-21 21:16:23 +08:00
|
|
|
var Messages = Cryptpad.Messages;
|
|
|
|
|
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
|
|
|
// Language selector
|
|
|
|
var $sel = $('#language-selector');
|
|
|
|
Cryptpad.createLanguageSelector(undefined, $sel);
|
|
|
|
$sel.find('button').addClass('btn').addClass('btn-secondary');
|
|
|
|
$sel.show();
|
|
|
|
|
2017-02-16 00:57:42 +08:00
|
|
|
// User admin menu
|
|
|
|
var $userMenu = $('#user-menu');
|
|
|
|
var userMenuCfg = {
|
|
|
|
$initBlock: $userMenu
|
|
|
|
};
|
|
|
|
var $userAdmin = Cryptpad.createUserAdminMenu(userMenuCfg);
|
|
|
|
$userAdmin.find('button').addClass('btn').addClass('btn-secondary');
|
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
$(window).click(function () {
|
|
|
|
$('.cryptpad-dropdown').hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
var name = localStorage[Cryptpad.userNameKey] || sessionStorage[Cryptpad.userNameKey];
|
|
|
|
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');
|
|
|
|
//return;
|
2017-02-03 17:48:15 +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 01:53:04 +08:00
|
|
|
var displayCreateButtons = function () {
|
|
|
|
var $parent = $('#buttons');
|
|
|
|
var options = [];
|
|
|
|
var $container = $('<div>', {'class': 'dropdown-bar'}).appendTo($parent);
|
|
|
|
Config.availablePadTypes.forEach(function (el) {
|
|
|
|
if (el === 'drive') { return; }
|
|
|
|
options.push({
|
|
|
|
tag: 'a',
|
|
|
|
attributes: {
|
|
|
|
'class': 'newdoc',
|
|
|
|
'href': '/' + el + '/',
|
|
|
|
'target': '_blank'
|
|
|
|
},
|
|
|
|
content: Messages['button_new' + el] // Pretty name of the language value
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var dropdownConfig = {
|
|
|
|
text: Messages.login_makeAPad, // Button initial text
|
|
|
|
options: options, // Entries displayed in the menu
|
|
|
|
container: $container
|
|
|
|
};
|
|
|
|
var $block = Cryptpad.createDropdown(dropdownConfig);
|
|
|
|
$block.find('button').addClass('btn').addClass('btn-primary');
|
|
|
|
$block.appendTo($parent);
|
|
|
|
};
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('button.login').click(function (e) {
|
2017-02-13 18:09:30 +08:00
|
|
|
Cryptpad.addLoadingScreen(Messages.login_hashing);
|
|
|
|
// 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) {
|
2017-03-16 01:57:13 +08:00
|
|
|
var proxy = result.proxy;
|
|
|
|
|
2017-02-13 18:09:30 +08:00
|
|
|
// successful validation and user already exists
|
|
|
|
// set user hash in localStorage and redirect to drive
|
2017-03-16 01:57:13 +08:00
|
|
|
if (proxy && !proxy.login_name) {
|
|
|
|
proxy.login_name = result.userName;
|
2017-02-13 18:15:30 +08:00
|
|
|
}
|
2017-03-16 01:57:13 +08:00
|
|
|
|
|
|
|
proxy.edPrivate = result.edPrivate;
|
|
|
|
proxy.edPublic = result.edPublic;
|
|
|
|
|
2017-03-08 23:15:31 +08:00
|
|
|
Cryptpad.whenRealtimeSyncs(result.realtime, function () {
|
|
|
|
Cryptpad.login(result.userHash, result.userName, function () {
|
|
|
|
document.location.href = '/drive/';
|
|
|
|
});
|
2017-02-13 18:09:30 +08:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (err) {
|
|
|
|
case 'NO_SUCH_USER':
|
|
|
|
Cryptpad.removeLoadingScreen(function () {
|
|
|
|
Cryptpad.alert(Messages.login_noSuchUser);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'INVAL_USER':
|
|
|
|
Cryptpad.removeLoadingScreen(function () {
|
|
|
|
Cryptpad.alert(Messages.login_invalUser);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'INVAL_PASS':
|
|
|
|
Cryptpad.removeLoadingScreen(function () {
|
|
|
|
Cryptpad.alert(Messages.login_invalPass);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default: // UNHANDLED ERROR
|
|
|
|
Cryptpad.errorLoadingScreen(Messages.login_unhandledError);
|
|
|
|
}
|
|
|
|
});
|
2017-02-10 23:49:17 +08:00
|
|
|
});
|
2017-02-13 18:09:30 +08:00
|
|
|
}, 0);
|
2017-02-10 23:49:17 +08:00
|
|
|
});
|
|
|
|
/* End Log in UI */
|
|
|
|
|
|
|
|
var addButtonHandlers = function () {
|
2017-02-10 01:53:04 +08:00
|
|
|
$('button.register').click(function (e) {
|
|
|
|
var username = $('#name').val();
|
|
|
|
var passwd = $('#password').val();
|
|
|
|
var remember = $('#rememberme').is(':checked');
|
|
|
|
sessionStorage.login_user = username;
|
|
|
|
sessionStorage.login_pass = passwd;
|
2017-02-10 23:49:17 +08:00
|
|
|
document.location.href = '/register/';
|
2017-02-03 01:09:27 +08:00
|
|
|
});
|
2017-02-10 18:21:10 +08:00
|
|
|
$('button.gotodrive').click(function (e) {
|
|
|
|
document.location.href = '/drive/';
|
|
|
|
});
|
2017-02-01 01:16:35 +08:00
|
|
|
};
|
2017-02-01 00:29:32 +08:00
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
displayCreateButtons();
|
2017-01-17 18:52:44 +08:00
|
|
|
|
2017-02-10 01:53:04 +08:00
|
|
|
addButtonHandlers();
|
|
|
|
console.log("ready");
|
|
|
|
});
|
2016-07-04 21:21:23 +08:00
|
|
|
});
|