sendproof-core 0.1.0.alpha.1

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.
Files changed (229) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +55 -0
  4. data/lib/sendproof/assets.rb +70 -0
  5. data/lib/sendproof/attribution.rb +98 -0
  6. data/lib/sendproof/capture.rb +66 -0
  7. data/lib/sendproof/clock.rb +54 -0
  8. data/lib/sendproof/configuration.rb +241 -0
  9. data/lib/sendproof/data_store.rb +34 -0
  10. data/lib/sendproof/dynamodb/store.rb +183 -0
  11. data/lib/sendproof/dynamodb.rb +17 -0
  12. data/lib/sendproof/elasticsearch/store.rb +24 -0
  13. data/lib/sendproof/elasticsearch.rb +17 -0
  14. data/lib/sendproof/firestore/interceptor.rb +38 -0
  15. data/lib/sendproof/firestore/store.rb +150 -0
  16. data/lib/sendproof/firestore.rb +99 -0
  17. data/lib/sendproof/handler.rb +23 -0
  18. data/lib/sendproof/identity.rb +37 -0
  19. data/lib/sendproof/limits.rb +113 -0
  20. data/lib/sendproof/middleware.rb +68 -0
  21. data/lib/sendproof/mongo/engine.rb +692 -0
  22. data/lib/sendproof/mongo/guarded.rb +100 -0
  23. data/lib/sendproof/mongo/plain.rb +45 -0
  24. data/lib/sendproof/mongo/store.rb +54 -0
  25. data/lib/sendproof/mongo/subscriber.rb +21 -0
  26. data/lib/sendproof/mongo.rb +41 -0
  27. data/lib/sendproof/mongoid/store.rb +58 -0
  28. data/lib/sendproof/mongoid.rb +65 -0
  29. data/lib/sendproof/observed_request.rb +108 -0
  30. data/lib/sendproof/opensearch/store.rb +21 -0
  31. data/lib/sendproof/opensearch.rb +17 -0
  32. data/lib/sendproof/operation_matcher.rb +28 -0
  33. data/lib/sendproof/pool_hooks.rb +80 -0
  34. data/lib/sendproof/preview_request.rb +44 -0
  35. data/lib/sendproof/preview_transport.rb +331 -0
  36. data/lib/sendproof/protocol.rb +81 -0
  37. data/lib/sendproof/provider.rb +83 -0
  38. data/lib/sendproof/recorder.rb +298 -0
  39. data/lib/sendproof/redaction.rb +98 -0
  40. data/lib/sendproof/redis/commands.rb +76 -0
  41. data/lib/sendproof/redis/store.rb +205 -0
  42. data/lib/sendproof/redis.rb +18 -0
  43. data/lib/sendproof/search/middleware.rb +39 -0
  44. data/lib/sendproof/search/store.rb +213 -0
  45. data/lib/sendproof/search.rb +82 -0
  46. data/lib/sendproof/stores/classify.rb +346 -0
  47. data/lib/sendproof/stores/guard.rb +127 -0
  48. data/lib/sendproof/stores/tables.rb +1328 -0
  49. data/lib/sendproof/stores.rb +12 -0
  50. data/lib/sendproof/trace_store.rb +110 -0
  51. data/lib/sendproof/transaction_control.rb +187 -0
  52. data/lib/sendproof/ui/action-bar.js +91 -0
  53. data/lib/sendproof/ui/api.js +239 -0
  54. data/lib/sendproof/ui/app.css +848 -0
  55. data/lib/sendproof/ui/app.js +742 -0
  56. data/lib/sendproof/ui/autofill.js +137 -0
  57. data/lib/sendproof/ui/code-copy.js +71 -0
  58. data/lib/sendproof/ui/code-picker.js +149 -0
  59. data/lib/sendproof/ui/commands.js +197 -0
  60. data/lib/sendproof/ui/components.js +77 -0
  61. data/lib/sendproof/ui/context-bar.js +82 -0
  62. data/lib/sendproof/ui/copy.js +92 -0
  63. data/lib/sendproof/ui/coverage-popover.js +35 -0
  64. data/lib/sendproof/ui/crumb-fit.js +80 -0
  65. data/lib/sendproof/ui/deprecation.js +164 -0
  66. data/lib/sendproof/ui/display-settings.js +175 -0
  67. data/lib/sendproof/ui/doc-diff.js +209 -0
  68. data/lib/sendproof/ui/docs-view.js +347 -0
  69. data/lib/sendproof/ui/favicon.png +0 -0
  70. data/lib/sendproof/ui/favicon.svg +1 -0
  71. data/lib/sendproof/ui/field-help.js +52 -0
  72. data/lib/sendproof/ui/field-modules.js +23 -0
  73. data/lib/sendproof/ui/fields/controls.js +179 -0
  74. data/lib/sendproof/ui/fields/dom.js +36 -0
  75. data/lib/sendproof/ui/fields/infer.js +71 -0
  76. data/lib/sendproof/ui/fields/lookup-source.js +63 -0
  77. data/lib/sendproof/ui/fields/lookup.js +285 -0
  78. data/lib/sendproof/ui/fields/render.js +496 -0
  79. data/lib/sendproof/ui/fields/select.js +175 -0
  80. data/lib/sendproof/ui/fields/validate.js +363 -0
  81. data/lib/sendproof/ui/fields/validation-formats.js +164 -0
  82. data/lib/sendproof/ui/fields/validation-references.js +68 -0
  83. data/lib/sendproof/ui/fields/validation-values.js +137 -0
  84. data/lib/sendproof/ui/generate-menu.js +44 -0
  85. data/lib/sendproof/ui/generate.js +184 -0
  86. data/lib/sendproof/ui/header-editor.js +777 -0
  87. data/lib/sendproof/ui/headers.js +188 -0
  88. data/lib/sendproof/ui/highlight.js +257 -0
  89. data/lib/sendproof/ui/history-popover.js +24 -0
  90. data/lib/sendproof/ui/i18n.js +253 -0
  91. data/lib/sendproof/ui/icons.js +87 -0
  92. data/lib/sendproof/ui/index.html +70 -0
  93. data/lib/sendproof/ui/issues.js +187 -0
  94. data/lib/sendproof/ui/json-view.js +166 -0
  95. data/lib/sendproof/ui/lang-icons.js +62 -0
  96. data/lib/sendproof/ui/layout.js +262 -0
  97. data/lib/sendproof/ui/listbox.js +334 -0
  98. data/lib/sendproof/ui/locales/ar-capture.js +366 -0
  99. data/lib/sendproof/ui/locales/ar-stores.js +116 -0
  100. data/lib/sendproof/ui/locales/ar.js +561 -0
  101. data/lib/sendproof/ui/locales/cs-capture.js +442 -0
  102. data/lib/sendproof/ui/locales/cs-stores.js +116 -0
  103. data/lib/sendproof/ui/locales/cs.js +689 -0
  104. data/lib/sendproof/ui/locales/da-capture.js +347 -0
  105. data/lib/sendproof/ui/locales/da-stores.js +116 -0
  106. data/lib/sendproof/ui/locales/da.js +522 -0
  107. data/lib/sendproof/ui/locales/de-capture.js +347 -0
  108. data/lib/sendproof/ui/locales/de-stores.js +116 -0
  109. data/lib/sendproof/ui/locales/de.js +519 -0
  110. data/lib/sendproof/ui/locales/el-capture.js +347 -0
  111. data/lib/sendproof/ui/locales/el-stores.js +116 -0
  112. data/lib/sendproof/ui/locales/el.js +520 -0
  113. data/lib/sendproof/ui/locales/en-capture.js +349 -0
  114. data/lib/sendproof/ui/locales/en-stores.js +95 -0
  115. data/lib/sendproof/ui/locales/en.js +522 -0
  116. data/lib/sendproof/ui/locales/es-capture.js +353 -0
  117. data/lib/sendproof/ui/locales/es-stores.js +116 -0
  118. data/lib/sendproof/ui/locales/es.js +525 -0
  119. data/lib/sendproof/ui/locales/fa-capture.js +348 -0
  120. data/lib/sendproof/ui/locales/fa-stores.js +116 -0
  121. data/lib/sendproof/ui/locales/fa.js +521 -0
  122. data/lib/sendproof/ui/locales/fi-capture.js +347 -0
  123. data/lib/sendproof/ui/locales/fi-stores.js +116 -0
  124. data/lib/sendproof/ui/locales/fi.js +523 -0
  125. data/lib/sendproof/ui/locales/fr-capture.js +353 -0
  126. data/lib/sendproof/ui/locales/fr-stores.js +116 -0
  127. data/lib/sendproof/ui/locales/fr.js +525 -0
  128. data/lib/sendproof/ui/locales/he-capture.js +358 -0
  129. data/lib/sendproof/ui/locales/he-stores.js +116 -0
  130. data/lib/sendproof/ui/locales/he.js +529 -0
  131. data/lib/sendproof/ui/locales/hi-capture.js +348 -0
  132. data/lib/sendproof/ui/locales/hi-stores.js +116 -0
  133. data/lib/sendproof/ui/locales/hi.js +523 -0
  134. data/lib/sendproof/ui/locales/id-capture.js +347 -0
  135. data/lib/sendproof/ui/locales/id-stores.js +116 -0
  136. data/lib/sendproof/ui/locales/id.js +521 -0
  137. data/lib/sendproof/ui/locales/it-capture.js +361 -0
  138. data/lib/sendproof/ui/locales/it-stores.js +116 -0
  139. data/lib/sendproof/ui/locales/it.js +523 -0
  140. data/lib/sendproof/ui/locales/ja-capture.js +346 -0
  141. data/lib/sendproof/ui/locales/ja-stores.js +116 -0
  142. data/lib/sendproof/ui/locales/ja.js +521 -0
  143. data/lib/sendproof/ui/locales/ko-capture.js +346 -0
  144. data/lib/sendproof/ui/locales/ko-stores.js +116 -0
  145. data/lib/sendproof/ui/locales/ko.js +522 -0
  146. data/lib/sendproof/ui/locales/nb-capture.js +349 -0
  147. data/lib/sendproof/ui/locales/nb-stores.js +116 -0
  148. data/lib/sendproof/ui/locales/nb.js +521 -0
  149. data/lib/sendproof/ui/locales/nl-capture.js +355 -0
  150. data/lib/sendproof/ui/locales/nl-stores.js +116 -0
  151. data/lib/sendproof/ui/locales/nl.js +520 -0
  152. data/lib/sendproof/ui/locales/pl-capture.js +432 -0
  153. data/lib/sendproof/ui/locales/pl-stores.js +116 -0
  154. data/lib/sendproof/ui/locales/pl.js +528 -0
  155. data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
  156. data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
  157. data/lib/sendproof/ui/locales/pt-BR.js +523 -0
  158. data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
  159. data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
  160. data/lib/sendproof/ui/locales/pt-PT.js +523 -0
  161. data/lib/sendproof/ui/locales/ru-capture.js +437 -0
  162. data/lib/sendproof/ui/locales/ru-stores.js +116 -0
  163. data/lib/sendproof/ui/locales/ru.js +624 -0
  164. data/lib/sendproof/ui/locales/sv-capture.js +349 -0
  165. data/lib/sendproof/ui/locales/sv-stores.js +116 -0
  166. data/lib/sendproof/ui/locales/sv.js +518 -0
  167. data/lib/sendproof/ui/locales/th-capture.js +347 -0
  168. data/lib/sendproof/ui/locales/th-stores.js +116 -0
  169. data/lib/sendproof/ui/locales/th.js +521 -0
  170. data/lib/sendproof/ui/locales/tr-capture.js +348 -0
  171. data/lib/sendproof/ui/locales/tr-stores.js +116 -0
  172. data/lib/sendproof/ui/locales/tr.js +521 -0
  173. data/lib/sendproof/ui/locales/uk-capture.js +442 -0
  174. data/lib/sendproof/ui/locales/uk-stores.js +116 -0
  175. data/lib/sendproof/ui/locales/uk.js +630 -0
  176. data/lib/sendproof/ui/locales/vi-capture.js +346 -0
  177. data/lib/sendproof/ui/locales/vi-stores.js +116 -0
  178. data/lib/sendproof/ui/locales/vi.js +523 -0
  179. data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
  180. data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
  181. data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
  182. data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
  183. data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
  184. data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
  185. data/lib/sendproof/ui/manifest.json +866 -0
  186. data/lib/sendproof/ui/markdown.js +39 -0
  187. data/lib/sendproof/ui/model-view.js +182 -0
  188. data/lib/sendproof/ui/navigation.js +233 -0
  189. data/lib/sendproof/ui/operation-header.js +76 -0
  190. data/lib/sendproof/ui/operations.js +7 -0
  191. data/lib/sendproof/ui/overview-view.js +63 -0
  192. data/lib/sendproof/ui/personas.js +59 -0
  193. data/lib/sendproof/ui/popover.js +71 -0
  194. data/lib/sendproof/ui/preview-false.js +108 -0
  195. data/lib/sendproof/ui/request-editor.js +496 -0
  196. data/lib/sendproof/ui/response-metadata.js +354 -0
  197. data/lib/sendproof/ui/response-viewer.js +618 -0
  198. data/lib/sendproof/ui/result-blocked.js +155 -0
  199. data/lib/sendproof/ui/result-capture.js +25 -0
  200. data/lib/sendproof/ui/result-database.js +185 -0
  201. data/lib/sendproof/ui/result-effects.js +176 -0
  202. data/lib/sendproof/ui/result-headers.js +106 -0
  203. data/lib/sendproof/ui/result-logs.js +408 -0
  204. data/lib/sendproof/ui/result-states.js +213 -0
  205. data/lib/sendproof/ui/results.js +371 -0
  206. data/lib/sendproof/ui/router.js +118 -0
  207. data/lib/sendproof/ui/schema-model.js +336 -0
  208. data/lib/sendproof/ui/schema-tree.js +344 -0
  209. data/lib/sendproof/ui/schema.js +397 -0
  210. data/lib/sendproof/ui/security.js +31 -0
  211. data/lib/sendproof/ui/seeded-menu.js +69 -0
  212. data/lib/sendproof/ui/settings.js +93 -0
  213. data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
  214. data/lib/sendproof/ui/shortcuts.js +190 -0
  215. data/lib/sendproof/ui/sidebar.js +289 -0
  216. data/lib/sendproof/ui/signin.js +259 -0
  217. data/lib/sendproof/ui/snippets.js +530 -0
  218. data/lib/sendproof/ui/store-marks.js +47 -0
  219. data/lib/sendproof/ui/stores.js +131 -0
  220. data/lib/sendproof/ui/styles.css +971 -0
  221. data/lib/sendproof/ui/toggletip.js +53 -0
  222. data/lib/sendproof/ui/uuid.js +10 -0
  223. data/lib/sendproof/ui/view.js +122 -0
  224. data/lib/sendproof/ui/views.css +653 -0
  225. data/lib/sendproof/version.rb +9 -0
  226. data/lib/sendproof/workbench.rb +102 -0
  227. data/lib/sendproof-core.rb +3 -0
  228. data/lib/sendproof.rb +121 -0
  229. metadata +292 -0
@@ -0,0 +1,334 @@
1
+ // The listbox popover shared by selects, the ID lookup and the language picker (mocks 78–81,
2
+ // 107, 112, m19, m20, m24, m25). It follows the WAI-ARIA select-only combobox pattern: focus
3
+ // stays on the control (or moves into the search box when the list is long) and
4
+ // aria-activedescendant points at the active option.
5
+ //
6
+ // Keys ↑ ↓ move · Home End jump · Enter or Space chooses · Esc closes (or clears the
7
+ // search first) · Tab chooses the active option and moves on · typing jumps to the
8
+ // first value that starts with what was typed (500 ms buffer, echoed in the footer)
9
+ // Desktop anchored to the control's start edge (the right edge in RTL), at least the
10
+ // control's width and 16rem, below it or above when there is no room; the list gives
11
+ // up height before the popover passes the viewport, so search and footer stay
12
+ // Phone (56rem and narrower) a bottom sheet: grab handle, the field's name and
13
+ // description as its title, a 44 px Close, a scrim, focus kept inside, safe-area
14
+ // padding. It never covers its own trigger.
15
+ //
16
+ // The listbox is its own layer above the anchored popovers in popover.js (it can open from
17
+ // Display settings), so it handles Esc and outside clicks before they reach that module.
18
+ import { esc, icon } from './components.js';
19
+ import { formatNumber, t } from './i18n.js';
20
+ import { shortcutHint } from './shortcuts.js';
21
+
22
+ const SHEET_QUERY = '(max-width: 56rem)';
23
+ const TYPE_AHEAD_MS = 500;
24
+ let open;
25
+ let counter = 0;
26
+
27
+ /** Whether a listbox popover is open now. */
28
+ export const listboxOpen = () => !!open;
29
+
30
+ /** Closes the open listbox. `restore` puts focus back on its control. */
31
+ export function closeListbox(restore = false) {
32
+ if (!open) return;
33
+ const current = open;
34
+ open = undefined;
35
+ current.pop.remove();
36
+ current.scrim?.remove();
37
+ current.anchor.setAttribute('aria-expanded', 'false');
38
+ current.anchor.removeAttribute('aria-activedescendant');
39
+ current.anchor.classList.remove('is-open');
40
+ current.onClose?.();
41
+ if (restore) current.anchor.focus({ preventScroll: true });
42
+ }
43
+
44
+ const escapeRegExp = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
45
+ const fold = text => String(text ?? '').toLocaleLowerCase().normalize('NFD').replace(/\p{M}/gu, '');
46
+
47
+ /** Escapes `text` and marks the part that matches `query`. */
48
+ function highlight(text, query) {
49
+ const value = String(text ?? '');
50
+ if (!query) return esc(value);
51
+ const at = fold(value).indexOf(fold(query));
52
+ if (at < 0) return esc(value);
53
+ return `${esc(value.slice(0, at))}<mark>${esc(value.slice(at, at + query.length))}</mark>${esc(value.slice(at + query.length))}`;
54
+ }
55
+
56
+ /**
57
+ * Places a listbox popover against its anchor in viewport coordinates.
58
+ * Exported for tests and for the language picker, which opens beside Display settings.
59
+ */
60
+ export function placeListbox(anchor, pop, { align = 'start', gap = 6, width = 'anchor' } = {}) {
61
+ const a = anchor.getBoundingClientRect();
62
+ const rtl = getComputedStyle(anchor).direction === 'rtl';
63
+ const rem = parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
64
+ const list = pop.querySelector('.lb-list');
65
+ if (list) list.style.maxBlockSize = '';
66
+ Object.assign(pop.style, { position: 'fixed', top: '', bottom: '', left: '', right: '' });
67
+ // At least the trigger's width (never capped below it) and at least 16rem.
68
+ if (width === 'anchor') pop.style.inlineSize = Math.min(Math.max(a.width, 16 * rem), innerWidth - 24) + 'px';
69
+ const w = pop.offsetWidth;
70
+ const end = (align === 'end') !== rtl;
71
+ const left = Math.max(12, Math.min(end ? a.right - w : a.left, innerWidth - w - 12));
72
+ pop.style.left = left + 'px';
73
+ const height = pop.offsetHeight;
74
+ const below = innerHeight - a.bottom - gap - 12;
75
+ const above = a.top - gap - 12;
76
+ pop.classList.toggle('is-above', height > below && above > below);
77
+ if (height > below && above > below) pop.style.bottom = (innerHeight - a.top + gap) + 'px';
78
+ else pop.style.top = (a.bottom + gap) + 'px';
79
+ // Never past the viewport: the list gives up height; the search and footer stay.
80
+ const room = pop.classList.contains('is-above') ? above : below;
81
+ if (list && height > room) list.style.maxBlockSize = Math.max(7.5 * rem, list.offsetHeight - (height - room)) + 'px';
82
+ }
83
+
84
+ /**
85
+ * Opens a listbox for `anchor` (the element with role="combobox").
86
+ *
87
+ * @param {HTMLElement} anchor
88
+ * @param {object} config
89
+ * @param {string} config.label accessible name of the listbox
90
+ * @param {Array<object>} config.items options: { value, label, detail, description, mono,
91
+ * lang, dir, glyph (chip text), icon, end (HTML), group, keywords, command, unset }
92
+ * @param {*} [config.value] the chosen value (shows the check)
93
+ * @param {boolean|'auto'} [config.search] a search box: 'auto' when there are more than 8 values
94
+ * @param {string} [config.searchLabel] its placeholder
95
+ * @param {string} [config.before] HTML shown above the options (states such as loading)
96
+ * @param {string} [config.after] HTML under the list (the lookup's source line, a preview)
97
+ * @param {string|false} [config.foot] footer HTML; false hides it; default shows the keys
98
+ * @param {{title: string, sub?: string}} [config.sheet] the bottom sheet's title (HTML) on phones
99
+ * @param {string} [config.className] extra classes for the popover
100
+ * @param {(item: object) => void} config.onPick
101
+ * @param {(item: object) => void} [config.onActive] the active option changed (language preview)
102
+ * @param {(query: string) => void} [config.onSearch]
103
+ * @param {() => void} [config.onClose]
104
+ * @param {*} [config.active] the option to start on (default: the chosen one)
105
+ * @param {(event: KeyboardEvent, api: {item: object, choose: Function}) => boolean} [config.onKey]
106
+ * handles a key before the listbox does; return true when it did
107
+ */
108
+ export function openListbox(anchor, config) {
109
+ closeListbox();
110
+ const id = `lb-${++counter}`;
111
+ const sheet = !!config.sheet && matchMedia(SHEET_QUERY).matches;
112
+ const pop = document.createElement('div');
113
+ pop.className = `pop lb${sheet ? ' is-sheet' : ''}${config.className ? ' ' + config.className : ''}`;
114
+ pop.dataset.layer = 'listbox';
115
+ const searchable = config.search === 'auto' ? config.items.filter(item => !item.command && !item.unset).length > 8 : !!config.search;
116
+ let query = '';
117
+ let typed = '';
118
+ let typedTimer;
119
+ let shown = [];
120
+ let active = -1;
121
+ let scrim;
122
+
123
+ if (sheet) {
124
+ pop.setAttribute('role', 'dialog');
125
+ pop.setAttribute('aria-modal', 'true');
126
+ pop.setAttribute('aria-label', String(config.sheet.title).replace(/<[^>]+>/g, ''));
127
+ scrim = document.createElement('div');
128
+ scrim.className = 'sheet-scrim';
129
+ scrim.dataset.layer = 'listbox';
130
+ scrim.setAttribute('aria-hidden', 'true');
131
+ scrim.addEventListener('click', () => closeListbox(true));
132
+ document.body.append(scrim);
133
+ }
134
+ const keys = config.foot === false ? '' : config.foot ?? `<span>${shortcutHint('updown')}${esc(t('select.move'))}</span><span>${shortcutHint('enter')}${esc(t('select.choose'))}</span><span class="typed" data-typed hidden></span><span class="spacer"></span><span>${shortcutHint('escape')}</span>`;
135
+ pop.innerHTML = `${sheet ? `<div class="sheet-head"><span class="grab" aria-hidden="true"></span>
136
+ <div class="sheet-title"><strong>${config.sheet.title}</strong>${config.sheet.sub ? `<span>${config.sheet.sub}</span>` : ''}</div>
137
+ <button type="button" class="btn btn-ghost btn-icon" data-sheet-close aria-label="${esc(t('select.close'))}">${icon('x')}</button></div>` : ''}
138
+ ${searchable ? `<div class="lb-search">${icon('search', true)}<input type="text" role="searchbox" autocomplete="off" spellcheck="false" aria-controls="${id}" aria-label="${esc(config.searchLabel || t('select.search', { n: formatNumber(config.items.length) }))}" placeholder="${esc(config.searchLabel || t('select.search', { n: formatNumber(config.items.length) }))}"><span class="lb-count" aria-live="polite"></span></div>` : ''}
139
+ <ul class="lb-list" role="listbox" id="${id}" aria-label="${esc(config.label)}" ${sheet && !searchable ? 'tabindex="0"' : ''}></ul>
140
+ <div data-lb-after>${config.after ?? ''}</div>
141
+ ${keys ? `<div class="pop-foot" aria-hidden="true">${keys}</div>` : ''}`;
142
+ document.body.append(pop);
143
+ const list = pop.querySelector('.lb-list');
144
+ const search = pop.querySelector('.lb-search input');
145
+ const count = pop.querySelector('.lb-count');
146
+ // Keys go to the search box when there is one, to the list in a sheet, else to the control.
147
+ const keyTarget = search || (sheet ? list : anchor);
148
+
149
+ function matches(item) {
150
+ if (!query || item.unset) return !query || !item.unset;
151
+ if (item.command) return true;
152
+ const q = fold(query);
153
+ return [item.label, item.detail, item.description, item.value, ...(item.keywords || [])].some(text => fold(text).includes(q));
154
+ }
155
+
156
+ function optionHtml(item, index) {
157
+ const selected = !item.command && !item.unset ? Object.is(item.value, config.value) : item.unset ? config.value === undefined : false;
158
+ const lead = item.glyph !== undefined ? `<span class="chip-ico" aria-hidden="true">${esc(item.glyph)}</span>` : item.icon ? `<span class="lead-ico" aria-hidden="true">${icon(item.icon, true)}</span>` : '';
159
+ return `<li class="lb-opt${item.unset ? ' is-unset' : ''}${item.description || item.detail ? ' has-desc' : ''}" role="option" id="${id}-o${index}" data-index="${index}" aria-selected="${selected}"${item.lang ? ` lang="${esc(item.lang)}"` : ''}>
160
+ <span class="lb-check" aria-hidden="true">${selected ? icon('check', true) : ''}</span>${lead}
161
+ <span class="lb-text"><span class="v${item.mono ? ' mono' : ''}"${item.valueLang ? ` lang="${esc(item.valueLang)}"` : ''}${item.dir ? ` dir="${esc(item.dir)}"` : ''}>${item.html ?? highlight(item.label, query)}</span>${item.detail ? `<span class="d mono">${highlight(item.detail, query)}</span>` : ''}${item.description ? `<span class="d"${item.descriptionLang ? ` lang="${esc(item.descriptionLang)}"` : ''}>${highlight(item.description, query)}</span>` : ''}</span>
162
+ ${item.end ? `<span class="end">${item.end}</span>` : ''}</li>`;
163
+ }
164
+
165
+ function render() {
166
+ shown = config.items.map((item, index) => ({ item, index })).filter(({ item }) => matches(item));
167
+ const real = shown.filter(({ item }) => !item.command && !item.unset);
168
+ let html = config.before ?? '';
169
+ let group;
170
+ let sawCommand = false;
171
+ for (const [position, { item, index }] of shown.entries()) {
172
+ if (item.group !== undefined && item.group !== group) { group = item.group; html += `<li class="lb-group" role="presentation">${item.group}</li>`; }
173
+ if (item.command && !sawCommand && position > 0) html += '<li class="lb-sep" role="presentation"></li>';
174
+ if (item.command) sawCommand = true;
175
+ html += optionHtml(item, index);
176
+ }
177
+ if (query && !real.length) html += `<li class="lb-empty" role="presentation">${esc(t('select.noMatch', { query }))}</li>`;
178
+ list.innerHTML = html;
179
+ if (count) count.textContent = query ? t('select.matches', { n: real.length }) : '';
180
+ const keep = shown.findIndex(({ index }) => index === active);
181
+ setActive(keep >= 0 ? active : (query ? real[0]?.index : undefined) ?? shown[0]?.index ?? -1, false);
182
+ config.onSearch?.(query);
183
+ }
184
+
185
+ function setActive(index, scroll = true) {
186
+ active = index;
187
+ for (const row of list.querySelectorAll('.lb-opt')) row.classList.toggle('is-active', Number(row.dataset.index) === index);
188
+ const row = list.querySelector(`[data-index="${index}"]`);
189
+ if (row) {
190
+ keyTarget.setAttribute('aria-activedescendant', row.id);
191
+ if (keyTarget !== anchor) anchor.setAttribute('aria-activedescendant', row.id);
192
+ if (scroll) row.scrollIntoView({ block: 'nearest' });
193
+ } else keyTarget.removeAttribute('aria-activedescendant');
194
+ if (index >= 0) config.onActive?.(config.items[index]);
195
+ }
196
+
197
+ function move(step) {
198
+ if (!shown.length) return;
199
+ const at = shown.findIndex(({ index }) => index === active);
200
+ const next = shown[Math.max(0, Math.min(shown.length - 1, at < 0 ? (step > 0 ? 0 : shown.length - 1) : at + step))];
201
+ setActive(next.index);
202
+ }
203
+
204
+ function choose(index = active) {
205
+ const item = config.items[index];
206
+ if (!item) return false;
207
+ closeListbox(true);
208
+ config.onPick(item);
209
+ return true;
210
+ }
211
+
212
+ /** Type-ahead: jump to the first value that starts with what was typed. */
213
+ function typeAhead(char) {
214
+ clearTimeout(typedTimer);
215
+ typed += char;
216
+ typedTimer = setTimeout(() => { typed = ''; echo(); }, TYPE_AHEAD_MS);
217
+ const found = shown.find(({ item }) => !item.command && !item.unset && fold(item.label).startsWith(fold(typed)))
218
+ ?? shown.find(({ item }) => !item.command && !item.unset && fold(item.label).startsWith(fold(char)));
219
+ if (found) setActive(found.index);
220
+ echo();
221
+ }
222
+ function echo() {
223
+ const slot = pop.querySelector('[data-typed]');
224
+ if (!slot) return;
225
+ slot.hidden = !typed;
226
+ slot.innerHTML = typed ? `${esc(t('select.typed'))} <code>${esc(typed)}</code>` : '';
227
+ }
228
+
229
+ function onKey(event) {
230
+ if (!open || open.pop !== pop) return;
231
+ // A caller's own shortcuts first (the lookup's ⌘↵ picks and previews).
232
+ if (config.onKey?.(event, { item: config.items[active], choose })) { event.preventDefault(); event.stopPropagation(); return; }
233
+ if (event.altKey && event.key !== 'ArrowUp' || event.ctrlKey || event.metaKey) return;
234
+ const inSearch = event.target === search;
235
+ let handled = true;
236
+ switch (event.key) {
237
+ case 'ArrowDown': move(1); break;
238
+ case 'ArrowUp': if (event.altKey) choose(); else move(-1); break;
239
+ case 'PageDown': move(8); break;
240
+ case 'PageUp': move(-8); break;
241
+ case 'Home': if (inSearch && search.value) handled = false; else setActive(shown[0]?.index ?? -1); break;
242
+ case 'End': if (inSearch && search.value) handled = false; else setActive(shown.at(-1)?.index ?? -1); break;
243
+ case 'Enter': choose(); break;
244
+ case 'Tab':
245
+ if (sheet) { trapFocus(event); return; }
246
+ handled = false;
247
+ if (active >= 0 && config.items[active] && !config.items[active].command) { const item = config.items[active]; closeListbox(); config.onPick(item); }
248
+ else closeListbox();
249
+ break;
250
+ case ' ':
251
+ if (inSearch) { handled = false; break; }
252
+ if (typed) typeAhead(' '); else choose();
253
+ break;
254
+ default:
255
+ if (!inSearch && event.key.length === 1 && !event.altKey) typeAhead(event.key);
256
+ else handled = false;
257
+ }
258
+ if (handled) { event.preventDefault(); event.stopPropagation(); }
259
+ }
260
+
261
+ /** Keeps Tab inside the sheet (it is modal). */
262
+ function trapFocus(event) {
263
+ const stops = [...pop.querySelectorAll('button, input, [tabindex="0"]')].filter(node => !node.disabled && node.offsetParent !== null);
264
+ if (!stops.length) return;
265
+ const at = stops.indexOf(document.activeElement);
266
+ event.preventDefault();
267
+ stops[(at + (event.shiftKey ? -1 : 1) + stops.length) % stops.length].focus();
268
+ }
269
+
270
+ list.addEventListener('pointerdown', event => event.preventDefault());
271
+ list.addEventListener('pointermove', event => {
272
+ const row = event.target.closest('.lb-opt');
273
+ if (row && Number(row.dataset.index) !== active && event.pointerType !== 'touch') setActive(Number(row.dataset.index), false);
274
+ });
275
+ list.addEventListener('click', event => {
276
+ const row = event.target.closest('.lb-opt');
277
+ if (row) choose(Number(row.dataset.index));
278
+ });
279
+ pop.addEventListener('click', event => {
280
+ if (event.target.closest('[data-sheet-close]')) closeListbox(true);
281
+ });
282
+ search?.addEventListener('input', () => { query = search.value.trim(); render(); });
283
+ pop.addEventListener('keydown', onKey);
284
+ if (keyTarget === anchor) anchor.addEventListener('keydown', onKey);
285
+
286
+ anchor.setAttribute('aria-expanded', 'true');
287
+ anchor.setAttribute('aria-controls', id);
288
+ anchor.classList.add('is-open');
289
+ open = { pop, scrim, anchor, onClose: () => { anchor.removeEventListener('keydown', onKey); clearTimeout(typedTimer); config.onClose?.(); }, sheet, reposition: () => !sheet && placeListbox(anchor, pop, config.place) };
290
+ const start = config.active !== undefined ? config.items.findIndex(item => Object.is(item.value, config.active))
291
+ : config.items.findIndex(item => item.unset ? config.value === undefined : !item.command && Object.is(item.value, config.value));
292
+ active = start;
293
+ render();
294
+ if (!sheet) placeListbox(anchor, pop, config.place);
295
+ setActive(active >= 0 ? active : shown[0]?.index ?? -1);
296
+ if (keyTarget !== anchor) keyTarget.focus({ preventScroll: true });
297
+
298
+ return {
299
+ pop,
300
+ /** Replaces the options (a lookup that finished loading) and keeps the active one if it's still there. */
301
+ update(items, { before, after } = {}) {
302
+ config.items = items;
303
+ if (before !== undefined) config.before = before;
304
+ if (after !== undefined) pop.querySelector('[data-lb-after]').innerHTML = after;
305
+ render();
306
+ if (!sheet) placeListbox(anchor, pop, config.place);
307
+ },
308
+ setAfter(html) { pop.querySelector('[data-lb-after]').innerHTML = html; },
309
+ close: closeListbox,
310
+ };
311
+ }
312
+
313
+
314
+ // Global listeners: one set, in a browser only (unit tests import this module in Node). The
315
+ // window capture phase runs before popover.js's document listeners, so Esc and outside clicks
316
+ // close the listbox first and leave Display settings open beneath it.
317
+ if (typeof window !== 'undefined') {
318
+ window.addEventListener('keydown', event => {
319
+ if (!open || event.key !== 'Escape') return;
320
+ const search = open.pop.querySelector('.lb-search input');
321
+ event.preventDefault();
322
+ event.stopPropagation();
323
+ // The search box has one control: Esc clears it, then closes.
324
+ if (search && search.value && document.activeElement === search) { search.value = ''; search.dispatchEvent(new Event('input')); return; }
325
+ closeListbox(true);
326
+ }, true);
327
+ window.addEventListener('pointerdown', event => {
328
+ if (!open || open.pop.contains(event.target) || open.anchor.contains(event.target)) return;
329
+ if (open.scrim?.contains(event.target)) return;
330
+ closeListbox();
331
+ }, true);
332
+ window.addEventListener('resize', () => open?.reposition());
333
+ window.addEventListener('scroll', event => { if (open && !open.pop.contains(event.target)) open.reposition(); }, true);
334
+ }