X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/8ed7634d0fa3b7154275c14477b361b358ee783f..9b8d7e610cef31f15f12c1058dfd45efffac5fb0:/word/quiz.js diff --git a/word/quiz.js b/word/quiz.js index a7194bb..f47fde1 100644 --- a/word/quiz.js +++ b/word/quiz.js @@ -23,15 +23,20 @@ class WordQuiz { let ids = new Set(Object.keys(json)); const selection = {...json}; // clone + for (let cat of selection[''][3]) { + if (selection[cat]) + selection[cat][1] = 0; // keep root categories + } + if (this.preset.cat !== undefined) { ids.clear(); - let children = [this.preset.cat]; + let children = this.preset.cat; for (let loop = 0; children.length && loop < 20; loop++) { for (let child of children) ids.add(child.toString()); children = children.map(cat => json[cat][3]).filter(is => is).flat() } } - if (this.preset.image) { + if (this.preset.images) { ids = ids.filter(id => json[id][2]); } if (this.preset.level !== undefined) { @@ -50,23 +55,30 @@ class WordQuiz { selection[id][3] = function subresolve(subs) { //console.log(subs); return (subs || []).flatMap(sub => - sub in selection ? [sub] : subresolve(json[sub][3]) + sub in selection ? [sub] : json[sub] ? subresolve(json[sub][3]) : [] ); }(selection[id][3]); } return selection; } - load(dataurl) { - this.preset = {}; - let input; - if (input = window.location.hash.match(/\d+/)) { - this.preset.cat = parseInt(input[0]); - } - if (window.location.hash.match(/a/)) { - this.preset.level = 3; + configure(params) { + const opts = new Map(params.map(arg => arg.split(/[:=](.*)/))); + for (let [query, val] of opts) { + if (query.match(/^\d+$/)) { + this.preset.cat = [parseInt(query)]; + } + else if (query === 'level') { + this.preset.level = parseInt(val); + } + else { + this.preset[query] = val; + } } + } + load(dataurl) { + this.configure(window.location.hash.split('#')); fetch(dataurl).then(res => res.json()).then(json => { this.words = this.dataselect(json) this.setup(); @@ -84,6 +96,7 @@ class WordQuiz { } constructor(dataurl) { + this.preset = {images: true}; this.load(dataurl); this.history = []; window.onbeforeunload = e => {