ShtukbaseStandard
package file→ .shtukbase/packages/lenses/kit/kit/templates/nest.js

kit/kit/templates/nest.js

71 lines, 3.6 KB.

  1. /* The "nest" template: records as boxes inside boxes by the parent field.
  2. *
  3. * from/type/where - which records to take; parent - the field with a link to the parent (parent by default)
  4. * group - the field by which the root boxes are laid out into sections (for example, kind)
  5. * groupOrder - the order of the values of group
  6. * chips - choice fields for the chips in the box header
  7. * refs - [{field, label}] links to show under the header (for example, which actions it calls)
  8. * color - the field whose option colors the left border of the nested boxes
  9. */
  10. (function () {
  11. const { h } = Lens;
  12. Lens.templates.nest = function (cfg) {
  13. const ui = Lens.page(cfg);
  14. const parentField = cfg.parent || "parent";
  15. const recs = Lens.query(cfg);
  16. const ids = new Set(recs.map((r) => r.id));
  17. const kids = new Map();
  18. const roots = [];
  19. for (const r of recs) {
  20. const p = Lens.targets(r.id, parentField).find((x) => ids.has(x));
  21. if (p) { if (!kids.has(p)) kids.set(p, []); kids.get(p).push(r); }
  22. else roots.push(r);
  23. }
  24. const colorOf = (r) => { const o = cfg.color && Lens.option(r.type, cfg.color, Lens.prop(r, cfg.color)); return o ? Lens.color(o.color) : null; };
  25. function box(r, depth) {
  26. const chips = (cfg.chips || ["kind"]).map((k) => Lens.optionChip(r, k)).filter(Boolean);
  27. const children = kids.get(r.id) || [];
  28. const el = h("div", { class: "lx-box", "data-id": r.id, "--kc": colorOf(r) },
  29. h("div", { class: "bh", onclick: (e) => { e.stopPropagation(); Lens.open(r.id); } }, Lens.icon(r),
  30. h("div", {}, h("div", { class: "ttl" }, r.title), h("div", { class: "meta" }, chips, children.length ? Lens.chip(`inside ${children.length}`, "gray") : null))));
  31. const refs = [];
  32. for (const rf of cfg.refs || []) {
  33. const t = Lens.targets(r.id, rf.field);
  34. if (t.length) refs.push(h("span", { class: "lbl" }, rf.label || Lens.label(r.type, rf.field)), t.map((x) => Lens.ref(x)));
  35. }
  36. if (refs.length) el.append(h("div", { class: "refs" }, refs));
  37. if (children.length) el.append(h("div", { class: "kids" }, children.map((c) => box(c, depth + 1))));
  38. return el;
  39. }
  40. const sections = new Map();
  41. for (const r of roots) {
  42. const g = cfg.group ? Lens.prop(r, cfg.group) : "";
  43. if (!sections.has(g)) sections.set(g, []);
  44. sections.get(g).push(r);
  45. }
  46. const order = cfg.groupOrder || [];
  47. const flow = h("div", { class: "lx-flow" });
  48. ui.main.append(flow);
  49. const keys = [...sections.keys()].sort((a, b) => (order.indexOf(a) + 1 || 99) - (order.indexOf(b) + 1 || 99));
  50. for (const g of keys) {
  51. const list = sections.get(g);
  52. if (cfg.group) {
  53. const o = Lens.option(list[0].type, cfg.group, g);
  54. flow.append(h("h2", { class: "lx-flow-h" }, Lens.chip(o ? o.text : g || "no value", o ? o.color : "gray", "dot"), h("span", { class: "n" }, list.length)));
  55. }
  56. flow.append(...list.map((r) => box(r, 0)));
  57. }
  58. const bar = ui.showBar();
  59. bar.append(...Lens.stats([["Records", recs.length], ["Top level", roots.length]]));
  60. Lens.on("search", (q) => {
  61. ui.main.querySelectorAll(".lx-box").forEach((b) => {
  62. const r = Lens.rec(b.dataset.id);
  63. const hit = q && (r.title + " " + r.id).toLowerCase().includes(q);
  64. b.classList.toggle("match", !!hit);
  65. b.classList.toggle("off", !!q && !hit);
  66. });
  67. // the parents of the matched ones stay visible
  68. if (q) ui.main.querySelectorAll(".lx-box.match").forEach((m) => { let p = m.parentElement.closest(".lx-box"); while (p) { p.classList.remove("off"); p = p.parentElement.closest(".lx-box"); } });
  69. });
  70. };
  71. })();