@monster-orange/den-ui 0.7.0 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/shell-html.js CHANGED
@@ -17,7 +17,8 @@
17
17
  * { text?, svg?, label } the mark beside it (text escaped;
18
18
  * svg the product's own markup; label its name)
19
19
  * nav [{ id, label, href, group, icon?, tip?, current?, badge?,
20
- * lead?, mono?, cls?, attrs?, pin?, after? }] rail entries
20
+ * lead?, mono?, cls?, attrs?, pin?, after?, kind?, off? }] rail
21
+ * entries
21
22
  * groups [{ id, label, from? }] in rail order; a group with no
22
23
  * entry is left out
23
24
  * foot entries under the rail's spacer (Settings, say), then the
@@ -34,11 +35,18 @@
34
35
  * collapsed the rail drawn collapsed (the toggle says Expand)
35
36
  * report false: no Report button (reports off)
36
37
  *
37
- * An entry: `badge` { n, bad?, label? } (label is read with the name);
38
+ * An entry: `badge` { n, bad?, what? } (what it counts: "open",
39
+ * "waiting", "down", "running", read with the name in the kit's words);
38
40
  * `lead` HTML in place of the icon (a status dot); `attrs` more
39
41
  * attributes { name: value } (escaped); `cls` more classes; `pin` the
40
42
  * pinned id (the row is a pin row: Delete unpins it); `after` HTML after
41
- * the link inside the pin row (a Visit link).
43
+ * the link inside the pin row (a Visit link); `kind` (0.7.3) "project",
44
+ * "den", "service", "hub" or { artifact: word }: a small uppercase tag at
45
+ * the row's end; its tooltip and name are the kit's ("North · Project");
46
+ * `off` (0.7.4) "connect", "reconnect", "forbidden", "unreachable" or
47
+ * "config": the entry is greyed and the kit says why ("Acme Co · Hub,
48
+ * reconnect in Settings"); `icon`
49
+ * { initial } a project's initial in a disc in place of the icon.
42
50
  *
43
51
  * 0.7.0, for the slots: scopeHtml (the scope crumbs), searchHtml (the
44
52
  * search field with ⌘K inside), bellButtonHtml and bellPanelHtml (the
@@ -61,14 +69,128 @@ function attrs(o) {
61
69
  return s;
62
70
  }
63
71
 
72
+ /** The fixed kinds a rail entry may name (issue 307). */
73
+ export const NAV_KINDS = ["project", "den", "service", "hub"];
74
+
75
+ /**
76
+ * A rail entry's kind (issue 307), from its data: one of NAV_KINDS (any
77
+ * case), or { artifact: "<word>" }, an artifact pin's own kind word (the
78
+ * host's, already in its language). Anything else is no kind: the tag
79
+ * never names a machine. → { id, word } or null.
80
+ */
81
+ export function navKind(kind, lang) {
82
+ if (kind == null) return null;
83
+ if (typeof kind === "string") {
84
+ const id = kind.trim().toLowerCase();
85
+ return NAV_KINDS.includes(id)
86
+ ? { id, word: kitT(lang, "kit.kind." + id) }
87
+ : null;
88
+ }
89
+ if (typeof kind === "object" && kind.artifact != null) {
90
+ const word = String(kind.artifact).trim();
91
+ return word ? { id: "artifact", word } : null;
92
+ }
93
+ return null;
94
+ }
95
+
96
+ /**
97
+ * Attributes that carry a host's words, not data: never in an entry of the rail
98
+ * (issue 307). data-* attributes are data and stay allowed.
99
+ */
100
+ export const HOST_TEXT_ATTRS = [
101
+ "title",
102
+ "aria-label",
103
+ "aria-labelledby",
104
+ "aria-description",
105
+ ];
106
+
107
+ /** What a badge counts (issue 307): the kit words it. */
108
+ export const BADGE_WHATS = ["open", "waiting", "down", "running"];
109
+
110
+ /**
111
+ * Why a rail entry is off (issue 326): the kit greys it (.den-off) and
112
+ * words why. "connect" (not connected yet), "reconnect" (its sign-in is
113
+ * no longer accepted), "forbidden" (not allowed; an admin decides),
114
+ * "unreachable" (can't be reached right now), "config" (its settings have
115
+ * a problem). Anything else is dropped.
116
+ */
117
+ export const NAV_OFFS = [
118
+ "connect",
119
+ "reconnect",
120
+ "forbidden",
121
+ "unreachable",
122
+ "config",
123
+ ];
124
+
125
+ /** An entry's off reason, from its data, or null. */
126
+ export function navOff(off) {
127
+ const id = typeof off === "string" ? off.trim().toLowerCase() : "";
128
+ return NAV_OFFS.includes(id) ? id : null;
129
+ }
130
+
131
+ /**
132
+ * A badge's words, from its data: "3 waiting". Without a `what` (a plain
133
+ * total) the kit reads the bare number, so "Hub Docs, 4" still says the
134
+ * count the visible badge (aria-hidden) shows. Nothing to count: none.
135
+ */
136
+ function badgeWords(b, lang) {
137
+ if (!b || b.n == null || b.n === "") return "";
138
+ const n = typeof b.n === "number" ? b.n : String(b.n);
139
+ if (BADGE_WHATS.includes(b.what))
140
+ return kitT(lang, "kit.badge." + b.what, { n });
141
+ if (typeof n !== "number") return n;
142
+ try {
143
+ return new Intl.NumberFormat(lang || undefined).format(n);
144
+ } catch {
145
+ return String(n);
146
+ }
147
+ }
148
+
149
+ /** The icon, or (issue 307) a project's initial in a disc: { initial }. */
150
+ function leadIcon(ic) {
151
+ if (ic && typeof ic === "object") {
152
+ const ch = Array.from(String(ic.initial ?? "").trim())[0] || "";
153
+ return (
154
+ '<span class="den-ini" aria-hidden="true">' +
155
+ esc(ch.toLocaleUpperCase()) +
156
+ "</span>"
157
+ );
158
+ }
159
+ return icon(ic);
160
+ }
161
+
64
162
  /**
65
163
  * One rail entry (a link, .den-ni). `navRole`: the data-nav-role the nav
66
164
  * inventory reads ("page" for a rail group's entries: host content).
165
+ * `lang`: the kit's words (the kind tag's).
166
+ *
167
+ * `kind` (issue 307): a small uppercase tag at the row's end, in the scope
168
+ * labels' look (.den-k), for pins and links into another product. The
169
+ * tooltip and the name are always the kit's: "<label> · <Kind>" (and a
170
+ * badge's label after a comma), so a collapsed rail still says it. A
171
+ * host's tip, title or aria-label on such an entry is not used: hosts pass
172
+ * data, the kit owns the words.
173
+ *
174
+ * `badge.what` ("open", "waiting", "down", "running"): what the badge
175
+ * counts; the kit words it ("North · Den, 3 waiting"). Without one, the
176
+ * bare number reads ("Hub Docs, 4"). A label that is
177
+ * its kind word reads once ("Hub", not "Hub · Hub"). A pin row gets
178
+ * aria-keyshortcuts="Delete" from the kit.
179
+ *
180
+ * `off` (issue 326, NAV_OFFS): the entry is greyed (.den-off) and its
181
+ * tooltip and name say why in the kit's words ("Acme Co · Hub, reconnect
182
+ * in Settings"); an entry without a kind reads "<label>, <why>". An `off`
183
+ * the kit doesn't know is dropped.
184
+ *
185
+ * Host text on any entry (HOST_TEXT_ATTRS in `attrs`, a badge's `label`)
186
+ * is stamped data-nav-host-text, and the nav inventory fails on it.
67
187
  */
68
- export function navItemHtml(n, navRole) {
188
+ export function navItemHtml(n, navRole, lang) {
69
189
  const role =
70
190
  typeof navRole === "string" ? ' data-nav-role="' + esc(navRole) + '"' : "";
71
191
  const b = n.badge || null;
192
+ const kind = navKind(n.kind, lang);
193
+ const off = navOff(n.off);
72
194
  const extra = b
73
195
  ? '<span class="den-bdg' +
74
196
  (b.bad ? " bad" : "") +
@@ -76,12 +198,42 @@ export function navItemHtml(n, navRole) {
76
198
  esc(b.n) +
77
199
  "</span>"
78
200
  : "";
201
+ const tag = kind
202
+ ? '<span class="den-k den-ni-kind" data-nav-role="kind" aria-hidden="true">' +
203
+ esc(kind.word) +
204
+ "</span>"
205
+ : "";
79
206
  const more = { ...(n.attrs || {}) };
80
- if (b && b.label && more["aria-label"] == null)
81
- more["aria-label"] = n.label + ", " + b.label;
207
+ // Host text: words a host put on the entry itself. Never used on an
208
+ // entry with a kind; stamped for the nav inventory, which fails on it.
209
+ const hostText = HOST_TEXT_ATTRS.filter((k) => more[k] != null);
210
+ if (b && b.label != null) hostText.push("badge.label");
211
+ // A label that is its kind word (any case) reads once: "Hub", never
212
+ // "Hub · Hub".
213
+ const same =
214
+ kind &&
215
+ String(n.label).trim().toLocaleLowerCase() ===
216
+ kind.word.trim().toLocaleLowerCase();
217
+ const named = kind && !same ? n.label + " · " + kind.word : n.label;
218
+ const counted = badgeWords(b, lang);
219
+ // Issue 326: why it is off, in the kit's words, after the name.
220
+ const why = off ? kitT(lang, "kit.off." + off) : "";
221
+ const tip = why ? named + ", " + why : named;
222
+ if (kind || off) {
223
+ for (const k of HOST_TEXT_ATTRS) delete more[k];
224
+ more["aria-label"] = [named, counted, why].filter(Boolean).join(", ");
225
+ if (kind) more["data-nav-kind"] = kind.id;
226
+ if (off) more["data-nav-off"] = off;
227
+ } else if (more["aria-label"] == null && (counted || (b && b.label)))
228
+ // Without a kind, as before: a badge's label still reads (flagged).
229
+ more["aria-label"] = named + ", " + (counted || b.label);
230
+ // A pin row's Delete (unpin) is the kit's own feature: its shortcut too.
231
+ if (n.pin != null) more["aria-keyshortcuts"] = "Delete";
232
+ if (hostText.length) more["data-nav-host-text"] = hostText.join(",");
82
233
  const link =
83
234
  '<a class="den-ni' +
84
235
  (n.cls ? " " + esc(n.cls) : "") +
236
+ (off ? " den-off" : "") +
85
237
  '" href="' +
86
238
  esc(n.href) +
87
239
  '" data-nav="' +
@@ -91,15 +243,16 @@ export function navItemHtml(n, navRole) {
91
243
  (n.current ? ' aria-current="page"' : "") +
92
244
  attrs(more) +
93
245
  ' title="' +
94
- esc(n.tip || n.label) +
246
+ esc(kind || off ? tip : n.tip || n.label) +
95
247
  '">' +
96
- (n.lead || icon(n.icon)) +
248
+ (n.lead || leadIcon(n.icon)) +
97
249
  '<span class="den-lbl' +
98
250
  (n.mono ? " den-mono" : "") +
99
251
  '">' +
100
252
  esc(n.label) +
101
253
  "</span>" +
102
254
  extra +
255
+ tag +
103
256
  "</a>";
104
257
  return n.pin != null
105
258
  ? '<div class="den-pin-row"' +
@@ -177,13 +330,13 @@ export function railHtml({
177
330
  if (!items.length) continue;
178
331
  html += railGroupHtml(
179
332
  g,
180
- items.map((n) => navItemHtml(n, "page")).join(""),
333
+ items.map((n) => navItemHtml(n, "page", lang)).join(""),
181
334
  lang,
182
335
  );
183
336
  }
184
337
  html += '<div class="den-rail-grow"></div>';
185
338
  html += foot
186
- .map((n) => (typeof n === "string" ? n : navItemHtml(n)))
339
+ .map((n) => (typeof n === "string" ? n : navItemHtml(n, undefined, lang)))
187
340
  .join("");
188
341
  html += settingsHtml(settings, lang);
189
342
  html += accountHtml(account);
@@ -196,7 +349,11 @@ export function versionChipHtml({ version, lang } = {}) {
196
349
  return (
197
350
  '<button class="den-ib v-pill" id="version-pill" title="' +
198
351
  esc(kitT(lang, "kit.version.title")) +
199
- '"><span class="v-txt">' +
352
+ '"><span class="v-txt"' +
353
+ (version
354
+ ? ' title="v' + esc(String(version).replace(/^v/, "")) + '"'
355
+ : "") +
356
+ ">" +
200
357
  esc(version ? "v" + String(version).replace(/^v/, "") : "v…") +
201
358
  '</span><span class="v-pip" id="v-pip"></span></button>'
202
359
  );
@@ -277,7 +434,15 @@ export function markHtml(mark) {
277
434
  );
278
435
  }
279
436
 
280
- /** The top bar (header.den-top). */
437
+ /** Where the kit's search starts in a `start` slot (searchHtml's markup). */
438
+ const SEARCH_AT = '<button type="button" class="den-search"';
439
+
440
+ /**
441
+ * The top bar (header.den-top), in three parts (issue 303): the left part
442
+ * (☰, the brand, and what `start` holds before the search: the crumbs),
443
+ * the search in a fixed slot, and the chips (the host's `end`, then
444
+ * Report, the bell, the version and ◐ ▾, pinned to the right edge).
445
+ */
281
446
  export function topBarHtml({
282
447
  product = {},
283
448
  lang,
@@ -285,8 +450,13 @@ export function topBarHtml({
285
450
  report = true,
286
451
  } = {}) {
287
452
  const name = product.name ?? "";
453
+ const start = slots.start || "";
454
+ const at = start.indexOf(SEARCH_AT);
455
+ const before = at < 0 ? start : start.slice(0, at);
456
+ const search = at < 0 ? "" : start.slice(at);
288
457
  return (
289
458
  '<header class="den-top">' +
459
+ '<div class="den-left">' +
290
460
  '<button class="den-ib den-menu-btn" id="den-menu-btn" aria-label="' +
291
461
  esc(kitT(lang, "kit.menu")) +
292
462
  '" aria-expanded="false" aria-controls="den-rail">☰</button>' +
@@ -300,9 +470,15 @@ export function topBarHtml({
300
470
  '<span class="den-title" data-nav-role="brand-title">' +
301
471
  esc(name) +
302
472
  "</span></a>" +
303
- (slots.start || "") +
473
+ before +
474
+ "</div>" +
475
+ search +
304
476
  '<div class="den-chips">' +
477
+ // The host's chips' group, always drawn (empty without chips): the
478
+ // bar's structure is the same in any host.
479
+ '<div class="den-end">' +
305
480
  (slots.end || "") +
481
+ "</div>" +
306
482
  (report === false ? "" : reportButtonHtml({ inTop: true, lang })) +
307
483
  (slots.bell || "") +
308
484
  versionChipHtml({ version: product.version, lang }) +
@@ -502,7 +678,12 @@ export function scopeHtml({ crumbs, label, lang, after } = {}) {
502
678
  '"><span class="den-sep" aria-hidden="true">/</span>';
503
679
  // Each separator after a crumb comes and goes with it: host content.
504
680
  list.forEach((c, i) => {
505
- html += scopeCrumbHtml(c, lang);
681
+ // The last crumb is kept whole when the crumbs give way (issue 303).
682
+ const last = i === list.length - 1;
683
+ html += scopeCrumbHtml(
684
+ last ? { ...c, cls: (c.cls ? c.cls + " " : "") + "den-last" } : c,
685
+ lang,
686
+ );
506
687
  if (i < list.length - 1)
507
688
  html +=
508
689
  '<span class="den-sep den-' +
@@ -515,25 +696,24 @@ export function scopeHtml({ crumbs, label, lang, after } = {}) {
515
696
  }
516
697
 
517
698
  /**
518
- * The search field: the placeholder, then ⌘K inside it, as a button (the
519
- * host's palette or page opens on it: hydrateSearch).
520
- * searchHtml({ placeholder?, label?, id?, shortcut?, lang })
521
- * `label`: its name (default the kit's "Search", in the page's language;
522
- * the host's own words are host content for the nav inventory);
699
+ * The search field: the kit's placeholder ("Search…"), then ⌘K inside it,
700
+ * as a button (the host's palette or page opens on it: hydrateSearch).
701
+ * searchHtml({ id?, shortcut?, lang })
702
+ * Its placeholder and its name ("Search") are the kit's words, in the
703
+ * page's language: one search in every product, so a host can't set them.
523
704
  * `shortcut: false`: no ⌘K.
524
705
  */
525
- export function searchHtml({ placeholder, label, id, shortcut, lang } = {}) {
526
- const ph = placeholder ?? kitT(lang, "kit.search.ph");
706
+ export function searchHtml({ id, shortcut, lang } = {}) {
527
707
  const keys = shortcut !== false;
528
708
  return (
529
709
  '<button type="button" class="den-search" id="' +
530
710
  esc(id || "den-search") +
531
- '" aria-haspopup="dialog" data-nav-name="host"' +
711
+ '" aria-haspopup="dialog" data-nav-kit="search"' +
532
712
  (keys ? ' aria-keyshortcuts="Meta+K Control+K"' : "") +
533
713
  ' aria-label="' +
534
- esc(label ?? kitT(lang, "kit.search.label")) +
535
- '"><span class="den-search-ph" data-nav-role="search-ph">' +
536
- esc(ph) +
714
+ esc(kitT(lang, "kit.search.label")) +
715
+ '"><span class="den-search-ph">' +
716
+ esc(kitT(lang, "kit.search.ph")) +
537
717
  "</span>" +
538
718
  (keys ? '<kbd aria-hidden="true">⌘K</kbd>' : "") +
539
719
  '<span class="den-search-ic" aria-hidden="true">⌕</span></button>'
@@ -654,18 +834,76 @@ export function bellPanelHtml({
654
834
  * name and tooltip read "label · detail". It is a rail entry (.den-ni)
655
835
  * marked data-den-account. No label: nothing.
656
836
  */
837
+ /**
838
+ * Who is signed in, as the kit draws it (0.7.1): the name on the first
839
+ * line, @login on the second; @login alone when no name is known. The
840
+ * role is shown in the account menu's header only ({ menu: true }), on
841
+ * its own line. From data: { name?, login?, role? }; with no
842
+ * identity, { local: true } draws the kit's word for this computer. The old
843
+ * { label, detail } (or the menu's { name, detail }) still work for one
844
+ * release, as they were given, marked data-nav-legacy for the nav
845
+ * inventory. Returns { lines: [first, second?], shape, legacy, full }.
846
+ */
847
+ export function accountWho(a, { menu = false } = {}) {
848
+ if (!a) return null;
849
+ const legacy = a.label != null || a.detail != null;
850
+ const name = a.name ?? a.label ?? "";
851
+ const login = a.login
852
+ ? String(a.login).startsWith("@")
853
+ ? String(a.login)
854
+ : "@" + a.login
855
+ : (a.detail ?? "");
856
+ // The role is shown in the account menu's header only, never in the
857
+ // rail's entry (it isn't part of the entry's shape).
858
+ const role = menu ? a.role || "" : "";
859
+ let first = name || login;
860
+ // No identity known at all: the kit's own word for this computer.
861
+ const local = !first && !!a.local;
862
+ if (local) first = kitT(a.lang, "kit.account.local");
863
+ if (!first) return null;
864
+ const second = name ? login : "";
865
+ const shape = [
866
+ name ? "name" : "",
867
+ a.login || a.detail ? "login" : "",
868
+ local ? "local" : "",
869
+ ]
870
+ .filter(Boolean)
871
+ .join(",");
872
+ const lines = [first, second].filter(Boolean);
873
+ return {
874
+ lines,
875
+ role,
876
+ shape,
877
+ legacy,
878
+ full: lines.join(" · "),
879
+ };
880
+ }
881
+
882
+ function whoLinesHtml(who) {
883
+ return (
884
+ '<span class="den-acct-n">' +
885
+ esc(who.lines[0]) +
886
+ "</span>" +
887
+ (who.lines[1]
888
+ ? '<span class="den-acct-l">' + esc(who.lines[1]) + "</span>"
889
+ : "") +
890
+ (who.role ? '<span class="den-acct-r">' + esc(who.role) + "</span>" : "")
891
+ );
892
+ }
893
+
657
894
  export function accountHtml(a) {
658
- if (!a || !a.label) return "";
659
- const full = a.detail ? a.label + " · " + a.detail : a.label;
895
+ const who = accountWho(a);
896
+ if (!who) return "";
897
+ const full = who.full;
660
898
  const more = { ...(a.attrs || {}) };
661
899
  if (a.id) more.id = a.id;
662
- // Who is signed in: the host's words (host content for the nav inventory).
900
+ more["data-nav-shape"] = who.shape;
901
+ if (who.legacy) more["data-nav-legacy"] = "account";
902
+ // Who is signed in: the host's data (host content for the nav
903
+ // inventory, which compares its shape: which fields, how many lines).
663
904
  const words =
664
- '<span class="den-lbl" data-nav-role="account-name">' +
665
- esc(a.label) +
666
- (a.detail
667
- ? '<span class="den-acct-d"> · ' + esc(a.detail) + "</span>"
668
- : "") +
905
+ '<span class="den-lbl den-acct" data-nav-role="account-name">' +
906
+ whoLinesHtml(who) +
669
907
  "</span>";
670
908
  if (a.href == null && a.menu && !a.id) more.id = "den-account";
671
909
  const open =
@@ -834,20 +1072,33 @@ export function appearanceMenuHtml({
834
1072
  }
835
1073
 
836
1074
  /**
837
- * The account entry's menu: who is signed in (name), where (detail), a
838
- * rule, then the entries.
839
- * accountMenuHtml({ name, detail?, items, lang })
1075
+ * The account entry's menu: who is signed in (accountWho: the name, then
1076
+ * @login), a rule, then the entries.
1077
+ * accountMenuHtml({ name?, login?, role?, items, lang })
1078
+ * ({ name, detail } is the old path, for one release.)
840
1079
  * items: [{ label, href }] links, [{ label, id }] buttons (onPick "item"),
841
1080
  * or [{ label, action: { method?, url, csrf?, field? } }]: a form that
842
1081
  * posts to url on its own, with no script (csrf, if any, in a hidden
843
1082
  * field named `field`, "csrf" by default).
844
1083
  */
845
- export function accountMenuHtml({ name, detail, items } = {}) {
1084
+ export function accountMenuHtml({ name, login, role, detail, items } = {}) {
846
1085
  const parts = [];
847
- const who =
848
- (name ? '<div class="den-ml">' + esc(name) + "</div>" : "") +
849
- (detail ? '<p class="den-mnote">' + esc(detail) + "</p>" : "");
850
- if (who) parts.push(who);
1086
+ // The menu's { name, detail } were free text: the old path maps them
1087
+ // as the entry's label and detail.
1088
+ const w = accountWho(
1089
+ detail != null ? { label: name, detail } : { name, login, role },
1090
+ { menu: true },
1091
+ );
1092
+ if (w)
1093
+ parts.push(
1094
+ '<div class="den-ml den-acct" data-nav-shape="' +
1095
+ esc(w.shape) +
1096
+ '"' +
1097
+ (w.legacy ? ' data-nav-legacy="account"' : "") +
1098
+ ">" +
1099
+ whoLinesHtml(w) +
1100
+ "</div>",
1101
+ );
851
1102
  const rows = (items || [])
852
1103
  .map((it) => {
853
1104
  if (!it || !it.label) return "";