word/finder: move html formatting into common method
[sheet.git] / word / finder.js
index 606f0334432b5d3f3a48ed0b0f3b51f3f62a20d9..3dca29b8711061be90d9cbe5193fbca7c8329c7e 100644 (file)
@@ -1,31 +1,67 @@
 class WordFinder extends WordQuiz {
-       add(parentitem, rows) {
-               const catitem = put(parentitem, 'ul');
-               rows.forEach(ref => {
-                       const [title, level, imgid] = this.data[ref];
+       add(catitem, rows) {
+               rows.forEach(word => {
+                       if (!word) return;
                        const worditem = put(catitem, 'li');
                        const figitem = put(worditem, 'figure');
-                       if (imgid) {
-                               put(figitem, 'img[src=$]', `/data/word/32/${imgid}.jpg`);
+                       if (word.imgid) {
+                               put(figitem, 'img[src=$]', word.thumb());
                        }
-                       if (title) {
-                               let html = title.replace(/\/(.*)/, ' <small>($1)</small>');
+                       if (word.title) {
                                put(figitem, 'figcaption', {
-                                       innerHTML: html,
+                                       innerHTML: word.html,
                                });
                        }
-                       if (this.cats[ref]) {
-                               // delve into subcategory
-                               put(worditem, '.parent');
-                               this.add(worditem, this.cats[ref]);
+                       if (this.preset.debug) {
+                               put(figitem, '[title=$]', `id ${word.id} level ${word.level}`);
                        }
+                       put(worditem, '.level' + word.level);
+                       if (!word.subs.length) {
+                               return;
+                       }
+                       if (word.level <= 1 && word.subs.length >= 4) {
+                               put(worditem, '.large');
+                       }
+                       put(worditem, '.parent.expand');
+
+                       put(figitem, '[data-sup=$]', word.subs.length);
+                       figitem.onclick = () => {
+                               let expansion;
+                               if (expansion = worditem.querySelector('ul')) {
+                                       put(expansion, '!');
+                                       put(worditem, '.expand');
+                                       return;
+                               }
+                               expansion = put(worditem, 'ul');
+                               this.add(expansion, word.subs);
+                               put(worditem, '!expand');
+                       };
+                       return;
+
+                       // delve into subcategory
+                       const expansion = put(worditem, 'ul');
+                       //expansion.style.display = 'none';
+                       this.add(expansion, word.subs);
+                       //worditem.onclick = () => expansion.style.display = '';
                });
        }
 
+       configure(input) {
+               this.preset.level = 3;
+               this.preset.images = false;
+               return super.configure(input);
+       }
+
        setup() {
-               this.gallery = document.getElementById('gallery');
-               put(this.gallery, '-p', 'Under construction.');
-               this.add(this.gallery, this.preset.cat ? [this.preset.cat] : this.cats[null]);
+               super.setup();
+               if (this.preset.debug) {
+                       put(document.head, 'link', {rel: 'stylesheet', href: '/word/debug.css'});
+               }
+               this.form.innerHTML = '';
+               put(this.form, 'p', 'Under construction.');
+               for (let cat of this.data.root()) {
+                       this.add(put(this.form, 'ul.gallery'), [cat]);
+               }
        }
 
        stop() {}