word/quiz: dynamic lang(uage) option
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 6 Jun 2022 19:12:14 +0000 (21:12 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 11 Jul 2022 02:02:34 +0000 (04:02 +0200)
word.plp
word/memory.js
word/quiz.js

index 909c6fdaf7da57e7120ed323cf8a5d07e14b284e..5571299e4d3e178c865e355d6bb0cdb815e26225 100644 (file)
--- a/word.plp
+++ b/word.plp
@@ -6,7 +6,6 @@ our $wordlistbase = "data/wordlist";
 if ($Request and $Request =~ m{\A([^/]+)}) {
        my $name = $1;
        my $page = "word/$name.plp";
-       $wordlistbase .= ".$lang";
        if (-e $page) {
                utf8::downgrade($page); # unicode filename breaks contents encoding
                Include $page;
@@ -20,7 +19,7 @@ if ($Request and $Request =~ m{\A([^/]+)}) {
                });
                say '<h1>Words</h1>';
                say '<section id="gallery"></section>';
-               say "<script>new Word\u$name('/$wordlistbase.json')</script>";
+               say "<script>new Word\u$name()</script>";
        }
        exit;
 }
index dfbb8a65b2b4dcdc22995701b26229a33de6757f..99e76e87ad66cec3e9d757f5b234ce68bacfc996 100644 (file)
@@ -41,17 +41,17 @@ class WordMemory extends WordQuiz {
                .forEach(card => put(card, '!.turn!.bad'));
        }
 
-       load(dataurl) {
-               if (dataurl) {
-                       super.load(dataurl);
-               }
-               else {
+       load() {
+               if (this.preset.pairs) {
                        this.dataurl = '/data/wordpairs.json';
                        fetch(this.dataurl).then(res => res.json()).then(pairs => {
                                this.pairs = pairs;
                                this.setup();
                        });
                }
+               else {
+                       super.load();
+               }
        }
 
        setup() {
index f47fde12f906aa6747dd6e071cd5b2a99c004c40..68c2ee81e69e1b7ccaaa34fa43e463cce76852bd 100644 (file)
@@ -75,11 +75,12 @@ class WordQuiz {
                                this.preset[query] = val;
                        }
                }
+               this.preset.dataurl = `/data/wordlist.${this.preset.lang}.json`
        }
 
-       load(dataurl) {
+       load() {
                this.configure(window.location.hash.split('#'));
-               fetch(dataurl).then(res => res.json()).then(json => {
+               fetch(this.preset.dataurl).then(res => res.json()).then(json => {
                        this.words = this.dataselect(json)
                        this.setup();
                });
@@ -95,15 +96,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();
                };
        }
 }