Sort initializers into separate init functions for DOM and WS events, stop passing session.loggedIn() around in refresh and setMode functions and send first message through session

This commit is contained in:
Tissevert 2019-11-24 22:48:43 +01:00
parent cd517821c3
commit 0b834b4876

View file

@ -19,12 +19,27 @@ function Login(modules) {
} }
}]; }];
init();
return {};
function init() {
initDOMEvents();
initMessageHandlers();
var name = modules.save.get('player.name');
if(name != undefined && name.length > 0) {
root.you.value = name;
formDisable('join', false);
}
}
function initDOMEvents() {
root.addEventListener('submit', function(e) { root.addEventListener('submit', function(e) {
e.preventDefault(); e.preventDefault();
if(modules.session.loggedIn()) { if(modules.session.loggedIn()) {
modules.messaging.send({tag: "Invitation", to: them}); modules.messaging.send({tag: "Invitation", to: them});
} else { } else {
modules.messaging.send({tag: "LogIn", name: root.you.value}); modules.session.start(root.you.value);
} }
}); });
@ -33,15 +48,17 @@ function Login(modules) {
lib.send({tag: "LogOut"}) lib.send({tag: "LogOut"})
}); });
root.you.addEventListener("input", function() {refreshPlayers(false);}); root.you.addEventListener("input", function() {refreshPlayers();});
root.them.addEventListener("input", function() {refreshPlayers(true);}); root.them.addEventListener("input", function() {refreshPlayers();});
}
function initMessageHandlers() {
modules.messaging.addEventListener(["Welcome"], function() { modules.messaging.addEventListener(["Welcome"], function() {
refreshPlayers(modules.session.loggedIn()); refreshPlayers();
}); });
modules.messaging.addEventListener(["Update"], function(o) { modules.messaging.addEventListener(["Update"], function(o) {
refreshPlayers(modules.session.loggedIn()); refreshPlayers();
}); });
modules.messaging.addEventListener(["Relay", "LogIn"], function() { modules.messaging.addEventListener(["Relay", "LogIn"], function() {
@ -72,17 +89,18 @@ function Login(modules) {
} }
}); });
return {}; modules.messaging.addEventListener('open', refreshPlayers);
modules.messaging.addEventListener('close', refreshPlayers);
function playersChanged() {
var loggedIn = modules.session.loggedIn();
setMode(loggedIn);
refreshPlayers(loggedIn);
} }
function refreshPlayers(loggedIn) { function playersChanged() {
setMode();
refreshPlayers();
}
function refreshPlayers() {
modules.dom.clear(players); modules.dom.clear(players);
if(loggedIn) { if(modules.session.loggedIn()) {
refreshThem(); refreshThem();
} else { } else {
refreshYou(); refreshYou();
@ -122,7 +140,7 @@ function Login(modules) {
function formDisable(name, disabled) { function formDisable(name, disabled) {
[submit, root[name]].forEach(function(button) { [submit, root[name]].forEach(function(button) {
button.disabled = disabled; button.disabled = disabled || !modules.messaging.isOn();
}); });
} }
@ -132,7 +150,8 @@ function Login(modules) {
}; };
} }
function setMode(loggedIn) { function setMode() {
var loggedIn = modules.session.loggedIn()
root.join.disabled = loggedIn; root.join.disabled = loggedIn;
root.invite.disabled = !loggedIn; root.invite.disabled = !loggedIn;
if(loggedIn) { if(loggedIn) {