From 9b8d7e610cef31f15f12c1058dfd45efffac5fb0 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Mon, 6 Jun 2022 20:44:05 +0200 Subject: [PATCH] word/quiz: configuration presets from request hash --- word/finder.js | 8 +++++++- word/memory.js | 2 +- word/multichoice.js | 1 - word/quiz.js | 28 ++++++++++++++++++---------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/word/finder.js b/word/finder.js index 9f56a2c..734e4b0 100644 --- a/word/finder.js +++ b/word/finder.js @@ -34,11 +34,17 @@ class WordFinder extends WordQuiz { }); } + configure(input) { + this.preset.level = 3; + this.preset.images = false; + return super.configure(input); + } + setup() { this.gallery = document.getElementById('gallery'); this.gallery.innerHTML = ''; put(this.gallery, 'p', 'Under construction.'); - for (let cat of this.preset.cat ? [this.preset.cat] : this.data[''][3]) { + for (let cat of this.preset.cat || this.data[''][3]) { this.add(put(this.gallery, 'ul.gallery'), [cat]); } } diff --git a/word/memory.js b/word/memory.js index ac1e829..dfbb8a6 100644 --- a/word/memory.js +++ b/word/memory.js @@ -62,7 +62,7 @@ class WordMemory extends WordQuiz { if (this.words) { const aspect = this.form.clientWidth / window.innerHeight; //TODO image ratio - let count = 35; + let count = parseInt(this.preset.n) || 35; let cols = Math.round(Math.sqrt(count) * aspect**.5); count = cols * Math.ceil(count / cols); this.form.style['grid-template-columns'] = `repeat(${cols}, 1fr)`; diff --git a/word/multichoice.js b/word/multichoice.js index f0520f9..58ed7b7 100644 --- a/word/multichoice.js +++ b/word/multichoice.js @@ -2,7 +2,6 @@ class WordMultiChoice extends WordQuiz { next() { if (this.words.length < 4) return; let word = this.words.shift(); - if (!word[2]) return this.next(); let form = put(this.form, '+img[src=$]+ul', `/data/word/32/${word[2]}.jpg`, ); diff --git a/word/quiz.js b/word/quiz.js index fb36be1..f47fde1 100644 --- a/word/quiz.js +++ b/word/quiz.js @@ -30,13 +30,13 @@ class WordQuiz { 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) { @@ -62,16 +62,23 @@ class WordQuiz { 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(); @@ -89,6 +96,7 @@ class WordQuiz { } constructor(dataurl) { + this.preset = {images: true}; this.load(dataurl); this.history = []; window.onbeforeunload = e => { -- 2.30.0