ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/lenses-records/lens/board.html

lens/board.html

145 líneas, 5.9 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. <!doctype html>
  2. <html lang="ru" data-skin="desktop">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Доска</title>
  7. <link rel="stylesheet" href="/_kit/kit.css">
  8. <link rel="stylesheet" href="/_kit/skins/calm.css">
  9. <link rel="stylesheet" href="/_kit/skins/desktop.css">
  10. <link rel="stylesheet" href="lens.css">
  11. <script src="/_pkg/layout-basic/lens.js"></script>
  12. </head>
  13. <body>
  14. <p class="lens-error" role="alert" hidden>Не удалось загрузить записи.</p>
  15. <main class="lx-board records-board" aria-label="Записи"></main>
  16. <script>
  17. const board = document.querySelector("main");
  18. const error = document.querySelector(".lens-error");
  19. const dateFormat = new Intl.DateTimeFormat("ru-RU", { dateStyle: "short", timeZone: "UTC" });
  20. const hues = new Set("default gray brown orange yellow green blue purple pink red teal".split(" "));
  21. let revision = 0;
  22. let ready = false;
  23. const read = (path) => shtuk.call("records.read", { path: path.endsWith(".md") ? path : path + ".md" });
  24. const isChoice = ({ kind }) => kind === "select" || kind === "phase";
  25. const isFilled = (value) => value != null && value !== "" && (!Array.isArray(value) || value.length > 0);
  26. async function optionsFor(property) {
  27. if (property.options || property.kind !== "phase" || !property.contour) return property.options ?? [];
  28. const contour = await read(property.contour).catch(() => null);
  29. return Promise.all((contour?.meta.phases ?? []).map(async (path) => {
  30. const name = path.split("/").at(-1).replace(/\.md$/, "");
  31. const phase = await read(path).catch(() => null);
  32. return { name, label: phase?.meta.title, color: phase?.meta.color };
  33. }));
  34. }
  35. function chip(text, color) {
  36. const span = document.createElement("span");
  37. span.className = "lx-chip dot hue-" + (hues.has(color) ? color : "default");
  38. span.textContent = text;
  39. return span;
  40. }
  41. function valueNode(value, property, options) {
  42. if (isChoice(property)) {
  43. const option = options.find(({ name }) => name === value);
  44. return chip(option?.label ?? value, option?.color);
  45. }
  46. if (property.kind === "date") {
  47. const date = new Date(value);
  48. if (Number.isFinite(date.getTime())) return dateFormat.format(date);
  49. }
  50. return String(value);
  51. }
  52. function card(item, fields) {
  53. const button = document.createElement("button");
  54. button.type = "button";
  55. button.className = "lx-card";
  56. button.dataset.path = item.path;
  57. button.onclick = () => shtuk.open(item.path);
  58. const title = document.createElement("span");
  59. title.className = "ttl";
  60. title.textContent = item.title;
  61. button.append(title);
  62. const details = document.createElement("span");
  63. details.className = "card-values";
  64. const filled = fields.filter(([key]) => isFilled(item.meta[key])).slice(0, 3);
  65. for (const [key, property, options] of filled) {
  66. const label = document.createElement("span");
  67. label.textContent = property.label ?? key;
  68. const value = document.createElement("span");
  69. value.append(valueNode(item.meta[key], property, options));
  70. details.append(label, value);
  71. }
  72. if (filled.length) button.append(details);
  73. return button;
  74. }
  75. async function draw() {
  76. const current = ++revision;
  77. try {
  78. const { collection, items } = await shtuk.call("records.list", { collection: shtuk.path });
  79. const fields = await Promise.all(Object.entries(collection.properties ?? {}).map(
  80. async ([key, property]) => [key, property, await optionsFor(property)],
  81. ));
  82. if (current !== revision) return;
  83. const [group, , options = []] = fields.find(([, property]) => isChoice(property)) ?? [];
  84. const details = fields.filter(([key]) => key !== group && key !== "title");
  85. const columns = new Map(options.map(({ name, label, color }) => [name, {
  86. label: label ?? name, color, items: [],
  87. }]));
  88. const unset = [];
  89. for (const item of items) {
  90. const value = group === undefined ? undefined : item.meta[group];
  91. if (!isFilled(value)) {
  92. unset.push(item);
  93. continue;
  94. }
  95. if (!columns.has(value)) columns.set(value, { label: String(value), items: [] });
  96. columns.get(value).items.push(item);
  97. }
  98. if (unset.length) columns.set(null, { label: "Без значения", items: unset });
  99. const next = document.createDocumentFragment();
  100. const focusedPath = document.activeElement.dataset.path;
  101. let focusedCard;
  102. for (const { label, color, items } of columns.values()) {
  103. const column = document.createElement("section");
  104. column.className = "lx-col";
  105. const head = document.createElement("h2");
  106. head.className = "ch";
  107. const count = document.createElement("span");
  108. count.className = "n";
  109. count.textContent = items.length;
  110. head.append(chip(label, color), count);
  111. column.append(head);
  112. for (const item of items) {
  113. const button = card(item, details);
  114. if (item.path === focusedPath) focusedCard = button;
  115. column.append(button);
  116. }
  117. next.append(column);
  118. }
  119. const left = board.scrollLeft;
  120. board.replaceChildren(next);
  121. board.setAttribute("aria-label", collection.title);
  122. board.scrollLeft = left;
  123. focusedCard?.focus({ preventScroll: true });
  124. error.hidden = true;
  125. } catch {
  126. if (current === revision) error.hidden = false;
  127. } finally {
  128. if (current === revision && !ready) {
  129. ready = true;
  130. shtuk.ready();
  131. }
  132. }
  133. }
  134. shtuk.on("changed", draw);
  135. draw();
  136. </script>
  137. </body>
  138. </html>