@reqvire-org/ads 0.5.8 → 0.5.9

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.
@@ -16,11 +16,25 @@ export function mountScenarioPage(container, scenario, mountSpecimen, available
16
16
  root.append(page);
17
17
  const frame = scenario.frame ?? "inline", states = scenario.states ?? {}, names = Object.keys(states), specimens = frame === "inline" ? scenario.specimens : scenario.specimens.slice(0, 1), mounts = new Map;
18
18
  let chosen = requestedState(names) ?? names[0] ?? null;
19
- const [subject] = scenario.covers ?? [], controls = scenario.controls === !0 && subject !== void 0 ? available[subject] ?? [] : [], picked = new Map, controlProps = () => Object.fromEntries(picked), draw = () => {
19
+ const [subject] = scenario.covers ?? [], controls = scenario.controls === !0 && subject !== void 0 ? available[subject] ?? [] : [], picked = new Map, controlProps = () => Object.fromEntries(picked), choose = (prop, option) => {
20
+ const value = controls.find((control) => control.prop === prop)?.values.find((item) => String(item) === option);
21
+ if (value === void 0)
22
+ picked.delete(prop);
23
+ else
24
+ picked.set(prop, value);
25
+ }, hosted = () => doc.documentElement.dataset.adsSettings === "host", draw = () => {
26
+ const controlRows = controls.map((control) => ({
27
+ prop: control.prop,
28
+ options: [UNSET, ...control.values.map(String)],
29
+ chosen: picked.has(control.prop) ? String(picked.get(control.prop)) : UNSET,
30
+ as: control.values.length > BUTTON_VALUES ? "list" : "buttons"
31
+ }));
32
+ doc.dispatchEvent(new CustomEvent("ads-scenario-settings", { detail: { states: names, ...chosen === null ? {} : { name: chosen }, controls: controlRows } }));
20
33
  renderScenarioPage(page, {
21
34
  title: scenario.title,
22
35
  description: scenario.description,
23
36
  frame,
37
+ hostSettings: hosted(),
24
38
  states: names,
25
39
  ...chosen === null ? {} : { state: chosen },
26
40
  onState: (name) => {
@@ -28,18 +42,9 @@ export function mountScenarioPage(container, scenario, mountSpecimen, available
28
42
  draw();
29
43
  doc.dispatchEvent(new CustomEvent("ads-scenario-state-chosen", { detail: { state: name } }));
30
44
  },
31
- controls: controls.map((control) => ({
32
- prop: control.prop,
33
- options: [UNSET, ...control.values.map(String)],
34
- chosen: picked.has(control.prop) ? String(picked.get(control.prop)) : UNSET,
35
- as: control.values.length > BUTTON_VALUES ? "list" : "buttons"
36
- })),
45
+ controls: controlRows,
37
46
  onControl: (prop, option) => {
38
- const value = controls.find((control) => control.prop === prop)?.values.find((item) => String(item) === option);
39
- if (value === void 0)
40
- picked.delete(prop);
41
- else
42
- picked.set(prop, value);
47
+ choose(prop, option);
43
48
  draw();
44
49
  },
45
50
  specimens: specimens.map((specimen, index) => ({ key: specimenKey(index), title: specimen.title, note: specimen.note, headed: specimen.title !== void 0 || specimen.note !== void 0, arrangement: specimen.arrangement, width: specimen.width }))
@@ -69,8 +74,28 @@ export function mountScenarioPage(container, scenario, mountSpecimen, available
69
74
  draw();
70
75
  };
71
76
  doc.addEventListener("ads-scenario-state", follow);
77
+ const redraw = () => draw(), followControls = (event) => {
78
+ const detail = event instanceof CustomEvent ? event.detail : void 0;
79
+ if (typeof detail !== "object" || detail === null)
80
+ return;
81
+ let changed = !1;
82
+ for (const control of controls) {
83
+ const option = Reflect.get(detail, control.prop);
84
+ if (typeof option !== "string")
85
+ continue;
86
+ const before = picked.get(control.prop);
87
+ choose(control.prop, option);
88
+ changed ||= picked.get(control.prop) !== before;
89
+ }
90
+ if (changed)
91
+ draw();
92
+ };
93
+ doc.addEventListener("ads-scenario-host-settings", redraw);
94
+ doc.addEventListener("ads-scenario-controls", followControls);
72
95
  return () => {
73
96
  doc.removeEventListener("ads-scenario-state", follow);
97
+ doc.removeEventListener("ads-scenario-host-settings", redraw);
98
+ doc.removeEventListener("ads-scenario-controls", followControls);
74
99
  for (const mount of mounts.values())
75
100
  mount.unmount();
76
101
  unmount(page);