ShtukbaseStandard
包文件→ .shtukbase/packages/lenses/kit/kit/templates/lanes.js

kit/kit/templates/lanes.js

200 行, 11.4 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. /* The "lanes" template: lane rows by the value of a field, inside them columns from left to right and arrows by link fields.
  2. * Fits scenario maps, processes, funnels: "entry → main → branches", plus a common column on the right.
  3. *
  4. * lane: {field, sort, other} - by which field of the main record to split into lanes (usually a link),
  5. * how to order the lanes (a field of the lane record), the caption for "no value"
  6. * columns: [{id, title, hint, color, width, from, type, where, not, sort, card, role, attach, parent, sortBy}]
  7. * 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)
  8. * role: main - the main column: each record is a lane row of its own (sort is the order of the rows)
  9. * role: child - to the right of the main one; parent is the link field to the parent (the main one or another child),
  10. * children stand opposite their main record, sortBy is the order of the children (for example, a step field)
  11. * side: {id, title, hint, color, width, from, where, card, near} - a common column on the right for the whole height;
  12. * near are the fields by which its records are referred to (the order follows where the referring records stand)
  13. * edges: as in the canvas: [{field, label, color, dash, from, to, reverse, off}] - from/to are column ids; off means hidden at first
  14. * filters: [field, ...] - filters in the side panel
  15. * show: "all" | "hover"
  16. */
  17. (function () {
  18. const { h } = Lens;
  19. Lens.templates.lanes = function (cfg) {
  20. const ui = Lens.page(cfg);
  21. const cols = cfg.columns || [];
  22. const entryCol = cols.find((c) => c.role === "entry");
  23. const mainCol = cols.find((c) => c.role === "main") || cols[0];
  24. const childCols = cols.filter((c) => c.role === "child");
  25. const side = cfg.side || null;
  26. const laneCfg = cfg.lane || {};
  27. const widths = [entryCol, mainCol, ...childCols].filter(Boolean).map((c) => c.width || "260px");
  28. const placed = new Map(); // id → column id
  29. 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; };
  30. const mains = take(mainCol);
  31. const kidsByCol = childCols.map((c) => ({ c, recs: take(c) }));
  32. const entries = entryCol ? take(entryCol) : [];
  33. const sideRecs = side ? take(side) : [];
  34. // the lane of the main record
  35. const laneOf = (r) => { const v = laneCfg.field ? Lens.prop(r, laneCfg.field) : null; return (Array.isArray(v) ? v[0] : v) || ""; };
  36. // children → root (the main record), we walk along parent through the children of any columns
  37. const mainSet = new Set(mains.map((r) => r.id));
  38. const parentField = new Map(childCols.map((c) => [c.id, [].concat(c.parent || [])]));
  39. function rootOf(id) {
  40. const seen = new Set();
  41. let cur = id;
  42. while (cur && !seen.has(cur)) {
  43. seen.add(cur);
  44. if (mainSet.has(cur)) return cur;
  45. const col = placed.get(cur);
  46. const pf = parentField.get(col) || [];
  47. cur = pf.map((f) => Lens.targets(cur, f)).flat().find((t) => placed.has(t)) || null;
  48. }
  49. return null;
  50. }
  51. // entry: which main records it belongs to
  52. const attach = [].concat((entryCol && entryCol.attach) || []);
  53. const entryMains = new Map(entries.map((e) => [e.id, new Set()]));
  54. for (const m of mains) {
  55. for (const t of attach.flatMap((f) => Lens.targets(m.id, f))) if (entryMains.has(t)) entryMains.get(t).add(m.id);
  56. }
  57. 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);
  58. const primaryEntry = new Map(); // main → its first entry (intent before automation - by the order of the column)
  59. for (const e of entries) for (const m of entryMains.get(e.id)) if (!primaryEntry.has(m)) primaryEntry.set(m, e.id);
  60. // lane rows: main records grouped by the common entry
  61. const lanes = new Map();
  62. const laneRec = (k) => (k && Lens.rec(k)) || null;
  63. for (const m of mains) {
  64. const k = laneOf(m);
  65. if (!lanes.has(k)) lanes.set(k, { key: k, rows: [] });
  66. lanes.get(k).rows.push({ main: m.id, kids: childCols.map(() => []), entries: [] });
  67. }
  68. for (const lane of lanes.values()) {
  69. const order = lane.rows.map((r) => r.main);
  70. const out = [], used = new Set();
  71. for (const id of order) {
  72. if (used.has(id)) continue;
  73. const pe = primaryEntry.get(id);
  74. 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)); }
  75. }
  76. lane.rows = out;
  77. }
  78. const rowOf = new Map();
  79. for (const lane of lanes.values()) lane.rows.forEach((row, i) => { row.lane = lane; row.i = i; rowOf.set(row.main, row); });
  80. // orphans: a row of their own at the bottom of the lane (by their own lane field)
  81. const orphanRow = (r) => {
  82. const k = laneOf(r);
  83. if (!lanes.has(k)) lanes.set(k, { key: k, rows: [] });
  84. const lane = lanes.get(k);
  85. let row = lane.rows.find((x) => x.orphan);
  86. if (!row) { row = { main: null, orphan: true, kids: childCols.map(() => []), entries: [], lane }; lane.rows.push(row); }
  87. return row;
  88. };
  89. kidsByCol.forEach(({ c, recs }, ci) => {
  90. const sortBy = c.sortBy;
  91. const key = (r) => String((sortBy && Lens.prop(r, sortBy)) || "");
  92. recs.slice().sort((a, b) => key(a).localeCompare(key(b), "ru", { numeric: true }) || a.title.localeCompare(b.title, "ru"))
  93. .forEach((r) => { const root = rootOf(r.id); (root ? rowOf.get(root) : orphanRow(r)).kids[ci].push(r.id); });
  94. // inside a row the children go as a tree: a child of a child right under it
  95. for (const lane of lanes.values()) for (const row of lane.rows) row.kids[ci] = treeOrder(row.kids[ci], c);
  96. });
  97. const entryDone = new Set();
  98. for (const e of entries) {
  99. const ms = [...entryMains.get(e.id)].map((m) => rowOf.get(m)).filter(Boolean);
  100. const row = ms.length ? ms.sort((a, b) => a.i - b.i)[0] : orphanRow(e);
  101. row.entries.push(e.id);
  102. entryDone.add(e.id);
  103. }
  104. // ---------- drawing ----------
  105. const tpl = widths.join(" ");
  106. const map = h("div", { class: "lx-lanes" });
  107. const left = h("div", { class: "lx-lanes-col" });
  108. const heads = h("div", { class: "lx-lhead", style: { gridTemplateColumns: tpl } },
  109. [entryCol, mainCol, ...childCols].filter(Boolean).map((c) => h("div", { class: "lx-lh", ...Lens.tone(c.color) },
  110. h("b", {}, c.title || c.id), h("span", { class: "n" }, [...placed.values()].filter((v) => v === c.id).length),
  111. c.hint ? h("div", { class: "hint" }, c.hint) : null)));
  112. left.append(heads);
  113. const laneSort = (a, b) => {
  114. if (!a.key !== !b.key) return a.key ? -1 : 1;
  115. const ra = laneRec(a.key), rb = laneRec(b.key);
  116. const ka = ra ? Lens.prop(ra, laneCfg.sort || "order") : null, kb = rb ? Lens.prop(rb, laneCfg.sort || "order") : null;
  117. if (ka != null && kb != null && ka !== kb) return typeof ka === "number" && typeof kb === "number" ? ka - kb : String(ka).localeCompare(String(kb), "ru", { numeric: true });
  118. if ((ka == null) !== (kb == null)) return ka == null ? 1 : -1;
  119. return (ra ? ra.title : a.key).localeCompare(rb ? rb.title : b.key, "ru");
  120. };
  121. const laneList = [...lanes.values()].sort(laneSort);
  122. const rowIndex = new Map(); // for the order of the common column
  123. let gi = 0;
  124. for (const lane of laneList) {
  125. const lr = laneRec(lane.key);
  126. const n = lane.rows.reduce((s, r) => s + (r.main ? 1 : 0) + r.entries.length + r.kids.flat().length, 0);
  127. const grid = h("div", { class: "lx-lgrid", style: { gridTemplateColumns: tpl } });
  128. // an entry takes several rows in a row if they have a common first entry
  129. let r0 = 0;
  130. while (r0 < lane.rows.length) {
  131. const pe = lane.rows[r0].main && primaryEntry.get(lane.rows[r0].main);
  132. let r1 = r0 + 1;
  133. while (pe && r1 < lane.rows.length && lane.rows[r1].main && primaryEntry.get(lane.rows[r1].main) === pe) r1++;
  134. const ents = lane.rows.slice(r0, r1).flatMap((r) => r.entries);
  135. if (entryCol) grid.append(cell(ents, entryCol, 1, r0 + 1, r1 - r0));
  136. r0 = r1;
  137. }
  138. lane.rows.forEach((row, i) => {
  139. let c = entryCol ? 2 : 1;
  140. grid.append(cell(row.main ? [row.main] : [], mainCol, c++, i + 1, 1));
  141. childCols.forEach((cc, ci) => grid.append(cell(row.kids[ci], cc, c++, i + 1, 1)));
  142. for (const id of [row.main, ...row.entries, ...row.kids.flat()]) if (id) rowIndex.set(id, gi);
  143. gi++;
  144. });
  145. 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))
  146. : h("span", { class: "lx-lt" }, lane.key ? lane.key : laneCfg.other || "No value");
  147. left.append(h("section", { class: "lx-lane", "data-lane": lane.key }, h("div", { class: "lx-lanehd" }, title, h("span", { class: "n" }, n)), grid));
  148. }
  149. map.append(left);
  150. if (side) {
  151. const near = [].concat(side.near || []);
  152. const pos = (r) => {
  153. const ys = Lens.back(r.id).filter((b) => (!near.length || near.includes(b.field)) && rowIndex.has(b.id)).map((b) => rowIndex.get(b.id));
  154. return ys.length ? Math.min(...ys) : 1e9;
  155. };
  156. 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);
  157. map.append(h("div", { class: "lx-lanes-side", style: { width: side.width || "250px" } },
  158. 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") })));
  159. }
  160. ui.main.append(map);
  161. const { edges, kinds, counts } = Lens.fieldEdges(placed, cfg.edges);
  162. const api = Lens.edges(map, edges, { kinds });
  163. ui.showBar().append(...Lens.stats([["Lanes", laneList.length], ["Cards", placed.size], ["Links", edges.length]]));
  164. Lens.filtersPanel(ui, cfg.filters, [...placed.keys()]);
  165. Lens.edgesPanel(ui, map, kinds, api, counts, cfg.show === "hover" ? "hover" : "all");
  166. ui.main.classList.add("is-canvas");
  167. Lens.zoom(ui.main, map);
  168. Lens.emit("ready", { grid: map, placed, edges, api });
  169. function cell(ids, col, c, r, span) {
  170. return h("div", { class: "lx-lcell", "data-col": col.id, style: { gridColumn: String(c), gridRow: `${r} / span ${span}` } },
  171. ids.map((id) => Lens.card(id, { ...(col.card || {}), extra: depthMark(id, col) })));
  172. }
  173. function depthMark(id, col) {
  174. if (col.role !== "child") return null;
  175. const p = (parentField.get(col.id) || []).flatMap((f) => Lens.targets(id, f)).find((t) => placed.get(t) === col.id);
  176. return p ? h("div", { class: "lx-from" }, "↳ from the branch \"" + Lens.rec(p).title + "\"") : null;
  177. }
  178. function treeOrder(ids, col) {
  179. const pf = parentField.get(col.id) || [];
  180. const inRow = new Set(ids);
  181. const parentIn = (id) => pf.flatMap((f) => Lens.targets(id, f)).find((t) => inRow.has(t) && t !== id);
  182. const out = [], seen = new Set();
  183. const visit = (id) => {
  184. if (seen.has(id)) return;
  185. seen.add(id); out.push(id);
  186. ids.filter((k) => parentIn(k) === id).forEach(visit);
  187. };
  188. ids.filter((id) => !parentIn(id)).forEach(visit);
  189. ids.forEach(visit);
  190. return out;
  191. }
  192. };
  193. })();