ShtukbaseStandard
package file→ .shtukbase/packages/lenses/kit/examples/access-matrix.js

kit/examples/access-matrix.js

33 lines, 2.1 KB.

  1. /* A custom layout of the lens "Access matrix": rows are agents, columns are accesses.
  2. * Uses the common elements of the kit: Lens.page, Lens.card, Lens.optionChip, Lens.open, the option colors.
  3. */
  4. (function () {
  5. const { h } = Lens;
  6. Lens.templates.custom = function (cfg) {
  7. const ui = Lens.page(cfg);
  8. const agents = Lens.query({ from: "/product/agents", sort: "kind" });
  9. const access = Lens.query({ from: "/deal/access", sort: "status" });
  10. const need = new Set();
  11. for (const a of agents) for (const x of Lens.targets(a.id, "access")) need.add(a.id + "|" + x);
  12. const used = access.filter((x) => agents.some((a) => need.has(a.id + "|" + x.id)));
  13. const status = (x) => Lens.option(x.type, "status", Lens.prop(x, "status")) || { text: "no state", color: "gray" };
  14. const statusColor = (x) => status(x).color;
  15. const head = h("tr", {}, h("th", { class: "corner" }, "Agent \\ access"),
  16. ...used.map((x) => h("th", { class: "col" }, h("div", { class: "rot", title: x.title, onclick: () => Lens.open(x.id) },
  17. h("span", { class: "dot", "--c": Lens.color(statusColor(x)) }), x.title))));
  18. const rows = agents.map((a) => h("tr", {},
  19. h("th", { class: "row" }, Lens.card(a.id, { chips: ["kind"], sub: false })),
  20. ...used.map((x) => {
  21. const on = need.has(a.id + "|" + x.id);
  22. return h("td", { class: on ? "on" : "", "--c": Lens.color(statusColor(x)), title: on ? a.title + " → " + x.title : "",
  23. onclick: on ? () => Lens.open(x.id) : null }, on ? "●" : "");
  24. })));
  25. const seen = new Map();
  26. for (const x of used) { const st = status(x); seen.set(st.text, (seen.get(st.text) || { ...st, n: 0 })); seen.get(st.text).n++; }
  27. ui.showBar().append(...Lens.stats([["Agents", agents.length], ["Accesses", used.length], ["Marks", need.size]]));
  28. ui.section("States", h("div", { style: { display: "flex", flexWrap: "wrap", gap: "4px" } },
  29. [...seen.values()].map((st) => Lens.chip(`${st.text} · ${st.n}`, st.color, "dot"))));
  30. ui.main.append(h("div", { class: "mx-wrap" }, h("table", { class: "mx" }, h("thead", {}, head), h("tbody", {}, rows))));
  31. };
  32. })();