package file→ .shtukbase/packages/lenses/kit/kit/templates/lanes.js
kit/kit/templates/lanes.js
200 lines, 11.4 KB.
- /* The "lanes" template: lane rows by the value of a field, inside them columns from left to right and arrows by link fields.
- * Fits scenario maps, processes, funnels: "entry → main → branches", plus a common column on the right.
- *
- * lane: {field, sort, other} - by which field of the main record to split into lanes (usually a link),
- * how to order the lanes (a field of the lane record), the caption for "no value"
- * columns: [{id, title, hint, color, width, from, type, where, not, sort, card, role, attach, parent, sortBy}]
- * role: entry - the entry on the left; a record stands opposite the main one if they are linked by the attach fields (in either direction)
- * role: main - the main column: each record is a lane row of its own (sort is the order of the rows)
- * role: child - to the right of the main one; parent is the link field to the parent (the main one or another child),
- * children stand opposite their main record, sortBy is the order of the children (for example, a step field)
- * side: {id, title, hint, color, width, from, where, card, near} - a common column on the right for the whole height;
- * near are the fields by which its records are referred to (the order follows where the referring records stand)
- * edges: as in the canvas: [{field, label, color, dash, from, to, reverse, off}] - from/to are column ids; off means hidden at first
- * filters: [field, ...] - filters in the side panel
- * show: "all" | "hover"
- */
- (function () {
- const { h } = Lens;
- Lens.templates.lanes = function (cfg) {
- const ui = Lens.page(cfg);
- const cols = cfg.columns || [];
- const entryCol = cols.find((c) => c.role === "entry");
- const mainCol = cols.find((c) => c.role === "main") || cols[0];
- const childCols = cols.filter((c) => c.role === "child");
- const side = cfg.side || null;
- const laneCfg = cfg.lane || {};
- const widths = [entryCol, mainCol, ...childCols].filter(Boolean).map((c) => c.width || "260px");
- const placed = new Map(); // id → column id
- const take = (c) => { const recs = Lens.query(c).filter((r) => !placed.has(r.id)); recs.forEach((r) => placed.set(r.id, c.id)); return recs; };
- const mains = take(mainCol);
- const kidsByCol = childCols.map((c) => ({ c, recs: take(c) }));
- const entries = entryCol ? take(entryCol) : [];
- const sideRecs = side ? take(side) : [];
- // the lane of the main record
- const laneOf = (r) => { const v = laneCfg.field ? Lens.prop(r, laneCfg.field) : null; return (Array.isArray(v) ? v[0] : v) || ""; };
- // children → root (the main record), we walk along parent through the children of any columns
- const mainSet = new Set(mains.map((r) => r.id));
- const parentField = new Map(childCols.map((c) => [c.id, [].concat(c.parent || [])]));
- function rootOf(id) {
- const seen = new Set();
- let cur = id;
- while (cur && !seen.has(cur)) {
- seen.add(cur);
- if (mainSet.has(cur)) return cur;
- const col = placed.get(cur);
- const pf = parentField.get(col) || [];
- cur = pf.map((f) => Lens.targets(cur, f)).flat().find((t) => placed.has(t)) || null;
- }
- return null;
- }
- // entry: which main records it belongs to
- const attach = [].concat((entryCol && entryCol.attach) || []);
- const entryMains = new Map(entries.map((e) => [e.id, new Set()]));
- for (const m of mains) {
- for (const t of attach.flatMap((f) => Lens.targets(m.id, f))) if (entryMains.has(t)) entryMains.get(t).add(m.id);
- }
- for (const e of entries) for (const t of attach.flatMap((f) => Lens.targets(e.id, f))) if (mainSet.has(t)) entryMains.get(e.id).add(t);
- const primaryEntry = new Map(); // main → its first entry (intent before automation - by the order of the column)
- for (const e of entries) for (const m of entryMains.get(e.id)) if (!primaryEntry.has(m)) primaryEntry.set(m, e.id);
- // lane rows: main records grouped by the common entry
- const lanes = new Map();
- const laneRec = (k) => (k && Lens.rec(k)) || null;
- for (const m of mains) {
- const k = laneOf(m);
- if (!lanes.has(k)) lanes.set(k, { key: k, rows: [] });
- lanes.get(k).rows.push({ main: m.id, kids: childCols.map(() => []), entries: [] });
- }
- for (const lane of lanes.values()) {
- const order = lane.rows.map((r) => r.main);
- const out = [], used = new Set();
- for (const id of order) {
- if (used.has(id)) continue;
- const pe = primaryEntry.get(id);
- for (const id2 of order) if (!used.has(id2) && (id2 === id || (pe && primaryEntry.get(id2) === pe))) { used.add(id2); out.push(lane.rows.find((r) => r.main === id2)); }
- }
- lane.rows = out;
- }
- const rowOf = new Map();
- for (const lane of lanes.values()) lane.rows.forEach((row, i) => { row.lane = lane; row.i = i; rowOf.set(row.main, row); });
- // orphans: a row of their own at the bottom of the lane (by their own lane field)
- const orphanRow = (r) => {
- const k = laneOf(r);
- if (!lanes.has(k)) lanes.set(k, { key: k, rows: [] });
- const lane = lanes.get(k);
- let row = lane.rows.find((x) => x.orphan);
- if (!row) { row = { main: null, orphan: true, kids: childCols.map(() => []), entries: [], lane }; lane.rows.push(row); }
- return row;
- };
- kidsByCol.forEach(({ c, recs }, ci) => {
- const sortBy = c.sortBy;
- const key = (r) => String((sortBy && Lens.prop(r, sortBy)) || "");
- recs.slice().sort((a, b) => key(a).localeCompare(key(b), "ru", { numeric: true }) || a.title.localeCompare(b.title, "ru"))
- .forEach((r) => { const root = rootOf(r.id); (root ? rowOf.get(root) : orphanRow(r)).kids[ci].push(r.id); });
- // inside a row the children go as a tree: a child of a child right under it
- for (const lane of lanes.values()) for (const row of lane.rows) row.kids[ci] = treeOrder(row.kids[ci], c);
- });
- const entryDone = new Set();
- for (const e of entries) {
- const ms = [...entryMains.get(e.id)].map((m) => rowOf.get(m)).filter(Boolean);
- const row = ms.length ? ms.sort((a, b) => a.i - b.i)[0] : orphanRow(e);
- row.entries.push(e.id);
- entryDone.add(e.id);
- }
- // ---------- drawing ----------
- const tpl = widths.join(" ");
- const map = h("div", { class: "lx-lanes" });
- const left = h("div", { class: "lx-lanes-col" });
- const heads = h("div", { class: "lx-lhead", style: { gridTemplateColumns: tpl } },
- [entryCol, mainCol, ...childCols].filter(Boolean).map((c) => h("div", { class: "lx-lh", ...Lens.tone(c.color) },
- h("b", {}, c.title || c.id), h("span", { class: "n" }, [...placed.values()].filter((v) => v === c.id).length),
- c.hint ? h("div", { class: "hint" }, c.hint) : null)));
- left.append(heads);
- const laneSort = (a, b) => {
- if (!a.key !== !b.key) return a.key ? -1 : 1;
- const ra = laneRec(a.key), rb = laneRec(b.key);
- const ka = ra ? Lens.prop(ra, laneCfg.sort || "order") : null, kb = rb ? Lens.prop(rb, laneCfg.sort || "order") : null;
- if (ka != null && kb != null && ka !== kb) return typeof ka === "number" && typeof kb === "number" ? ka - kb : String(ka).localeCompare(String(kb), "ru", { numeric: true });
- if ((ka == null) !== (kb == null)) return ka == null ? 1 : -1;
- return (ra ? ra.title : a.key).localeCompare(rb ? rb.title : b.key, "ru");
- };
- const laneList = [...lanes.values()].sort(laneSort);
- const rowIndex = new Map(); // for the order of the common column
- let gi = 0;
- for (const lane of laneList) {
- const lr = laneRec(lane.key);
- const n = lane.rows.reduce((s, r) => s + (r.main ? 1 : 0) + r.entries.length + r.kids.flat().length, 0);
- const grid = h("div", { class: "lx-lgrid", style: { gridTemplateColumns: tpl } });
- // an entry takes several rows in a row if they have a common first entry
- let r0 = 0;
- while (r0 < lane.rows.length) {
- const pe = lane.rows[r0].main && primaryEntry.get(lane.rows[r0].main);
- let r1 = r0 + 1;
- while (pe && r1 < lane.rows.length && lane.rows[r1].main && primaryEntry.get(lane.rows[r1].main) === pe) r1++;
- const ents = lane.rows.slice(r0, r1).flatMap((r) => r.entries);
- if (entryCol) grid.append(cell(ents, entryCol, 1, r0 + 1, r1 - r0));
- r0 = r1;
- }
- lane.rows.forEach((row, i) => {
- let c = entryCol ? 2 : 1;
- grid.append(cell(row.main ? [row.main] : [], mainCol, c++, i + 1, 1));
- childCols.forEach((cc, ci) => grid.append(cell(row.kids[ci], cc, c++, i + 1, 1)));
- for (const id of [row.main, ...row.entries, ...row.kids.flat()]) if (id) rowIndex.set(id, gi);
- gi++;
- });
- const title = lr ? h("a", { class: "lx-lt", href: "#" + lr.id, onclick: (e) => (e.preventDefault(), Lens.open(lr.id)) }, Lens.icon(lr), h("span", {}, lr.title))
- : h("span", { class: "lx-lt" }, lane.key ? lane.key : laneCfg.other || "No value");
- left.append(h("section", { class: "lx-lane", "data-lane": lane.key }, h("div", { class: "lx-lanehd" }, title, h("span", { class: "n" }, n)), grid));
- }
- map.append(left);
- if (side) {
- const near = [].concat(side.near || []);
- const pos = (r) => {
- const ys = Lens.back(r.id).filter((b) => (!near.length || near.includes(b.field)) && rowIndex.has(b.id)).map((b) => rowIndex.get(b.id));
- return ys.length ? Math.min(...ys) : 1e9;
- };
- const list = sideRecs.map((r) => ({ r, p: pos(r) })).sort((a, b) => a.p - b.p || a.r.title.localeCompare(b.r.title, "ru")).map((x) => x.r);
- map.append(h("div", { class: "lx-lanes-side", style: { width: side.width || "250px" } },
- Lens.region({ ...side, count: list.length, body: list.length ? h("div", { class: "lx-cards" }, list.map((r) => Lens.card(r.id, side.card))) : h("div", { class: "lx-empty" }, "Empty so far") })));
- }
- ui.main.append(map);
- const { edges, kinds, counts } = Lens.fieldEdges(placed, cfg.edges);
- const api = Lens.edges(map, edges, { kinds });
- ui.showBar().append(...Lens.stats([["Lanes", laneList.length], ["Cards", placed.size], ["Links", edges.length]]));
- Lens.filtersPanel(ui, cfg.filters, [...placed.keys()]);
- Lens.edgesPanel(ui, map, kinds, api, counts, cfg.show === "hover" ? "hover" : "all");
- ui.main.classList.add("is-canvas");
- Lens.zoom(ui.main, map);
- Lens.emit("ready", { grid: map, placed, edges, api });
- function cell(ids, col, c, r, span) {
- return h("div", { class: "lx-lcell", "data-col": col.id, style: { gridColumn: String(c), gridRow: `${r} / span ${span}` } },
- ids.map((id) => Lens.card(id, { ...(col.card || {}), extra: depthMark(id, col) })));
- }
- function depthMark(id, col) {
- if (col.role !== "child") return null;
- const p = (parentField.get(col.id) || []).flatMap((f) => Lens.targets(id, f)).find((t) => placed.get(t) === col.id);
- return p ? h("div", { class: "lx-from" }, "↳ from the branch \"" + Lens.rec(p).title + "\"") : null;
- }
- function treeOrder(ids, col) {
- const pf = parentField.get(col.id) || [];
- const inRow = new Set(ids);
- const parentIn = (id) => pf.flatMap((f) => Lens.targets(id, f)).find((t) => inRow.has(t) && t !== id);
- const out = [], seen = new Set();
- const visit = (id) => {
- if (seen.has(id)) return;
- seen.add(id); out.push(id);
- ids.filter((k) => parentIn(k) === id).forEach(visit);
- };
- ids.filter((id) => !parentIn(id)).forEach(visit);
- ids.forEach(visit);
- return out;
- }
- };
- })();