word: change directory name of 3:2 thumbnails
[sheet.git] / word / memory.js
index c3adfd4590d3d7dd6e4edc519620a84b8442b78f..a6b19061fee1e42dbbc6f9df3e7ffc94c80e1f8d 100644 (file)
@@ -1,4 +1,4 @@
-class WordMemory {
+class WordMemory extends WordQuiz {
        turn(click) {
                let target = click.currentTarget;
                if (!target.classList.contains('turn')) {
@@ -15,8 +15,10 @@ class WordMemory {
                }
 
                // 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;
@@ -37,23 +39,43 @@ class WordMemory {
                .forEach(card => put(card, '!.turn!.bad'));
        }
 
-       constructor() {
-               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())
-                               .sort(() => {return .5 - Math.random()}) // shuffle
-                       this.cards.forEach(word => {
-                               put(this.form,
-                                       'figure>img[src=$]<', `/data/word/en/${word}.jpg`,
-                                       {onclick: e => this.turn(e), id: word}
-                               );
+       load(dataurl) {
+               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' : ''}
+                       );
                });
        }
 };
-
-new WordMemory();