+ datafilter(json) {
+ // find viable rows from json data
+ const selection = new Words(json, this.preset.cat);
+
+ if (this.preset.images) {
+ selection.filter(id => json[id][2]);
+ }
+ if (this.preset.level !== undefined) {
+ selection.filter(id => json[id][1] <= this.preset.level);
+ }
+
+ if (this.preset.distinct) {
+ // remove referenced categories
+ selection.filter(id => !selection.get(id).subs.length);
+ }
+
+ return selection;
+ }
+
+ configure(params = hashparams()) {
+ 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;
+ }
+ }
+ this.preset.dataurl = `/data/wordlist.${this.preset.lang}.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();
+ });
+ }
+
+ 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() {
+ this.preset = {images: true, lang: 'en'};
+ this.load();
+ this.history = [];
+ window.onbeforeunload = e => {
+ this.stop('abort');
+ };
+ window.onhashchange = e => {
+ this.load();
+ };
+ }
+}