100 lines
2.2 KiB
JavaScript
100 lines
2.2 KiB
JavaScript
import I18n;
|
|
import GUI.ConnectedForm;
|
|
import {dialog, register, select} from GUI.Screen;
|
|
import Messaging;
|
|
import Session;
|
|
import Save;
|
|
|
|
var login;
|
|
var loginForm;
|
|
var accountLoader;
|
|
var accountLoaderForm;
|
|
|
|
return {
|
|
init: init
|
|
};
|
|
|
|
function init() {
|
|
register('login');
|
|
initLogin();
|
|
initLoader();
|
|
initMessageHandlers();
|
|
restoreName();
|
|
}
|
|
|
|
function initLogin() {
|
|
login = GUI.ConnectedForm.get('loginForm');
|
|
loginForm = login.root;
|
|
loginForm.getElementsByTagName('label')[0].textContent = I18n.get('pickName');
|
|
loginForm.join.value = I18n.get('join');
|
|
loginForm.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
if(!Session.isLoggedIn()) {
|
|
Session.start(loginForm.you.value);
|
|
} else {
|
|
Messaging.send({tag: 'Hello', name: loginForm.you.value});
|
|
}
|
|
});
|
|
loginForm.you.addEventListener("input", validate);
|
|
}
|
|
|
|
function initLoader() {
|
|
accountLoader = GUI.ConnectedForm.get('accountLoader');
|
|
accountLoaderForm = accountLoader.root;
|
|
accountLoaderForm.getElementsByTagName('label')[0].textContent = I18n.get('pickFile');
|
|
accountLoaderForm.doLoad.value = I18n.get('doLoad');
|
|
accountLoaderForm.addEventListener('submit', loadAccountConfirm);
|
|
accountLoaderForm.pickFile.addEventListener("input", function() {
|
|
accountLoader.enable();
|
|
});
|
|
}
|
|
|
|
function initMessageHandlers() {
|
|
Messaging.addEventListener(["LogIn"], function(o) {
|
|
if(Session.is(o.from)) {
|
|
select('hall');
|
|
}
|
|
});
|
|
}
|
|
|
|
function loadAccountConfirm(e) {
|
|
e.preventDefault();
|
|
if(Save.get('player') != undefined) {
|
|
dialog({
|
|
text: I18n.get('warnExistingAccount'),
|
|
answers: [
|
|
{label: 'confirmReplace', action: loadAccount},
|
|
{label: 'cancel', action: function() {}}
|
|
]
|
|
});
|
|
} else {
|
|
loadAccount();
|
|
}
|
|
}
|
|
|
|
function loadAccount() {
|
|
var fileReader = new FileReader();
|
|
fileReader.addEventListener('load', function() {
|
|
Save.set(null, JSON.parse(fileReader.result));
|
|
var name = restoreName();
|
|
Messaging.reset();
|
|
if(name != undefined) {
|
|
Session.start(name);
|
|
}
|
|
});
|
|
fileReader.readAsText(accountLoaderForm.pickFile.files[0]);
|
|
}
|
|
|
|
function restoreName() {
|
|
var name = Save.get('player.name');
|
|
if(name != undefined && name.length > 0) {
|
|
loginForm.you.value = name;
|
|
login.enable();
|
|
return name;
|
|
}
|
|
}
|
|
|
|
function validate(e) {
|
|
login.enable(e.target.value != "");
|
|
}
|