X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/557e9a9b737294f5e170f8f5474ff750ab72cb4a..91810a36f31f93d25209e62f40aa24851aa33351:/word/memory.js diff --git a/word/memory.js b/word/memory.js index 48e8b44..a6b1906 100644 --- a/word/memory.js +++ b/word/memory.js @@ -15,8 +15,10 @@ class WordMemory extends WordQuiz { } // compare two cards - let match = this.pairs[this.turned[0].id] == this.turned[1].id - || this.pairs[this.turned[1].id] == this.turned[0].id; + let match = !this.pairs ? this.turned[0].id == this.turned[1].id : ( + this.pairs[this.turned[0].id] == this.turned[1].id + || this.pairs[this.turned[1].id] == this.turned[0].id + ); if (!match && !this.turned[0].classList.contains('bad')) { put(this.turned[0], '.bad'); // indicate failure on first card return; @@ -38,20 +40,42 @@ class WordMemory extends WordQuiz { } load(dataurl) { - this.dataurl = '/data/wordpairs.json'; - fetch(this.dataurl).then(res => res.json()).then(pairs => { - this.turned = []; - this.pairs = pairs; - this.form = document.getElementById('quiz'); - this.cards = Object.entries(pairs).flat() - .map(e => e.toString()) - .shuffle() - this.cards.forEach(word => { - put(this.form, - 'figure>img[src=$]<', `/data/word/en/${word}.jpg`, - {onclick: e => this.turn(e), id: word} - ); + if (dataurl) { + super.load(dataurl); + } + else { + this.dataurl = '/data/wordpairs.json'; + fetch(this.dataurl).then(res => res.json()).then(pairs => { + this.pairs = pairs; + this.setup(); }); + } + } + + setup() { + this.turned = []; + this.form = document.getElementById('quiz'); + + let cards; + if (this.words) { + const formstyle = window.getComputedStyle(this.form) + const gridsize = [ + formstyle['grid-template-rows'], formstyle['grid-template-columns'] + ].map(val => val.match(/ /g).length + 1).reduce((x, y) => x * y) / 2; + cards = this.words.splice(0, gridsize || 6).map(row => row[2]); + cards.push(...cards.map(val => -val)); + } + else { + cards = Object.entries(this.pairs).flat() + .map(e => e.toString()) + } + + cards.shuffle().forEach(word => { + let ref = Math.abs(word); + put(this.form, + 'figure>img[src=$]<', `/data/word/32/${ref}.jpg`, + {onclick: e => this.turn(e), id: ref, className: word < 0 ? 'mirror' : ''} + ); }); } };