word/quiz: configuration presets from request hash
[sheet.git] / word / quiz.js
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 => {