word/quiz: generic page container setup
[sheet.git] / word / quiz.js
index f47fde12f906aa6747dd6e071cd5b2a99c004c40..3243b19ed5c2f52ef019868a32136f253bf41481 100644 (file)
@@ -62,7 +62,7 @@ class WordQuiz {
                return selection;
        }
 
-       configure(params) {
+       configure(params = window.location.hash.split('#')) {
                const opts = new Map(params.map(arg => arg.split(/[:=](.*)/)));
                for (let [query, val] of opts) {
                        if (query.match(/^\d+$/)) {
@@ -75,11 +75,16 @@ class WordQuiz {
                                this.preset[query] = val;
                        }
                }
+               this.preset.dataurl = `/data/wordlist.${this.preset.lang}.json`
        }
 
-       load(dataurl) {
-               this.configure(window.location.hash.split('#'));
-               fetch(dataurl).then(res => res.json()).then(json => {
+       setup() {
+               this.form = document.getElementById('quiz');
+       }
+
+       load() {
+               this.configure();
+               fetch(this.preset.dataurl).then(res => res.json()).then(json => {
                        this.words = this.dataselect(json)
                        this.setup();
                });
@@ -95,15 +100,15 @@ class WordQuiz {
                fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)});
        }
 
-       constructor(dataurl) {
-               this.preset = {images: true};
-               this.load(dataurl);
+       constructor() {
+               this.preset = {images: true, lang: 'en'};
+               this.load();
                this.history = [];
                window.onbeforeunload = e => {
                        this.stop('abort');
                };
                window.onhashchange = e => {
-                       this.load(dataurl);
+                       this.load();
                };
        }
 }