/* ZimiGames' shelf: two views of the same tiles, Lobby and Index, and seven orders.
The page is plain HTML and lists every game as a link without this file.
Here the tiles already in the page (the sections of index.html) are read, not
listed, so a game added to the shelf shows in both views, in the section chips
and in search with no change here. Lobby is a feed of art tiles; Index is
search first, one section at a time as thumbnail rows. The choice is kept in
localStorage ("zimi.games.view"). recent.js draws the game table and Game of the
day; this file only says where they sit.
The order control (kept in "zimi.games.sort") regroups the same tiles by what
games.json says of each game: when it began, where, how many play, how long it
takes. It is an enhancement: the page lists every game with or without it. */
(function (root) {
'use strict';
var T = function (key, vars) { return root.ZimiText ? root.ZimiText.T(key, vars) : key; };
var VIEW_KEY = 'zimi.games.view';
var SORT_KEY = 'zimi.games.sort';
var RECENT_KEY = 'zimi.games.recent';
var ROOM_KEY = 'zimi.games.room';
var VIEWS = ['lobby', 'index'];
// ── Orders (pure, no DOM; tests/test_games_meta.cjs) ─────────────────────
// Each game's facts live in games.json (origin, region, players, length, kind): the
// shelf only reads them. A tile with no row there (a game with no page of its own, or
// one added after this file) uses FALLBACK, and one with neither sorts last.
var SORTS = ['sections', 'az', 'oldest', 'region', 'players', 'quick', 'recent'];
// The names of the orders, and what the chips are headed in each: strings by id (shelf.sort.*, shelf.head.*).
var SORT_LABEL = {}, SORT_HEAD = {};
SORTS.forEach(function (k) {
Object.defineProperty(SORT_LABEL, k, { enumerable: true, get: function () { return T('shelf.sort.' + k); } });
Object.defineProperty(SORT_HEAD, k, { enumerable: true, get: function () { return T('shelf.head.' + k); } });
});
var REGIONS = ['East Asia', 'South Asia', 'Southeast Asia', 'Middle East', 'Africa', 'Europe', 'Americas', 'Oceania', 'Worldwide/Modern'];
var FALLBACK = {
'puzzles.html': { origin: { year: 2004, era: '2004' }, region: 'Worldwide/Modern', players: '1', length: '10 min', kind: 'puzzle', computer: false },
'stacker.html': { origin: { year: null, era: '1980s' }, region: 'Americas', players: '1', length: '5 min', kind: 'arcade', computer: false },
'whereintheworld.html': { origin: { year: null, era: null }, region: 'Worldwide/Modern', players: '1', length: '5 min', kind: 'library', computer: false }
};
// The eras of the timeline, oldest first; a game of no known age comes last.
var ERAS = [
{ id: 'ancient', to: 500 },
{ id: 'medieval', to: 1700 },
{ id: 'parlour', to: 1900 },
{ id: 'modern', to: 1970 },
{ id: 'video', to: Infinity }
];
var LENGTHS = [
{ id: 'l5', to: 5 }, { id: 'l10', to: 10 }, { id: 'l15', to: 15 }, { id: 'l30', to: 30 }, { id: 'l30p', to: Infinity }
];
var eraLabel = function (e) { return T('shelf.era.' + e.id); }, eraNote = function (e) { return T('shelf.era.' + e.id + '.note'); };
var lengthLabel = function (l) { return T('shelf.length.' + l.id); };
// "c. 500 BC" is -500, "1890s" is 1890, "c. AD 1150" is 1150; nothing readable is null.
function eraYear(era) {
var m = /(\d+)\s*(BC)?/.exec(era || '');
return m ? (m[2] ? -1 : 1) * Number(m[1]) : null;
}
// The year a game sorts by: its year, else the one its era names.
function ageOf(m) {
if (!m || !m.origin) return null;
return m.origin.year != null ? m.origin.year : eraYear(m.origin.era);
}
function yearText(y) { return y < 0 ? T('shelf.year.bc', { y: -y }) : String(y); }
// What a chip says of a game's age.
function ageText(m) {
if (!m || !m.origin) return '';
return m.origin.era || (m.origin.year != null ? yearText(m.origin.year) : '');
}
function maxPlayers(m) {
var all = /\d+/g, last = 0, n;
while ((n = all.exec(m && m.players || ''))) last = Number(n[0]);
return last;
}
function playersText(m) {
var p = m && m.players;
return p ? T('shelf.players.line', { p: p, n: maxPlayers(m) }) : '';
}
// Minutes from "15 min" ("30 min+" sorts just after "30 min"); 0 when unknown.
function lengthText(m) { var n = minutesOf(m); return n ? T('shelf.minutes' + (/\+/.test(m.length || '') ? '.plus' : ''), { n: Math.floor(n) }) : (m && m.length) || ''; }
function minutesOf(m) {
var n = /(\d+)\s*min(\+?)/.exec(m && m.length || '');
return n ? Number(n[1]) + (n[2] ? 0.5 : 0) : 0;
}
function eraOf(m) {
var y = ageOf(m);
if (y == null) return null;
for (var i = 0; i < ERAS.length; i++) if (y < ERAS[i].to) return ERAS[i];
return null;
}
function byTitle(a, b) { return a.title.localeCompare(b.title, root.ZimiText && root.ZimiText.lang); }
// Oldest first, games of no known age last, then by name.
function byAge(meta) {
return function (a, b) {
var x = ageOf(meta(a.path)), y = ageOf(meta(b.path));
if (x == null || y == null) return x == y ? byTitle(a, b) : (x == null ? 1 : -1);
return x - y || byTitle(a, b);
};
}
// items [{path, title}] -> [{id, label, note, region, items}] in display order, empty groups
// left out. meta(path) gives a game's row (or null); recent is the paths played, newest first.
// null for an order that is not a grouping (sections).
function groupsFor(sort, items, meta, recent) {
var out = [], by = {};
function add(id, label, note, region) {
if (!by[id]) { by[id] = { id: 'g-' + sort + '-' + id, label: label, note: note || '', items: [] }; if (region) by[id].region = region; out.push(by[id]); }
return by[id];
}
var sorted = items.slice();
if (sort === 'az') {
add('all', T('shelf.head.az')).items = sorted.sort(byTitle);
} else if (sort === 'oldest') {
ERAS.forEach(function (e) { add(e.id, eraLabel(e), eraNote(e)); });
add('unknown', T('shelf.era.unknown'), T('shelf.era.unknown.note'));
sorted.sort(byAge(meta)).forEach(function (it) { var e = eraOf(meta(it.path)); by[e ? e.id : 'unknown'].items.push(it); });
} else if (sort === 'region') {
REGIONS.forEach(function (r) { add(r, T('shelf.region.' + r), '', r); });
sorted.sort(byAge(meta)).forEach(function (it) { var m = meta(it.path); by[m && by[m.region] ? m.region : 'Worldwide/Modern'].items.push(it); });
} else if (sort === 'players') {
add('solo', T('shelf.players.solo'), T('shelf.players.solo.note'));
add('two', T('shelf.players.two'), T('shelf.players.two.note'));
add('many', T('shelf.players.many'), T('shelf.players.many.note'));
sorted.sort(byTitle).forEach(function (it) { var n = maxPlayers(meta(it.path)); by[n <= 1 ? 'solo' : n === 2 ? 'two' : 'many'].items.push(it); });
} else if (sort === 'quick') {
LENGTHS.forEach(function (l) { add(l.id, lengthLabel(l)); });
sorted.sort(byTitle).forEach(function (it) {
var n = minutesOf(meta(it.path));
for (var i = 0; i < LENGTHS.length; i++) if (n <= LENGTHS[i].to) { by[LENGTHS[i].id].items.push(it); break; }
});
} else if (sort === 'recent') {
var order = {};
(recent || []).forEach(function (p, i) { if (!(p in order)) order[p] = i; });
var seen = sorted.filter(function (it) { return it.path in order; }).sort(function (a, b) { return order[a.path] - order[b.path]; });
add('played', T('shelf.recent.played'), T('shelf.recent.played.note')).items = seen;
add('rest', seen.length ? T('shelf.recent.rest') : T('shelf.recent.none')).items = sorted.filter(function (it) { return !(it.path in order); }).sort(byTitle);
} else return null;
return out.filter(function (g) { return g.items.length; });
}
// ── Rooms (pure): a feel to play in, across the types. Membership is what games.json already says of a game (its kind, how many
// play), with one short list for the kids' games no fact names. No room is themed yet: a room is a filter.
var ROOMS = ['all', 'kids', 'arcade', 'night', 'solo'];
var KIDS_TOO = { 'fourinarow.html': 1, 'dotsandboxes.html': 1, 'crazyeights.html': 1, 'higherlower.html': 1, 'jigsaw.html': 1, 'ludo.html': 1 };
function inRoom(room, m, path) {
if (room === 'all') return true;
if (!m) return false;
if (room === 'kids') return m.kind === 'kids' || !!KIDS_TOO[path];
if (room === 'arcade') return m.kind === 'arcade';
if (room === 'night') return maxPlayers(m) >= 3; // a table of three or more: people, or the computer's seats
if (room === 'solo') return maxPlayers(m) === 1 && m.kind !== 'arcade'; // the quiet one: puzzles, solitaires, words
return true;
}
var api = { ROOMS: ROOMS, KIDS_TOO: KIDS_TOO, inRoom: inRoom, SORTS: SORTS, SORT_LABEL: SORT_LABEL, REGIONS: REGIONS, ERAS: ERAS, FALLBACK: FALLBACK, eraYear: eraYear, ageOf: ageOf, ageText: ageText, playersText: playersText, maxPlayers: maxPlayers, minutesOf: minutesOf, eraOf: eraOf, groupsFor: groupsFor };
if (typeof module !== 'undefined' && module.exports) { module.exports = api; return; }
var doc = root.document;
var shelf = doc && doc.querySelector('.ar-shelf');
if (!shelf) return;
var script = doc.currentScript;
var base = script && script.src ? new URL('.', script.src) : null;
var SEARCH_ICON = '';
// Zimi's Home sort mark (static/app.js _LIB_SORT_SVG).
var CHEVRON_ICON = '';
// The view and order button's mark: two sliders.
var OPT_ICON = '';
var VIEW_LABEL = { lobby: 'shelf.view.lobby', index: 'shelf.view.index' };
var SVG_NS = 'http://www.w3.org/2000/svg';
// Where each region sits on world-land.js's 800 x 400 grid: [x, y, radius] spots.
var REGION_SPOTS = {
'East Asia': [[655, 122, 46]], 'South Asia': [[573, 151, 34]], 'Southeast Asia': [[640, 182, 34]],
'Middle East': [[500, 138, 34]], 'Africa': [[446, 205, 56]], 'Europe': [[438, 88, 40]],
'Americas': [[180, 108, 60], [268, 236, 56]], 'Oceania': [[715, 255, 46]]
};
function el(tag, cls, html) {
var e = doc.createElement(tag);
if (cls) e.className = cls;
if (html != null) e.innerHTML = html;
return e;
}
function stored(key, allowed, fallback) {
try {
var v = root.localStorage.getItem(key);
if (allowed.indexOf(v) >= 0) return v;
} catch (e) { /* blocked storage: the default */ }
return fallback;
}
function store(key, v) {
try { root.localStorage.setItem(key, v); } catch (e) { /* nothing to remember it in */ }
}
// The pages played lately, newest first (recent.js keeps the list).
function readRecent() {
try {
var v = JSON.parse(root.localStorage.getItem(RECENT_KEY));
return Array.isArray(v) ? v.filter(function (e) { return e && e.path; }).map(function (e) { return e.path; }) : [];
} catch (e) { return []; }
}
// The shelf's own words are written in the page in English. When the player's language has them, they are swapped in
// before anything below reads them (text.js reads the language's file while the page is parsed).
(function localizeShelf() {
var Z = root.ZimiText, intro = shelf.querySelector(':scope > header > p');
if (!Z) return;
var swap = function (node, key, attr) {
if (!node || !Z.has(key)) return;
if (attr) node.setAttribute(attr, T(key)); else node.textContent = T(key);
};
swap(intro, 'shelf.intro');
swap(doc.querySelector('#ar-credits > summary'), 'shelf.credits');
doc.documentElement.style.setProperty('--play-label', JSON.stringify(T('shelf.play')));
Array.prototype.forEach.call(shelf.querySelectorAll('.sh-list > .ar-section'), function (s) {
var h = s.querySelector('h2'), key = 'shelf.section.' + (h.id || '').replace(/^ar-/, '');
swap(h, key);
swap(s.querySelector('nav.ar-games'), key, 'aria-label');
});
Array.prototype.forEach.call(shelf.querySelectorAll('.ar-family'), function (f) {
var slug = (f.getAttribute('data-name') || '').toLowerCase().replace(/[^a-z]/g, '');
swap(f, 'shelf.family.' + slug + '.name', 'data-name');
swap(f, 'shelf.family.' + slug + '.line', 'data-line');
});
if (root.ZimiShell && root.ZimiShell.localizeTiles) root.ZimiShell.localizeTiles(shelf);
})();
// The sections, read from the page: id, label and the tiles each holds.
var list = shelf.querySelector('.sh-list');
var sections = Array.prototype.map.call(list.querySelectorAll(':scope > .ar-section'), function (s) {
var h = s.querySelector('h2');
var tiles = Array.prototype.slice.call(s.querySelectorAll('a.ar-game'));
h.appendChild(el('span', 'sh-count', String(tiles.length)));
var grid = s.querySelector('.ar-games');
return { id: h.id, label: h.firstChild.textContent, node: s, grid: grid, tiles: tiles, entries: Array.prototype.slice.call(grid.children) };
});
var total = sections.reduce(function (n, s) { return n + s.tiles.length; }, 0);
var tilePath = new Map(), tileTitle = new Map();
sections.forEach(function (s) {
s.tiles.forEach(function (t) {
tilePath.set(t, t.getAttribute('href'));
tileTitle.set(t, t.querySelector('strong').textContent);
});
});
// A tile's words, lowercased, for search: its name and its line, the section and family it sits in ("cards", "logic
// puzzles"), and, once known, where and when.
var words = new Map();
sections.forEach(function (s) {
s.tiles.forEach(function (t) {
var fam = t.closest('.ar-family');
words.set(t, (t.textContent + ' ' + s.label + ' ' + (fam ? fam.getAttribute('data-name') : '')).toLowerCase());
});
});
// ── Families: Chess and its cousins, the Solitaires, Draughts ────────────
// The page wraps each family's tiles in
. Here the wrapper
// gets a card (the family's name, how many, a mosaic of its games) and the tiles move into a panel the card
// opens. A tile is still a tile: search, the orders and the game table all go on seeing every variant.
var families = Array.prototype.map.call(shelf.querySelectorAll('.ar-family'), function (node) { return makeFamily(node); });
function makeFamily(node) {
var tiles = Array.prototype.slice.call(node.querySelectorAll('a.ar-game'));
var card = el('button', 'ar-game sh-fam-card');
card.type = 'button';
card.setAttribute('aria-expanded', 'false');
var art = el('span', 'ar-game-art sh-fam-art');
tiles.slice(0, 4).forEach(function (t) {
var img = el('img');
img.alt = '';
img.loading = 'lazy';
img.src = 'thumbs/' + t.getAttribute('href').replace(/\.html$/, '.svg');
art.appendChild(img);
});
var text = el('span', 'ar-game-text');
text.appendChild(el('strong')).textContent = node.getAttribute('data-name');
text.appendChild(el('span')).textContent = T('shelf.family.line', { n: tiles.length, line: node.getAttribute('data-line') });
var tag = el('span', 'sh-fam-tag');
tag.appendChild(el('b')).textContent = String(tiles.length);
tag.insertAdjacentHTML('beforeend', CHEVRON_ICON);
card.appendChild(art);
card.appendChild(text);
card.appendChild(tag);
var panel = el('div', 'ar-games sh-fam-panel');
tiles.forEach(function (t) { panel.appendChild(t); });
node.classList.add('sh-fam');
node.appendChild(card);
node.appendChild(panel);
var fam = { node: node, tiles: tiles, panel: panel, card: card, user: null };
card.addEventListener('click', function () { fam.user = !node.classList.contains('sh-open'); syncFamilies(); });
return fam;
}
// A section on its own shows every game it holds, so its number is what is on the screen: its families are open
// (a family card chosen by hand stays as the player left it until the section changes).
function syncFamilies() {
var isolated = state.q.trim() === '' && state.sec !== 'all' && state.sec !== 'recent';
families.forEach(function (f) {
var open = f.user === null ? isolated : f.user;
f.node.classList.toggle('sh-open', open);
f.card.setAttribute('aria-expanded', String(open));
});
}
var state = { view: stored(VIEW_KEY, VIEWS, 'lobby'), sort: stored(SORT_KEY, SORTS, 'sections'), room: stored(ROOM_KEY, ROOMS, 'all'), sec: 'all', q: '' };
var rows = null; // games.json by page, once read
var groups = sections; // what is on the shelf now: the sections, or the groups of an order
var made = []; // group nodes this file made for an order
var tableCount = 0;
var landState = '';
function meta(path) { return (rows && rows[path]) || FALLBACK[path] || null; }
root.ZimiShelf = { meta: meta }; // a tile's own row, for its long-press menu (tilemenu.js)
function sortedNow() { return rows && state.sort !== 'sections' ? state.sort : 'sections'; }
// Index opens on its first section; a room opens whole (a type chip then narrows it).
function defaultSection() { return state.room === 'all' && state.view === 'index' && sortedNow() === 'sections' && sections.length ? sections[0].id : 'all'; }
state.sec = defaultSection();
if (state.sort !== 'sections') shelf.classList.add('sh-wait');
// ── Header: search; the line below it: the room, the types, the view and order ──
var header = shelf.querySelector('header');
var tools = el('div', 'sh-tools');
var search = el('label', 'sh-search', SEARCH_ICON);
var input = el('input');
input.type = 'search';
input.setAttribute('aria-label', T('shelf.search'));
input.autocomplete = 'off';
input.spellcheck = false;
search.appendChild(input);
// The view and the order are set now and then, not every visit: one small button at the end of the line opens both, so the
// line keeps its width for the games. Its mark turns accent while an order other than Sections is on.
var optBtn = el('button', 'sh-opt', OPT_ICON);
optBtn.type = 'button';
optBtn.title = T('shelf.viewSort');
optBtn.setAttribute('aria-label', T('shelf.viewSort'));
optBtn.setAttribute('aria-haspopup', 'dialog');
var sortReady = false;
function optRow(label, choices, now, pick) {
var row = el('div', 'sh-opt-row');
row.appendChild(el('h3', 'gs-subhead', '')).textContent = label;
var seg = el('div', 'sh-opt-seg');
seg.setAttribute('role', 'group');
seg.setAttribute('aria-label', label);
choices.forEach(function (c) {
var b = el('button', 'ar-btn');
b.type = 'button';
b.textContent = c[1];
b.setAttribute('aria-pressed', String(c[0] === now));
b.addEventListener('click', function () { pick(c[0]); });
seg.appendChild(b);
});
row.appendChild(seg);
return row;
}
optBtn.addEventListener('click', function () {
if (!root.ZimiShell || !root.ZimiShell.sheet) return;
var api, body = [optRow(T('shelf.view'), [['lobby', T(VIEW_LABEL.lobby)], ['index', T(VIEW_LABEL.index)]], state.view, function (v) { api.close(); setView(v); })];
if (sortReady) body.push(optRow(T('shelf.sortGames'), SORTS.map(function (k) { return [k, SORT_LABEL[k]]; }), state.sort, function (k) { api.close(); setSort(k); }));
api = root.ZimiShell.sheet({ title: T('shelf.viewSort'), body: body, actions: [{ label: T('shell.close'), primary: true }] });
});
// The room, at the start of the line: one pill showing it, the native list of rooms lying over it (kept like the view).
var roomPick = el('label', 'sh-room-pick');
var roomNow = el('span', 'sh-room-now', '');
var roomSel = el('select', 'sh-room-sel');
roomSel.setAttribute('aria-label', T('shelf.room'));
ROOMS.forEach(function (id) { var o = el('option', '', ''); o.value = id; o.textContent = T('shelf.room.' + id); roomSel.appendChild(o); });
roomSel.addEventListener('change', function () { setRoom(roomSel.value); });
roomPick.appendChild(roomNow);
roomPick.insertAdjacentHTML('beforeend', CHEVRON_ICON);
roomPick.appendChild(roomSel);
// In Zimi the top bar is the search box (Zimi calls shelfSearch / shelfGo below), so the page has none of its own:
// one search box. Out of Zimi (a ZIM opened by itself) the page keeps its box.
var hosted = !!root.parent && root.parent !== root;
if (!hosted) header.appendChild(search);
tools.appendChild(optBtn);
// ── Body: the rail (section chips), the top (the game table, Game of the day), the list ──
var body = el('div', 'sh-body');
var rail = el('div', 'sh-rail');
var nav = el('nav', 'sh-nav');
nav.setAttribute('aria-label', T('shelf.head.sections'));
var chipBar = el('div', 'sh-bar');
chipBar.appendChild(roomPick);
chipBar.appendChild(nav);
rail.appendChild(chipBar);
var main = el('div', 'sh-main');
var top = el('div', 'sh-top');
var empty = el('p', 'sh-empty', T('shelf.noMatch'));
empty.hidden = true;
list.parentNode.insertBefore(body, list);
main.appendChild(top);
main.appendChild(list);
main.appendChild(empty);
body.appendChild(rail);
body.appendChild(main);
// The chips (tabs in Index on a phone, a rail on a desktop): All, Recent, then one per group.
var chips = {};
var recentChip;
function chip(id, label, count, cls) {
var b = el('button', 'sh-chip' + (cls ? ' ' + cls : ''));
b.type = 'button';
b.setAttribute('aria-pressed', 'false');
b.appendChild(doc.createTextNode(label));
var n = el('span', 'sh-chip-n', String(count));
b.appendChild(n);
b.addEventListener('click', function () { state.sec = id; state.q = ''; input.value = ''; apply(); });
chips[id] = { btn: b, n: n };
nav.appendChild(b);
return b;
}
function setRoom(id) {
state.room = id;
state.sec = 'all'; // a room opens whole, and All is every game; a type chip then narrows it
store(ROOM_KEY, id);
apply();
}
function roomOf(tile) { var p = tilePath.get(tile) || (tile.getAttribute('href') || '').replace(/[?#].*$/, ''); return inRoom(state.room, meta(p), p); }
function buildChips() {
nav.textContent = '';
chips = {};
chip('all', T('shelf.chip.allTypes'), total, 'sh-chip-all'); // every type (the room pill says which room); quiet, being no choice
recentChip = chip('recent', T('shelf.chip.recent'), tableCount, 'sh-chip-recent');
recentChip.hidden = !tableCount;
nav.setAttribute('aria-label', SORT_HEAD[sortedNow()] || T('shelf.head.sections'));
// One group is no choice: A to Z has no type chips, only the rooms.
if (groups.length < 2) return;
nav.appendChild(el('span', 'sh-rail-head', SORT_HEAD[sortedNow()]));
groups.forEach(function (g) { chip(g.id, g.label, g.tiles.length); });
}
// ── Tiles: what and when, in small chips ─────────────────────────────────
// Index shows them on every row; the other orders show them in Lobby too, since the order is the point.
function metaLine(path) {
var m = meta(path);
if (!m) return null;
var line = el('small', 'sh-meta');
[[ageText(m), 'sh-m-age'], [T('shelf.region.' + m.region), 'sh-m-region'], [playersText(m), 'sh-m-players'], [state.sort === 'quick' ? lengthText(m) : '', 'sh-m-length']].forEach(function (c) {
if (!c[0]) return;
var e = el('em', c[1]);
e.textContent = c[0];
line.appendChild(e);
});
return line.children.length ? line : null;
}
function decorate(tile, path) {
var text = tile.querySelector('.ar-game-text');
if (!text) return;
var old = text.querySelector('.sh-meta');
if (old) old.remove();
var line = metaLine(path);
if (line) text.appendChild(line);
}
function decorateAll() {
sections.forEach(function (s) { s.tiles.forEach(function (t) { decorate(t, tilePath.get(t)); }); });
}
// ── Layout: the sections as written, or the groups of an order ───────────
function groupNode(def, tiles) {
var s = el('section', 'ar-section sh-group');
s.setAttribute('aria-labelledby', def.id);
var h = el('h2');
h.id = def.id;
if (def.region && landState === 'ready') h.appendChild(regionMap(def.region));
var name = el('span', 'sh-gt');
name.textContent = def.label;
h.appendChild(name);
if (def.note) {
var note = el('span', 'sh-note');
note.textContent = def.note;
h.appendChild(note);
}
h.appendChild(el('span', 'sh-count', String(tiles.length)));
var grid = el('nav', 'ar-games');
grid.setAttribute('aria-label', def.label);
tiles.forEach(function (t) { grid.appendChild(t); });
s.appendChild(h);
s.appendChild(grid);
return { id: def.id, label: def.label, node: s, grid: grid, tiles: tiles };
}
// The world in one grey outline (world-land.js, loaded the first time By region is chosen), a region glowing on it.
function regionMap(region) {
var svg = doc.createElementNS(SVG_NS, 'svg');
svg.setAttribute('class', 'sh-map');
svg.setAttribute('viewBox', '0 0 800 400');
svg.setAttribute('aria-hidden', 'true');
var land = doc.createElementNS(SVG_NS, 'path');
land.setAttribute('d', root.WORLD_LAND_D);
land.setAttribute('class', 'sh-land');
svg.appendChild(land);
(REGION_SPOTS[region] || [[400, 200, 400]]).forEach(function (s) {
var c = doc.createElementNS(SVG_NS, 'circle');
c.setAttribute('cx', s[0]); c.setAttribute('cy', s[1]); c.setAttribute('r', s[2]);
c.setAttribute('class', 'sh-spot');
svg.appendChild(c);
});
if (!REGION_SPOTS[region]) svg.setAttribute('class', 'sh-map sh-map-all');
return svg;
}
function withLand(done) {
if (landState || !base) { done(); return; }
landState = 'loading';
var s = el('script');
s.src = new URL('world-land.js', base).href;
s.onload = function () { landState = root.WORLD_LAND_D ? 'ready' : 'none'; done(); };
s.onerror = function () { landState = 'none'; done(); };
doc.head.appendChild(s);
}
function layout() {
families.forEach(function (f) { f.tiles.forEach(function (t) { f.panel.appendChild(t); }); });
sections.forEach(function (s) { s.entries.forEach(function (e) { s.grid.appendChild(e); }); });
made.forEach(function (g) { g.node.remove(); });
made = [];
var order = sortedNow();
var defs = order === 'sections' ? null : groupsFor(order, sections.reduce(function (all, s) {
return all.concat(s.tiles.map(function (t) { return { path: tilePath.get(t), title: tileTitle.get(t), el: t }; }));
}, []), meta, readRecent());
if (defs) {
groups = defs.map(function (d) {
var g = groupNode(d, d.items.map(function (it) { return it.el; }));
list.appendChild(g.node);
made.push(g);
return g;
});
} else groups = sections;
// An order lays every game out on its own; the families keep to the page's own sections.
families.forEach(function (f) { f.node.hidden = !!defs; });
shelf.dataset.sort = order;
if (state.sec !== 'all' && state.sec !== 'recent' && !groups.some(function (g) { return g.id === state.sec; })) state.sec = defaultSection();
decorateAll();
buildChips();
apply();
}
// ── Layout and filtering ─────────────────────────────────────────────────
function daily() { return doc.querySelector('.ar-daily'); }
// Continue and the Daily row (Game of the day leads it) come from recent.js, after the page is up.
// The order and the view switch share the chips' row, so no row is spent on them. Index on a wide screen has the
// chips down a rail, so there they stay on the header's row. Out of Zimi the header holds the search box as well.
var wide = root.matchMedia('(min-width: 900px)');
function placeTools() {
var target = state.view === 'index' && wide.matches ? header : chipBar;
if (tools.parentNode !== target) target.appendChild(tools);
}
if (wide.addEventListener) wide.addEventListener('change', placeTools);
function place() {
var c = doc.querySelector('.ar-continue');
if (c && c.parentNode !== top) top.appendChild(c);
// Under the table, in this order: the world's favorites, what is new, then the rest.
var prev = c;
['.ar-world', '.ar-new'].forEach(function (sel) {
var r = doc.querySelector(sel);
if (!r) return;
if (r.parentNode !== top || r.previousSibling !== prev) top.insertBefore(r, prev ? prev.nextSibling : top.firstChild);
prev = r;
});
var dl = daily();
if (dl && dl.parentNode !== top) top.appendChild(dl);
tableCount = c && !c.classList.contains('ar-table-idle') ? c.querySelectorAll('.ar-box').length : 0;
if (recentChip) {
recentChip.hidden = !tableCount;
chips.recent.n.textContent = String(tableCount);
}
if (!tableCount && state.sec === 'recent') state.sec = defaultSection();
}
var lastSec = null;
function apply() {
if (state.sec !== lastSec) { lastSec = state.sec; families.forEach(function (f) { f.user = null; }); }
syncFamilies();
var q = state.q.trim().toLowerCase();
var searching = q !== '';
shelf.dataset.view = state.view;
shelf.dataset.sec = searching ? 'search' : state.sec;
shelf.classList.add('sh-on');
// A phone's lobby header has room for one word beside the mark.
input.placeholder = state.view === 'lobby' && root.matchMedia('(max-width: 699px)').matches ? T('shelf.searchShort') : T('shelf.searchN', { n: total });
Object.keys(chips).forEach(function (id) { chips[id].btn.setAttribute('aria-pressed', String(!searching && id === state.sec)); });
roomNow.textContent = T('shelf.room.' + state.room);
roomSel.value = state.room;
roomPick.classList.toggle('sh-room-on', state.room !== 'all');
shelf.dataset.room = state.room;
var shown = 0, roomAll = 0;
sections.concat(made).forEach(function (s) {
if (groups.indexOf(s) < 0) { s.node.hidden = true; return; }
var n = 0, inRoomN = 0;
s.tiles.forEach(function (t) {
var room = roomOf(t), hit = room && (!searching || words.get(t).indexOf(q) >= 0);
t.hidden = !hit;
if (hit) n++;
if (room) inRoomN++;
});
// A type chip counts what the room leaves of its group, and goes when the room leaves none.
if (chips[s.id]) { chips[s.id].n.textContent = String(inRoomN); chips[s.id].btn.hidden = !inRoomN; }
roomAll += inRoomN;
var inSec = searching || state.sec === 'all' || state.sec === s.id;
s.node.hidden = !inSec || n === 0;
s.node.querySelector('.sh-count').textContent = String(n);
if (inSec) shown += n;
});
if (chips.all) chips.all.n.textContent = String(roomAll);
var c = doc.querySelector('.ar-continue');
// The feed shows the table and the Daily row on All only; Index keeps them on every tab.
var feedAll = state.sec === 'all';
if (c) c.hidden = searching || (state.view === 'lobby' && !feedAll);
var rowsTop = Array.prototype.slice.call(doc.querySelectorAll('.ar-world, .ar-new'));
rowsTop.forEach(function (r) { r.hidden = searching || !feedAll || state.room !== 'all'; }); // like the Daily row: the unfiltered shelf's opening
var dl = daily();
if (dl) {
var inside = 0, done = 0;
Array.prototype.forEach.call(dl.querySelectorAll('a.ar-daily-main'), function (a) { inside++; if (a.classList.contains('ar-done')) done++; });
if (dl.dailyLine) dl.dailyLine(done, inside);
// The day's games are the unfiltered shelf's opening: with a room or a type chosen (or a search) they step aside, so the
// games asked for come first.
dl.hidden = searching || !feedAll || state.room !== 'all';
}
if (state.sec === 'recent' && !searching) { sections.concat(made).forEach(function (s) { s.node.hidden = true; }); shown = 1; }
empty.hidden = shown > 0;
top.hidden = [c, dl].concat(rowsTop).every(function (x) { return !x || x.hidden; });
}
function setView(v) {
if (v === state.view) return;
state.view = v;
state.sec = defaultSection();
store(VIEW_KEY, v);
place();
placeTools();
apply();
}
function setSort(k) {
if (SORTS.indexOf(k) < 0 || !rows) return;
state.sort = k;
store(SORT_KEY, k);
state.sec = defaultSection();
if (k === 'region') withLand(layout); else layout();
}
function setQuery(v) { state.q = String(v || ''); input.value = state.q; apply(); }
// The first game the search leaves on the shelf, in the order it is shown.
function topHit() {
var tiles = shelf.querySelectorAll('a.ar-game');
for (var i = 0; i < tiles.length; i++) if (!tiles[i].hidden && !tiles[i].closest('[hidden]')) return tiles[i];
return null;
}
// Zimi's top bar asks: what is typed filters the shelf as you type, Enter plays the first game left.
root.shelfSearch = setQuery;
root.shelfGo = function (v) {
if (v != null && String(v) !== state.q) setQuery(v);
var hit = state.q.trim() ? topHit() : null;
if (hit) hit.click();
};
input.addEventListener('input', function () { state.q = input.value; apply(); });
input.addEventListener('keydown', function (e) { if (e.key === 'Enter') root.shelfGo(); });
doc.addEventListener('keydown', function (e) {
if (e.key === '/' && !e.ctrlKey && !e.metaKey && !e.altKey && doc.activeElement !== input && !/^(INPUT|TEXTAREA|SELECT)$/.test(doc.activeElement && doc.activeElement.tagName)) {
e.preventDefault();
var bar = null;
try { bar = hosted && root.parent.document.getElementById('q'); } catch (err) { /* another origin: no bar to reach */ }
(bar || input).focus();
}
});
doc.addEventListener('zimi-shelf-today', function () { place(); apply(); });
buildChips();
place();
placeTools();
apply();
// games.json is the shelf's own data (a file of this ZIM). Until it is read, or if it cannot be
// (a page opened from a place that cannot fetch), the shelf is just its sections.
function ready(list) {
rows = {};
(list || []).forEach(function (r) { if (r && r.path && !rows[r.path] && r.origin) rows[r.path] = r; });
words.forEach(function (w, t) {
var m = meta(tilePath.get(t));
if (m) words.set(t, w + ' ' + [m.region, ageText(m), m.kind, playersText(m), m.computer ? 'computer' : ''].join(' ').toLowerCase());
});
sortReady = true;
shelf.classList.remove('sh-wait');
if (state.sort === 'region') withLand(layout); else layout();
}
if (base) {
root.fetch(new URL('games.json', base).href).then(function (r) { return r.ok ? r.json() : []; }).catch(function () { return []; }).then(function (data) {
if (data && data.length) ready(data); else { state.sort = 'sections'; shelf.classList.remove('sh-wait'); }
});
} else shelf.classList.remove('sh-wait');
})(typeof window !== 'undefined' ? window : globalThis);