git » hoplite.git » commit 6a96b07

el kids can be arrays

author Alan Dipert
2021-06-13 05:51:32 UTC
committer Alan Dipert
2021-06-13 05:51:32 UTC
parent bbc73d938be8390cd19b132664bc3cd355a37a62

el kids can be arrays

el.mjs +15 -1
hntr2.mjs +5 -33

diff --git a/el.mjs b/el.mjs
index 1cc9517..9bdd873 100644
--- a/el.mjs
+++ b/el.mjs
@@ -41,6 +41,10 @@ function el(tag, attrs, kids) {
         }
       })(kid);
       el.appendChild(node);
+    } else if (kid instanceof Array) {
+      // TODO formulas/etc in array
+      kid.forEach(x => el.appendChild(x));
+      offset += kid.length;
     } else if (kid instanceof Promise) {
       el.appendChild(placeholder);
       kid.then(x => el.replaceChild(x, placeholder));
@@ -240,4 +244,14 @@ function entitiesBy(db, sortAttr, makeNode) {
 
 let nbsp = "\u00A0";
 
-export { $el, entities, nbsp, entitiesBy };
+function bindTo(input, cell, event = "change") {
+  input.addEventListener(event, ({target: {value}}) => {
+    cell.set(value);
+  })
+  watch((oldv, newv) => {
+    if (!newv) input.value = "";
+  })(cell);
+  return input;
+}
+
+export { $el, entities, nbsp, entitiesBy, bindTo };
diff --git a/hntr2.mjs b/hntr2.mjs
index 0f7a30c..5990fad 100644
--- a/hntr2.mjs
+++ b/hntr2.mjs
@@ -1,5 +1,6 @@
 import { input, formula, watch, cond, not, database, transaction } from './reactives.mjs';
-import { $el, nbsp, entities } from './el.mjs';
+import { $el, nbsp, entities, bindTo } from './el.mjs';
+import { record } from './record.mjs';
 
 let user = input(),
     loaded = input(false),
@@ -17,32 +18,6 @@ firebase.auth().onAuthStateChanged(result => {
   loaded.set(true);
 });
 
-function bindTo(input, cell) {
-  input.addEventListener("change", ({target: {value}}) => {
-    cell.set(value);
-  })
-  watch((oldv, newv) => {
-    if (!newv) input.value = "";
-  })(cell);
-  return input;
-}
-
-function record(...fields) {
-  return Object.fromEntries(fields.map(f => [f, input()]));
-}
-
-function recordClear(rec) {
-  transaction(() => {
-    Object.values(rec).forEach(c => c.set());
-  });
-}
-
-function recordTuples(rec, eid) {
-  return Object.entries(rec)
-    .filter(([f, c]) => c.value)
-    .map(([f, c]) => [eid, f, c.value]);
-}
-
 function deleteCredential(eid) {
   db.remove([...db].filter(([e]) => e === eid));
 }
@@ -71,14 +46,11 @@ function authed() {
             type: "button",
             value: "Add",
             onclick: () => {
-              db.add(recordTuples(rec, db.maxEid + 1));
-              recordClear(rec);
+              db.add(rec.asTuples(db.maxEid + 1));
+              rec.clear();
             }
           })),
-          $el.td(bindTo($el.input({type: "text"}), rec.site)),
-          $el.td(bindTo($el.input({type: "text"}), rec.username)),
-          $el.td(bindTo($el.input({type: "text"}), rec.password)),
-          $el.td(bindTo($el.input({type: "text"}), rec.note))
+          rec.fieldNames.map(f => $el.td(bindTo($el.input({type: "text"}), rec.get[f])))
         ),
         entities(db, "_eid", "asc", attrs => {
           return $el.tr(