package file→ .shtukbase/packages/lenses/kit/kit/templates/nest.js
kit/kit/templates/nest.js
71 lines, 3.6 KB.
- /* The "nest" template: records as boxes inside boxes by the parent field.
- *
- * from/type/where - which records to take; parent - the field with a link to the parent (parent by default)
- * group - the field by which the root boxes are laid out into sections (for example, kind)
- * groupOrder - the order of the values of group
- * chips - choice fields for the chips in the box header
- * refs - [{field, label}] links to show under the header (for example, which actions it calls)
- * color - the field whose option colors the left border of the nested boxes
- */
- (function () {
- const { h } = Lens;
- Lens.templates.nest = function (cfg) {
- const ui = Lens.page(cfg);
- const parentField = cfg.parent || "parent";
- const recs = Lens.query(cfg);
- const ids = new Set(recs.map((r) => r.id));
- const kids = new Map();
- const roots = [];
- for (const r of recs) {
- const p = Lens.targets(r.id, parentField).find((x) => ids.has(x));
- if (p) { if (!kids.has(p)) kids.set(p, []); kids.get(p).push(r); }
- else roots.push(r);
- }
- 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; };
- function box(r, depth) {
- const chips = (cfg.chips || ["kind"]).map((k) => Lens.optionChip(r, k)).filter(Boolean);
- const children = kids.get(r.id) || [];
- const el = h("div", { class: "lx-box", "data-id": r.id, "--kc": colorOf(r) },
- h("div", { class: "bh", onclick: (e) => { e.stopPropagation(); Lens.open(r.id); } }, Lens.icon(r),
- h("div", {}, h("div", { class: "ttl" }, r.title), h("div", { class: "meta" }, chips, children.length ? Lens.chip(`inside ${children.length}`, "gray") : null))));
- const refs = [];
- for (const rf of cfg.refs || []) {
- const t = Lens.targets(r.id, rf.field);
- if (t.length) refs.push(h("span", { class: "lbl" }, rf.label || Lens.label(r.type, rf.field)), t.map((x) => Lens.ref(x)));
- }
- if (refs.length) el.append(h("div", { class: "refs" }, refs));
- if (children.length) el.append(h("div", { class: "kids" }, children.map((c) => box(c, depth + 1))));
- return el;
- }
- const sections = new Map();
- for (const r of roots) {
- const g = cfg.group ? Lens.prop(r, cfg.group) : "";
- if (!sections.has(g)) sections.set(g, []);
- sections.get(g).push(r);
- }
- const order = cfg.groupOrder || [];
- const flow = h("div", { class: "lx-flow" });
- ui.main.append(flow);
- const keys = [...sections.keys()].sort((a, b) => (order.indexOf(a) + 1 || 99) - (order.indexOf(b) + 1 || 99));
- for (const g of keys) {
- const list = sections.get(g);
- if (cfg.group) {
- const o = Lens.option(list[0].type, cfg.group, g);
- 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)));
- }
- flow.append(...list.map((r) => box(r, 0)));
- }
- const bar = ui.showBar();
- bar.append(...Lens.stats([["Records", recs.length], ["Top level", roots.length]]));
- Lens.on("search", (q) => {
- ui.main.querySelectorAll(".lx-box").forEach((b) => {
- const r = Lens.rec(b.dataset.id);
- const hit = q && (r.title + " " + r.id).toLowerCase().includes(q);
- b.classList.toggle("match", !!hit);
- b.classList.toggle("off", !!q && !hit);
- });
- // the parents of the matched ones stay visible
- 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"); } });
- });
- };
- })();