@vehicle-sil/gui-client 0.0.0-stage → 0.2.0-rc.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.ja.md +839 -0
  3. package/README.md +836 -2
  4. package/dist/elements.js +872 -0
  5. package/dist/index.js +5354 -0
  6. package/package.json +39 -4
  7. package/timeline/vehicle-sil-timeline.d.ts +108 -0
  8. package/timeline/vehicle-sil-timeline.js +4391 -0
  9. package/timeline/vehicle-sil-timeline.mjs +4393 -0
  10. package/types/client.d.ts +148 -0
  11. package/types/connection.d.ts +155 -0
  12. package/types/elements/index.d.ts +25 -0
  13. package/types/elements/outside-parts.d.ts +20 -0
  14. package/types/elements/page-intent.d.ts +10 -0
  15. package/types/elements/part.d.ts +48 -0
  16. package/types/elements/reasons.d.ts +12 -0
  17. package/types/elements/run-parts.d.ts +21 -0
  18. package/types/elements/scenario-parts.d.ts +18 -0
  19. package/types/elements/variable-parts.d.ts +28 -0
  20. package/types/front-actions.d.ts +122 -0
  21. package/types/front-compose.d.ts +61 -0
  22. package/types/front-outside.d.ts +19 -0
  23. package/types/front-types.d.ts +353 -0
  24. package/types/front-variable.d.ts +28 -0
  25. package/types/front.d.ts +50 -0
  26. package/types/http.d.ts +71 -0
  27. package/types/index.d.ts +28 -0
  28. package/types/protocol/launcher.d.ts +578 -0
  29. package/types/protocol/read.d.ts +72 -0
  30. package/types/protocol/server.d.ts +758 -0
  31. package/types/protocol.d.ts +15 -0
  32. package/types/run.d.ts +74 -0
  33. package/types/session-state.d.ts +289 -0
  34. package/types/session.d.ts +130 -0
  35. package/types/time.d.ts +133 -0
  36. package/types/token.d.ts +35 -0
  37. package/types/urls.d.ts +8 -0
  38. package/types/values.d.ts +54 -0
  39. package/types/variable-binding.d.ts +181 -0
  40. package/types/variable-state.d.ts +134 -0
  41. package/types/variables.d.ts +382 -0
@@ -0,0 +1,872 @@
1
+ // ../client/src/elements/page-intent.ts
2
+ var intent = {};
3
+ var listeners = /* @__PURE__ */ new Set();
4
+ var pageIntent = {
5
+ get: () => intent,
6
+ subscribe(listener) {
7
+ listeners.add(listener);
8
+ return () => listeners.delete(listener);
9
+ },
10
+ /** Merges `patch`; a field that is undefined or an empty string is removed. */
11
+ update(patch) {
12
+ const next = { ...intent, ...patch };
13
+ for (const key of Object.keys(next))
14
+ if (next[key] === void 0 || next[key] === "") delete next[key];
15
+ intent = next;
16
+ for (const listener of [...listeners]) listener(intent);
17
+ }
18
+ };
19
+
20
+ // ../client/src/elements/reasons.ts
21
+ var custom = null;
22
+ var reported = false;
23
+ function setReasonText(fn) {
24
+ custom = fn;
25
+ reported = false;
26
+ }
27
+ function reasonText(reason) {
28
+ if (!custom) return reason.message;
29
+ try {
30
+ return custom(reason) ?? reason.message;
31
+ } catch (error) {
32
+ if (!reported) {
33
+ reported = true;
34
+ console.error(
35
+ "The function given to setReasonText threw, so the parts show the English sentences instead. Fix the function; this is said once.",
36
+ error
37
+ );
38
+ }
39
+ return reason.message;
40
+ }
41
+ }
42
+
43
+ // ../client/src/elements/outside-parts.ts
44
+ import { watch as watch2 } from "./index.js";
45
+
46
+ // ../client/src/elements/part.ts
47
+ import { connect, watch } from "./index.js";
48
+ var ids = 0;
49
+ var failed = /* @__PURE__ */ new Map();
50
+ var reconciling = false;
51
+ function speaker(document2) {
52
+ let chosen = null;
53
+ const rank = (part) => part.localName === "vsil-status" ? 0 : 1;
54
+ for (const part of failed.keys()) {
55
+ if (part.ownerDocument !== document2) continue;
56
+ if (chosen === null || rank(part) < rank(chosen) || rank(part) === rank(chosen) && !!(part.compareDocumentPosition(chosen) & Node.DOCUMENT_POSITION_FOLLOWING)) {
57
+ chosen = part;
58
+ }
59
+ }
60
+ return chosen;
61
+ }
62
+ function reconcile() {
63
+ if (reconciling) return;
64
+ reconciling = true;
65
+ queueMicrotask(() => {
66
+ reconciling = false;
67
+ const speakers = /* @__PURE__ */ new Set();
68
+ for (const part of failed.keys()) {
69
+ const chosen = speaker(part.ownerDocument);
70
+ if (chosen) speakers.add(chosen);
71
+ }
72
+ for (const [part, speak] of failed) speak(speakers.has(part));
73
+ });
74
+ }
75
+ function h(tag, attributes = {}, ...children) {
76
+ const element = document.createElement(tag);
77
+ for (const [name, value] of Object.entries(attributes)) element.setAttribute(name, value);
78
+ element.append(...children);
79
+ return element;
80
+ }
81
+ function setData(element, name, value) {
82
+ if (value === null) delete element.dataset[name];
83
+ else element.dataset[name] = value;
84
+ }
85
+ async function quietly(operation) {
86
+ try {
87
+ await operation();
88
+ } catch {
89
+ }
90
+ }
91
+ var QUIET = /* @__PURE__ */ new Set(["not-running", "offline"]);
92
+ var Part = class extends HTMLElement {
93
+ static observedAttributes = [];
94
+ #stops = [];
95
+ #client = null;
96
+ #bindings = 0;
97
+ #root = null;
98
+ #sentence = null;
99
+ /** Another connection than the page's (`connect()`). Setting it while the part is in the document binds it again. */
100
+ get client() {
101
+ return this.#client;
102
+ }
103
+ set client(value) {
104
+ if (value === this.#client) return;
105
+ this.#client = value;
106
+ if (this.isConnected) void this.#bind();
107
+ }
108
+ constructor() {
109
+ super();
110
+ this.upgrade("client");
111
+ }
112
+ /**
113
+ * A property that the page set before this class was defined sits on the object itself and hides the accessor:
114
+ * take it off and set it again.
115
+ */
116
+ upgrade(name) {
117
+ if (!Object.hasOwn(this, name)) return;
118
+ const value = Reflect.get(this, name);
119
+ Reflect.deleteProperty(this, name);
120
+ Reflect.set(this, name, value);
121
+ }
122
+ /** Stops `stop` when the part is unbound. */
123
+ own(stop) {
124
+ this.#stops.push(stop);
125
+ }
126
+ /** The attribute `name`, or `fallback` when it is not there. */
127
+ text(name, fallback) {
128
+ return this.getAttribute(name) ?? fallback;
129
+ }
130
+ connectedCallback() {
131
+ void this.#bind();
132
+ }
133
+ disconnectedCallback() {
134
+ this.#bindings++;
135
+ this.#clear();
136
+ }
137
+ attributeChangedCallback(_name, before, after) {
138
+ if (before !== after && this.isConnected) void this.#bind();
139
+ }
140
+ /** Shows the sentence of the failed connection (`speaking`) or not; the mark `data-error` stays either way. */
141
+ #speak = (speaking) => {
142
+ if (this.#sentence === null) return;
143
+ const shown = this.#root?.querySelector(":scope > [role=alert]") ?? null;
144
+ if (speaking && !shown) {
145
+ this.#root ??= this.appendChild(h("span", { class: "vsil" }));
146
+ this.#root.append(h("small", { role: "alert" }, this.#sentence));
147
+ } else if (!speaking && shown) {
148
+ this.#root?.remove();
149
+ this.#root = null;
150
+ }
151
+ };
152
+ #fail(error, shared) {
153
+ const message = error instanceof Error ? error.message : String(error);
154
+ const code = Reflect.get(Object(error), "code");
155
+ setData(this, "error", typeof code === "string" && code !== "" ? code : "error");
156
+ if (shared) {
157
+ this.#sentence = message;
158
+ failed.set(this, this.#speak);
159
+ reconcile();
160
+ return;
161
+ }
162
+ this.#root ??= this.appendChild(h("span", { class: "vsil" }));
163
+ this.#root.append(h("small", { role: "alert" }, message));
164
+ }
165
+ #clear() {
166
+ this.#sentence = null;
167
+ setData(this, "error", null);
168
+ if (failed.delete(this)) reconcile();
169
+ for (const stop of this.#stops.splice(0)) stop();
170
+ this.#root?.remove();
171
+ this.#root = null;
172
+ }
173
+ async #bind() {
174
+ this.#clear();
175
+ const binding = ++this.#bindings;
176
+ const alive = () => binding === this.#bindings && this.isConnected;
177
+ let shared = false;
178
+ try {
179
+ let client = this.#client;
180
+ if (!client) {
181
+ shared = true;
182
+ client = await connect();
183
+ shared = false;
184
+ }
185
+ if (!alive()) return;
186
+ this.#root = this.appendChild(h("span", { class: "vsil" }));
187
+ await this.draw(client, this.#root, alive);
188
+ } catch (error) {
189
+ if (!alive()) return;
190
+ this.#fail(error, shared);
191
+ }
192
+ }
193
+ /**
194
+ * Binds `element` to one control of `client.controls`: `aria-disabled` (the caller ignores input while it is true),
195
+ * `data-reason` and `data-caution` (the codes), and a sentence in an element that describes it (`aria-describedby`;
196
+ * returned, to be put next to `element`). A reason that only says there is nothing to do (`not-running`, `offline`)
197
+ * is in that element too, but hidden: a screen reader reads it, the page does not show it seven times.
198
+ */
199
+ describe(client, control, element) {
200
+ const why = h("small", { id: `vsil-why-${++ids}`, class: "vsil-why" });
201
+ element.setAttribute("aria-describedby", why.id);
202
+ this.own(
203
+ watch(client.controls, (controls) => {
204
+ const { ok, reason, caution } = controls[control];
205
+ element.setAttribute("aria-disabled", String(!ok));
206
+ setData(element, "reason", reason?.code ?? null);
207
+ setData(element, "caution", caution?.code ?? null);
208
+ const shown = reason ?? caution;
209
+ why.textContent = shown ? reasonText(shown) : "";
210
+ why.hidden = shown !== null && QUIET.has(shown.code);
211
+ })
212
+ );
213
+ return why;
214
+ }
215
+ /**
216
+ * A button bound to one control of `client.controls`. When it cannot be used it stays focusable (`aria-disabled`),
217
+ * the reason is in an element that describes it (`aria-describedby`) and its code is in `data-reason`.
218
+ */
219
+ button(client, label, control, action) {
220
+ const button = h("button", { type: "button" }, label);
221
+ const why = this.describe(client, control, button);
222
+ button.addEventListener("click", () => {
223
+ if (button.getAttribute("aria-disabled") !== "true") void quietly(action);
224
+ });
225
+ return [button, why];
226
+ }
227
+ };
228
+
229
+ // ../client/src/elements/outside-parts.ts
230
+ var ids2 = 0;
231
+ var codeOf = (error) => {
232
+ const code = Reflect.get(Object(error), "code");
233
+ return typeof code === "string" && code !== "" ? code : "error";
234
+ };
235
+ var messageOf = (error) => error instanceof Error ? error.message : String(error);
236
+ var NONE_TEXT = "SIL Kit only (not connected)";
237
+ var prefixed = (finding, prefixes) => prefixes.some((prefix) => finding.code.startsWith(prefix));
238
+ var CHECK_DELAY_MS = 200;
239
+ var PASSING_KEYS = /* @__PURE__ */ new Set(["Tab", "Shift", "Escape", "Control", "Alt", "Meta"]);
240
+ var keyOf = (intent2, file) => JSON.stringify([file ?? null, intent2]);
241
+ function findingsFor(result, intent2, network) {
242
+ const lock = result.raw?.["time_lock"];
243
+ const because = typeof lock === "object" && lock !== null ? Reflect.get(lock, "because") : null;
244
+ const locks = Array.isArray(because) && because.includes(network);
245
+ const others = new Set(
246
+ Object.entries(intent2.connect ?? {}).filter(([name, target2]) => name !== network && target2 !== "none").map(([name]) => name)
247
+ );
248
+ const lines = [];
249
+ for (const finding of result.findings) {
250
+ if (finding.network === network && prefixed(finding, ["connect-", "outside-"])) {
251
+ lines.push({ text: finding.message, finding });
252
+ } else if (finding.network !== null && others.has(finding.network) && prefixed(finding, ["connect-"])) {
253
+ lines.push({ text: `${finding.network}: ${finding.message}`, finding });
254
+ } else if (locks && finding.network === null && prefixed(finding, ["outside-"])) {
255
+ lines.push({ text: finding.message, finding });
256
+ }
257
+ }
258
+ return lines;
259
+ }
260
+ function targetText(target2) {
261
+ const about = [target2.detail, target2.usable ? "" : target2.reason ?? ""].filter((t) => t !== "");
262
+ return about.length > 0 ? `${target2.label} \u2014 ${about.join("; ")}` : target2.label;
263
+ }
264
+ var VsilConnectElement = class extends Part {
265
+ static observedAttributes = ["network", "file"];
266
+ draw(client, root, alive) {
267
+ const network = this.getAttribute("network");
268
+ if (!network) {
269
+ throw new Error(
270
+ `<${this.localName}> needs the attribute network="<name>" (a can or ethernet network of the configuration file)`
271
+ );
272
+ }
273
+ const select = h("select", {}, new Option(NONE_TEXT, "none"));
274
+ const notes = h("ul", { id: `vsil-connect-notes-${++ids2}`, class: "vsil-why" });
275
+ const findings = h("ul", { id: `vsil-connect-findings-${ids2}`, class: "vsil-findings" });
276
+ select.setAttribute("aria-describedby", `${notes.id} ${findings.id}`);
277
+ root.append(select, notes, findings);
278
+ setData(this, "network", network);
279
+ const file = () => this.getAttribute("file") ?? client.status.get().file ?? void 0;
280
+ const chosen = () => pageIntent.get().connect?.[network] ?? "none";
281
+ const say = (list2, lines) => list2.replaceChildren(...lines.map((line) => h("li", {}, line)));
282
+ let listNotes = [];
283
+ let reverted = null;
284
+ const sayNotes = () => say(notes, reverted === null ? listNotes : [...listNotes, reverted]);
285
+ const write = (value) => {
286
+ const connect2 = { ...pageIntent.get().connect };
287
+ if (value === "none") delete connect2[network];
288
+ else connect2[network] = value;
289
+ pageIntent.update({ connect: Object.keys(connect2).length > 0 ? connect2 : void 0 });
290
+ };
291
+ let written = null;
292
+ const show = () => {
293
+ const value = chosen();
294
+ if (written && written.value !== value) {
295
+ written.remove();
296
+ written = null;
297
+ }
298
+ if (![...select.options].some((o) => o.value === value)) {
299
+ written = new Option(value, value);
300
+ select.add(written);
301
+ }
302
+ select.value = value;
303
+ setData(this, "chosen", value);
304
+ };
305
+ let opened = false;
306
+ let relisted = false;
307
+ let listing = null;
308
+ const list = () => {
309
+ if (listing) return listing;
310
+ select.setAttribute("aria-busy", "true");
311
+ listing = client.connections({ network }, { file: file() }).then(
312
+ (entries) => {
313
+ if (!alive()) return;
314
+ setData(this, "error", null);
315
+ fill(entries);
316
+ },
317
+ (error) => {
318
+ if (!alive()) return;
319
+ setData(this, "error", codeOf(error));
320
+ listNotes = [messageOf(error)];
321
+ sayNotes();
322
+ }
323
+ ).finally(() => {
324
+ listing = null;
325
+ select.removeAttribute("aria-busy");
326
+ });
327
+ return listing;
328
+ };
329
+ const fill = (entries) => {
330
+ select.replaceChildren(new Option(NONE_TEXT, "none"));
331
+ written = null;
332
+ listNotes = [];
333
+ let group = null;
334
+ for (const entry of entries) {
335
+ if ("error" in entry) {
336
+ listNotes.push(entry.backend ? `${entry.backend}: ${entry.error}` : entry.error);
337
+ continue;
338
+ }
339
+ const label = entry.backend ?? "";
340
+ if (group?.label !== label) {
341
+ group = h("optgroup", { label });
342
+ select.append(group);
343
+ }
344
+ const option = new Option(targetText(entry), entry.value);
345
+ option.disabled = !entry.usable;
346
+ group.append(option);
347
+ }
348
+ const value = chosen();
349
+ const offered = entries.find((e) => !("error" in e) && e.value === value);
350
+ if (value !== "none" && !offered?.usable) {
351
+ reverted = offered ? `'${value}' cannot be used${offered.reason ? ` (${offered.reason})` : ""}: back to SIL Kit only` : `'${value}' is not in the list of this computer: back to SIL Kit only`;
352
+ setData(this, "reverted", value);
353
+ sayNotes();
354
+ write("none");
355
+ } else {
356
+ sayNotes();
357
+ show();
358
+ }
359
+ };
360
+ let checks = 0;
361
+ let checked = null;
362
+ let timer;
363
+ let asking = null;
364
+ const stopAsking = () => {
365
+ asking?.abort();
366
+ asking = null;
367
+ };
368
+ const check = () => {
369
+ const intent2 = pageIntent.get();
370
+ const asked = ++checks;
371
+ checked = keyOf(intent2, file());
372
+ stopAsking();
373
+ asking = new AbortController();
374
+ const { signal } = asking;
375
+ client.check(intent2, { file: file(), signal }).then(
376
+ (result) => {
377
+ if (!alive() || asked !== checks || signal.aborted) return;
378
+ relisted = false;
379
+ findings.replaceChildren(
380
+ ...findingsFor(result, intent2, network).map(({ text, finding }) => {
381
+ const item = h("li", {}, text);
382
+ setData(item, "severity", finding.severity);
383
+ setData(item, "code", finding.code);
384
+ return item;
385
+ })
386
+ );
387
+ },
388
+ (error) => {
389
+ if (!alive() || asked !== checks || signal.aborted) return;
390
+ checked = null;
391
+ say(findings, [messageOf(error)]);
392
+ if (codeOf(error) !== "target-not-listed" || !opened || relisted) return;
393
+ relisted = true;
394
+ void list().then(() => {
395
+ if (alive() && asked === checks && chosen() !== "none") check();
396
+ });
397
+ }
398
+ );
399
+ };
400
+ const changed = () => {
401
+ show();
402
+ clearTimeout(timer);
403
+ if (chosen() === "none") {
404
+ checks++;
405
+ stopAsking();
406
+ checked = null;
407
+ findings.replaceChildren();
408
+ return;
409
+ }
410
+ if (keyOf(pageIntent.get(), file()) === checked) return;
411
+ stopAsking();
412
+ timer = setTimeout(check, CHECK_DELAY_MS);
413
+ };
414
+ const open = () => {
415
+ opened = true;
416
+ void list();
417
+ };
418
+ select.addEventListener("pointerdown", open);
419
+ select.addEventListener("keydown", (event) => {
420
+ if (!PASSING_KEYS.has(event.key)) open();
421
+ });
422
+ select.addEventListener("change", () => {
423
+ reverted = null;
424
+ setData(this, "reverted", null);
425
+ relisted = false;
426
+ sayNotes();
427
+ write(select.value);
428
+ });
429
+ this.own(pageIntent.subscribe(changed));
430
+ this.own(() => {
431
+ clearTimeout(timer);
432
+ stopAsking();
433
+ checks++;
434
+ setData(this, "network", null);
435
+ setData(this, "chosen", null);
436
+ setData(this, "reverted", null);
437
+ if (chosen() !== "none") write("none");
438
+ });
439
+ changed();
440
+ }
441
+ };
442
+ var countText = (count) => count === null ? "-" : String(count);
443
+ function row(outside) {
444
+ const item = h(
445
+ "li",
446
+ {},
447
+ h("span", { class: "vsil-network" }, outside.network),
448
+ " \u2192 ",
449
+ h("span", { class: "vsil-target" }, outside.target ?? "-"),
450
+ ": ",
451
+ h(
452
+ "span",
453
+ { class: "vsil-link" },
454
+ outside.link !== null ? `link ${outside.link}` : outside.bus !== null ? `bus ${outside.bus}` : "-"
455
+ ),
456
+ ", ",
457
+ h("span", { class: "vsil-condition" }, outside.condition),
458
+ outside.reasons.length > 0 ? h("span", { class: "vsil-reasons" }, ` (${outside.reasons.join(", ")})`) : "",
459
+ ", ",
460
+ h(
461
+ "span",
462
+ { class: "vsil-counts" },
463
+ `to outside ${countText(outside.to_outside)}, from outside ${countText(outside.from_outside)}`
464
+ ),
465
+ outside.nothing_received ? h("span", { class: "vsil-nothing" }, ", nothing received") : ""
466
+ );
467
+ setData(item, "network", outside.network);
468
+ setData(item, "condition", outside.condition);
469
+ setData(item, "nothingReceived", String(outside.nothing_received));
470
+ return item;
471
+ }
472
+ var VsilOutsideElement = class extends Part {
473
+ draw(client, root) {
474
+ const rows = h("ul");
475
+ root.append(rows);
476
+ this.own(
477
+ watch2(client.outside, (outside) => {
478
+ root.hidden = outside.length === 0;
479
+ rows.replaceChildren(...outside.map(row));
480
+ })
481
+ );
482
+ }
483
+ };
484
+
485
+ // ../client/src/elements/run-parts.ts
486
+ import { watch as watch3 } from "./index.js";
487
+ var VsilStatusElement = class extends Part {
488
+ draw(client, root) {
489
+ const state = h("span", { class: "vsil-state" });
490
+ const time = h("span", { class: "vsil-time" });
491
+ const problem = h("span", { class: "vsil-problem", role: "alert" });
492
+ root.append(state, " ", time, " ", problem);
493
+ this.own(
494
+ watch3(client.status, (s) => {
495
+ state.textContent = s.link !== "online" ? s.link : s.run === "none" ? "ready" : (s.run === "ended" || s.run === "crashed") && s.result && !s.problem ? s.result.message : s.run;
496
+ problem.textContent = s.problem ? reasonText(s.problem) : "";
497
+ setData(this, "link", s.link);
498
+ setData(this, "run", s.run);
499
+ })
500
+ );
501
+ this.own(
502
+ watch3(client.clock, (c) => {
503
+ time.textContent = c.sim_ms === null ? "" : `${c.sim_ms} ms${c.pacing === "late" ? " (behind real time)" : ""}`;
504
+ setData(this, "pacing", c.pacing);
505
+ })
506
+ );
507
+ }
508
+ };
509
+ var VsilRunControlsElement = class extends Part {
510
+ static observedAttributes = ["label-start", "label-stop", "label-force-stop"];
511
+ /** What to start with, over what the choosing parts of the page selected. */
512
+ get intent() {
513
+ return this.#intent;
514
+ }
515
+ set intent(value) {
516
+ this.#intent = value;
517
+ }
518
+ #intent = null;
519
+ constructor() {
520
+ super();
521
+ this.upgrade("intent");
522
+ }
523
+ draw(client, root) {
524
+ const force = h(
525
+ "span",
526
+ { class: "vsil-force" },
527
+ ...this.button(
528
+ client,
529
+ this.text("label-force-stop", "Stop without waiting"),
530
+ "force_stop",
531
+ () => client.run.forceStop()
532
+ )
533
+ );
534
+ root.append(
535
+ ...this.button(
536
+ client,
537
+ this.text("label-start", "Start"),
538
+ "start",
539
+ () => (
540
+ // the file that the scenario part lists the scenarios of (null: the server's start-up file)
541
+ client.run.start(
542
+ { ...pageIntent.get(), ...this.intent },
543
+ client.status.get().file === null ? void 0 : { file: client.status.get().file }
544
+ )
545
+ )
546
+ ),
547
+ ...this.button(client, this.text("label-stop", "Stop"), "stop", () => client.run.stop()),
548
+ force
549
+ );
550
+ this.own(watch3(client.controls, (controls) => force.hidden = !controls.force_stop.ok));
551
+ }
552
+ };
553
+ var speedValue = (text) => {
554
+ if (text === "max") return "max";
555
+ const number = Number(text);
556
+ return text !== "" && Number.isFinite(number) && number > 0 ? number : null;
557
+ };
558
+ var takes = (text, range) => {
559
+ const value = speedValue(text);
560
+ return value === "max" ? range.fastest : value !== null && value >= range.min && value <= range.max;
561
+ };
562
+ var VsilTimeControlsElement = class extends Part {
563
+ static observedAttributes = [
564
+ "speeds",
565
+ "step-ms",
566
+ "label-pause",
567
+ "label-resume",
568
+ "label-step",
569
+ "label-run-until"
570
+ ];
571
+ draw(client, root) {
572
+ const stepMs = h("input", { type: "number", min: "1", value: this.text("step-ms", "10") });
573
+ const untilMs = h("input", { type: "number", min: "0", value: "1000" });
574
+ const speed = h("select");
575
+ const speeds = [];
576
+ speed.add(new Option("-", ""));
577
+ const addSpeed = (text) => {
578
+ speed.add(new Option(text === "max" ? "fastest" : `${text}\xD7`, text));
579
+ speeds.push(text);
580
+ };
581
+ for (const text of this.text("speeds", "0.5 1 5 max").split(/\s+/))
582
+ if (speedValue(text) !== null) addSpeed(text);
583
+ const markSpeeds = () => {
584
+ const range = client.controls.get().speed;
585
+ for (const option of speed.options) {
586
+ if (option.value === "") continue;
587
+ option.disabled = !takes(option.value, range);
588
+ option.hidden = option.value === "max" && !range.fastest;
589
+ }
590
+ };
591
+ const showSpeed = () => {
592
+ const { speed: now } = client.clock.get();
593
+ const text = now === null ? "" : String(now);
594
+ if (text !== "" && !speeds.includes(text)) {
595
+ addSpeed(text);
596
+ markSpeeds();
597
+ }
598
+ speed.value = text;
599
+ };
600
+ speed.addEventListener("change", () => {
601
+ const value = speedValue(speed.value);
602
+ if (value === null || speed.getAttribute("aria-disabled") === "true" || !takes(speed.value, client.controls.get().speed))
603
+ showSpeed();
604
+ else void quietly(() => client.time.setSpeed(value)).then(showSpeed);
605
+ });
606
+ const speedWhy = this.describe(client, "speed", speed);
607
+ root.append(
608
+ ...this.button(client, this.text("label-pause", "Pause"), "pause", () => client.time.pause()),
609
+ ...this.button(
610
+ client,
611
+ this.text("label-resume", "Resume"),
612
+ "resume",
613
+ () => client.time.resume()
614
+ ),
615
+ h("label", {}, "Step (ms) ", stepMs),
616
+ ...this.button(
617
+ client,
618
+ this.text("label-step", "Step"),
619
+ "step",
620
+ () => client.time.step(Number(stepMs.value))
621
+ ),
622
+ h("label", {}, "Until (ms) ", untilMs),
623
+ ...this.button(
624
+ client,
625
+ this.text("label-run-until", "Run until"),
626
+ "run_until",
627
+ () => client.time.runUntil(Number(untilMs.value))
628
+ ),
629
+ h("label", {}, "Speed ", speed),
630
+ speedWhy
631
+ );
632
+ this.own(
633
+ watch3(client.controls, (controls) => {
634
+ stepMs.min = String(controls.step.min_ms);
635
+ stepMs.max = String(controls.step.max_ms);
636
+ markSpeeds();
637
+ })
638
+ );
639
+ this.own(watch3(client.clock, showSpeed));
640
+ }
641
+ };
642
+
643
+ // ../client/src/elements/scenario-parts.ts
644
+ import { watch as watch4 } from "./index.js";
645
+ var VsilScenarioSelectElement = class extends Part {
646
+ draw(client, root, alive) {
647
+ const select = h("select", {}, new Option("(no scenario)", ""));
648
+ root.append(select);
649
+ select.addEventListener("change", () => pageIntent.update({ scenario: select.value }));
650
+ this.own(pageIntent.subscribe((intent2) => select.value = intent2.scenario ?? ""));
651
+ let file;
652
+ this.own(
653
+ watch4(client.status, (status) => {
654
+ if (status.file === file) return;
655
+ file = status.file;
656
+ const asked = file;
657
+ const options = asked === null ? Promise.resolve([]) : client.scenarios({ file: asked });
658
+ options.then(
659
+ (scenarios) => {
660
+ if (!alive() || file !== asked) return;
661
+ select.replaceChildren(new Option("(no scenario)", ""));
662
+ for (const scenario of scenarios) select.add(new Option(scenario.name, scenario.path));
663
+ select.value = pageIntent.get().scenario ?? "";
664
+ if (select.value !== (pageIntent.get().scenario ?? ""))
665
+ pageIntent.update({ scenario: "" });
666
+ },
667
+ () => void 0
668
+ // no list: the select keeps what it has
669
+ );
670
+ })
671
+ );
672
+ }
673
+ };
674
+ var VsilScenarioResultElement = class extends Part {
675
+ draw(client, root) {
676
+ const result = h("output");
677
+ const steps = h("ol");
678
+ root.append(result, steps);
679
+ this.own(
680
+ watch4(client.scenario, (scenario) => {
681
+ result.textContent = scenario?.status ?? "-";
682
+ setData(this, "status", scenario?.status ?? null);
683
+ steps.replaceChildren(
684
+ ...(scenario?.steps ?? []).map((step) => {
685
+ const item = h(
686
+ "li",
687
+ {},
688
+ `${step.text} \u2014 ${step.status}${step.at_ms !== null ? ` at ${step.at_ms} ms` : ""}${step.message ? `: ${step.message}` : ""}`
689
+ );
690
+ setData(item, "status", step.status);
691
+ return item;
692
+ })
693
+ );
694
+ })
695
+ );
696
+ }
697
+ };
698
+ var VsilTimelineElement = class extends Part {
699
+ draw(client) {
700
+ let timeline = this.querySelector("vehicle-sil-timeline");
701
+ if (!timeline) {
702
+ const own = timeline = document.createElement("vehicle-sil-timeline");
703
+ this.append(own);
704
+ this.own(() => own.remove());
705
+ }
706
+ const shown = timeline;
707
+ this.own(
708
+ watch4(client.scenario, (scenario) => {
709
+ const url = scenario?.timeline_url ?? null;
710
+ shown.hidden = url === null;
711
+ setData(this, "timeline", url === null ? "none" : "shown");
712
+ if (url !== null && shown.getAttribute("src") !== `/${url}`)
713
+ shown.setAttribute("src", `/${url}`);
714
+ })
715
+ );
716
+ this.own(() => setData(this, "timeline", null));
717
+ }
718
+ };
719
+
720
+ // ../client/src/elements/variable-parts.ts
721
+ import { watch as watch5 } from "./index.js";
722
+ var noteIds = 0;
723
+ var writeNote = (v) => {
724
+ if (v.problem) return reasonText(v.problem);
725
+ if (!v.can_write && v.why) return reasonText(v.why);
726
+ return v.writing ? "writing\u2026" : "";
727
+ };
728
+ var VariablePart = class extends Part {
729
+ static observedAttributes = ["variable"];
730
+ /** Whether the part writes (so that it says why it cannot). */
731
+ writes = false;
732
+ draw(client, root) {
733
+ const name = this.getAttribute("variable");
734
+ if (!name) {
735
+ throw new Error(
736
+ `<${this.localName}> needs the attribute variable="ECU.variable" (the ECU's name, a dot and the variable's name)`
737
+ );
738
+ }
739
+ const variable = client.variable(name);
740
+ const note = h("small", { id: `vsil-note-${++noteIds}`, class: "vsil-note" });
741
+ const show = this.build(variable, root, note);
742
+ root.append(" ", note);
743
+ this.own(
744
+ watch5(variable, (v) => {
745
+ setData(this, "stale", String(v.stale));
746
+ setData(this, "canWrite", String(v.can_write));
747
+ setData(this, "availability", v.availability);
748
+ if (this.writes) note.textContent = writeNote(v);
749
+ note.hidden = false;
750
+ show(v);
751
+ })
752
+ );
753
+ }
754
+ };
755
+ var VsilValueElement = class extends VariablePart {
756
+ static observedAttributes = ["variable", "on", "off", "no-unit"];
757
+ build(_variable, root, note) {
758
+ const output = root.appendChild(h("output"));
759
+ return (v) => {
760
+ if (typeof v.value === "boolean") {
761
+ setData(this, "on", String(v.value));
762
+ output.textContent = v.value ? this.text("on", "on") : this.text("off", "off");
763
+ } else {
764
+ setData(this, "on", null);
765
+ const unit = this.hasAttribute("no-unit") || !v.unit ? "" : ` ${v.unit}`;
766
+ output.textContent = v.value === null ? "-" : `${v.text}${unit}`;
767
+ }
768
+ note.textContent = v.note ?? "";
769
+ };
770
+ }
771
+ };
772
+ var VsilSliderElement = class extends VariablePart {
773
+ writes = true;
774
+ build(variable, root, note) {
775
+ const input = h("input", { type: "range", "aria-describedby": note.id });
776
+ const output = h("output", { "aria-hidden": "true" });
777
+ note.setAttribute("aria-hidden", "true");
778
+ let dragging = false;
779
+ input.addEventListener("input", () => {
780
+ dragging = true;
781
+ input.removeAttribute("aria-valuetext");
782
+ });
783
+ input.addEventListener("change", () => {
784
+ dragging = false;
785
+ void quietly(() => variable.write(Number(input.value)));
786
+ });
787
+ for (const name of ["pointerup", "pointercancel", "keyup", "blur"])
788
+ input.addEventListener(name, () => dragging = false);
789
+ root.append(input, " ", output);
790
+ return (v) => {
791
+ const ranged = v.min !== null && v.max !== null;
792
+ input.disabled = !v.can_write || !ranged;
793
+ if (!v.can_write) dragging = false;
794
+ if (ranged) {
795
+ input.min = String(v.min);
796
+ input.max = String(v.max);
797
+ input.step = v.step === null ? "any" : String(v.step);
798
+ } else if (v.can_write && !v.problem) {
799
+ note.textContent = "this variable has no range to set";
800
+ }
801
+ if (!dragging && !v.writing && typeof v.value === "number") input.value = String(v.value);
802
+ output.textContent = v.value === null ? "-" : `${v.text}${v.unit ? ` ${v.unit}` : ""}`;
803
+ if (v.value === null || dragging || v.writing) input.removeAttribute("aria-valuetext");
804
+ else input.setAttribute("aria-valuetext", output.textContent);
805
+ };
806
+ }
807
+ };
808
+ var target = (text) => {
809
+ if (text === null) return true;
810
+ try {
811
+ const value = JSON.parse(text);
812
+ if (typeof value === "number" || typeof value === "boolean") return value;
813
+ } catch {
814
+ }
815
+ return text;
816
+ };
817
+ var VsilWriteElement = class extends VariablePart {
818
+ static observedAttributes = ["variable", "value", "label"];
819
+ writes = true;
820
+ build(variable, root, note) {
821
+ const value = target(this.getAttribute("value"));
822
+ const button = h(
823
+ "button",
824
+ { type: "button", "aria-describedby": note.id },
825
+ this.text("label", `${this.getAttribute("variable")} = ${String(value)}`)
826
+ );
827
+ button.addEventListener("click", () => {
828
+ if (button.getAttribute("aria-disabled") !== "true")
829
+ void quietly(() => variable.write(value));
830
+ });
831
+ root.append(button);
832
+ return (v) => {
833
+ const already = v.can_write && v.value === value;
834
+ button.setAttribute("aria-disabled", String(!v.can_write || already));
835
+ if (already && !v.problem) {
836
+ note.textContent = `already ${String(value)}`;
837
+ note.hidden = true;
838
+ }
839
+ };
840
+ }
841
+ };
842
+
843
+ // ../client/src/elements/index.ts
844
+ var PARTS = [
845
+ ["vsil-status", VsilStatusElement],
846
+ ["vsil-run-controls", VsilRunControlsElement],
847
+ ["vsil-time-controls", VsilTimeControlsElement],
848
+ ["vsil-value", VsilValueElement],
849
+ ["vsil-slider", VsilSliderElement],
850
+ ["vsil-write", VsilWriteElement],
851
+ ["vsil-scenario-select", VsilScenarioSelectElement],
852
+ ["vsil-scenario-result", VsilScenarioResultElement],
853
+ ["vsil-timeline", VsilTimelineElement],
854
+ ["vsil-connect", VsilConnectElement],
855
+ ["vsil-outside", VsilOutsideElement]
856
+ ];
857
+ for (const [name, part] of PARTS) if (!customElements.get(name)) customElements.define(name, part);
858
+ export {
859
+ VsilConnectElement,
860
+ VsilOutsideElement,
861
+ VsilRunControlsElement,
862
+ VsilScenarioResultElement,
863
+ VsilScenarioSelectElement,
864
+ VsilSliderElement,
865
+ VsilStatusElement,
866
+ VsilTimeControlsElement,
867
+ VsilTimelineElement,
868
+ VsilValueElement,
869
+ VsilWriteElement,
870
+ pageIntent,
871
+ setReasonText
872
+ };