contour-ds 1.7.2 → 1.9.0

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 (150) hide show
  1. package/README.md +244 -326
  2. package/dist/agent-cookbook.json +960 -0
  3. package/dist/agent-examples.json +65 -0
  4. package/dist/agent-map-wiring.json +152 -0
  5. package/dist/agent-rules.json +16 -0
  6. package/dist/build-manifest.json +344 -126
  7. package/dist/contour-wc-content.esm.js +1939 -0
  8. package/dist/contour-wc-content.esm.min.js +1 -0
  9. package/dist/contour-wc-core.esm.js +3724 -0
  10. package/dist/contour-wc-core.esm.min.js +1 -0
  11. package/dist/contour-wc-dashboard.esm.js +6986 -0
  12. package/dist/contour-wc-dashboard.esm.min.js +1 -0
  13. package/dist/contour-wc-data.esm.js +6572 -0
  14. package/dist/contour-wc-data.esm.min.js +1 -0
  15. package/dist/contour-wc-docs.esm.js +976 -0
  16. package/dist/contour-wc-docs.esm.min.js +1 -0
  17. package/dist/contour-wc-feedback.esm.js +1742 -0
  18. package/dist/contour-wc-feedback.esm.min.js +1 -0
  19. package/dist/contour-wc-forms.esm.js +2437 -0
  20. package/dist/contour-wc-forms.esm.min.js +1 -0
  21. package/dist/contour-wc-interaction.esm.js +1343 -0
  22. package/dist/contour-wc-interaction.esm.min.js +1 -0
  23. package/dist/contour-wc-layout.esm.js +845 -0
  24. package/dist/contour-wc-layout.esm.min.js +1 -0
  25. package/dist/contour-wc-map.esm.js +2411 -0
  26. package/dist/contour-wc-map.esm.min.js +1 -0
  27. package/dist/contour-wc-misc.esm.js +945 -0
  28. package/dist/contour-wc-misc.esm.min.js +1 -0
  29. package/dist/contour-wc-navigation.esm.js +2842 -0
  30. package/dist/contour-wc-navigation.esm.min.js +1 -0
  31. package/dist/contour-wc-ui.esm.js +6519 -0
  32. package/dist/contour-wc-ui.esm.min.js +1 -0
  33. package/dist/contour-wc.esm.js +14435 -5723
  34. package/dist/contour-wc.esm.min.js +1 -1
  35. package/dist/contour-wc.js +14436 -5725
  36. package/dist/contour-wc.min.js +1 -1
  37. package/dist/contour.a11y.json +402 -0
  38. package/dist/contour.code-snippets +4534 -0
  39. package/dist/contour.components.json +4870 -1222
  40. package/dist/contour.css +6672 -4711
  41. package/dist/contour.d.ts +753 -1085
  42. package/dist/contour.esm.js +440 -18
  43. package/dist/contour.esm.min.js +1 -1
  44. package/dist/contour.html-data.json +11343 -0
  45. package/dist/contour.js +440 -18
  46. package/dist/contour.min.css +1 -1
  47. package/dist/contour.min.js +1 -1
  48. package/dist/contour.react.d.ts +422 -0
  49. package/dist/contour.single.html +17 -0
  50. package/dist/contour.single.min.css +1 -0
  51. package/dist/contour.themes.json +56 -12
  52. package/dist/contour.tokens.json +1318 -1053
  53. package/dist/contour.vue.d.ts +420 -0
  54. package/dist/contour.web-types.json +11703 -0
  55. package/dist/custom-elements.json +36120 -0
  56. package/dist/docs/actions.md +16 -6
  57. package/dist/docs/affordances.md +21 -85
  58. package/dist/docs/apireference.md +9 -5
  59. package/dist/docs/colour.md +10 -4
  60. package/dist/docs/componentindex.md +9 -22
  61. package/dist/docs/data.md +39 -58
  62. package/dist/docs/docs.md +12 -7
  63. package/dist/docs/feedback.md +30 -12
  64. package/dist/docs/field.md +28 -13
  65. package/dist/docs/forms.md +35 -19
  66. package/dist/docs/index.json +70 -61
  67. package/dist/docs/install.md +12 -7
  68. package/dist/docs/landing.md +7 -4
  69. package/dist/docs/mapengine.md +30 -8
  70. package/dist/docs/motion.md +12 -6
  71. package/dist/docs/navigation.md +31 -7
  72. package/dist/docs/overlays.md +32 -25
  73. package/dist/docs/overview.md +3 -2
  74. package/dist/docs/patterns.md +20 -10
  75. package/dist/docs/playground.md +11 -4
  76. package/dist/docs/principles.md +8 -6
  77. package/dist/docs/recipes.md +117 -13
  78. package/dist/docs/space.md +10 -6
  79. package/dist/docs/status.md +13 -14
  80. package/dist/docs/surfaces.md +17 -22
  81. package/dist/docs/theming.md +18 -32
  82. package/dist/docs/type.md +12 -5
  83. package/dist/docs/webcomponents.md +75 -33
  84. package/dist/hljs.css +1 -20
  85. package/dist/map-chrome.mapbox.css +68 -92
  86. package/dist/map-chrome.maplibre.css +68 -92
  87. package/dist/registry/items/contour-core.json +2 -2
  88. package/dist/registry/items/contour-data.json +2 -2
  89. package/dist/registry/items/contour-docs.json +2 -2
  90. package/dist/registry/items/contour-landing.json +2 -2
  91. package/dist/registry/registry.json +8 -8
  92. package/dist/surfaces/contour-core.min.css +1 -1
  93. package/dist/surfaces/contour-data.min.css +1 -1
  94. package/dist/surfaces/contour-docs.min.css +1 -1
  95. package/dist/surfaces/contour-landing.min.css +1 -1
  96. package/dist/svg/icons.svg +367 -0
  97. package/dist/wc/behaviours.mjs +222 -192
  98. package/dist/wc/content.mjs +486 -142
  99. package/dist/wc/core.mjs +917 -395
  100. package/dist/wc/dashboard.mjs +252 -25
  101. package/dist/wc/data-chart.mjs +1333 -0
  102. package/dist/wc/data-export.mjs +391 -0
  103. package/dist/wc/data-gauge.mjs +779 -0
  104. package/dist/wc/data-geo.mjs +696 -0
  105. package/dist/wc/data-glyphs.mjs +133 -0
  106. package/dist/wc/data-legend.mjs +431 -0
  107. package/dist/wc/data-readout.mjs +227 -0
  108. package/dist/wc/data-series.mjs +1004 -0
  109. package/dist/wc/data-table.mjs +751 -0
  110. package/dist/wc/data.mjs +15 -3707
  111. package/dist/wc/docs.mjs +113 -19
  112. package/dist/wc/feedback.mjs +320 -116
  113. package/dist/wc/forms-controls.mjs +635 -0
  114. package/dist/wc/forms-field.mjs +547 -0
  115. package/dist/wc/forms-groups.mjs +347 -0
  116. package/dist/wc/forms-input.mjs +292 -0
  117. package/dist/wc/forms.mjs +10 -1122
  118. package/dist/wc/glyph.mjs +204 -0
  119. package/dist/wc/index.mjs +16 -7
  120. package/dist/wc/interaction.mjs +322 -75
  121. package/dist/wc/layout.mjs +100 -29
  122. package/dist/wc/map.mjs +888 -285
  123. package/dist/wc/misc.mjs +137 -26
  124. package/dist/wc/navigation-menu.mjs +735 -0
  125. package/dist/wc/navigation-shell.mjs +495 -0
  126. package/dist/wc/navigation-tree.mjs +995 -0
  127. package/dist/wc/navigation.mjs +9 -1636
  128. package/dist/wc/registration.mjs +1 -8
  129. package/dist/wc/shadow.mjs +285 -113
  130. package/dist/wc/states.mjs +75 -78
  131. package/dist/wc/svg.mjs +209 -34
  132. package/dist/wc/ui-atoms.mjs +608 -0
  133. package/dist/wc/ui-data.mjs +1404 -0
  134. package/dist/wc/ui-feedback.mjs +419 -0
  135. package/dist/wc/ui-inputs.mjs +1570 -0
  136. package/dist/wc/ui-menus.mjs +307 -0
  137. package/dist/wc/ui-overlays.mjs +1605 -0
  138. package/integrations/README.md +25 -5
  139. package/integrations/ai/CLAUDE.md +6 -3
  140. package/integrations/ai/COPILOT.md +14 -0
  141. package/integrations/ai/GEMINI.md +14 -0
  142. package/integrations/ai/WINDSURF.md +14 -0
  143. package/integrations/ai/v0-prompt.md +13 -1
  144. package/integrations/frameworks/README.md +235 -0
  145. package/integrations/native/README.md +0 -3
  146. package/integrations/native/style-dictionary.config.json +1 -1
  147. package/integrations/tailwind/contour-theme.css +71 -13
  148. package/integrations/validate-markup.mjs +172 -0
  149. package/package.json +103 -39
  150. package/dist/wc/ui.mjs +0 -3560
@@ -0,0 +1,1605 @@
1
+ import { setHTML, ContourBase, define, contourId, onDispose, navKey, safeColor, safeHref, cssLength , warnBadJson, labelText, flagOn, formatValue, wcEmitOverlay } from './core.mjs';
2
+ import { wireRemovable, modalContain } from './behaviours.mjs';
3
+ import { parseArrayAttr, renderEmpty } from './states.mjs';
4
+
5
+ /**
6
+ * MapPopup: a map popup dialog with title, content, and optional cluster
7
+ * badge (cluster-count / cluster-expanded). Attributes: `title`, `content`,
8
+ * `cluster-count`, `cluster-expanded`.
9
+ *
10
+ * Default content area (the light-DOM equivalent of a default `<slot>`): the
11
+ * author's own child nodes are moved into `.c-map-popup__body`, so a map
12
+ * infowindow can host an image, a table or a nested component rather than a
13
+ * single `content` string. The `content` attribute still fills the body when the
14
+ * author supplies no children; authored children win when both exist. The base
15
+ * class unwraps `.c-map-popup__body` before dropping generated nodes on
16
+ * re-render (see core.mjs), so authored children survive an attribute change.
17
+ */
18
+ class ContourMapPopup extends ContourBase {
19
+ static get observed() { return ["title", "content", "cluster-count", "cluster-expanded", "cluster-items", "anchor"]; }
20
+ static get properties() { return { title: { attribute: "title" }, content: { attribute: "content" }, "cluster-count": { attribute: "cluster-count", type: Number }, "cluster-expanded": { attribute: "cluster-expanded", type: Boolean }, "cluster-items": { attribute: "cluster-items" } }; }
21
+ render() {
22
+ this.classList.add("c-map-popup");
23
+ this.setAttribute("role", "dialog");
24
+ if (!this.hasAttribute("popover")) this.setAttribute("popover", "auto");
25
+ if (!this.id) this.id = contourId("map-popup");
26
+ const title = this.getAttribute("title");
27
+ if (title) {
28
+ const t = this.ensure(".c-map-popup__title", () => { const el = document.createElement("div"); el.className = "c-map-popup__title"; return el; });
29
+ t.textContent = title;
30
+ }
31
+ // Default content area: authored children moved to .c-map-popup__body; content attr fills when no children.
32
+ const authored = Array.from(this.childNodes).filter((n) => {
33
+ if (n.nodeType === 1 && n.classList && (n.classList.contains("c-map-popup__title") || n.classList.contains("c-map-popup__body"))) return false;
34
+ if (n.nodeType === 3 && !n.textContent.trim()) return false;
35
+ return true;
36
+ });
37
+ const content = this.getAttribute("content");
38
+ if (authored.length) {
39
+ const c = this.ensure(".c-map-popup__body", () => { const el = document.createElement("div"); el.className = "c-map-popup__body"; return el; });
40
+ for (const n of authored) c.appendChild(n);
41
+ } else if (content) {
42
+ const c = this.ensure(".c-map-popup__body", () => { const el = document.createElement("div"); el.className = "c-map-popup__body"; return el; });
43
+ c.textContent = content;
44
+ }
45
+ const clusterCount = Number(this.getAttribute("cluster-count"));
46
+ if (Number.isFinite(clusterCount) && clusterCount > 1) {
47
+ const badge = this.ensure(".c-map-popup__cluster", () => { const el = document.createElement("button"); el.className = "c-map-popup__cluster c-btn c-btn--sm"; el.type = "button"; return el; });
48
+ const expanded = this.hasAttribute("cluster-expanded");
49
+ badge.textContent = expanded ? "Hide locations" : `${clusterCount} locations`;
50
+ badge.setAttribute("aria-expanded", String(expanded));
51
+ badge.setAttribute("aria-label", expanded ? "Hide locations" : `Show ${clusterCount} locations`);
52
+ } else {
53
+ const badge = this.querySelector(".c-map-popup__cluster");
54
+ if (badge) badge.remove();
55
+ }
56
+ // Spatial clustering (R14-28): render clustered locations when expanded.
57
+ const expanded = this.hasAttribute("cluster-expanded");
58
+ let items = parseArrayAttr(this, "cluster-items", warnBadJson);
59
+ const list = this.querySelector(".c-map-popup__cluster-list");
60
+ if (expanded && Array.isArray(items) && items.length) {
61
+ const ul = list || document.createElement("ul");
62
+ ul.className = "c-map-popup__cluster-list";
63
+ setHTML(ul, "");
64
+ items.forEach((it) => {
65
+ const li = document.createElement("li");
66
+ li.className = "c-map-popup__cluster-item";
67
+ li.textContent = typeof it === "string" ? it : (it.label || it.name || JSON.stringify(it));
68
+ ul.appendChild(li);
69
+ });
70
+ if (ul.parentNode !== this) this.appendChild(ul);
71
+ } else if (list) {
72
+ list.remove();
73
+ }
74
+ if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", title || "Map popup");
75
+ // Re-resolve anchor on every render so anchor attr changes re-point popup.
76
+ const anchorSel = this.getAttribute("anchor");
77
+ let anchorEl = null;
78
+ if (anchorSel) {
79
+ anchorEl = anchorSel.startsWith("#") ? document.getElementById(anchorSel.slice(1)) : document.querySelector(anchorSel);
80
+ }
81
+ this._mapPopupAnchorEl = anchorEl;
82
+ const anchorName = "--map-popup-" + this.id;
83
+ if (anchorEl) {
84
+ anchorEl.style.setProperty("anchor-name", anchorName);
85
+ this.style.setProperty("position-anchor", anchorName);
86
+ }
87
+ }
88
+ enhance() {
89
+ if (this._wiredMapPopup) return;
90
+ this._wiredMapPopup = true;
91
+ const close = () => { try { this.hidePopover(); } catch (e) {} this.classList.remove("is-open"); this.dispatchEvent(new CustomEvent("contour:close", { bubbles: true, composed: true })); };
92
+ const keyHandler = (e) => { if (e.key === "Escape" && (this.classList.contains("is-open") || this.matches(":popover-open"))) { e.preventDefault(); close(); } };
93
+ document.addEventListener("keydown", keyHandler);
94
+ onDispose(this, () => document.removeEventListener("keydown", keyHandler));
95
+ this.addEventListener("click", (e) => {
96
+ const btn = e.target && e.target.closest ? e.target.closest(".c-map-popup__close") : null;
97
+ if (btn && this.contains(btn)) close();
98
+ const cluster = e.target && e.target.closest ? e.target.closest(".c-map-popup__cluster") : null;
99
+ if (cluster && this.contains(cluster)) {
100
+ const expanded = !this.hasAttribute("cluster-expanded");
101
+ this.toggleAttribute("cluster-expanded", expanded);
102
+ this.dispatchEvent(new CustomEvent("contour:cluster-toggle", { bubbles: true, composed: true, detail: { count: Number(this.getAttribute("cluster-count")), expanded } }));
103
+ }
104
+ });
105
+ // Anchor + collision handling.
106
+ const anchorName = "--map-popup-" + this.id;
107
+ const applyAnchor = () => {
108
+ const anchorEl = this._mapPopupAnchorEl;
109
+ if (anchorEl) {
110
+ anchorEl.style.setProperty("anchor-name", anchorName);
111
+ this.style.setProperty("position-anchor", anchorName);
112
+ }
113
+ };
114
+ const flipIfNeeded = () => {
115
+ try {
116
+ const r = this.getBoundingClientRect();
117
+ const flipX = r.right > window.innerWidth || r.left < 0;
118
+ const flipY = r.bottom > window.innerHeight || r.top < 0;
119
+ const flipped = flipX || flipY;
120
+ this.classList.toggle("c-map-popup--flip-x", flipX && !flipY);
121
+ this.classList.toggle("c-map-popup--flip-y", flipY && !flipX);
122
+ this.classList.toggle("c-map-popup--flip-both", flipX && flipY);
123
+ if (flipped) {
124
+ this.dispatchEvent(new CustomEvent("contour:map-popup-reposition", {
125
+ bubbles: true, composed: true,
126
+ detail: { anchor: this.getAttribute("anchor") || "", flippedX: flipX, flippedY: flipY, left: Math.round(r.left), top: Math.round(r.top) }
127
+ }));
128
+ }
129
+ } catch (e) {}
130
+ };
131
+ this.addEventListener("toggle", (e) => {
132
+ if (e.newState === "open") {
133
+ requestAnimationFrame(() => { applyAnchor(); flipIfNeeded(); });
134
+ }
135
+ });
136
+ // Re-check on resize/scroll to keep popup in view.
137
+ const onResize = () => flipIfNeeded();
138
+ window.addEventListener("resize", onResize);
139
+ onDispose(this, () => { window.removeEventListener("resize", onResize); if (this._mapPopupAnchorEl) this._mapPopupAnchorEl.style.removeProperty("anchor-name"); });
140
+ }
141
+ }
142
+ define("contour-map-popup", ContourMapPopup);
143
+
144
+ /**
145
+ * AnimationControls: a media control toolbar with play/pause, speed, scrubber
146
+ * and caption. Attributes: `playing`, `speed`, `progress`, `min`, `max`,
147
+ * `caption`, `show-thumb`, `show-thumb-caption`.
148
+ */
149
+ class ContourAnimationControls extends ContourBase {
150
+ static get observed() { return ["playing", "speed", "progress", "min", "max", "caption", "show-thumb", "show-thumb-caption", "loop", "step", "markers"]; }
151
+ static get properties() { return { playing: { attribute: "playing", type: Boolean }, speed: { attribute: "speed" }, progress: { attribute: "progress" }, min: { attribute: "min" }, max: { attribute: "max" }, loop: { attribute: "loop", type: Boolean }, step: { attribute: "step" }, markers: { attribute: "markers" } }; }
152
+ render() {
153
+ this.classList.add("c-animation-controls");
154
+ this.setAttribute("role", "toolbar");
155
+ this.setAttribute("aria-label", labelText(this, "animation-controls", "Animation controls"));
156
+ const playBtn = this.ensure(".c-animation-controls__play", () => { const el = document.createElement("button"); el.className = "c-btn c-btn--sm c-btn--icon c-animation-controls__play"; el.type = "button"; return el; });
157
+ // R31-137: transport CTAs are icon-only; the accessible name carries the
158
+ // action, so the glyph can change without the label drifting.
159
+ setHTML(playBtn, this.hasAttribute("playing") ? '<svg aria-hidden="true" viewBox="0 0 24 24"><use href="#i-pause" /></svg>' : '<svg aria-hidden="true" viewBox="0 0 24 24"><use href="#i-play" /></svg>');
160
+ playBtn.setAttribute("aria-label", this.hasAttribute("playing") ? labelText(this, "pause", "Pause") : labelText(this, "play", "Play"));
161
+ const speed = this.getAttribute("speed") || "1x";
162
+ const speedBtn = this.ensure(".c-animation-controls__speed", () => { const el = document.createElement("button"); el.className = "c-btn c-btn--sm c-animation-controls__speed"; el.type = "button"; return el; });
163
+ speedBtn.textContent = speed;
164
+ speedBtn.setAttribute("aria-label", labelText(this, "playback-speed", "Playback speed") + " " + speed);
165
+ // Loop is a control, not just an aria-label suffix. R31-137: icon-only.
166
+ const loopBtn = this.ensure(".c-animation-controls__loop", () => { const el = document.createElement("button"); el.className = "c-btn c-btn--sm c-btn--icon c-animation-controls__loop"; el.type = "button"; setHTML(el, '<svg aria-hidden="true" viewBox="0 0 24 24"><use href="#i-refresh" /></svg>'); return el; });
167
+ loopBtn.setAttribute("aria-label", labelText(this, "loop", "Loop"));
168
+ loopBtn.setAttribute("aria-pressed", String(this.hasAttribute("loop")));
169
+ // Toolbar: one tab stop, arrows rove (scrubber keeps native keys).
170
+ playBtn.tabIndex = 0;
171
+ speedBtn.tabIndex = -1;
172
+ loopBtn.tabIndex = -1;
173
+ // scrubber
174
+ const scrubber = this.ensure(".c-animation-controls__scrubber", () => { const el = document.createElement("input"); el.className = "c-range c-animation-controls__scrubber"; el.type = "range"; return el; });
175
+ if (this.getAttribute("show-thumb") === "false") scrubber.classList.remove("c-animation-controls__scrubber--thumb");
176
+ else scrubber.classList.add("c-animation-controls__scrubber--thumb");
177
+ scrubber.min = this.getAttribute("min") || "0";
178
+ scrubber.max = this.getAttribute("max") || "100";
179
+ scrubber.step = this.getAttribute("step") || "1";
180
+ scrubber.value = this.getAttribute("progress") || "0";
181
+ // Paint the filled portion: the scrubber is created after ds.js's
182
+ // one-shot range pass, so it never receives --fill otherwise and the
183
+ // track reads as an empty (inverted) bar.
184
+ {
185
+ const sMin = Number(scrubber.min) || 0;
186
+ const sMax = Number(scrubber.max) || 100;
187
+ const sVal = Number(scrubber.value) || 0;
188
+ const pct = sMax === sMin ? 0 : ((sVal - sMin) / (sMax - sMin)) * 100;
189
+ scrubber.style.setProperty("--fill", pct + "%");
190
+ }
191
+ // Accessible time values: expose position as aria-valuenow, human-readable as aria-valuetext.
192
+ const min = Number(scrubber.min) || 0;
193
+ const max = Number(scrubber.max) || 100;
194
+ const v = Number(scrubber.value) || min;
195
+ scrubber.setAttribute("aria-valuenow", String(v));
196
+ scrubber.setAttribute("aria-valuetext", String(Math.round(v)));
197
+ scrubber.setAttribute("aria-valuemin", String(min));
198
+ scrubber.setAttribute("aria-valuemax", String(max));
199
+ const caption = this.getAttribute("caption");
200
+ if (caption) scrubber.setAttribute("aria-label", caption);
201
+ else if (!scrubber.getAttribute("aria-label")) scrubber.setAttribute("aria-label", labelText(this, "playhead", "Playhead"));
202
+ // Markers: attribute observed but never read; native range draws datalist ticks.
203
+ const markersRaw = this.getAttribute("markers");
204
+ let markers = [];
205
+ if (markersRaw) { try { const p = JSON.parse(markersRaw); markers = Array.isArray(p) ? p : []; } catch (e) { warnBadJson(this, "markers", e); } }
206
+ const oldList = this.querySelector("datalist.c-animation-controls__markers");
207
+ if (markers.length) {
208
+ const dl = this.ensure("datalist.c-animation-controls__markers", () => { const el = document.createElement("datalist"); el.className = "c-animation-controls__markers"; return el; });
209
+ if (!dl.id) dl.id = this._animMarkersId || (this._animMarkersId = contourId("anim-markers"));
210
+ setHTML(dl, "");
211
+ markers.forEach((m) => {
212
+ const opt = document.createElement("option");
213
+ const val = typeof m === "object" && m !== null ? m.value : m;
214
+ opt.value = String(val);
215
+ if (typeof m === "object" && m !== null && m.label) opt.label = String(m.label);
216
+ dl.appendChild(opt);
217
+ });
218
+ scrubber.setAttribute("list", dl.id);
219
+ } else {
220
+ if (oldList) oldList.remove();
221
+ scrubber.removeAttribute("list");
222
+ }
223
+ // caption display
224
+ const showCaption = this.hasAttribute("show-thumb-caption") || this.hasAttribute("show-thumb");
225
+ if (caption && showCaption) {
226
+ const cap = this.ensure(".c-animation-controls__caption", () => { const el = document.createElement("span"); el.className = "c-animation-controls__caption"; return el; });
227
+ cap.textContent = caption;
228
+ } else {
229
+ const cap = this.querySelector(".c-animation-controls__caption");
230
+ if (cap) cap.remove();
231
+ }
232
+ // Loop indicator
233
+ if (this.hasAttribute("loop")) {
234
+ this.setAttribute("aria-label", labelText(this, "animation-controls-loop", "Animation controls, loop"));
235
+ }
236
+ // showThumb: hide thumb via attribute? keep default
237
+ }
238
+ enhance() {
239
+ if (this._wiredAnimControls) return;
240
+ this._wiredAnimControls = true;
241
+ const SPEEDS = [0.5, 1, 2, 4];
242
+ // Playhead writes back to `progress` but not mid-drag (write re-renders, breaks gesture).
243
+ const writeProgress = (v) => { if (!this._animScrubbing) this.setAttribute("progress", String(v)); };
244
+ this.addEventListener("pointerdown", (e) => {
245
+ if (e.target && e.target.closest && e.target.closest(".c-animation-controls__scrubber")) this._animScrubbing = true;
246
+ }, { passive: true });
247
+ const endScrub = () => { this._animScrubbing = false; };
248
+ this.addEventListener("pointerup", endScrub, { passive: true });
249
+ this.addEventListener("pointercancel", endScrub, { passive: true });
250
+ this.addEventListener("click", (e) => {
251
+ const play = e.target && e.target.closest ? e.target.closest(".c-animation-controls__play") : null;
252
+ if (play && this.contains(play)) {
253
+ // Attribute is source of truth: observer starts/stops timer, re-render flips label.
254
+ const next = !this.hasAttribute("playing");
255
+ this.toggleAttribute("playing", next);
256
+ this.dispatchEvent(new CustomEvent("contour:toggle", { bubbles: true, composed: true, detail: { playing: next } }));
257
+ return;
258
+ }
259
+ const speed = e.target && e.target.closest ? e.target.closest(".c-animation-controls__speed") : null;
260
+ if (speed && this.contains(speed)) {
261
+ const cur = parseFloat(this.getAttribute("speed") || "1x");
262
+ const idx = SPEEDS.indexOf(Number.isFinite(cur) ? cur : 1);
263
+ const next = SPEEDS[(idx + 1) % SPEEDS.length];
264
+ this.setAttribute("speed", next + "x");
265
+ this.dispatchEvent(new CustomEvent("contour:speed-change", { bubbles: true, composed: true, detail: { speed: next } }));
266
+ return;
267
+ }
268
+ const loop = e.target && e.target.closest ? e.target.closest(".c-animation-controls__loop") : null;
269
+ if (loop && this.contains(loop)) {
270
+ const next = !this.hasAttribute("loop");
271
+ this.toggleAttribute("loop", next);
272
+ this.dispatchEvent(new CustomEvent("contour:loop-change", { bubbles: true, composed: true, detail: { loop: next } }));
273
+ }
274
+ });
275
+ this.addEventListener("input", (e) => {
276
+ const scrubber = e.target && e.target.closest ? e.target.closest(".c-animation-controls__scrubber") : null;
277
+ if (scrubber && this.contains(scrubber)) {
278
+ const v = Number(scrubber.value);
279
+ // Paint the fill live: the attribute write is suppressed mid-drag,
280
+ // so the re-render that normally repaints --fill does not run.
281
+ const sMin = Number(scrubber.min) || 0;
282
+ const sMax = Number(scrubber.max) || 100;
283
+ const pct = sMax === sMin ? 0 : ((v - sMin) / (sMax - sMin)) * 100;
284
+ scrubber.style.setProperty("--fill", pct + "%");
285
+ writeProgress(v);
286
+ this.dispatchEvent(new CustomEvent("contour:seek", { bubbles: true, composed: true, detail: { progress: v, value: v } }));
287
+ }
288
+ });
289
+ this.addEventListener("change", (e) => {
290
+ const scrubber = e.target && e.target.closest ? e.target.closest(".c-animation-controls__scrubber") : null;
291
+ if (scrubber && this.contains(scrubber)) this.setAttribute("progress", scrubber.value);
292
+ });
293
+ // Toolbar keyboard: arrows rove buttons; scrubber keeps native range keys when focused.
294
+ this.addEventListener("keydown", (e) => {
295
+ if (e.target && e.target.closest && e.target.closest(".c-animation-controls__scrubber")) return;
296
+ const buttons = Array.from(this.querySelectorAll(".c-animation-controls__play, .c-animation-controls__speed, .c-animation-controls__loop"));
297
+ if (!buttons.includes(e.target)) return;
298
+ navKey(e, buttons, { wrap: true, homeEnd: true, onNavigate: (i) => { buttons.forEach((b, j) => { b.tabIndex = j === i ? 0 : -1; }); }, onKey: (k, idx, item) => { if (k === "Enter" || k === " ") item.click(); } });
299
+ });
300
+ // Playback ownership: when `playing` set, advance playhead on timer, emit contour:time-change for sync.
301
+ let timer = null;
302
+ const speedFactor = () => {
303
+ const s = this.getAttribute("speed") || "1x";
304
+ const n = parseFloat(s);
305
+ return Number.isFinite(n) && n > 0 ? n : 1;
306
+ };
307
+ const tick = () => {
308
+ const scrubber = this.querySelector(".c-animation-controls__scrubber");
309
+ if (!scrubber) return;
310
+ const min = Number(scrubber.min || 0);
311
+ const max = Number(scrubber.max || 100);
312
+ let v = Number(scrubber.value || 0) + speedFactor();
313
+ let ended = false;
314
+ if (v > max) {
315
+ if (this.hasAttribute("loop")) v = min;
316
+ else { v = max; stop(); ended = true; }
317
+ }
318
+ scrubber.value = String(v);
319
+ writeProgress(v);
320
+ this.dispatchEvent(new CustomEvent("contour:time-change", { bubbles: true, composed: true, detail: { progress: v, value: v } }));
321
+ if (ended) {
322
+ this.removeAttribute("playing");
323
+ this.dispatchEvent(new CustomEvent("contour:toggle", { bubbles: true, composed: true, detail: { playing: false } }));
324
+ }
325
+ };
326
+ const start = () => { if (timer) return; timer = setInterval(tick, 100); };
327
+ const stop = () => { if (timer) { clearInterval(timer); timer = null; } };
328
+ if (this.hasAttribute("playing")) start();
329
+ const obs = new MutationObserver(() => { if (this.hasAttribute("playing")) start(); else stop(); });
330
+ obs.observe(this, { attributes: true, attributeFilter: ["playing"] });
331
+ onDispose(this, () => { stop(); obs.disconnect(); });
332
+ }
333
+ }
334
+ define("contour-animation-controls", ContourAnimationControls);
335
+ /**
336
+ * Annotation: a positioned, movable note. Attributes: `label`, `x`, `y`
337
+ * (percentages of the offset parent, e.g. "40%"). The note is focusable:
338
+ * dragging it or nudging with the arrow keys (Shift for 10% steps) writes
339
+ * `x`/`y` back — the attributes ARE the serialization — and emits
340
+ * `contour:drag` per move and `contour:dragend` on release (R20 T-11).
341
+ */
342
+ class ContourAnnotation extends ContourBase {
343
+ static get observed() { return ["label", "x", "y"]; }
344
+ static get properties() { return { label: { attribute: "label" }, x: { attribute: "x" }, y: { attribute: "y" } }; }
345
+ render() {
346
+ this.classList.add("c-annotation");
347
+ this.setAttribute("role", "note");
348
+ if (!this.hasAttribute("tabindex")) this.setAttribute("tabindex", "0");
349
+ const label = this.getAttribute("label");
350
+ if (label) this.setAttribute("aria-label", label);
351
+ if (label) {
352
+ const l = this.ensure(".c-annotation__label", () => { const el = document.createElement("span"); el.className = "c-annotation__label"; return el; });
353
+ l.textContent = label;
354
+ }
355
+ const x = this.getAttribute("x");
356
+ const y = this.getAttribute("y");
357
+ if (x !== null) this.style.setProperty("--annotation-x", x);
358
+ if (y !== null) this.style.setProperty("--annotation-y", y);
359
+ }
360
+ enhance() {
361
+ if (this._wiredAnnotation) return;
362
+ this._wiredAnnotation = true;
363
+ // Percent of offset parent, clamped [0,100]. Host survives re-render; pointer capture holds across writeback.
364
+ const pct = (v, fallback) => {
365
+ const n = parseFloat(v);
366
+ return Number.isFinite(n) ? Math.min(100, Math.max(0, n)) : fallback;
367
+ };
368
+ const write = (x, y, key) => {
369
+ // One render per move: batch coalesces x and y writes.
370
+ this.batch(() => {
371
+ this.setAttribute("x", x + "%");
372
+ this.setAttribute("y", y + "%");
373
+ });
374
+ this.dispatchEvent(new CustomEvent("contour:drag", { bubbles: true, composed: true, detail: { delta: key ? 1 : 0, key: key || "pointer", value: x + "," + y } }));
375
+ };
376
+ let drag = null;
377
+ this.addEventListener("pointerdown", (e) => {
378
+ if (e.button !== undefined && e.button !== 0) return;
379
+ const parent = this.offsetParent || this.parentElement;
380
+ if (!parent) return;
381
+ const rect = parent.getBoundingClientRect();
382
+ drag = { px: e.clientX, py: e.clientY, x: pct(this.getAttribute("x"), 50), y: pct(this.getAttribute("y"), 50), w: rect.width || 1, h: rect.height || 1 };
383
+ try { this.setPointerCapture(e.pointerId); } catch (err) {}
384
+ try { this.focus(); } catch (err) {}
385
+ e.preventDefault();
386
+ });
387
+ this.addEventListener("pointermove", (e) => {
388
+ if (!drag) return;
389
+ const x = Math.round((drag.x + ((e.clientX - drag.px) / drag.w) * 100) * 10) / 10;
390
+ const y = Math.round((drag.y + ((e.clientY - drag.py) / drag.h) * 100) * 10) / 10;
391
+ write(Math.min(100, Math.max(0, x)), Math.min(100, Math.max(0, y)));
392
+ });
393
+ const endDrag = () => {
394
+ if (!drag) return;
395
+ drag = null;
396
+ this.dispatchEvent(new CustomEvent("contour:dragend", { bubbles: true, composed: true, detail: { value: (this.getAttribute("x") || "") + "," + (this.getAttribute("y") || "") } }));
397
+ };
398
+ this.addEventListener("pointerup", endDrag);
399
+ this.addEventListener("pointercancel", endDrag);
400
+ this.addEventListener("keydown", (e) => {
401
+ const step = e.shiftKey ? 10 : 1;
402
+ let dx = 0, dy = 0;
403
+ if (e.key === "ArrowLeft") dx = -step;
404
+ else if (e.key === "ArrowRight") dx = step;
405
+ else if (e.key === "ArrowUp") dy = -step;
406
+ else if (e.key === "ArrowDown") dy = step;
407
+ else return;
408
+ e.preventDefault();
409
+ const x = Math.min(100, Math.max(0, pct(this.getAttribute("x"), 50) + dx));
410
+ const y = Math.min(100, Math.max(0, pct(this.getAttribute("y"), 50) + dy));
411
+ write(x, y, e.key);
412
+ this.dispatchEvent(new CustomEvent("contour:dragend", { bubbles: true, composed: true, detail: { value: x + "," + y } }));
413
+ });
414
+ }
415
+ }
416
+ define("contour-annotation", ContourAnnotation);
417
+
418
+ /**
419
+ * Style Switcher: a toggle-button group over map/styles. Attributes: `items`
420
+ * (JSON) or authored `.c-style-switcher__option` buttons, and `value`.
421
+ * Selecting an option writes `value` back — the pressed/active state is the
422
+ * attribute's projection, not a one-shot render — and emits `contour:change`
423
+ * (R20 T-12). Arrow keys rove one tab stop; Enter/Space select.
424
+ */
425
+ class ContourStyleSwitcher extends ContourBase {
426
+ static get observed() { return ["items", "value"]; }
427
+ static get properties() { return { items: { attribute: "items" }, value: { attribute: "value" } }; }
428
+ render() {
429
+ this.classList.add("c-style-switcher");
430
+ if (!this.hasAttribute("role")) this.setAttribute("role", "group");
431
+ if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", labelText(this, "style-switcher", "Style switcher"));
432
+ const items = this.getAttribute("items");
433
+ const value = this.getAttribute("value");
434
+ // Authored options win over JSON attribute (dropdown contract); they used to be destroyed on every render.
435
+ const authored = Array.from(this.children).filter((n) => n.classList && n.classList.contains("c-style-switcher__option"));
436
+ if (items && !authored.length) {
437
+ Array.from(this.querySelectorAll(".c-style-switcher__option")).forEach((n) => n.remove());
438
+ try {
439
+ const parsed = JSON.parse(items);
440
+ parsed.forEach((it) => {
441
+ const name = typeof it === "string" ? it : (it.name || it.label || "");
442
+ const btn = document.createElement("button");
443
+ btn.className = "c-style-switcher__option c-btn c-btn--sm";
444
+ btn.type = "button";
445
+ btn.textContent = name;
446
+ btn.dataset.value = name;
447
+ this.appendChild(btn);
448
+ });
449
+ } catch (e) { warnBadJson(this, "attribute", e); }
450
+ }
451
+ // Project the value onto every option, authored or generated, and keep
452
+ // one tab stop: the active option (or the first).
453
+ const opts = Array.from(this.querySelectorAll(".c-style-switcher__option"));
454
+ let active = null;
455
+ opts.forEach((btn) => {
456
+ const v = btn.dataset.value != null ? btn.dataset.value : btn.textContent.trim();
457
+ const on = v === value;
458
+ btn.classList.toggle("c-style-switcher__option--active", on);
459
+ btn.setAttribute("aria-pressed", String(on));
460
+ btn.tabIndex = -1;
461
+ if (on && !active) active = btn;
462
+ });
463
+ (active || opts[0])?.setAttribute("tabindex", "0");
464
+ }
465
+ enhance() {
466
+ if (this._wiredStyleSwitcher) return;
467
+ this._wiredStyleSwitcher = true;
468
+ const select = (btn) => {
469
+ const v = btn.dataset.value != null ? btn.dataset.value : btn.textContent.trim();
470
+ // Writeback re-renders pressed state; event carries same value attribute now holds.
471
+ this.setAttribute("value", v);
472
+ this.dispatchEvent(new CustomEvent("contour:change", { bubbles: true, composed: true, detail: { value: v } }));
473
+ };
474
+ this.addEventListener("click", (e) => {
475
+ const btn = e.target && e.target.closest ? e.target.closest(".c-style-switcher__option") : null;
476
+ if (btn && this.contains(btn)) select(btn);
477
+ });
478
+ this.addEventListener("keydown", (e) => {
479
+ const opts = Array.from(this.querySelectorAll(".c-style-switcher__option"));
480
+ navKey(e, opts, { onKey: (k) => { if (k === "Enter" || k === " ") { const btn = document.activeElement; if (btn && btn.classList.contains("c-style-switcher__option")) select(btn); } } });
481
+ });
482
+ }
483
+ }
484
+ define("contour-style-switcher", ContourStyleSwitcher);
485
+
486
+ /**
487
+ * Style Panel: web component wrapper.
488
+ * Attributes: `title`, `layers`, `layer`, `single`, `classification`, `palette`, `breaks`, `opacity`, `n`, `colors`, `property`.
489
+ */
490
+ class ContourStylePanel extends ContourBase {
491
+ static get observed() { return ["title", "layers", "layer", "single", "classification", "palette", "breaks", "opacity", "n", "colors", "property"]; }
492
+ static get properties() { return { title: { attribute: "title" }, layers: { attribute: "layers" }, classification: { attribute: "classification" }, palette: { attribute: "palette" }, breaks: { attribute: "breaks" }, opacity: { attribute: "opacity" } }; }
493
+ render() {
494
+ this.classList.add("c-style-panel");
495
+ // Authored defaults captured once; Revert restores these, not last rendered state.
496
+ if (!this._stylePanelDefault) {
497
+ this._stylePanelDefault = {};
498
+ for (const a of ["property", "classification", "palette", "breaks", "opacity", "n", "colors"]) this._stylePanelDefault[a] = this.getAttribute(a);
499
+ }
500
+ this.setAttribute("role", "group");
501
+ this.setAttribute("aria-label", labelText(this, "style-panel", "Style panel"));
502
+ const title = this.getAttribute("title");
503
+ if (title) {
504
+ const t = this.ensure(".c-style-panel__title", () => { const el = document.createElement("div"); el.className = "c-style-panel__title"; return el; });
505
+ t.textContent = title;
506
+ }
507
+ const singleMode = this.hasAttribute("single") || !!this.getAttribute("layer");
508
+ if (singleMode) {
509
+ const body = this.ensure(".c-style-panel__body", () => { const el = document.createElement("div"); el.className = "c-style-panel__body"; return el; });
510
+ setHTML(body, "");
511
+ // Unique ids: old code used global "property"/"policy"/"palette"/"n" — duplicate ids broke label[for].
512
+ const uid = contourId("style-panel");
513
+ const propId = uid + "-property";
514
+ const policyId = uid + "-policy";
515
+ const paletteId = uid + "-palette";
516
+ const nId = uid + "-n";
517
+ // property field
518
+ const propLabel = document.createElement("label"); propLabel.textContent = labelText(this, "colour-field", "Colour field"); propLabel.setAttribute("for", propId); body.appendChild(propLabel);
519
+ const propIn = document.createElement("input"); propIn.type = "text"; propIn.id = propId; propIn.className = "c-style-panel__property c-input"; propIn.setAttribute("aria-label", labelText(this, "colour-field", "Colour field"));
520
+ const pv = this.getAttribute("property"); if (pv) propIn.value = pv;
521
+ body.appendChild(propIn);
522
+ // classification method selector (policy)
523
+ const clsLabel = document.createElement("label"); clsLabel.textContent = labelText(this, "classification", "Classification"); clsLabel.setAttribute("for", policyId); body.appendChild(clsLabel);
524
+ const clsSel = document.createElement("select"); clsSel.id = policyId; clsSel.className = "c-style-panel__classification c-input"; clsSel.setAttribute("aria-label", labelText(this, "classification-method", "Classification method"));
525
+ ["equal","quantile","jenks","manual"].forEach((m) => { const o = document.createElement("option"); o.value = m; o.textContent = m; clsSel.appendChild(o); });
526
+ const cls = this.getAttribute("classification"); if (cls) clsSel.value = cls;
527
+ body.appendChild(clsSel);
528
+ // palette picker
529
+ const palLabel = document.createElement("label"); palLabel.textContent = labelText(this, "palette", "Palette"); palLabel.setAttribute("for", paletteId); body.appendChild(palLabel);
530
+ const pal = document.createElement("input"); pal.type = "text"; pal.id = paletteId; pal.className = "c-style-panel__palette c-input"; pal.placeholder = labelText(this, "palette", "Palette"); pal.setAttribute("aria-label", labelText(this, "palette", "Palette"));
531
+ const palv = this.getAttribute("palette"); if (palv) pal.value = palv;
532
+ body.appendChild(pal);
533
+ // n (class count)
534
+ const nLabel = document.createElement("label"); nLabel.textContent = labelText(this, "classes", "Classes"); nLabel.setAttribute("for", nId); body.appendChild(nLabel);
535
+ const nIn = document.createElement("input"); nIn.type = "number"; nIn.id = nId; nIn.className = "c-style-panel__n c-input"; nIn.min = "2"; nIn.max = "12"; nIn.setAttribute("aria-label", labelText(this, "class-count", "Class count"));
536
+ const nv = this.getAttribute("n"); if (nv) nIn.value = nv; else nIn.value = "5";
537
+ body.appendChild(nIn);
538
+ // opacity slider
539
+ const op = document.createElement("input"); op.type = "range"; op.className = "c-style-panel__opacity"; op.setAttribute("aria-label", labelText(this, "opacity", "Opacity")); op.min = "0"; op.max = "100";
540
+ const ov = this.getAttribute("opacity"); op.value = ov != null ? ov : "100";
541
+ body.appendChild(op);
542
+ // breaks as .break elements per class
543
+ const breaksAttr = this.getAttribute("breaks");
544
+ let breaksArr = null;
545
+ if (breaksAttr) { try { breaksArr = JSON.parse(breaksAttr); } catch (e) { breaksArr = breaksAttr.split(",").map((s) => s.trim()); } }
546
+ if (Array.isArray(breaksArr) && breaksArr.length) {
547
+ const breaksWrap = document.createElement("div"); breaksWrap.className = "c-style-panel__breaks";
548
+ breaksArr.forEach((b, i) => {
549
+ const br = document.createElement("div"); br.className = "break";
550
+ const span = document.createElement("span"); span.textContent = String(b); br.appendChild(span);
551
+ const inp = document.createElement("input"); inp.type = "number"; inp.className = "c-style-panel__breaks c-input"; inp.setAttribute("aria-label", labelText(this, "class-lower-bound", "Class lower bound")); inp.value = String(b);
552
+ br.appendChild(inp);
553
+ breaksWrap.appendChild(br);
554
+ });
555
+ body.appendChild(breaksWrap);
556
+ }
557
+ // color swatches
558
+ const colorsAttr = this.getAttribute("colors");
559
+ let colors = null;
560
+ if (colorsAttr) { try { colors = JSON.parse(colorsAttr); } catch (e) { colors = colorsAttr.split(",").map((s) => s.trim()); } }
561
+ if (Array.isArray(colors)) {
562
+ const list = document.createElement("div"); list.className = "c-style-panel__colors";
563
+ colors.forEach((c, i) => {
564
+ const sw = document.createElement("input"); sw.type = "color"; sw.className = "c-style-panel__color c-input"; sw.setAttribute("aria-label", labelText(this, "colour-for-class", "Colour for class") + " " + (i + 1));
565
+ sw.value = c;
566
+ // data-edited marked by delegated input handler in enhance() — per-node listener dies with render.
567
+ list.appendChild(sw);
568
+ });
569
+ body.appendChild(list);
570
+ }
571
+ // Apply/Revert buttons
572
+ const actions = document.createElement("div"); actions.className = "c-style-panel__actions c-btn-group";
573
+ const applyBtn = document.createElement("button"); applyBtn.type = "button"; applyBtn.className = "c-btn c-btn--primary"; applyBtn.textContent = labelText(this, "apply", "Apply"); applyBtn.dataset.action = "apply";
574
+ const revertBtn = document.createElement("button"); revertBtn.type = "button"; revertBtn.className = "c-btn"; revertBtn.textContent = labelText(this, "revert", "Revert"); revertBtn.dataset.action = "revert";
575
+ actions.appendChild(applyBtn); actions.appendChild(revertBtn);
576
+ body.appendChild(actions);
577
+ return;
578
+ }
579
+ const layers = this.getAttribute("layers");
580
+ const body = this.ensure(".c-style-panel__body", () => { const el = document.createElement("div"); el.className = "c-style-panel__body"; return el; });
581
+ // Slotted content: authored .c-style-panel__row children take precedence over JSON `layers`.
582
+ const authoredRows = Array.from(this.querySelectorAll(":scope > .c-style-panel__row"));
583
+ let parsed = null;
584
+ if (authoredRows.length) {
585
+ parsed = authoredRows.map((row) => ({
586
+ name: row.querySelector(".c-style-panel__name")?.textContent || "",
587
+ color: row.querySelector(".c-style-panel__color")?.value,
588
+ classification: row.querySelector(".c-style-panel__classification")?.value,
589
+ palette: row.querySelector(".c-style-panel__palette")?.value,
590
+ breaks: row.querySelector(".c-style-panel__breaks")?.value,
591
+ opacity: row.querySelector(".c-style-panel__opacity")?.value,
592
+ }));
593
+ authoredRows.forEach((row) => row.remove());
594
+ } else if (layers) {
595
+ try {
596
+ parsed = JSON.parse(layers);
597
+ } catch (e) { warnBadJson(this, "layers", e); parsed = null; }
598
+ }
599
+ if (parsed) {
600
+ setHTML(body, "");
601
+ parsed.forEach((l) => {
602
+ const row = document.createElement("div");
603
+ row.className = "c-style-panel__row";
604
+ const name = document.createElement("span");
605
+ name.className = "c-style-panel__name";
606
+ name.textContent = typeof l === "string" ? l : (l.name || l.label || "");
607
+ row.appendChild(name);
608
+ const input = document.createElement("input");
609
+ input.type = "color";
610
+ input.className = "c-style-panel__color";
611
+ input.setAttribute("aria-label", labelText(this, "colour-for", "Colour for") + " " + (typeof l === "string" ? l : (l.name || l.label || labelText(this, "layer", "layer"))));
612
+ if (typeof l === "object" && l.color) input.value = l.color;
613
+ row.appendChild(input);
614
+ // classification method selector
615
+ const clsSel = document.createElement("select");
616
+ clsSel.className = "c-style-panel__classification";
617
+ clsSel.setAttribute("aria-label", labelText(this, "classification-method", "Classification method"));
618
+ ["equal","quantile","jenks","manual"].forEach((m) => { const o = document.createElement("option"); o.value = m; o.textContent = m; clsSel.appendChild(o); });
619
+ if (typeof l === "object" && l.classification) clsSel.value = l.classification;
620
+ row.appendChild(clsSel);
621
+ // palette picker
622
+ const pal = document.createElement("input");
623
+ pal.type = "text";
624
+ pal.className = "c-style-panel__palette";
625
+ pal.setAttribute("aria-label", labelText(this, "palette", "Palette"));
626
+ pal.placeholder = "palette";
627
+ if (typeof l === "object" && l.palette) pal.value = l.palette;
628
+ row.appendChild(pal);
629
+ // breaks editor
630
+ const breaks = document.createElement("input");
631
+ breaks.type = "text";
632
+ breaks.className = "c-style-panel__breaks";
633
+ breaks.placeholder = "breaks";
634
+ breaks.setAttribute("aria-label", labelText(this, "class-breaks", "Class breaks"));
635
+ if (typeof l === "object" && l.breaks) breaks.value = (Array.isArray(l.breaks) ? l.breaks.join(",") : l.breaks);
636
+ row.appendChild(breaks);
637
+ // opacity control
638
+ const op = document.createElement("input");
639
+ op.type = "range";
640
+ op.className = "c-style-panel__opacity";
641
+ op.setAttribute("aria-label", labelText(this, "opacity", "Opacity"));
642
+ op.min = "0"; op.max = "100";
643
+ op.value = (typeof l === "object" && l.opacity != null) ? l.opacity : "100";
644
+ row.appendChild(op);
645
+ body.appendChild(row);
646
+ });
647
+ }
648
+ }
649
+ enhance() {
650
+ if (this._wiredStylePanel) return;
651
+ this._wiredStylePanel = true;
652
+ this.addEventListener("change", (e) => {
653
+ const t = e.target;
654
+ if (!t || !this.contains(t)) return;
655
+ const row = t.closest(".c-style-panel__row");
656
+ const name = row ? (row.querySelector(".c-style-panel__name")?.textContent || "") : "";
657
+ if (t.classList.contains("c-style-panel__color")) {
658
+ this.dispatchEvent(new CustomEvent("contour:style-resolved", { detail: { value: t.value, layer: name, property: "color" }, bubbles: true, composed: true }));
659
+ } else if (t.classList.contains("c-style-panel__classification")) {
660
+ this.dispatchEvent(new CustomEvent("contour:break-change", { detail: { value: t.value, layer: name }, bubbles: true, composed: true }));
661
+ } else if (t.classList.contains("c-style-panel__opacity")) {
662
+ const layerId = name ? name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") : "";
663
+ this.dispatchEvent(new CustomEvent("contour:layer-opacity", { detail: { layerId, name, opacity: Number(t.value) }, bubbles: true, composed: true }));
664
+ } else if (t.classList.contains("c-style-panel__property")) {
665
+ this.dispatchEvent(new CustomEvent("contour:style-resolved", { detail: { value: t.value, layer: name, property: "property" }, bubbles: true, composed: true }));
666
+ } else if (t.classList.contains("c-style-panel__n")) {
667
+ this.dispatchEvent(new CustomEvent("contour:style-resolved", { detail: { value: Number(t.value), layer: name, property: "n" }, bubbles: true, composed: true }));
668
+ }
669
+ });
670
+ this.addEventListener("input", (e) => {
671
+ const t = e.target;
672
+ if (!t || !this.contains(t)) return;
673
+ if (t.classList.contains("c-style-panel__color")) t.setAttribute("data-edited", "true");
674
+ if (t.classList.contains("c-style-panel__palette") || t.classList.contains("c-style-panel__breaks")) {
675
+ const row = t.closest(".c-style-panel__row");
676
+ const name = row ? (row.querySelector(".c-style-panel__name")?.textContent || "") : "";
677
+ this.dispatchEvent(new CustomEvent("contour:style-resolved", { detail: { value: t.value, layer: name, property: t.classList.contains("c-style-panel__palette") ? "palette" : "breaks" }, bubbles: true, composed: true }));
678
+ }
679
+ });
680
+ this.addEventListener("click", (e) => {
681
+ const t = e.target && e.target.closest ? e.target.closest("button") : null;
682
+ if (!t || !this.contains(t)) return;
683
+ // Match action, not English label: localized panel went inert comparing textContent to "Apply".
684
+ if (t.dataset.action === "apply") {
685
+ // Carry current control values, not just { applied: true }: consumer needs to know what was applied.
686
+ const body = this.querySelector(".c-style-panel__body");
687
+ const payload = { applied: true };
688
+ if (body) {
689
+ const prop = body.querySelector(".c-style-panel__property");
690
+ const cls = body.querySelector(".c-style-panel__classification");
691
+ const pal = body.querySelector(".c-style-panel__palette");
692
+ const n = body.querySelector(".c-style-panel__n");
693
+ const colors = Array.from(body.querySelectorAll(".c-style-panel__color")).map((c) => c.value);
694
+ if (prop) payload.property = prop.value;
695
+ if (cls) payload.classification = cls.value;
696
+ if (pal) payload.palette = pal.value;
697
+ if (n) payload.n = Number(n.value);
698
+ if (colors.length) payload.colors = colors;
699
+ }
700
+ this.dispatchEvent(new CustomEvent("contour:style-resolved", { detail: payload, bubbles: true, composed: true }));
701
+ } else if (t.dataset.action === "revert") {
702
+ // Revert restores authored defaults; skip the base class value-restore (_contourResetting) and force __rerender__ since editing live controls leaves attributes unchanged.
703
+ const def = this._stylePanelDefault || {};
704
+ this._contourResetting = true;
705
+ try {
706
+ for (const a of ["property", "classification", "palette", "breaks", "opacity", "n", "colors"]) {
707
+ if (def[a] === null || def[a] === undefined) this.removeAttribute(a);
708
+ else this.setAttribute(a, def[a]);
709
+ }
710
+ this.attributeChangedCallback("__rerender__", null, "");
711
+ } finally {
712
+ this._contourResetting = false;
713
+ }
714
+ this.dispatchEvent(new CustomEvent("contour:style-resolved", { detail: { applied: false }, bubbles: true, composed: true }));
715
+ }
716
+ });
717
+ }
718
+ }
719
+ define("contour-style-panel", ContourStylePanel);
720
+ /**
721
+ * Popover: web component wrapper.
722
+ * Attributes: `title`, `content`.
723
+ *
724
+ * Default content area (the light-DOM equivalent of a default `<slot>`): any
725
+ * authored child nodes are moved into `.c-popover__body`, so a consumer can host
726
+ * arbitrary markup — an infowindow's image, a form, a nested component — rather
727
+ * than a single `content` string. The `content` attribute still fills the body
728
+ * when the author supplies no children, and authored children win when both are
729
+ * present. The base class unwraps `.c-popover__body` before dropping generated
730
+ * nodes on re-render (see core.mjs), so authored children survive an attribute
731
+ * change.
732
+ */
733
+ class ContourPopover extends ContourBase {
734
+ static get observed() { return ["title", "content", "anchor"]; }
735
+ static get properties() { return { title: { attribute: "title" }, content: { attribute: "content" } }; }
736
+ render() {
737
+ this.classList.add("c-popover");
738
+ if (!this.hasAttribute("role")) this.setAttribute("role", "dialog");
739
+ if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", labelText(this, "popover", "Popover"));
740
+ if (!this.hasAttribute("popover")) this.setAttribute("popover", "auto");
741
+ if (!this.id) this.id = contourId("popover");
742
+ const title = this.getAttribute("title");
743
+ if (title) {
744
+ const t = this.ensure(".c-popover__title", () => { const el = document.createElement("div"); el.className = "c-popover__title"; return el; });
745
+ t.textContent = title;
746
+ }
747
+ // Collect author's child nodes (base class dropped generated title/body; this is authored light DOM). Whitespace-only text ignored.
748
+ const authored = Array.from(this.childNodes).filter((n) => {
749
+ if (n.nodeType === 1 && n.classList && (n.classList.contains("c-popover__title") || n.classList.contains("c-popover__body"))) return false;
750
+ if (n.nodeType === 3 && !n.textContent.trim()) return false;
751
+ return true;
752
+ });
753
+ const content = this.getAttribute("content");
754
+ if (authored.length) {
755
+ const body = this.ensure(".c-popover__body", () => { const el = document.createElement("div"); el.className = "c-popover__body"; return el; });
756
+ for (const n of authored) body.appendChild(n);
757
+ } else if (content) {
758
+ const body = this.ensure(".c-popover__body", () => { const el = document.createElement("div"); el.className = "c-popover__body"; return el; });
759
+ body.textContent = content;
760
+ }
761
+ this._applyPopoverAnchor();
762
+ }
763
+ // Wire anchor like map-popup from render() so anchor attr changes after upgrade re-anchor. Idempotent: re-render re-applies names; new target releases old.
764
+ _applyPopoverAnchor() {
765
+ const anchorSel = this.getAttribute("anchor");
766
+ const anchorEl = anchorSel ? (anchorSel.startsWith("#") ? document.getElementById(anchorSel.slice(1)) : document.querySelector(anchorSel)) : null;
767
+ if (this._popoverAnchorEl && this._popoverAnchorEl !== anchorEl) {
768
+ this._popoverAnchorEl.style.removeProperty("anchor-name");
769
+ this._popoverAnchorEl.removeAttribute("aria-controls");
770
+ this._popoverAnchorEl.removeAttribute("aria-expanded");
771
+ }
772
+ this._popoverAnchorEl = anchorEl || null;
773
+ if (anchorEl) {
774
+ const anchorName = "--popover-" + this.id;
775
+ anchorEl.style.setProperty("anchor-name", anchorName);
776
+ this.style.setProperty("position-anchor", anchorName);
777
+ anchorEl.setAttribute("aria-controls", this.id);
778
+ if (!this._popoverAnchorDispose) {
779
+ this._popoverAnchorDispose = true;
780
+ onDispose(this, () => {
781
+ if (this._popoverAnchorEl) this._popoverAnchorEl.style.removeProperty("anchor-name");
782
+ this._popoverAnchorDispose = false;
783
+ });
784
+ }
785
+ } else {
786
+ this.style.removeProperty("position-anchor");
787
+ }
788
+ }
789
+ // Reflect popover open state on anchor trigger: announces control (aria-controls) and expansion (aria-expanded). No-op without anchor.
790
+ _setPopoverTriggerExpanded(open) {
791
+ const trg = this._popoverAnchorEl;
792
+ if (!trg) return;
793
+ trg.setAttribute("aria-controls", this.id);
794
+ trg.setAttribute("aria-expanded", String(!!open));
795
+ }
796
+ enhance() {
797
+ if (this._wiredPopover) return;
798
+ this._wiredPopover = true;
799
+ const close = () => { try { this.hidePopover(); } catch (e) {} this.classList.remove("is-open"); wcEmitOverlay(this, "contour:close", {}); this._setPopoverTriggerExpanded(false); };
800
+ const keyHandler = (e) => {
801
+ const open = this.classList.contains("is-open") || this.matches(":popover-open");
802
+ if (!open) return;
803
+ if (e.key === "Escape") { e.preventDefault(); close(); return; }
804
+ // Focus trap: keep Tab within the popover.
805
+ if (e.key === "Tab") {
806
+ const focusable = Array.from(this.querySelectorAll('a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'));
807
+ if (!focusable.length) { e.preventDefault(); return; }
808
+ const first = focusable[0];
809
+ const last = focusable[focusable.length - 1];
810
+ if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); }
811
+ else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); }
812
+ }
813
+ };
814
+ document.addEventListener("keydown", keyHandler);
815
+ onDispose(this, () => document.removeEventListener("keydown", keyHandler));
816
+ // Focus first focusable on open, emit contour:open (+ deprecated contour:show), reflect open state on anchor.
817
+ const showHandler = () => {
818
+ this.classList.add("is-open");
819
+ wcEmitOverlay(this, "contour:open", {});
820
+ const focusable = this.querySelector('a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])');
821
+ if (focusable) { try { focusable.focus(); } catch (e) {} }
822
+ else { try { this.focus(); } catch (e) {} }
823
+ this._setPopoverTriggerExpanded(true);
824
+ };
825
+ this.addEventListener("beforetoggle", (e) => {
826
+ if (e.newState === "open") showHandler();
827
+ });
828
+ // Anchor wiring in render() (_applyPopoverAnchor) so post-upgrade anchor change re-anchors — enhance() is one-shot.
829
+ // Nested popover support: mark parent on nested open. Listen to contour:open and deprecated contour:show.
830
+ const markParent = (e) => { if (e.target && e.target !== this && this.contains(e.target)) this.classList.add("is-parent"); };
831
+ const unmarkParent = (e) => { if (e.target && e.target !== this) this.classList.remove("is-parent"); };
832
+ this.addEventListener("contour:open", markParent);
833
+ this.addEventListener("contour:show", markParent);
834
+ this.addEventListener("contour:close", unmarkParent);
835
+ this.addEventListener("click", (e) => {
836
+ const btn = e.target && e.target.closest ? e.target.closest(".c-popover__close") : null;
837
+ if (btn && this.contains(btn)) close();
838
+ });
839
+ }
840
+ }
841
+ define("contour-popover", ContourPopover);
842
+ /**
843
+ * Hover Card: web component wrapper.
844
+ * Attributes: `title`, `content`, `anchor`.
845
+ *
846
+ * Default content area (the light-DOM equivalent of a default `<slot>`): the
847
+ * author's own child nodes are moved into `.c-hover-card__body`, so a hover card
848
+ * can host an image, a table or a nested component rather than a single
849
+ * `content` string. The `content` attribute still fills the body when the author
850
+ * supplies no children; authored children win when both exist. The base class
851
+ * unwraps `.c-hover-card__body` before dropping generated nodes on re-render (see
852
+ * core.mjs), so authored children survive an attribute change.
853
+ */
854
+ class ContourHoverCard extends ContourBase {
855
+ static get observed() { return ["title", "content", "anchor"]; }
856
+ static get properties() { return { title: { attribute: "title" }, content: { attribute: "content" }, anchor: { attribute: "anchor" } }; }
857
+ render() {
858
+ this.classList.add("c-hover-card");
859
+ if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", labelText(this, "hover-card", "Hover card"));
860
+ if (!this.hasAttribute("popover")) this.setAttribute("popover", "auto");
861
+ if (!this.id) this.id = contourId("hover-card");
862
+ const title = this.getAttribute("title");
863
+ if (title) {
864
+ const t = this.ensure(".c-hover-card__title", () => { const el = document.createElement("div"); el.className = "c-hover-card__title"; return el; });
865
+ t.textContent = title;
866
+ }
867
+ const authored = Array.from(this.childNodes).filter((n) => {
868
+ if (n.nodeType === 1 && n.classList && (n.classList.contains("c-hover-card__title") || n.classList.contains("c-hover-card__body"))) return false;
869
+ if (n.nodeType === 3 && !n.textContent.trim()) return false;
870
+ return true;
871
+ });
872
+ const content = this.getAttribute("content");
873
+ if (authored.length) {
874
+ const c = this.ensure(".c-hover-card__body", () => { const el = document.createElement("div"); el.className = "c-hover-card__body"; return el; });
875
+ for (const n of authored) c.appendChild(n);
876
+ } else if (content) {
877
+ const c = this.ensure(".c-hover-card__body", () => { const el = document.createElement("div"); el.className = "c-hover-card__body"; return el; });
878
+ c.textContent = content;
879
+ }
880
+ // Role fit: interactive content inside a tooltip is unreachable for some
881
+ // screen readers; use dialog when the card contains focusable controls.
882
+ if (!this.hasAttribute("role")) {
883
+ const hasInteractive = !!this.querySelector("a[href], button, input, select, textarea");
884
+ this.setAttribute("role", hasInteractive ? "dialog" : "tooltip");
885
+ }
886
+ if (typeof this._wireHoverAnchor === "function") this._wireHoverAnchor();
887
+ }
888
+ enhance() {
889
+ if (this._wiredHoverCard) return;
890
+ this._wiredHoverCard = true;
891
+ let showTimer = null;
892
+ let hideTimer = null;
893
+ const hasPopover = typeof this.showPopover === "function";
894
+ const cancelTimers = () => {
895
+ if (showTimer) { clearTimeout(showTimer); showTimer = null; }
896
+ if (hideTimer) { clearTimeout(hideTimer); hideTimer = null; }
897
+ };
898
+ const show = (trigger) => {
899
+ cancelTimers();
900
+ if (hasPopover) {
901
+ try { this.showPopover(trigger ? { source: trigger } : undefined); }
902
+ catch (e) { try { this.showPopover(); } catch (e2) {} }
903
+ } else this.classList.add("is-open");
904
+ this.dispatchEvent(new CustomEvent("contour:open", { bubbles: true, composed: true }));
905
+ };
906
+ const hide = () => {
907
+ cancelTimers();
908
+ if (hasPopover) { try { this.hidePopover(); } catch (e) {} }
909
+ else this.classList.remove("is-open");
910
+ this.dispatchEvent(new CustomEvent("contour:close", { bubbles: true, composed: true }));
911
+ };
912
+ const scheduleHide = () => {
913
+ if (hideTimer) clearTimeout(hideTimer);
914
+ hideTimer = setTimeout(hide, 300);
915
+ };
916
+ // WCAG 1.4.13: hoverable — moving the pointer onto the card keeps it open.
917
+ this.addEventListener("mouseenter", cancelTimers);
918
+ this.addEventListener("mouseleave", scheduleHide);
919
+ this.addEventListener("focusin", cancelTimers);
920
+ this.addEventListener("focusout", (e) => {
921
+ if (!this.contains(e.relatedTarget)) scheduleHide();
922
+ });
923
+ this._wireHoverAnchor = () => {
924
+ const anchorSel = this.getAttribute("anchor") || "";
925
+ if (this._hoverAnchorSel === anchorSel && this._hoverAnchorCleanup) return;
926
+ if (this._hoverAnchorCleanup) { this._hoverAnchorCleanup(); this._hoverAnchorCleanup = null; }
927
+ this._hoverAnchorSel = anchorSel;
928
+ if (!anchorSel) return;
929
+ const trigger = anchorSel.startsWith("#") ? document.getElementById(anchorSel.slice(1)) : document.querySelector(anchorSel);
930
+ if (!trigger) return;
931
+ trigger.setAttribute("popovertarget", this.id);
932
+ trigger.setAttribute("aria-describedby", this.id);
933
+ const anchorName = "--hover-card-" + this.id;
934
+ trigger.style.setProperty("anchor-name", anchorName);
935
+ this.style.setProperty("position-anchor", anchorName);
936
+ const onEnter = () => { cancelTimers(); showTimer = setTimeout(() => show(trigger), 300); };
937
+ const onLeave = () => { scheduleHide(); };
938
+ const onFocus = () => show(trigger);
939
+ const onBlur = () => { scheduleHide(); };
940
+ const onClick = (e) => { if (e && e.preventDefault) e.preventDefault(); };
941
+ trigger.addEventListener("mouseenter", onEnter);
942
+ trigger.addEventListener("mouseleave", onLeave);
943
+ trigger.addEventListener("focus", onFocus);
944
+ trigger.addEventListener("blur", onBlur);
945
+ trigger.addEventListener("click", onClick);
946
+ this._hoverAnchorCleanup = () => {
947
+ trigger.removeEventListener("mouseenter", onEnter);
948
+ trigger.removeEventListener("mouseleave", onLeave);
949
+ trigger.removeEventListener("focus", onFocus);
950
+ trigger.removeEventListener("blur", onBlur);
951
+ trigger.removeEventListener("click", onClick);
952
+ trigger.style.removeProperty("anchor-name");
953
+ if (trigger.getAttribute("aria-describedby") === this.id) trigger.removeAttribute("aria-describedby");
954
+ };
955
+ };
956
+ this._wireHoverAnchor();
957
+ const keyHandler = (e) => { if (e.key === "Escape" && (this.classList.contains("is-open") || this.matches(":popover-open"))) { e.preventDefault(); hide(); } };
958
+ document.addEventListener("keydown", keyHandler);
959
+ onDispose(this, () => {
960
+ cancelTimers();
961
+ if (this._hoverAnchorCleanup) { this._hoverAnchorCleanup(); this._hoverAnchorCleanup = null; }
962
+ this._hoverAnchorSel = null;
963
+ document.removeEventListener("keydown", keyHandler);
964
+ });
965
+ }
966
+ }
967
+ define("contour-hover-card", ContourHoverCard);
968
+
969
+ /**
970
+ * Context Menu: web component wrapper.
971
+ * Attributes: `items`.
972
+ */
973
+ class ContourContextMenu extends ContourBase {
974
+ static get observed() { return ["items", "trigger"]; }
975
+ static get properties() { return { items: { attribute: "items" }, trigger: { attribute: "trigger" } }; }
976
+ render() {
977
+ this.classList.add("c-context-menu");
978
+ if (!this.hasAttribute("role")) this.setAttribute("role", "menu");
979
+ if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", labelText(this, "context-menu", "Context menu"));
980
+ if (!this.hasAttribute("popover")) this.setAttribute("popover", "auto");
981
+ if (!this.id) this.id = contourId("context-menu");
982
+ const items = this.getAttribute("items");
983
+ if (items) {
984
+ try {
985
+ const parsed = JSON.parse(items);
986
+ Array.from(this.querySelectorAll("[data-contour-generated='1']")).forEach((n) => n.remove());
987
+ parsed.forEach((it) => {
988
+ const btn = document.createElement("button");
989
+ btn.className = "c-menu__item";
990
+ btn.type = "button";
991
+ btn.setAttribute("role", "menuitem");
992
+ btn.dataset.contourGenerated = "1";
993
+ btn.textContent = typeof it === "string" ? it : (it.label || it.title || "");
994
+ this.appendChild(btn);
995
+ });
996
+ } catch (e) { warnBadJson(this, "attribute", e); }
997
+ }
998
+ }
999
+ enhance() {
1000
+ if (this._wiredContextMenu) return;
1001
+ this._wiredContextMenu = true;
1002
+ const closeMenu = (restoreFocus) => {
1003
+ if (typeof this.hidePopover === "function") { try { this.hidePopover(); } catch (err) {} }
1004
+ this.classList.remove("is-open");
1005
+ if (restoreFocus && this._ctxOpener && this._ctxOpener.isConnected && typeof this._ctxOpener.focus === "function") {
1006
+ try { this._ctxOpener.focus(); } catch (err) {}
1007
+ }
1008
+ this._ctxOpener = null;
1009
+ };
1010
+ const openAt = (x, y) => {
1011
+ this._ctxOpener = document.activeElement;
1012
+ if (typeof this.showPopover === "function") { try { this.showPopover(); } catch (err) {} }
1013
+ else this.classList.add("is-open");
1014
+ if (Number.isFinite(x) && Number.isFinite(y)) {
1015
+ this.style.position = "fixed";
1016
+ this.style.left = x + "px";
1017
+ this.style.top = y + "px";
1018
+ const rect = this.getBoundingClientRect();
1019
+ const vw = window.innerWidth || document.documentElement.clientWidth;
1020
+ const vh = window.innerHeight || document.documentElement.clientHeight;
1021
+ const cx = Math.max(8, Math.min(x, Math.max(8, vw - rect.width - 8)));
1022
+ const cy = Math.max(8, Math.min(y, Math.max(8, vh - rect.height - 8)));
1023
+ this.style.left = cx + "px";
1024
+ this.style.top = cy + "px";
1025
+ }
1026
+ const first = this.querySelector(".c-menu__item:not([disabled])");
1027
+ if (first && typeof first.focus === "function") {
1028
+ try { first.focus(); } catch (err) {}
1029
+ }
1030
+ };
1031
+ // A context menu is opened by its trigger, not by a right-click on the
1032
+ // menu itself (which is empty until opened). `trigger` names the element.
1033
+ const triggerSel = this.getAttribute("trigger");
1034
+ if (triggerSel) {
1035
+ const trigger = document.querySelector(triggerSel);
1036
+ if (trigger) {
1037
+ if (!trigger.hasAttribute("aria-haspopup")) trigger.setAttribute("aria-haspopup", "menu");
1038
+ const onCtx = (e) => { e.preventDefault(); openAt(e.clientX, e.clientY); };
1039
+ trigger.addEventListener("contextmenu", onCtx);
1040
+ onDispose(this, () => trigger.removeEventListener("contextmenu", onCtx));
1041
+ }
1042
+ }
1043
+ this.addEventListener("contextmenu", (e) => {
1044
+ e.preventDefault();
1045
+ openAt(e.clientX, e.clientY);
1046
+ });
1047
+ this.addEventListener("click", (e) => {
1048
+ const item = e.target && e.target.closest ? e.target.closest(".c-menu__item") : null;
1049
+ if (item && this.contains(item)) {
1050
+ this.dispatchEvent(new CustomEvent("contour:select", { bubbles: true, composed: true, detail: { label: item.textContent.trim() } }));
1051
+ closeMenu(false);
1052
+ }
1053
+ });
1054
+ this.addEventListener("keydown", (e) => {
1055
+ const items = Array.from(this.querySelectorAll(".c-menu__item"));
1056
+ navKey(e, items, { wrap: true, onKey: () => { if (e.key === "Enter" || e.key === " ") document.activeElement.click(); } });
1057
+ if (e.key === "Escape") { e.preventDefault(); closeMenu(true); }
1058
+ });
1059
+ }
1060
+ }
1061
+ define("contour-context-menu", ContourContextMenu);
1062
+
1063
+ /**
1064
+ * Fab: web component wrapper.
1065
+ * Attributes: `label`, `icon`.
1066
+ */
1067
+ class ContourFab extends ContourBase {
1068
+ static get observed() { return ["label", "icon"]; }
1069
+ static get properties() { return { label: { attribute: "label" }, icon: { attribute: "icon" } }; }
1070
+ render() {
1071
+ this.classList.add("c-fab");
1072
+ if (!this.hasAttribute("role")) this.setAttribute("role", "button");
1073
+ if (!this.hasAttribute("tabindex")) this.setAttribute("tabindex", "0");
1074
+ const label = this.getAttribute("label");
1075
+ if (label) this.setAttribute("aria-label", label);
1076
+ const icon = this.getAttribute("icon");
1077
+ if (icon && !this.querySelector("svg")) {
1078
+ // Render the icon through the sprite, not as text: an <svg><use> paints
1079
+ // the glyph; the old code wrote the icon name into the button.
1080
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
1081
+ svg.setAttribute("aria-hidden", "true");
1082
+ svg.setAttribute("viewBox", "0 0 24 24");
1083
+ const use = document.createElementNS("http://www.w3.org/2000/svg", "use");
1084
+ use.setAttribute("href", icon.startsWith("#") ? icon : "#" + icon);
1085
+ svg.appendChild(use);
1086
+ this.appendChild(svg);
1087
+ }
1088
+ }
1089
+ enhance() {
1090
+ if (this._wiredFab) return;
1091
+ this._wiredFab = true;
1092
+ this.addEventListener("click", () => {
1093
+ this.dispatchEvent(new CustomEvent("contour:activate", { bubbles: true, composed: true }));
1094
+ });
1095
+ this.addEventListener("keydown", (e) => {
1096
+ if (e.key === "Enter" || e.key === " ") { e.preventDefault(); this.dispatchEvent(new CustomEvent("contour:activate", { bubbles: true, composed: true })); }
1097
+ });
1098
+ }
1099
+ }
1100
+ define("contour-fab", ContourFab);
1101
+ /**
1102
+ * Command Palette: web component wrapper.
1103
+ * Attributes: `open`, `items`.
1104
+ */
1105
+ class ContourCommandPalette extends ContourBase {
1106
+ static get observed() { return ["open", "items"]; }
1107
+ static get properties() { return { open: { attribute: "open", type: Boolean }, items: { attribute: "items" } }; }
1108
+ attributeChangedCallback(name, oldValue, newValue) {
1109
+ super.attributeChangedCallback(name, oldValue, newValue);
1110
+ // The open attribute is a first-class input (R20 T-18): setting it opens
1111
+ // the palette, removing it closes. It used to be observed but ignored.
1112
+ if (name === "open" && this.isConnected && oldValue !== newValue) {
1113
+ if (newValue !== null) this.show(); else this.hide();
1114
+ }
1115
+ }
1116
+ _recentsKey() { return "contour-command-palette:" + (this.id || "default") + ":recents"; }
1117
+ _recents() {
1118
+ try { const r = JSON.parse(localStorage.getItem(this._recentsKey()) || "[]"); return Array.isArray(r) ? r : []; } catch (e) { warnBadJson(this, "recents", e); return []; }
1119
+ }
1120
+ _remember(label) {
1121
+ try {
1122
+ const r = this._recents().filter((l) => l !== label);
1123
+ r.unshift(label);
1124
+ localStorage.setItem(this._recentsKey(), JSON.stringify(r.slice(0, 5)));
1125
+ } catch (e) { /* storage is best-effort */ }
1126
+ }
1127
+ _cmdRow(label, index, recent) {
1128
+ const row = document.createElement("div");
1129
+ row.className = "c-command-palette__item";
1130
+ row.setAttribute("role", "option");
1131
+ row.id = this.id + "-opt-" + (recent ? "r" : "") + index;
1132
+ row.textContent = label;
1133
+ row.dataset.index = String(index);
1134
+ row.dataset.label = label;
1135
+ if (recent) row.dataset.recent = "true";
1136
+ return row;
1137
+ }
1138
+ render() {
1139
+ this.classList.add("c-command-palette");
1140
+ if (!this.hasAttribute("role")) this.setAttribute("role", "dialog");
1141
+ if (!this.hasAttribute("aria-modal")) this.setAttribute("aria-modal", "true");
1142
+ if (!this.hasAttribute("aria-label") && !this.hasAttribute("aria-labelledby")) {
1143
+ this.setAttribute("aria-label", labelText(this, "command-palette", "Command palette"));
1144
+ }
1145
+ if (!this.hasAttribute("popover")) this.setAttribute("popover", "auto");
1146
+ if (!this.id) this.id = contourId("cmd-palette");
1147
+ // R24-38: use div.c-command__input > input.c-command__field structure
1148
+ // so the CSS border/bg/focus-ring applies.
1149
+ const inputWrap = this.ensure(".c-command__input", () => { const el = document.createElement("div"); el.className = "c-command__input"; return el; });
1150
+ const search = this.ensure(".c-command__field", () => { const el = document.createElement("input"); el.className = "c-command__field"; el.type = "search"; el.placeholder = labelText(this, "search", "Search…"); el.setAttribute("aria-label", labelText(this, "search-commands", "Search commands")); el.setAttribute("role", "combobox"); el.setAttribute("aria-autocomplete", "list"); el.setAttribute("aria-expanded", "false"); return el; });
1151
+ const isOpen = this.hasAttribute("open") || this.classList.contains("is-open") || (typeof this.matches === "function" && this.matches(":popover-open"));
1152
+ search.setAttribute("aria-expanded", String(isOpen));
1153
+ if (!inputWrap.contains(search)) inputWrap.appendChild(search);
1154
+ const list = this.ensure(".c-command-palette__list", () => { const el = document.createElement("div"); el.className = "c-command-palette__list"; el.setAttribute("role", "listbox"); return el; });
1155
+ if (!list.hasAttribute("aria-label") && !list.hasAttribute("aria-labelledby")) {
1156
+ list.setAttribute("aria-label", labelText(this, "commands", "Commands"));
1157
+ }
1158
+ if (!list.id) list.id = this.id + "-list";
1159
+ search.setAttribute("aria-controls", list.id);
1160
+ let parsed = [];
1161
+ const items = this.getAttribute("items");
1162
+ if (items) { try { parsed = JSON.parse(items); } catch (e) { warnBadJson(this, "items", e); } }
1163
+ if (!Array.isArray(parsed)) parsed = [];
1164
+ const labelOf = (it) => typeof it === "string" ? it : (it.label || it.title || "");
1165
+ setHTML(list, "");
1166
+ // Recents: the last committed labels, shown while the query is empty.
1167
+ const q = (this._cmdQuery || "").toLowerCase().trim();
1168
+ const recents = q ? [] : this._recents().filter((l) => parsed.some((it) => labelOf(it) === l));
1169
+ if (recents.length) {
1170
+ const g = document.createElement("div");
1171
+ g.className = "c-command-palette__group";
1172
+ g.setAttribute("role", "presentation");
1173
+ g.textContent = labelText(this, "recent", "Recent");
1174
+ list.appendChild(g);
1175
+ recents.forEach((label) => list.appendChild(this._cmdRow(label, parsed.findIndex((it) => labelOf(it) === label), true)));
1176
+ }
1177
+ // Group headers for items that carry {group}; presentation rows, not
1178
+ // options, so the cursor and the filter skip them.
1179
+ let lastGroup = null;
1180
+ parsed.forEach((it, i) => {
1181
+ const g = typeof it === "object" && it !== null ? (it.group || null) : null;
1182
+ if (g && g !== lastGroup) {
1183
+ const h = document.createElement("div");
1184
+ h.className = "c-command-palette__group";
1185
+ h.setAttribute("role", "presentation");
1186
+ h.textContent = g;
1187
+ list.appendChild(h);
1188
+ lastGroup = g;
1189
+ }
1190
+ list.appendChild(this._cmdRow(labelOf(it), i, false));
1191
+ });
1192
+ // The query survives re-render: the search node is rebuilt, the host
1193
+ // remembers what was typed and re-applies the filter.
1194
+ if (this._cmdQuery) search.value = this._cmdQuery;
1195
+ if (this._cmdFilter) this._cmdFilter(this._cmdQuery || "");
1196
+ else if (this._cmdSetActive) this._cmdSetActive(this._cmdCursor || 0);
1197
+ }
1198
+ show() {
1199
+ if (this.classList.contains("is-open") || this.matches(":popover-open")) {
1200
+ if (!this.hasAttribute("open")) this.setAttribute("open", "");
1201
+ return;
1202
+ }
1203
+ if (typeof this.showPopover === "function") { try { this.showPopover(); } catch (e) {} }
1204
+ this.classList.add("is-open");
1205
+ modalContain(this, true);
1206
+ const s = this.querySelector(".c-command__field");
1207
+ if (s) { s.setAttribute("aria-expanded", "true"); try { s.focus(); } catch (e) {} }
1208
+ if (!this.hasAttribute("open")) this.setAttribute("open", "");
1209
+ }
1210
+ hide() {
1211
+ const isOpen = this.classList.contains("is-open") || this.matches(":popover-open");
1212
+ if (!isOpen) { if (this.hasAttribute("open")) this.removeAttribute("open"); return; }
1213
+ if (typeof this.hidePopover === "function") { try { this.hidePopover(); } catch (e) {} }
1214
+ this.classList.remove("is-open");
1215
+ modalContain(this, false);
1216
+ const s = this.querySelector(".c-command__field");
1217
+ if (s) s.setAttribute("aria-expanded", "false");
1218
+ if (this.hasAttribute("open")) this.removeAttribute("open");
1219
+ }
1220
+ toggle() { if (this.classList.contains("is-open") || this.matches(":popover-open")) this.hide(); else this.show(); }
1221
+ enhance() {
1222
+ // Rows are rebuilt on every render: re-apply the cursor before the
1223
+ // one-shot wiring guard.
1224
+ if (this._cmdSetActive) this._cmdSetActive(this._cmdCursor || 0);
1225
+ if (this._wiredCmdPalette) return;
1226
+ this._wiredCmdPalette = true;
1227
+ const search = () => this.querySelector(".c-command__field");
1228
+ const list = () => this.querySelector(".c-command-palette__list");
1229
+ const items = () => Array.from(this.querySelectorAll(".c-command-palette__item"));
1230
+ const visibleItems = () => items().filter((it) => !it.hidden);
1231
+ // A cursor, not the focus position: items are not focusable, so the cursor is the single source of truth for the active option.
1232
+ const setActive = (idx) => {
1233
+ const vis = visibleItems();
1234
+ if (!vis.length) { this._cmdCursor = 0; return; }
1235
+ this._cmdCursor = Math.max(0, Math.min(idx, vis.length - 1));
1236
+ vis.forEach((it, i) => it.classList.toggle("is-active", i === this._cmdCursor));
1237
+ const active = vis[this._cmdCursor];
1238
+ const s = search();
1239
+ if (s && active) s.setAttribute("aria-activedescendant", active.id);
1240
+ else if (s) s.removeAttribute("aria-activedescendant");
1241
+ };
1242
+ const filter = (q) => {
1243
+ const needle = (q || "").toLowerCase().trim();
1244
+ items().forEach((it) => {
1245
+ const label = (it.dataset.label || it.textContent || "").toLowerCase();
1246
+ it.hidden = needle !== "" && !label.includes(needle);
1247
+ });
1248
+ // A group header stays only while it has a visible item before the
1249
+ // next header.
1250
+ Array.from(list().querySelectorAll(".c-command-palette__group")).forEach((g) => {
1251
+ let n = g.nextElementSibling, any = false;
1252
+ while (n && !n.classList.contains("c-command-palette__group")) {
1253
+ if (!n.hidden && n.classList.contains("c-command-palette__item")) any = true;
1254
+ n = n.nextElementSibling;
1255
+ }
1256
+ g.hidden = !any;
1257
+ });
1258
+ // No-results is a state, not an empty box.
1259
+ let empty = list().querySelector(".c-command-palette__empty");
1260
+ const anyVisible = items().some((it) => !it.hidden);
1261
+ if (!anyVisible) {
1262
+ if (!empty) { empty = document.createElement("div"); empty.className = "c-command-palette__empty"; empty.setAttribute("role", "status"); list().appendChild(empty); }
1263
+ empty.textContent = labelText(this, "no-matches", "No matches");
1264
+ empty.hidden = false;
1265
+ } else if (empty) empty.hidden = true;
1266
+ setActive(0);
1267
+ };
1268
+ const select = (it) => {
1269
+ if (!it) return;
1270
+ const detail = { index: Number(it.dataset.index), label: it.dataset.label || it.textContent };
1271
+ // The return value: the last committed command, readable without
1272
+ // listening for the event (R20 T-18).
1273
+ this.value = detail;
1274
+ this._remember(detail.label);
1275
+ this.dispatchEvent(new CustomEvent("contour:select", { bubbles: true, composed: true, detail }));
1276
+ this.hide();
1277
+ };
1278
+ this._cmdSetActive = setActive;
1279
+ this._cmdFilter = filter;
1280
+ // Delegated on the host: the search node is rebuilt every render, so node-bound listeners would die.
1281
+ this.addEventListener("input", (e) => {
1282
+ const s = search();
1283
+ if (!s || e.target !== s) return;
1284
+ this._cmdQuery = s.value;
1285
+ filter(s.value);
1286
+ });
1287
+ this.addEventListener("keydown", (e) => {
1288
+ const s = search();
1289
+ if (!s || e.target !== s) return;
1290
+ const vis = visibleItems();
1291
+ if (e.key === "Escape") { e.preventDefault(); this.hide(); return; }
1292
+ if (!vis.length) return;
1293
+ if (e.key === "ArrowDown") { e.preventDefault(); setActive(this._cmdCursor + 1); }
1294
+ else if (e.key === "ArrowUp") { e.preventDefault(); setActive(this._cmdCursor - 1); }
1295
+ else if (e.key === "Home") { e.preventDefault(); setActive(0); }
1296
+ else if (e.key === "End") { e.preventDefault(); setActive(vis.length - 1); }
1297
+ else if (e.key === "Enter") { e.preventDefault(); select(vis[this._cmdCursor]); }
1298
+ });
1299
+ this.addEventListener("click", (e) => {
1300
+ const it = e.target && e.target.closest ? e.target.closest(".c-command-palette__item") : null;
1301
+ if (it && this.contains(it) && !it.hidden) select(it);
1302
+ });
1303
+ // focus return on close
1304
+ let lastFocus = null;
1305
+ this.addEventListener("beforetoggle", (e) => {
1306
+ if (e.newState === "open") {
1307
+ lastFocus = document.activeElement;
1308
+ this.dispatchEvent(new CustomEvent("contour:open", { bubbles: true, composed: true }));
1309
+ } else {
1310
+ // Release containment when the popover closes via the platform (Escape, light-dismiss), not only via hide().
1311
+ modalContain(this, false);
1312
+ if (lastFocus && typeof lastFocus.focus === "function") {
1313
+ this.dispatchEvent(new CustomEvent("contour:close", { bubbles: true, composed: true }));
1314
+ try { lastFocus.focus(); } catch (err) {}
1315
+ }
1316
+ }
1317
+ });
1318
+ // R24-112: release containment if the palette is removed while open.
1319
+ onDispose(this, () => { if (this.classList.contains("is-open") || this.matches(":popover-open")) modalContain(this, false); });
1320
+ setActive(0);
1321
+ }
1322
+ }
1323
+ define("contour-command-palette", ContourCommandPalette);
1324
+ /**
1325
+ * Side Sheet: web component wrapper.
1326
+ * Attributes: `open`, `side`, `title`.
1327
+ */
1328
+ class ContourSideSheet extends ContourBase {
1329
+ static get observed() { return ["open", "side", "title", "mode", "size"]; }
1330
+ static get properties() { return { open: { attribute: "open", type: Boolean }, side: { attribute: "side" }, title: { attribute: "title" }, mode: { attribute: "mode" }, size: { attribute: "size" } }; }
1331
+ // Dialog-family parity: the open attribute is the state; these methods route through it so the observer owns events, containment and focus.
1332
+ show() { this.setAttribute("open", ""); }
1333
+ // showModal is an action, not a state write (native dialog semantics):
1334
+ // it forces the modal path for this opening without touching `mode`.
1335
+ showModal() { this._sheetForceModal = true; this.show(); }
1336
+ close() { this.removeAttribute("open"); }
1337
+ get open() { return this.hasAttribute("open"); }
1338
+ set open(v) { if (v) this.show(); else this.close(); }
1339
+ _modal() { return this._sheetForceModal || (this.getAttribute("mode") || "modal") !== "non-modal"; }
1340
+ render() {
1341
+ this.classList.add("c-side-sheet");
1342
+ this.setAttribute("role", "dialog");
1343
+ // Non-modal mode keeps the background live: no aria-modal, no
1344
+ // containment, no focus trap (R20 T-19).
1345
+ if (this._modal()) this.setAttribute("aria-modal", "true");
1346
+ else this.removeAttribute("aria-modal");
1347
+ if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", this.getAttribute("title") || "Side sheet");
1348
+ // Strip stale modifiers before projecting the current ones: changing
1349
+ // `side` or `size` used to accumulate classes.
1350
+ for (const c of Array.from(this.classList)) {
1351
+ if (/^c-side-sheet--(start|end|sm|md|lg)$/.test(c)) this.classList.remove(c);
1352
+ }
1353
+ const side = this.getAttribute("side") === "start" ? "start" : "end";
1354
+ this.classList.add("c-side-sheet--" + side);
1355
+ const size = this.getAttribute("size");
1356
+ if (size === "sm" || size === "md" || size === "lg") this.classList.add("c-side-sheet--" + size);
1357
+ if (this.hasAttribute("open")) { this.classList.add("is-open"); this.removeAttribute("inert"); }
1358
+ else { this.classList.remove("is-open"); // R24-119: inert when closed so content is not focusable offscreen.
1359
+ this.setAttribute("inert", ""); }
1360
+ const title = this.getAttribute("title");
1361
+ if (title) {
1362
+ const t = this.ensure(".c-side-sheet__title", () => { const el = document.createElement("div"); el.className = "c-side-sheet__title"; return el; });
1363
+ t.textContent = title;
1364
+ }
1365
+ }
1366
+ enhance() {
1367
+ if (this._wiredSideSheet) return;
1368
+ this._wiredSideSheet = true;
1369
+ let lastFocus = null;
1370
+ const keyHandler = (e) => {
1371
+ if (e.key === "Escape" && this.hasAttribute("open")) { e.preventDefault(); this.close(); return; }
1372
+ // The focus trap is modal-only; a non-modal sheet shares the page.
1373
+ if (e.key === "Tab" && this.hasAttribute("open") && this._modal()) {
1374
+ const focusables = Array.from(this.querySelectorAll("button, [href], input, select, textarea, [tabindex]:not([tabindex='-1'])")).filter((el) => !el.hasAttribute("disabled"));
1375
+ if (!focusables.length) { e.preventDefault(); return; }
1376
+ const first = focusables[0], last = focusables[focusables.length - 1];
1377
+ if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); }
1378
+ else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); }
1379
+ }
1380
+ };
1381
+ document.addEventListener("keydown", keyHandler);
1382
+ onDispose(this, () => document.removeEventListener("keydown", keyHandler));
1383
+ this.addEventListener("click", (e) => {
1384
+ if (e.target === this) this.close();
1385
+ });
1386
+ // Focus management: capture focus on open, restore on close; the transition guard decides whether a close happened.
1387
+ let wasOpen = false;
1388
+ const transition = () => {
1389
+ const isOpen = this.hasAttribute("open");
1390
+ if (isOpen && !wasOpen) {
1391
+ wasOpen = true;
1392
+ lastFocus = document.activeElement;
1393
+ if (this._modal()) modalContain(this, true);
1394
+ const first = this.querySelector("button, [href], input, select, textarea, [tabindex]:not([tabindex='-1'])");
1395
+ if (first) try { first.focus(); } catch (err) {}
1396
+ this.dispatchEvent(new CustomEvent("contour:open", { bubbles: true, composed: true }));
1397
+ } else if (!isOpen && wasOpen) {
1398
+ wasOpen = false;
1399
+ if (this._modal()) modalContain(this, false);
1400
+ this._sheetForceModal = false;
1401
+ if (lastFocus && typeof lastFocus.focus === "function") try { lastFocus.focus(); } catch (err) {}
1402
+ lastFocus = null;
1403
+ this.dispatchEvent(new CustomEvent("contour:close", { bubbles: true, composed: true }));
1404
+ }
1405
+ };
1406
+ const obs = new MutationObserver(transition);
1407
+ obs.observe(this, { attributes: true, attributeFilter: ["open"] });
1408
+ if (this.hasAttribute("open")) transition();
1409
+ onDispose(this, () => { obs.disconnect(); if (this.hasAttribute("open") && this._modal()) modalContain(this, false); });
1410
+ }
1411
+ }
1412
+ define("contour-side-sheet", ContourSideSheet);
1413
+
1414
+ /**
1415
+ * Tour: a coach-mark walkthrough. Attributes: `steps` (JSON of strings or
1416
+ * {text, target}), `current`, `open`, and `persist` (with an `id`) to remember
1417
+ * completion in localStorage. The tour advances `current` itself (R20 T-20) —
1418
+ * clicks used to emit events only, so without a consumer round-trip the tour
1419
+ * never moved. ArrowLeft/Right step, Escape closes (only while open), the tip
1420
+ * positions near its step's target, and a step counter names the place.
1421
+ * Non-modal by design: the page behind stays live. The tip is a polite live
1422
+ * region; each step scrolls its target into view and focuses it when it is
1423
+ * focusable, otherwise the Next/Done button (UG-12).
1424
+ */
1425
+ class ContourTour extends ContourBase {
1426
+ static get observed() { return ["steps", "current", "open", "persist", "id"]; }
1427
+ static get properties() { return { steps: { attribute: "steps" }, current: { attribute: "current" }, open: { attribute: "open", type: Boolean }, persist: { attribute: "persist", type: Boolean } }; }
1428
+ _steps() {
1429
+ let parsed = [];
1430
+ try { const s = this.getAttribute("steps"); parsed = s ? JSON.parse(s) : []; } catch (e) { warnBadJson(this, "steps", e); parsed = []; }
1431
+ return Array.isArray(parsed) ? parsed : [];
1432
+ }
1433
+ _current(n) {
1434
+ const c = Number(this.getAttribute("current") || 0);
1435
+ return Number.isFinite(c) ? Math.min(Math.max(0, c), Math.max(0, n - 1)) : 0;
1436
+ }
1437
+ _storageKey() {
1438
+ if (!this.id) {
1439
+ if (this.hasAttribute("persist") && !this._tourWarnedNoId) {
1440
+ this._tourWarnedNoId = true;
1441
+ console.warn("[contour-tour] persist requires an id attribute");
1442
+ }
1443
+ return null;
1444
+ }
1445
+ return "contour-tour:" + this.id;
1446
+ }
1447
+ _completed() {
1448
+ if (!this.hasAttribute("persist")) return false;
1449
+ const key = this._storageKey();
1450
+ try { return key ? localStorage.getItem(key) === "done" : false; } catch (e) { return false; }
1451
+ }
1452
+ _markCompleted() {
1453
+ if (!this.hasAttribute("persist")) return;
1454
+ const key = this._storageKey();
1455
+ try { if (key) localStorage.setItem(key, "done"); } catch (e) { /* storage is best-effort */ }
1456
+ }
1457
+ show() { this.setAttribute("open", ""); }
1458
+ close() {
1459
+ if (!this.hasAttribute("open")) return;
1460
+ this.removeAttribute("open");
1461
+ this._markCompleted();
1462
+ this.dispatchEvent(new CustomEvent("contour:close", { bubbles: true, composed: true }));
1463
+ }
1464
+ render() {
1465
+ this.classList.add("c-tour");
1466
+ this.setAttribute("role", "dialog");
1467
+ // No aria-modal: a tour is a coach mark pointing at a page target, so the page behind stays live.
1468
+ this.setAttribute("aria-label", labelText(this, "tour", "Tour"));
1469
+ const parsed = this._steps();
1470
+ // First render normalizes open state: a tour with steps starts open unless persistence says it completed; after that `open` is the source of truth.
1471
+ if (!this._tourInitialized) {
1472
+ this._tourInitialized = true;
1473
+ if (parsed.length && !this.hasAttribute("open") && !this._completed()) this.setAttribute("open", "");
1474
+ }
1475
+ const isOpen = this.hasAttribute("open") && parsed.length > 0;
1476
+ if (isOpen) this.removeAttribute("hidden"); else this.setAttribute("hidden", "");
1477
+ const current = this._current(parsed.length);
1478
+ const step = parsed[current] || {};
1479
+ const counter = this.ensure(".c-tour__step", () => { const el = document.createElement("div"); el.className = "c-tour__step"; return el; });
1480
+ counter.textContent = labelText(this, "step", "Step") + " " + (current + 1) + " / " + parsed.length;
1481
+ const tip = this.ensure(".c-tour__tip", () => { const el = document.createElement("div"); el.className = "c-tour__tip"; return el; });
1482
+ // UG-12: step changes must reach assistive tech — the tip is a polite
1483
+ // live region, so each new step text is announced without interrupting.
1484
+ tip.setAttribute("aria-live", "polite");
1485
+ tip.textContent = typeof step === "string" ? step : (step.text || step.title || "");
1486
+ if (counter.nextSibling !== tip) this.insertBefore(counter, tip);
1487
+ const nav = this.ensure(".c-tour__nav", () => { const el = document.createElement("div"); el.className = "c-tour__nav c-btn-group"; return el; });
1488
+ const back = this.ensure(".c-tour__back", () => { const el = document.createElement("button"); el.className = "c-btn c-btn--sm c-tour__back"; el.type = "button"; return el; }, nav);
1489
+ back.textContent = labelText(this, "back", "Back");
1490
+ back.disabled = current === 0;
1491
+ const next = this.ensure(".c-tour__next", () => { const el = document.createElement("button"); el.className = "c-btn c-btn--sm c-tour__next"; el.type = "button"; return el; }, nav);
1492
+ next.textContent = current >= parsed.length - 1 ? labelText(this, "done", "Done") : labelText(this, "next", "Next");
1493
+ const close = this.ensure(".c-tour__close", () => { const el = document.createElement("button"); el.className = "c-btn c-btn--sm c-tour__close"; el.type = "button"; return el; }, nav);
1494
+ close.textContent = labelText(this, "close", "Close");
1495
+ // Keep the controls in order regardless of which were just created.
1496
+ if (nav.firstChild !== back) nav.insertBefore(back, nav.firstChild);
1497
+ if (back.nextSibling !== next) nav.insertBefore(next, back.nextSibling);
1498
+ if (next.nextSibling !== close) nav.insertBefore(close, next.nextSibling);
1499
+ // Target highlight: a step may name the element it points at, so the
1500
+ // reader sees what the tip is about instead of a floating box.
1501
+ if (this._tourTarget && this._tourTarget.classList) this._tourTarget.classList.remove("c-tour__target");
1502
+ this._tourTarget = null;
1503
+ const targetSel = isOpen && typeof step === "object" && step ? step.target : null;
1504
+ if (targetSel) {
1505
+ const target = document.querySelector(targetSel);
1506
+ if (target && target.classList) {
1507
+ target.classList.add("c-tour__target");
1508
+ this._tourTarget = target;
1509
+ // Scroll an offscreen target into view before positioning the tip, so both land on what the reader can see.
1510
+ if (typeof target.scrollIntoView === "function") {
1511
+ try { target.scrollIntoView({ block: "nearest" }); } catch (e) { /* best-effort */ }
1512
+ }
1513
+ }
1514
+ }
1515
+ if (isOpen) this._positionTour();
1516
+ // Focus rule: on open and step change, focus the spotlighted target when focusable, else Next; applied in a microtask after the base class's focus restoration.
1517
+ if (isOpen) {
1518
+ if (this._tourFocusStep !== current || !this._tourOpen) {
1519
+ this._tourFocusStep = current;
1520
+ queueMicrotask(() => {
1521
+ if (!this.isConnected || !this.hasAttribute("open")) return;
1522
+ // Re-resolve both nodes here (rule 12): a later render in the same
1523
+ // task may have superseded this step's target or rebuilt Next.
1524
+ const target = this._tourTarget;
1525
+ const focusable = target && target.isConnected && typeof target.focus === "function" && !target.disabled &&
1526
+ target.matches("a[href], button, input, select, textarea, [tabindex]:not([tabindex='-1'])");
1527
+ const el = focusable ? target : this.querySelector(".c-tour__next");
1528
+ if (el) { try { el.focus({ preventScroll: true }); } catch (e) { try { el.focus(); } catch (e2) { /* best-effort */ } } }
1529
+ });
1530
+ }
1531
+ } else {
1532
+ this._tourFocusStep = null;
1533
+ }
1534
+ this._tourOpen = isOpen;
1535
+ }
1536
+ _positionTour() {
1537
+ // Position near the target (below, or above when there is no room), clamped to the viewport; otherwise bottom-center.
1538
+ const vw = window.innerWidth, vh = window.innerHeight;
1539
+ const w = this.offsetWidth || 280, h = this.offsetHeight || 120;
1540
+ const target = this._tourTarget;
1541
+ if (target && target.getBoundingClientRect) {
1542
+ const r = target.getBoundingClientRect();
1543
+ let top = r.bottom + 8;
1544
+ if (top + h > vh - 8) top = Math.max(8, r.top - h - 8);
1545
+ const left = Math.min(Math.max(8, r.left), Math.max(8, vw - w - 8));
1546
+ this.style.insetBlockStart = top + "px";
1547
+ this.style.insetInlineStart = left + "px";
1548
+ this.style.insetBlockEnd = "auto";
1549
+ this.style.transform = "";
1550
+ } else {
1551
+ this.style.insetBlockStart = "auto";
1552
+ this.style.insetBlockEnd = "1rem";
1553
+ this.style.insetInlineStart = "50%";
1554
+ this.style.transform = "translateX(-50%)";
1555
+ }
1556
+ }
1557
+ enhance() {
1558
+ if (this._wiredTour) return;
1559
+ this._wiredTour = true;
1560
+ // A tour is a coach mark, not a modal: no modalContain, and focus follows the render() rule, never a page-wide trap.
1561
+ const step = (dir) => {
1562
+ const parsed = this._steps();
1563
+ if (!parsed.length || !this.hasAttribute("open")) return;
1564
+ const cur = this._current(parsed.length);
1565
+ if (dir === "next") {
1566
+ if (cur >= parsed.length - 1) { this.close(); return; }
1567
+ this.setAttribute("current", String(cur + 1));
1568
+ this.dispatchEvent(new CustomEvent("contour:step-change", { bubbles: true, composed: true, detail: { direction: "next", index: cur + 1 } }));
1569
+ } else {
1570
+ if (cur === 0) return;
1571
+ this.setAttribute("current", String(cur - 1));
1572
+ this.dispatchEvent(new CustomEvent("contour:step-change", { bubbles: true, composed: true, detail: { direction: "prev", index: cur - 1 } }));
1573
+ }
1574
+ };
1575
+ this._tourStep = step;
1576
+ this.addEventListener("click", (e) => {
1577
+ const t = e.target && e.target.closest ? e.target : null;
1578
+ if (!t) return;
1579
+ if (t.closest(".c-tour__next")) { step("next"); return; }
1580
+ if (t.closest(".c-tour__back")) { step("prev"); return; }
1581
+ if (t.closest(".c-tour__close")) this.close();
1582
+ });
1583
+ const keyHandler = (e) => {
1584
+ if (!this.hasAttribute("open")) return;
1585
+ const tag = e.target && e.target.tagName;
1586
+ if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") return;
1587
+ if (e.key === "Escape") { this.close(); return; }
1588
+ const inTour = this.contains(e.target) || (this._tourTarget && (e.target === this._tourTarget || this._tourTarget.contains(e.target)));
1589
+ if (!inTour) return;
1590
+ if (e.key === "ArrowRight") { e.preventDefault(); step("next"); }
1591
+ else if (e.key === "ArrowLeft") { e.preventDefault(); step("prev"); }
1592
+ };
1593
+ document.addEventListener("keydown", keyHandler);
1594
+ const reposition = () => { if (this.hasAttribute("open")) this._positionTour(); };
1595
+ window.addEventListener("scroll", reposition, { passive: true, capture: true });
1596
+ window.addEventListener("resize", reposition, { passive: true });
1597
+ onDispose(this, () => {
1598
+ document.removeEventListener("keydown", keyHandler);
1599
+ window.removeEventListener("scroll", reposition, { capture: true });
1600
+ window.removeEventListener("resize", reposition);
1601
+ if (this._tourTarget && this._tourTarget.classList) this._tourTarget.classList.remove("c-tour__target");
1602
+ });
1603
+ }
1604
+ }
1605
+ define("contour-tour", ContourTour);