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 const encodedhash = window.location.href.split('#').slice(1) || '';
11 if (encodedhash.length == 1) {
12 // location.hash is not encoded in firefox
13 return decodeURIComponent(encodedhash).split('#');
19 constructor(data, root = undefined) {
21 this.selection = root || this.data[''][3];
22 this.visible = new Set(root || Object.keys(data).flatMap(id => id && parseInt(id)));
25 for (let loop = 0; children.length && loop < 20; loop++) {
26 for (let child of children) this.visible.add(child);
27 children = children.map(cat => data[cat][3]).filter(is => is).flat();
33 // keep only matching entries, and root selection regardless
34 this.visible = new Set([...this.visible].filter(f).concat(this.selection));
38 for (let i of this.selection) {
47 let order = [...this.visible.keys()].shuffle();
48 for (let i of order) {
57 return this.visible.has(id);
61 let refs = this.data[id][3];
65 for (let ref of refs) {
66 // retain orphaned references in grandparent categories
68 refs = refs.concat(this.subs(ref));
79 const row = this.data[id];
84 return row[0].replace(/\/.*/, ''); // primary form
87 let aliases = this.title.split('/');
88 let html = aliases.shift();
89 html = html.replace(/\((.+)\)/, '<small>$1</small>');
90 for (let alias of aliases) {
91 html += ` <small>(${alias})</small>`;
98 return `/data/word/${size}/${row[2]}.jpg`;
101 return p.subs(id).map(e => p.get(e));
109 this.data = this.datafilter(json);
110 return [...this.data.random()];
114 // find viable rows from json data
115 const selection = new Words(json, this.preset.cat);
117 if (this.preset.images) {
118 selection.filter(id => json[id][2]);
120 if (this.preset.level !== undefined) {
121 selection.filter(id => json[id][1] <= this.preset.level);
124 if (this.preset.distinct) {
125 // remove referenced categories
126 selection.filter(id => !selection.get(id).subs.length);
132 configure(params = hashparams()) {
133 const opts = new Map(params.map(arg => arg.split(/[:=](.*)/)));
134 for (let [query, val] of opts) {
135 if (query.match(/^\d+$/)) {
136 this.preset.cat = [parseInt(query)];
138 else if (query === 'level') {
139 this.preset.level = parseInt(val);
141 else if (query === 'debug') {
142 this.preset.debug = true;
145 this.preset[query] = val;
148 this.preset.dataurl = `/data/wordlist.${this.preset.lang}.json`
152 this.form = document.getElementById('quiz');
157 fetch(this.preset.dataurl).then(res => res.json()).then(json => {
158 this.words = this.dataselect(json)
164 this.history.push([new Date().toISOString(), ...args]);
169 window.onbeforeunload = null;
170 fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)});
174 this.preset = {images: true, lang: 'en'};
177 window.onbeforeunload = e => {
180 window.onhashchange = e => {