X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/588ae20483b15e9accb6a14090f127ef87cbf4a5..20332e82f5c6365a8d65c75192985d846a99a2dd:/word/quiz.js diff --git a/word/quiz.js b/word/quiz.js index 2385455..d32368b 100644 --- a/word/quiz.js +++ b/word/quiz.js @@ -8,21 +8,30 @@ Array.prototype.shuffle = function () { class WordQuiz { dataselect(json) { + this.data = json; + this.cats = {}; // category lookup + for (let i in json) { + let cat = json[i][3]; + if (this.cats[cat]) { + this.cats[cat].push(i); + } + else { + this.cats[cat] = [i]; + } + } + return this.datafilter(json); + } + + datafilter(json) { // find viable rows from json data let rows = Object.values(json); - if (this.preset.cat !== undefined) { - let cats = {}; // category lookup - for (let i in json) { - let cat = json[i][3]; - if (!cats[cat]) cats[cat] = []; - cats[cat].push(i) - } + if (this.preset.cat !== undefined) { rows = []; - let children = cats[this.preset.cat]; + let children = this.cats[this.preset.cat]; for (let loop = 0; children.length && loop < 20; loop++) { rows.push(...children); - children = children.map(cat => cats[cat]).filter(is => is).flat(); + children = children.map(cat => this.cats[cat]).filter(is => is).flat(); } rows = rows.map(row => json[row]).filter(row => row[2]); } @@ -58,7 +67,21 @@ class WordQuiz { }); } + log(...args) { + this.history.push([new Date().toISOString(), ...args]); + } + + stop(...args) { + this.log(...args); + window.onbeforeunload = null; + fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)}); + } + constructor(dataurl) { this.load(dataurl); + this.history = []; + window.onbeforeunload = e => { + this.stop('abort'); + }; } }