1 Array.prototype.shuffle = function () {
2 for (let i = this.length - 1; i > 0; i--) {
3 const j = Math.floor(Math.random() * (i + 1)); // random index 0..i
4 [this[i], this[j]] = [this[j], this[i]]; // swap elements
9 function hashparams() {
10 // location.hash is not encoded in firefox
11 const encodedhash = (window.location.href.split('#'))[1] || '';
12 return decodeURIComponent(encodedhash).split('#');
16 constructor(data, root = undefined) {
18 this.selection = root || this.data[''][3];
19 this.visible = new Set(root || Object.keys(data).flatMap(id => id && parseInt(id)));
22 for (let loop = 0; children.length && loop < 20; loop++) {
23 for (let child of children) this.visible.add(child);
24 children = children.map(cat => data[cat][3]).filter(is => is).flat();
30 // keep only matching entries, and root selection regardless
31 this.visible = new Set([...this.visible].filter(f).concat(this.selection));
35 for (let i of this.selection) {
44 let order = [...this.visible.keys()].shuffle();
45 for (let i of order) {
54 return this.visible.has(id);
58 let refs = this.data[id][3];
62 for (let ref of refs) {
63 // retain orphaned references in grandparent categories
65 refs = refs.concat(this.subs(ref));
76 const row = this.data[id];
81 return row[0].replace(/\/.*/, ''); // primary form
86 return `/data/word/${size}/${row[2]}.jpg`;
89 return p.subs(id).map(e => p.get(e));
97 this.data = this.datafilter(json);
98 return [...this.data.random()];
102 // find viable rows from json data
103 const selection = new Words(json, this.preset.cat);
105 if (this.preset.images) {
106 selection.filter(id => json[id][2]);
108 if (this.preset.level !== undefined) {
109 selection.filter(id => json[id][1] <= this.preset.level);
112 if (this.preset.distinct) {
113 // remove referenced categories
114 selection.filter(id => !selection.get(id).subs.length);
120 configure(params = hashparams()) {
121 const opts = new Map(params.map(arg => arg.split(/[:=](.*)/)));
122 for (let [query, val] of opts) {
123 if (query.match(/^\d+$/)) {
124 this.preset.cat = [parseInt(query)];
126 else if (query === 'level') {
127 this.preset.level = parseInt(val);
130 this.preset[query] = val;
133 this.preset.dataurl = `/data/wordlist.${this.preset.lang}.json`
137 this.form = document.getElementById('quiz');
142 fetch(this.preset.dataurl).then(res => res.json()).then(json => {
143 this.words = this.dataselect(json)
149 this.history.push([new Date().toISOString(), ...args]);
154 window.onbeforeunload = null;
155 fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)});
159 this.preset = {images: true, lang: 'en'};
162 window.onbeforeunload = e => {
165 window.onhashchange = e => {