word/quiz: configuration presets from request hash
[sheet.git] / word / quiz.js
index 146aff73e61171c10f81779aad8be30777d7da35..f47fde12f906aa6747dd6e071cd5b2a99c004c40 100644 (file)
@@ -6,25 +6,104 @@ Array.prototype.shuffle = function () {
        return this;
 };
 
+Set.prototype.filter = function (f) {
+       return new Set([...this].filter(f));
+};
+
 class WordQuiz {
        dataselect(json) {
+               this.data = this.datafilter(json);
+               let rows = Object.values(this.data);
+               rows = rows.filter(row => !row[3].length); // remove referenced categories
+               return rows.shuffle();
+       }
+
+       datafilter(json) {
                // find viable rows from json data
-               let rows = Object.values(json);
+               let ids = new Set(Object.keys(json));
+               const selection = {...json}; // clone
+
+               for (let cat of selection[''][3]) {
+                       if (selection[cat])
+                       selection[cat][1] = 0; // keep root categories
+               }
+
+               if (this.preset.cat !== undefined) {
+                       ids.clear();
+                       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.images) {
+                       ids = ids.filter(id => json[id][2]);
+               }
                if (this.preset.level !== undefined) {
-                       rows = rows.filter(row => row[1] <= this.preset.level);
+                       ids = ids.filter(id => json[id][1] <= this.preset.level);
+               }
+
+               // keep only wanted ids
+               for (let id in selection) {
+                       if (id && !ids.has(id)) {
+                               delete selection[id];
+                       }
+               }
+
+               // retain orphaned references in grandparent categories
+               for (let id in selection) {
+                       selection[id][3] = function subresolve(subs) {
+                               //console.log(subs);
+                               return (subs || []).flatMap(sub =>
+                                       sub in selection ? [sub] : json[sub] ? subresolve(json[sub][3]) : []
+                               );
+                       }(selection[id][3]);
+               }
+               return selection;
+       }
+
+       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;
+                       }
                }
-               return rows.shuffle();
        }
 
        load(dataurl) {
-               this.preset = {};
+               this.configure(window.location.hash.split('#'));
                fetch(dataurl).then(res => res.json()).then(json => {
                        this.words = this.dataselect(json)
                        this.setup();
                });
        }
 
+       log(...args) {
+               this.history.push([new Date().toISOString(), ...args]);
+       }
+
+       stop(...args) {
+               this.log(...args);
+               window.onbeforeunload = null;
+               fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)});
+       }
+
        constructor(dataurl) {
+               this.preset = {images: true};
                this.load(dataurl);
+               this.history = [];
+               window.onbeforeunload = e => {
+                       this.stop('abort');
+               };
+               window.onhashchange = e => {
+                       this.load(dataurl);
+               };
        }
 }