From 6902af3db01df1c88f9a0f2afc86a3105576f800 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 10 Nov 2017 00:50:30 +0000 Subject: [PATCH] sort category listing --- www/res/puzzles.js | 88 +++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/www/res/puzzles.js b/www/res/puzzles.js index bbba387..1e1bfee 100644 --- a/www/res/puzzles.js +++ b/www/res/puzzles.js @@ -6,56 +6,62 @@ function loadPuzzle(cat, id, points) { } function puzzlesRefresh(term, obj) { - term.clear(); + term.clear(); + + let cats = []; + for (let cat in obj) { + cats.push(cat); + } + cats.sort(); - for (var cat in obj) { - var puzzles = obj[cat]; - - var pdiv = createElement('div'); - pdiv.className = 'category'; - - var h = createElement('h2'); - pdiv.appendChild(h); - h.textContent = cat; - - var l = createElement('ul'); - pdiv.appendChild(l); - - for (var puzzle of puzzles) { - var points = puzzle[0]; - var id = puzzle[1]; - - var i = createElement('li'); - l.appendChild(i); - - if (points == 0) { - i.textContent = "‡"; - } else { - var a = createElement('a'); - i.appendChild(a); - a.className = "link"; - a.textContent = points; - a.href = cat + "/" + id + "/index.html"; - // a.addEventListener("click", loadPuzzle.bind(undefined, cat, id, points)); + for (let cat of cats) { + let puzzles = obj[cat]; + + let pdiv = createElement('div'); + pdiv.className = 'category'; + + let h = createElement('h2'); + pdiv.appendChild(h); + h.textContent = cat; + + let l = createElement('ul'); + pdiv.appendChild(l); + + for (var puzzle of puzzles) { + var points = puzzle[0]; + var id = puzzle[1]; + + var i = createElement('li'); + l.appendChild(i); + + if (points === 0) { + i.textContent = "‡"; + } else { + var a = createElement('a'); + i.appendChild(a); + a.className = "link"; + a.textContent = points; + a.href = cat + "/" + id + "/index.html"; + // a.addEventListener("click", loadPuzzle.bind(undefined, cat, id, points)); } - } + } term.appendShallow(pdiv); - } + } } function puzzles_start() { - var element = document.getElementById("puzzles"); - var puzzlesTerminal = new Terminal(element); - var refreshInterval = 40 * 1000; + var element = document.getElementById("puzzles"); + var puzzlesTerminal = new Terminal(element); + var refreshInterval = 40 * 1000; - var refreshCallback = puzzlesRefresh.bind(undefined, puzzlesTerminal); - var refreshFunction = loadJSON.bind(undefined, puzzlesJsonUrl, refreshCallback); + var refreshCallback = puzzlesRefresh.bind(undefined, puzzlesTerminal); + var refreshFunction = loadJSON.bind(undefined, puzzlesJsonUrl, refreshCallback); - puzzlesTerminal.clear(); - puzzlesTerminal.par("Loading..."); - refreshFunction(); - setInterval(refreshFunction, refreshInterval); + puzzlesTerminal.clear(); + puzzlesTerminal.par("Loading..."); + refreshFunction(); + setInterval(refreshFunction, refreshInterval); } window.addEventListener("load", puzzles_start);