minor cleanup in refreshSelectOptions
This commit is contained in:
parent
d9b6210841
commit
6c4cb52579
|
|
@ -988,7 +988,7 @@ $(function (){
|
|||
function updateTitle(opt){
|
||||
opt = $(opt);
|
||||
|
||||
const then = opt.data('then');
|
||||
const then = opt.data('timestamp');
|
||||
const now = +new Date();
|
||||
let age_str = '';
|
||||
if (then > now) {
|
||||
|
|
@ -1016,13 +1016,6 @@ $(function (){
|
|||
|
||||
const refreshSelectOptions = (function(){
|
||||
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);
|
||||
|
||||
if (!d.lightName && !d.darkName && !d.moves && key !== $('#cb_board').data('gameId')) {
|
||||
|
|
@ -1035,29 +1028,27 @@ $(function (){
|
|||
opt.attr('id', 'cb_game_' + key);
|
||||
}
|
||||
|
||||
let stat = '';
|
||||
if (d.status) {
|
||||
stat = ', ' + d.status;
|
||||
}
|
||||
const lightName = shortenName(String(d.lightName || 'Light'));
|
||||
const darkName = shortenName(String(d.darkName || 'Dark'));
|
||||
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('title', lightName + ' vs. ' + darkName + moves + stat);
|
||||
opt.data('then', d.timestamp || +new Date());
|
||||
opt.data('timestamp', timestamp);
|
||||
opt.addClass('cb-game-option');
|
||||
opt.appendTo('#cb_select_game');
|
||||
updateTitle(opt);
|
||||
|
||||
let select = $('#cb_select_game');
|
||||
let list = select.children('.cb-game-option').get();
|
||||
const select = $('#cb_select_game');
|
||||
const list = select.children('.cb-game-option').get();
|
||||
list.push(opt[0]);
|
||||
list.sort(function(a,b) {
|
||||
const then_a = $(a).data('then');
|
||||
const then_b = $(b).data('then');
|
||||
const then_a = $(a).data('timestamp');
|
||||
const then_b = $(b).data('timestamp');
|
||||
return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1;
|
||||
});
|
||||
|
||||
for (const e of list) {
|
||||
$(e).appendTo(select);
|
||||
}
|
||||
$(list).appendTo(select);
|
||||
}
|
||||
|
||||
let selOpt = $('#cb_game_' + $('#cb_board').data('gameId'));
|
||||
|
|
@ -1072,7 +1063,6 @@ $(function (){
|
|||
|
||||
return function() {
|
||||
$('#cb_select_game .cb-game-option').remove();
|
||||
updateSelectMeta(null, $('#cb_board').data('gameId'));
|
||||
|
||||
/* cancel and resubscribe to get all the cached data again */
|
||||
if (cancelMeta) {
|
||||
|
|
@ -1081,7 +1071,11 @@ $(function (){
|
|||
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) {
|
||||
debug('got meta for key ' + gameId, data);
|
||||
updateSelectMeta(data, gameId);
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue