word/quiz: configuration presets from request hash
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 6 Jun 2022 18:44:05 +0000 (20:44 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 11 Jul 2022 02:02:34 +0000 (04:02 +0200)
word/finder.js
word/memory.js
word/multichoice.js
word/quiz.js

index 9f56a2c28c553ff7f5690278c46ec6cc0937ae71..734e4b086d9556b21603bb7d0233448ac825a360 100644 (file)
@@ -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]);
                }
        }
index ac1e829996258598c1b710bded8c182b99c8c84a..dfbb8a65b2b4dcdc22995701b26229a33de6757f 100644 (file)
@@ -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)`;
index f0520f9d428cccbcc376a89eecb7e2f2c960d884..58ed7b7bef26408dfa9e0579d93264cd3457bf47 100644 (file)
@@ -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`,
                );
index fb36be12f94bc5f7b026c85ed42014f1c5ba6df5..f47fde12f906aa6747dd6e071cd5b2a99c004c40 100644 (file)
@@ -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 => {