webclient/js/GUI/Screen/Hall/Players.js

66 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

import Games;
import * as ConnectedForm from GUI.ConnectedForm;
import * as ListSelector from GUI.ListSelector;
import * as GamesGUI from GUI.Screen.Hall.Games;
import I18n;
import Messaging;
import players from Room;
import * as Dom from UnitJS.Dom;
var form;
var list;
var them;
return {
init: init,
refresh: refresh
};
function init() {
form = ConnectedForm.get('room');
list = ListSelector.make('players', showPlayer);
form.root.getElementsByTagName('label')[0].textContent = I18n.get('startGameWith');
form.root.invite.value = I18n.get('invite');
form.root.addEventListener('submit', function(e) {
e.preventDefault();
Messaging.send({tag: "Invitation", to: them});
Games.proposal(them, false);
GamesGUI.refresh();
});
form.root.them.addEventListener("input", function() {refresh();});
}
function showPlayer(player) {
return Dom.make('li', {
textContent: player.value,
onClick: function() {form.root.them.value = player.value; refresh();},
class: 'clickable'
});
}
function refresh() {
var name = form.root.them.value;
them = null;
var filtered = players.getAll(
function(player) {return player.value.match(name);}
);
list.refresh(filtered);
var exact = filtered.find(exactMatch(name));
if(exact != undefined) {
them = exact.key;
} else if(filtered.length == 1) {
them = filtered[0].key;
} else if(filtered.length == 0) {
list.message.textContent = I18n.get(
name.length > 0 ? "notFound" : "alone"
);
}
form.enable(them != undefined);
}
function exactMatch(name) {
return function(player) {
return player.value === name;
};
}