minor cleanup in refreshSelectOptions

This commit is contained in:
Jesse D. McDonald 2020-04-29 02:17:56 -05:00
parent d9b6210841
commit 6c4cb52579
1 changed files with 17 additions and 23 deletions

View File

@ -988,7 +988,7 @@ $(function (){
function updateTitle(opt){ function updateTitle(opt){
opt = $(opt); opt = $(opt);
const then = opt.data('then'); const then = opt.data('timestamp');
const now = +new Date(); const now = +new Date();
let age_str = ''; let age_str = '';
if (then > now) { if (then > now) {
@ -1016,13 +1016,6 @@ $(function (){
const refreshSelectOptions = (function(){ const refreshSelectOptions = (function(){
function updateSelectMeta(d, key) { function updateSelectMeta(d, key) {
d = d || {};
debug('got meta for key ' + key, d);
const lightName = shortenName(String(d.lightName || 'Light'));
const darkName = shortenName(String(d.darkName || 'Dark'));
const moves = !d.moves ? '' :
(', ' + d.moves + (d.moves === 1 ? ' turn' : ' turns'));
let opt = $('#cb_game_' + key); let opt = $('#cb_game_' + key);
if (!d.lightName && !d.darkName && !d.moves && key !== $('#cb_board').data('gameId')) { if (!d.lightName && !d.darkName && !d.moves && key !== $('#cb_board').data('gameId')) {
@ -1035,29 +1028,27 @@ $(function (){
opt.attr('id', 'cb_game_' + key); opt.attr('id', 'cb_game_' + key);
} }
let stat = ''; const lightName = shortenName(String(d.lightName || 'Light'));
if (d.status) { const darkName = shortenName(String(d.darkName || 'Dark'));
stat = ', ' + d.status; const moves = !d.moves ? '' : `, ${d.moves} turn${d.moves === 1 ? '' : 's'}`;
} const stat = !d.status ? '' : `, ${d.status}`;
const timestamp = d.timestamp || +new Date();
opt.data('gameId', key); opt.data('gameId', key);
opt.data('title', lightName + ' vs. ' + darkName + moves + stat); opt.data('title', lightName + ' vs. ' + darkName + moves + stat);
opt.data('then', d.timestamp || +new Date()); opt.data('timestamp', timestamp);
opt.addClass('cb-game-option'); opt.addClass('cb-game-option');
opt.appendTo('#cb_select_game');
updateTitle(opt); updateTitle(opt);
let select = $('#cb_select_game'); const select = $('#cb_select_game');
let list = select.children('.cb-game-option').get(); const list = select.children('.cb-game-option').get();
list.push(opt[0]);
list.sort(function(a,b) { list.sort(function(a,b) {
const then_a = $(a).data('then'); const then_a = $(a).data('timestamp');
const then_b = $(b).data('then'); const then_b = $(b).data('timestamp');
return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1; return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1;
}); });
$(list).appendTo(select);
for (const e of list) {
$(e).appendTo(select);
}
} }
let selOpt = $('#cb_game_' + $('#cb_board').data('gameId')); let selOpt = $('#cb_game_' + $('#cb_board').data('gameId'));
@ -1072,7 +1063,6 @@ $(function (){
return function() { return function() {
$('#cb_select_game .cb-game-option').remove(); $('#cb_select_game .cb-game-option').remove();
updateSelectMeta(null, $('#cb_board').data('gameId'));
/* cancel and resubscribe to get all the cached data again */ /* cancel and resubscribe to get all the cached data again */
if (cancelMeta) { if (cancelMeta) {
@ -1081,7 +1071,11 @@ $(function (){
cancelMeta(true); cancelMeta(true);
} }
/* ensure there is an entry for the current game, whatever the server says */
updateSelectMeta({}, $('#cb_board').data('gameId'));
cancelMeta = IO.onMetaUpdate(function(data, gameId) { cancelMeta = IO.onMetaUpdate(function(data, gameId) {
debug('got meta for key ' + gameId, data);
updateSelectMeta(data, gameId); updateSelectMeta(data, gameId);
}); });
}; };