@opendata-ai/openchart-vanilla 8.9.1 → 9.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/{chunk-REH5UQCL.js → chunk-46E3QB6O.js} +6 -20
- package/dist/chunk-46E3QB6O.js.map +1 -0
- package/dist/{chunk-APMUBW72.js → chunk-5OQZRGUL.js} +1565 -294
- package/dist/chunk-5OQZRGUL.js.map +1 -0
- package/dist/{chunk-GX6BWMJD.js → chunk-5ZLSMZRM.js} +466 -330
- package/dist/chunk-5ZLSMZRM.js.map +1 -0
- package/dist/{chunk-SDJHUELU.js → chunk-A5C4VCUO.js} +26 -45
- package/dist/chunk-A5C4VCUO.js.map +1 -0
- package/dist/{chunk-MGFXAROR.js → chunk-CMIJA63X.js} +5 -3
- package/dist/chunk-CMIJA63X.js.map +1 -0
- package/dist/chunk-FFP5GXIP.js +45 -0
- package/dist/chunk-FFP5GXIP.js.map +1 -0
- package/dist/{chunk-Q7YEEF2X.js → chunk-LDSPJFOU.js} +16 -50
- package/dist/chunk-LDSPJFOU.js.map +1 -0
- package/dist/chunk-NLKQ5AOA.js +448 -0
- package/dist/chunk-NLKQ5AOA.js.map +1 -0
- package/dist/{chunk-R5TZHNTG.js → chunk-RBXIZJPE.js} +17 -5
- package/dist/chunk-RBXIZJPE.js.map +1 -0
- package/dist/{chunk-CCRFYGHU.js → chunk-RY5JEGFM.js} +2368 -2339
- package/dist/chunk-RY5JEGFM.js.map +1 -0
- package/dist/export-gif.d.ts +2 -2
- package/dist/export-gif.js +4 -3
- package/dist/graph-3d/index.d.ts +28 -1
- package/dist/graph-3d/index.js +26 -12
- package/dist/graph-3d/index.js.map +1 -1
- package/dist/index.d.ts +155 -29
- package/dist/index.js +1774 -186
- package/dist/index.js.map +1 -1
- package/dist/{mount-TEjIdF0m.d.ts → mount-types-DeUCa2hy.d.ts} +34 -21
- package/dist/{renderer-registry-Cng76n8F.d.ts → renderer-registry-Bu22Roek.d.ts} +5 -22
- package/dist/simulation-worker.js +27 -26
- package/dist/static.d.ts +4 -2
- package/dist/static.js +34 -10
- package/dist/static.js.map +1 -1
- package/dist/story/index.d.ts +8 -6
- package/dist/story/index.js +15 -12
- package/dist/story/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/styles.css.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-APMUBW72.js.map +0 -1
- package/dist/chunk-CCRFYGHU.js.map +0 -1
- package/dist/chunk-GX6BWMJD.js.map +0 -1
- package/dist/chunk-MGFXAROR.js.map +0 -1
- package/dist/chunk-Q7YEEF2X.js.map +0 -1
- package/dist/chunk-R5TZHNTG.js.map +0 -1
- package/dist/chunk-REH5UQCL.js.map +0 -1
- package/dist/chunk-SDJHUELU.js.map +0 -1
- package/dist/chunk-UC4IHEUO.js +0 -73
- package/dist/chunk-UC4IHEUO.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -3,20 +3,21 @@ import {
|
|
|
3
3
|
paletteFromCanvas,
|
|
4
4
|
readCanvasSRGB,
|
|
5
5
|
resolveRepeat
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-CMIJA63X.js";
|
|
7
7
|
import {
|
|
8
8
|
renderTileMapSVG
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-LDSPJFOU.js";
|
|
10
10
|
import {
|
|
11
11
|
GRAPH_3D_NOT_REGISTERED_ERROR,
|
|
12
|
+
createAttributionFooter,
|
|
12
13
|
createGraph,
|
|
13
14
|
registerGraphRenderer
|
|
14
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-5ZLSMZRM.js";
|
|
15
16
|
import {
|
|
16
17
|
CANVAS_DEFAULT_UPDATE_MAX_MARKS,
|
|
17
18
|
DEFAULT_UPDATE_MAX_MARKS,
|
|
18
19
|
VECTOR_EXPORT_MAX_POINTS,
|
|
19
|
-
|
|
20
|
+
acquireReaderHost,
|
|
20
21
|
cancelAnimations,
|
|
21
22
|
captureMarkFocus,
|
|
22
23
|
createChart,
|
|
@@ -25,13 +26,15 @@ import {
|
|
|
25
26
|
createTextEditOverlay,
|
|
26
27
|
createYouDrawIt,
|
|
27
28
|
materializeCanvasModeSVG,
|
|
29
|
+
readerEventExcluded,
|
|
28
30
|
resolveFontFamily,
|
|
31
|
+
resolveInteractionMode,
|
|
29
32
|
restoreMarkFocus,
|
|
30
33
|
scheduleFontReload,
|
|
31
34
|
setupAnimationCleanup,
|
|
32
35
|
setupTableAnimationCleanup,
|
|
33
36
|
wireMarkNavigation
|
|
34
|
-
} from "./chunk-
|
|
37
|
+
} from "./chunk-RY5JEGFM.js";
|
|
35
38
|
import {
|
|
36
39
|
embedFonts,
|
|
37
40
|
ensureSVGDimensions,
|
|
@@ -43,34 +46,41 @@ import {
|
|
|
43
46
|
getSVGBackgroundColor,
|
|
44
47
|
getSVGDimensions,
|
|
45
48
|
rasterizeSVGToCanvas
|
|
46
|
-
} from "./chunk-
|
|
49
|
+
} from "./chunk-RBXIZJPE.js";
|
|
47
50
|
import {
|
|
51
|
+
applySrOnlyStyles,
|
|
52
|
+
geoViewMapLayout,
|
|
53
|
+
mapLabelAffine,
|
|
54
|
+
nextSvgId,
|
|
48
55
|
rectPathWithCorners,
|
|
49
56
|
registerMarkRenderer,
|
|
57
|
+
renderActiveGeoColorKeys,
|
|
50
58
|
renderChartSVG,
|
|
51
|
-
renderChromeElement,
|
|
52
59
|
stampAnimationVars
|
|
53
|
-
} from "./chunk-
|
|
60
|
+
} from "./chunk-5OQZRGUL.js";
|
|
54
61
|
import {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
} from "./chunk-
|
|
62
|
+
injectThemeStyleBlock,
|
|
63
|
+
prepareSettledExportTheme
|
|
64
|
+
} from "./chunk-A5C4VCUO.js";
|
|
58
65
|
import {
|
|
59
66
|
createTooltipManager,
|
|
60
67
|
observeResize,
|
|
61
68
|
resolveDarkMode
|
|
62
|
-
} from "./chunk-
|
|
69
|
+
} from "./chunk-46E3QB6O.js";
|
|
63
70
|
import {
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
renderAttributionBrand,
|
|
72
|
+
renderChromeElement,
|
|
73
|
+
stampThemeProperties
|
|
74
|
+
} from "./chunk-NLKQ5AOA.js";
|
|
75
|
+
import {
|
|
76
|
+
applyTextStyle
|
|
77
|
+
} from "./chunk-FFP5GXIP.js";
|
|
66
78
|
|
|
67
79
|
// src/barlist-mount.ts
|
|
68
80
|
import { compileBarList } from "@opendata-ai/openchart-engine";
|
|
69
81
|
|
|
70
82
|
// src/barlist-renderer.ts
|
|
71
83
|
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
72
|
-
var XLINK_NS = "http://www.w3.org/1999/xlink";
|
|
73
|
-
var BRAND_URL = "https://tryopendata.ai";
|
|
74
84
|
function createSVGElement(tag) {
|
|
75
85
|
return document.createElementNS(SVG_NS, tag);
|
|
76
86
|
}
|
|
@@ -98,35 +108,6 @@ function renderChrome(parent, layout) {
|
|
|
98
108
|
}
|
|
99
109
|
parent.appendChild(g);
|
|
100
110
|
}
|
|
101
|
-
function renderWatermark(parent, layout) {
|
|
102
|
-
if (layout.width < 480) return;
|
|
103
|
-
const { width, height, theme } = layout;
|
|
104
|
-
const padding = theme.spacing.padding;
|
|
105
|
-
const rightEdge = width - padding;
|
|
106
|
-
const bottomEdge = height - padding;
|
|
107
|
-
const fill = theme.colors.axis;
|
|
108
|
-
const a = createSVGElement("a");
|
|
109
|
-
a.setAttribute("href", BRAND_URL);
|
|
110
|
-
a.setAttributeNS(XLINK_NS, "xlink:href", BRAND_URL);
|
|
111
|
-
a.setAttribute("target", "_blank");
|
|
112
|
-
a.setAttribute("rel", "noopener");
|
|
113
|
-
a.setAttribute("class", "oc-chrome-ref");
|
|
114
|
-
const text = createSVGElement("text");
|
|
115
|
-
setAttrs(text, {
|
|
116
|
-
x: rightEdge,
|
|
117
|
-
y: bottomEdge,
|
|
118
|
-
"dominant-baseline": "alphabetic",
|
|
119
|
-
"text-anchor": "end",
|
|
120
|
-
"font-family": theme.fonts.family,
|
|
121
|
-
"font-size": 12,
|
|
122
|
-
"fill-opacity": 0.55
|
|
123
|
-
});
|
|
124
|
-
text.style.setProperty("fill", fill);
|
|
125
|
-
setAttrs(text, { "font-weight": 600, "font-size": 16 });
|
|
126
|
-
text.textContent = "OpenData";
|
|
127
|
-
a.appendChild(text);
|
|
128
|
-
parent.appendChild(a);
|
|
129
|
-
}
|
|
130
111
|
function renderRows(parent, rows, animation) {
|
|
131
112
|
const g = createSVGElement("g");
|
|
132
113
|
g.setAttribute("class", "oc-barlist-rows");
|
|
@@ -220,7 +201,7 @@ function renderRows(parent, rows, animation) {
|
|
|
220
201
|
parent.appendChild(g);
|
|
221
202
|
}
|
|
222
203
|
function renderBarListSVG(layout, opts) {
|
|
223
|
-
const { width, height, rows, a11y,
|
|
204
|
+
const { width, height, rows, a11y, animation } = layout;
|
|
224
205
|
const animate = opts?.animate && !!animation?.enter;
|
|
225
206
|
const svg = createSVGElement("svg");
|
|
226
207
|
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
|
@@ -231,6 +212,7 @@ function renderBarListSVG(layout, opts) {
|
|
|
231
212
|
}
|
|
232
213
|
const classes = animate ? "oc-barlist oc-animate" : "oc-barlist";
|
|
233
214
|
svg.setAttribute("class", classes);
|
|
215
|
+
stampThemeProperties(svg, layout.theme);
|
|
234
216
|
if (animate && animation?.enter) {
|
|
235
217
|
stampAnimationVars(svg, {
|
|
236
218
|
duration: animation.enter.duration,
|
|
@@ -240,9 +222,12 @@ function renderBarListSVG(layout, opts) {
|
|
|
240
222
|
}
|
|
241
223
|
renderChrome(svg, layout);
|
|
242
224
|
renderRows(svg, rows, animate ? animation : void 0);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
225
|
+
renderAttributionBrand(
|
|
226
|
+
svg,
|
|
227
|
+
layout.chrome,
|
|
228
|
+
layout.chrome.bottomAnchorY ?? layout.area.y + layout.area.height,
|
|
229
|
+
layout.measureText
|
|
230
|
+
);
|
|
246
231
|
return svg;
|
|
247
232
|
}
|
|
248
233
|
|
|
@@ -434,6 +419,7 @@ function createBarList(container, spec, options) {
|
|
|
434
419
|
if (!svgElement) return "";
|
|
435
420
|
const terminalSvg = renderBarListSVG(currentLayout, { animate: false });
|
|
436
421
|
const resolvedOptions = { theme: currentLayout.theme, ...options_ };
|
|
422
|
+
prepareSettledExportTheme(svgElement, terminalSvg, resolvedOptions.theme, !!options_?.theme);
|
|
437
423
|
switch (format) {
|
|
438
424
|
case "svg":
|
|
439
425
|
return exportSVG(terminalSvg);
|
|
@@ -487,7 +473,7 @@ function createBarList(container, spec, options) {
|
|
|
487
473
|
}
|
|
488
474
|
);
|
|
489
475
|
container.dataset.ocFontsState = fontsPending ? "pending" : "ready";
|
|
490
|
-
if (options?.
|
|
476
|
+
if (options?.autoResize !== false) {
|
|
491
477
|
disconnectResize = observeResize(container, () => resize());
|
|
492
478
|
}
|
|
493
479
|
return {
|
|
@@ -505,10 +491,7 @@ function createBarList(container, spec, options) {
|
|
|
505
491
|
function settleAnimation(spec) {
|
|
506
492
|
const authored = spec.animation;
|
|
507
493
|
const update = typeof authored === "object" && typeof authored.update === "object" ? { ...authored.update } : true;
|
|
508
|
-
return {
|
|
509
|
-
...spec,
|
|
510
|
-
animation: { enter: false, update }
|
|
511
|
-
};
|
|
494
|
+
return { ...spec, animation: { enter: false, update } };
|
|
512
495
|
}
|
|
513
496
|
function mountOffscreen(spec, width, height, darkMode, themeConfig, renderer) {
|
|
514
497
|
const container = document.createElement("div");
|
|
@@ -521,7 +504,7 @@ function mountOffscreen(spec, width, height, darkMode, themeConfig, renderer) {
|
|
|
521
504
|
darkMode,
|
|
522
505
|
theme: themeConfig,
|
|
523
506
|
renderer,
|
|
524
|
-
|
|
507
|
+
autoResize: false
|
|
525
508
|
});
|
|
526
509
|
} catch (err) {
|
|
527
510
|
container.remove();
|
|
@@ -635,28 +618,1041 @@ function createSimulationWorker() {
|
|
|
635
618
|
return new Worker(new URL("./simulation-worker.js", import.meta.url), { type: "module" });
|
|
636
619
|
}
|
|
637
620
|
|
|
638
|
-
// src/reader.ts
|
|
621
|
+
// src/map-reader.ts
|
|
622
|
+
import {
|
|
623
|
+
buildGeoReaderIndex,
|
|
624
|
+
compareReaderValues
|
|
625
|
+
} from "@opendata-ai/openchart-engine";
|
|
626
|
+
|
|
627
|
+
// src/map-companion.ts
|
|
628
|
+
import { queryGeoReaderRecords } from "@opendata-ai/openchart-engine";
|
|
629
|
+
function createMapCompanion(actions) {
|
|
630
|
+
let options;
|
|
631
|
+
let index;
|
|
632
|
+
let root;
|
|
633
|
+
let theme;
|
|
634
|
+
let query = "";
|
|
635
|
+
let page = 0;
|
|
636
|
+
let restoringFocus = false;
|
|
637
|
+
function render() {
|
|
638
|
+
const active = document.activeElement instanceof HTMLElement && root?.contains(document.activeElement) ? document.activeElement : void 0;
|
|
639
|
+
const focused = active ? { action: active.dataset.companionAction, observation: active.dataset.companionObservation } : void 0;
|
|
640
|
+
root?.remove();
|
|
641
|
+
root = void 0;
|
|
642
|
+
if (!options || !index) return;
|
|
643
|
+
root = document.createElement("section");
|
|
644
|
+
root.className = "oc-root oc-map-companion";
|
|
645
|
+
if (theme) stampThemeProperties(root, theme);
|
|
646
|
+
root.setAttribute("aria-label", "Find a place");
|
|
647
|
+
if (options.search !== false) {
|
|
648
|
+
const input = document.createElement("input");
|
|
649
|
+
input.type = "search";
|
|
650
|
+
input.dataset.companionAction = "search";
|
|
651
|
+
input.setAttribute("aria-label", "Find a place");
|
|
652
|
+
input.value = query;
|
|
653
|
+
input.addEventListener("input", () => {
|
|
654
|
+
query = input.value;
|
|
655
|
+
page = 0;
|
|
656
|
+
render();
|
|
657
|
+
const next = root?.querySelector("input");
|
|
658
|
+
next?.focus();
|
|
659
|
+
next?.setSelectionRange(query.length, query.length);
|
|
660
|
+
});
|
|
661
|
+
const label = document.createElement("label");
|
|
662
|
+
label.textContent = "Find a place";
|
|
663
|
+
label.append(input);
|
|
664
|
+
root.append(label);
|
|
665
|
+
}
|
|
666
|
+
const hint = document.createElement("p");
|
|
667
|
+
hint.textContent = "Choose a place to pin it for comparison. Hover or focus to preview.";
|
|
668
|
+
root.append(hint);
|
|
669
|
+
const rows = queryGeoReaderRecords(index, query, options.rank, options.kind);
|
|
670
|
+
const size = Math.max(1, Math.floor(options.pageSize ?? 50));
|
|
671
|
+
if (focused?.observation) {
|
|
672
|
+
const position = rows.findIndex(
|
|
673
|
+
(record) => record.observation.observation === focused.observation
|
|
674
|
+
);
|
|
675
|
+
if (position >= 0) page = Math.floor(position / size);
|
|
676
|
+
}
|
|
677
|
+
page = Math.min(page, Math.max(0, Math.ceil(rows.length / size) - 1));
|
|
678
|
+
const status = document.createElement("p");
|
|
679
|
+
status.setAttribute("role", "status");
|
|
680
|
+
status.textContent = rows.length ? `${rows.length} ${rows.length === 1 ? "place" : "places"}${query ? rows.length === 1 ? " matches your search" : " match your search" : " available"}.` : query ? "No places match your search." : "No places available.";
|
|
681
|
+
root.append(status);
|
|
682
|
+
const list = document.createElement("ol");
|
|
683
|
+
for (const record of rows.slice(page * size, (page + 1) * size)) {
|
|
684
|
+
const item = document.createElement("li");
|
|
685
|
+
const button = document.createElement("button");
|
|
686
|
+
button.type = "button";
|
|
687
|
+
button.dataset.companionAction = "pin";
|
|
688
|
+
button.dataset.companionObservation = record.observation.observation;
|
|
689
|
+
const tied = record.rank !== void 0 && rows.filter((row) => row.rank === record.rank).length > 1;
|
|
690
|
+
button.textContent = `${record.rank !== void 0 ? `${tied ? "Tied " : ""}${record.rank}. ` : ""}${record.name}: ${record.valueLabel}${record.valueStatus === "value" && record.unit ? ` ${record.unit}` : ""}`;
|
|
691
|
+
button.addEventListener("mouseenter", () => actions.preview(record));
|
|
692
|
+
button.addEventListener("focus", () => {
|
|
693
|
+
if (!restoringFocus) actions.preview(record);
|
|
694
|
+
});
|
|
695
|
+
button.addEventListener("click", () => actions.pin(record));
|
|
696
|
+
item.append(button);
|
|
697
|
+
const show = document.createElement("button");
|
|
698
|
+
show.type = "button";
|
|
699
|
+
show.dataset.companionAction = "show";
|
|
700
|
+
show.dataset.companionObservation = record.observation.observation;
|
|
701
|
+
show.textContent = "Show on map";
|
|
702
|
+
show.setAttribute("aria-label", `Show ${record.name} on map`);
|
|
703
|
+
show.addEventListener("click", () => actions.show(record));
|
|
704
|
+
item.append(show);
|
|
705
|
+
if (record.viewIds?.length === 0) {
|
|
706
|
+
const unavailable = document.createElement("span");
|
|
707
|
+
unavailable.textContent = "Not visible in this projection.";
|
|
708
|
+
item.append(unavailable);
|
|
709
|
+
}
|
|
710
|
+
list.append(item);
|
|
711
|
+
}
|
|
712
|
+
root.append(list);
|
|
713
|
+
for (const [label, delta] of [
|
|
714
|
+
["Previous", -1],
|
|
715
|
+
["Next", 1]
|
|
716
|
+
]) {
|
|
717
|
+
const button = document.createElement("button");
|
|
718
|
+
button.type = "button";
|
|
719
|
+
button.textContent = label;
|
|
720
|
+
button.dataset.companionAction = label;
|
|
721
|
+
button.disabled = delta < 0 ? page === 0 : (page + 1) * size >= rows.length;
|
|
722
|
+
button.addEventListener("click", () => {
|
|
723
|
+
page += delta;
|
|
724
|
+
render();
|
|
725
|
+
});
|
|
726
|
+
root.append(button);
|
|
727
|
+
}
|
|
728
|
+
options.container.append(root);
|
|
729
|
+
if (focused && document.activeElement === document.body) {
|
|
730
|
+
const target = [...root.querySelectorAll("[data-companion-action]")].find(
|
|
731
|
+
(element) => element.dataset.companionAction === focused.action && element.dataset.companionObservation === focused.observation
|
|
732
|
+
);
|
|
733
|
+
restoringFocus = true;
|
|
734
|
+
try {
|
|
735
|
+
target?.focus();
|
|
736
|
+
} finally {
|
|
737
|
+
restoringFocus = false;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return {
|
|
742
|
+
setTheme(next) {
|
|
743
|
+
theme = next;
|
|
744
|
+
if (root) stampThemeProperties(root, next);
|
|
745
|
+
},
|
|
746
|
+
update(next) {
|
|
747
|
+
if (index === next) return;
|
|
748
|
+
index = next;
|
|
749
|
+
render();
|
|
750
|
+
},
|
|
751
|
+
setOptions(next) {
|
|
752
|
+
if (!options && !next) return;
|
|
753
|
+
const sameSemantics = options && next && options.search !== false === (next.search !== false) && options.rank === next.rank && options.kind === next.kind && (options.pageSize ?? 50) === (next.pageSize ?? 50);
|
|
754
|
+
if (sameSemantics && options.container === next.container) return;
|
|
755
|
+
if (!sameSemantics) page = 0;
|
|
756
|
+
options = next ? { ...next } : void 0;
|
|
757
|
+
render();
|
|
758
|
+
},
|
|
759
|
+
destroy() {
|
|
760
|
+
root?.remove();
|
|
761
|
+
options = void 0;
|
|
762
|
+
index = void 0;
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// src/map-reader-overlays.ts
|
|
768
|
+
var NS = "http://www.w3.org/2000/svg";
|
|
769
|
+
function renderMapReaderOverlay(svg, layout, camera, index, selection, getCamera) {
|
|
770
|
+
svg.querySelector("[data-oc-reader-overlay]")?.remove();
|
|
771
|
+
const group = document.createElementNS(NS, "g");
|
|
772
|
+
group.setAttribute("data-oc-reader-overlay", "");
|
|
773
|
+
group.setAttribute("pointer-events", "none");
|
|
774
|
+
group.setAttribute("aria-hidden", "true");
|
|
775
|
+
for (const view of layout.views ?? [void 0]) {
|
|
776
|
+
const frame = view ? geoViewMapLayout(layout, view) : layout;
|
|
777
|
+
const clipId = nextSvgId("oc-reader-clip");
|
|
778
|
+
const clip = document.createElementNS(NS, "clipPath");
|
|
779
|
+
clip.id = clipId;
|
|
780
|
+
const rect = document.createElementNS(NS, "rect");
|
|
781
|
+
for (const [key, value] of Object.entries(frame.area)) rect.setAttribute(key, String(value));
|
|
782
|
+
clip.append(rect);
|
|
783
|
+
group.append(clip);
|
|
784
|
+
const outlines = document.createElementNS(NS, "g");
|
|
785
|
+
outlines.setAttribute("clip-path", `url(#${clipId})`);
|
|
786
|
+
group.append(outlines);
|
|
787
|
+
const affine = mapLabelAffine(view && getCamera ? getCamera(view.viewId) : camera, frame);
|
|
788
|
+
const records = [
|
|
789
|
+
...selection.pinned,
|
|
790
|
+
...selection.preview && !selection.pinned.some((pin) => pin.observation === selection.preview.observation) ? [selection.preview] : []
|
|
791
|
+
];
|
|
792
|
+
for (const [position, observation] of records.entries()) {
|
|
793
|
+
const record = index.byObservation.get(observation.observation);
|
|
794
|
+
if (!record || observation.state === "filtered") continue;
|
|
795
|
+
let element;
|
|
796
|
+
if (observation.entity.kind === "feature") {
|
|
797
|
+
const matches = frame.features.filter(
|
|
798
|
+
(feature) => String(feature.id) === String(observation.entity.id)
|
|
799
|
+
);
|
|
800
|
+
if (matches.length !== 1) continue;
|
|
801
|
+
element = document.createElementNS(NS, "path");
|
|
802
|
+
element.setAttribute("d", matches[0].path);
|
|
803
|
+
element.setAttribute(
|
|
804
|
+
"transform",
|
|
805
|
+
`translate(${frame.area.x + affine.tx},${frame.area.y + affine.ty}) scale(${affine.k})`
|
|
806
|
+
);
|
|
807
|
+
element.setAttribute("vector-effect", "non-scaling-stroke");
|
|
808
|
+
} else {
|
|
809
|
+
const point = view ? view.pointMarks.find(
|
|
810
|
+
(mark) => mark.data === observation.datum || String(mark.key) === String(observation.entity.id)
|
|
811
|
+
) : record.point;
|
|
812
|
+
if (!point) continue;
|
|
813
|
+
element = document.createElementNS(NS, "circle");
|
|
814
|
+
element.setAttribute("cx", String(frame.area.x + point.cx * affine.k + affine.tx));
|
|
815
|
+
element.setAttribute("cy", String(frame.area.y + point.cy * affine.k + affine.ty));
|
|
816
|
+
element.setAttribute("r", String(point.r + 3));
|
|
817
|
+
}
|
|
818
|
+
element.setAttribute("fill", "none");
|
|
819
|
+
element.setAttribute(
|
|
820
|
+
"stroke",
|
|
821
|
+
position === 0 ? layout.theme.colors.text : layout.theme.colors.annotationLeader
|
|
822
|
+
);
|
|
823
|
+
element.setAttribute("stroke-width", position < selection.pinned.length ? "2.5" : "1.5");
|
|
824
|
+
if (position === 1) element.setAttribute("stroke-dasharray", "5 3");
|
|
825
|
+
outlines.append(element);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
renderActiveGeoColorKeys(group, layout, index, selection);
|
|
829
|
+
svg.append(group);
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
// src/reader-panel.ts
|
|
833
|
+
function createReaderPanel(initial, actions, announcementHost) {
|
|
834
|
+
let container = initial;
|
|
835
|
+
let theme;
|
|
836
|
+
let root;
|
|
837
|
+
let last = { pinned: [], announcement: "" };
|
|
838
|
+
let lastAnnouncement = "";
|
|
839
|
+
let live;
|
|
840
|
+
const text = (tag, value) => {
|
|
841
|
+
const el = document.createElement(tag);
|
|
842
|
+
el.textContent = value;
|
|
843
|
+
return el;
|
|
844
|
+
};
|
|
845
|
+
const button = (label, action) => {
|
|
846
|
+
const el = text("button", label);
|
|
847
|
+
el.type = "button";
|
|
848
|
+
el.addEventListener("click", action);
|
|
849
|
+
return el;
|
|
850
|
+
};
|
|
851
|
+
function render() {
|
|
852
|
+
if (!live) {
|
|
853
|
+
live = text("p", "");
|
|
854
|
+
live.className = "oc-sr-only";
|
|
855
|
+
applySrOnlyStyles(live);
|
|
856
|
+
live.setAttribute("aria-live", "polite");
|
|
857
|
+
live.setAttribute("aria-atomic", "true");
|
|
858
|
+
}
|
|
859
|
+
if (!container) {
|
|
860
|
+
root?.remove();
|
|
861
|
+
if (last.announcement !== lastAnnouncement) live.textContent = last.announcement;
|
|
862
|
+
lastAnnouncement = last.announcement;
|
|
863
|
+
announcementHost?.append(live);
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
const focused = root?.contains(document.activeElement) ? document.activeElement.textContent : null;
|
|
867
|
+
if (!root) {
|
|
868
|
+
root = document.createElement("section");
|
|
869
|
+
root.className = "oc-root oc-reader-panel";
|
|
870
|
+
root.setAttribute("aria-label", "Chart inspection");
|
|
871
|
+
}
|
|
872
|
+
if (theme) stampThemeProperties(root, theme);
|
|
873
|
+
for (const child of [...root.children]) if (child !== live) child.remove();
|
|
874
|
+
const records = [
|
|
875
|
+
...last.pinned.map((pin) => ({ label: pin.slot, record: pin.record, slot: pin.slot })),
|
|
876
|
+
...last.preview && !last.pinned.some((pin) => pin.record.id === last.preview.id) ? [{ label: "Preview", record: last.preview, slot: void 0 }] : []
|
|
877
|
+
];
|
|
878
|
+
if (!records.length) {
|
|
879
|
+
const empty = text("p", "Inspect a value, then pin two selections to compare.");
|
|
880
|
+
empty.className = "oc-reader-empty";
|
|
881
|
+
root.append(empty);
|
|
882
|
+
}
|
|
883
|
+
for (const item of records) {
|
|
884
|
+
const article = document.createElement("article");
|
|
885
|
+
if (item.slot) article.dataset.readerSlot = item.slot;
|
|
886
|
+
article.append(text("h3", `${item.label}: ${item.record.name}`));
|
|
887
|
+
if (item.record.domainLabel) article.append(text("p", item.record.domainLabel));
|
|
888
|
+
article.append(
|
|
889
|
+
text(
|
|
890
|
+
"p",
|
|
891
|
+
`${item.record.valueLabel}${item.record.unit ? ` ${item.record.unit}` : ""}${item.record.state === "filtered" ? " (not currently shown)" : ""}`
|
|
892
|
+
)
|
|
893
|
+
);
|
|
894
|
+
const list = document.createElement("dl");
|
|
895
|
+
for (const field of item.record.fields) {
|
|
896
|
+
list.append(text("dt", field.label), text("dd", field.value));
|
|
897
|
+
}
|
|
898
|
+
article.append(list);
|
|
899
|
+
if (item.slot) article.append(button(`Remove ${item.slot}`, () => actions.unpin(item.slot)));
|
|
900
|
+
else if (actions.pinPreview) article.append(button("Pin preview", actions.pinPreview));
|
|
901
|
+
if (actions.showOnMap)
|
|
902
|
+
article.append(
|
|
903
|
+
button(`Show ${item.record.name} on map`, () => actions.showOnMap(item.record.id))
|
|
904
|
+
);
|
|
905
|
+
root.append(article);
|
|
906
|
+
}
|
|
907
|
+
for (const comparison of last.comparisons ?? []) {
|
|
908
|
+
const section = document.createElement("p");
|
|
909
|
+
section.className = "oc-reader-comparison";
|
|
910
|
+
section.append(text("strong", comparison.label));
|
|
911
|
+
if (comparison.absolute !== void 0)
|
|
912
|
+
section.append(
|
|
913
|
+
document.createTextNode(
|
|
914
|
+
` ${comparison.absolute}${comparison.unit ? ` ${comparison.unit}` : ""}`
|
|
915
|
+
)
|
|
916
|
+
);
|
|
917
|
+
if (comparison.relative !== void 0)
|
|
918
|
+
section.append(document.createTextNode(` (${comparison.relative})`));
|
|
919
|
+
if (comparison.explanation)
|
|
920
|
+
section.append(document.createTextNode(` ${comparison.explanation}`));
|
|
921
|
+
root.append(section);
|
|
922
|
+
}
|
|
923
|
+
if (last.pinned.length) root.append(button("Clear selection", actions.clear));
|
|
924
|
+
if (live && last.announcement !== lastAnnouncement) live.textContent = last.announcement;
|
|
925
|
+
lastAnnouncement = last.announcement;
|
|
926
|
+
if (live) root.append(live);
|
|
927
|
+
container.append(root);
|
|
928
|
+
if (focused)
|
|
929
|
+
[...root.querySelectorAll("button")].find((button2) => button2.textContent === focused)?.focus();
|
|
930
|
+
}
|
|
931
|
+
return {
|
|
932
|
+
setTheme(next) {
|
|
933
|
+
theme = next;
|
|
934
|
+
if (root) stampThemeProperties(root, next);
|
|
935
|
+
},
|
|
936
|
+
update(model) {
|
|
937
|
+
last = model;
|
|
938
|
+
render();
|
|
939
|
+
},
|
|
940
|
+
setContainer(next) {
|
|
941
|
+
if (next !== container) {
|
|
942
|
+
container = next;
|
|
943
|
+
render();
|
|
944
|
+
}
|
|
945
|
+
},
|
|
946
|
+
destroy() {
|
|
947
|
+
root?.remove();
|
|
948
|
+
live?.remove();
|
|
949
|
+
root = void 0;
|
|
950
|
+
container = void 0;
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
// src/map-reader.ts
|
|
956
|
+
function createGeoReader(container, instance, initial, onChange, initialOptions = {}) {
|
|
957
|
+
let spec = initial;
|
|
958
|
+
let options = initialOptions;
|
|
959
|
+
let index = buildGeoReaderIndex(spec, instance.layout);
|
|
960
|
+
let state = { preview: null, pinned: [], announcement: "" };
|
|
961
|
+
let destroyed = false;
|
|
962
|
+
let keyboardIndex = -1;
|
|
963
|
+
let gesturing = false;
|
|
964
|
+
let pointer;
|
|
965
|
+
let rect;
|
|
966
|
+
let visibility;
|
|
967
|
+
const activatedViews = /* @__PURE__ */ new Map();
|
|
968
|
+
const lease = acquireReaderHost(instance, {});
|
|
969
|
+
const inspectionTarget = document.createElement("button");
|
|
970
|
+
inspectionTarget.type = "button";
|
|
971
|
+
inspectionTarget.className = "oc-map-inspect-places";
|
|
972
|
+
inspectionTarget.textContent = "Inspect places";
|
|
973
|
+
inspectionTarget.setAttribute(
|
|
974
|
+
"aria-label",
|
|
975
|
+
"Inspect places. Use arrow keys to move and Enter to pin."
|
|
976
|
+
);
|
|
977
|
+
container.append(inspectionTarget);
|
|
978
|
+
inspectionTarget.addEventListener("click", () => {
|
|
979
|
+
if (!state.preview && index.records.length) preview(index.records[0].observation.entity);
|
|
980
|
+
});
|
|
981
|
+
let previewFrame;
|
|
982
|
+
let pendingMove;
|
|
983
|
+
if (!lease) {
|
|
984
|
+
inspectionTarget.disabled = true;
|
|
985
|
+
}
|
|
986
|
+
if (!lease)
|
|
987
|
+
state.announcement = "Map inspection is unavailable because another reader owns this map.";
|
|
988
|
+
const refKey = (ref) => `${ref.kind}:${ref.canonicalId}`;
|
|
989
|
+
const byRef = (ref) => index.byObservation.get(refKey(ref));
|
|
990
|
+
const panelRecord = (record) => ({
|
|
991
|
+
id: record.observation.observation,
|
|
992
|
+
name: record.name,
|
|
993
|
+
valueLabel: record.valueLabel,
|
|
994
|
+
unit: record.valueStatus === "value" ? record.unit : void 0,
|
|
995
|
+
state: record.observation.state,
|
|
996
|
+
fields: [
|
|
997
|
+
...record.fields,
|
|
998
|
+
...record.identityStatus === "ephemeral" ? [
|
|
999
|
+
{
|
|
1000
|
+
label: "Selection",
|
|
1001
|
+
value: "Temporary selection; clears when the source data is replaced."
|
|
1002
|
+
}
|
|
1003
|
+
] : record.identityStatus === "ambiguous" ? [{ label: "Selection", value: "Ambiguous identity; pinning is unavailable." }] : []
|
|
1004
|
+
]
|
|
1005
|
+
});
|
|
1006
|
+
function comparison() {
|
|
1007
|
+
if (state.pinned.length !== 2 || !options.comparison || options.comparison === "values")
|
|
1008
|
+
return void 0;
|
|
1009
|
+
const [a, b] = state.pinned.map((pin2) => index.byObservation.get(pin2.observation));
|
|
1010
|
+
if (!a?.primary || !b?.primary || a.primary.value === void 0 || b.primary.value === void 0)
|
|
1011
|
+
return [
|
|
1012
|
+
{
|
|
1013
|
+
id: "difference",
|
|
1014
|
+
label: "B \u2212 A",
|
|
1015
|
+
explanation: "A comparison needs two observed finite values."
|
|
1016
|
+
}
|
|
1017
|
+
];
|
|
1018
|
+
const operand = (record) => ({
|
|
1019
|
+
value: record.primary.value,
|
|
1020
|
+
metadata: record.primary.metadata,
|
|
1021
|
+
binding: {
|
|
1022
|
+
dataset: options.binding?.dataset ?? "geo:source",
|
|
1023
|
+
transform: options.binding?.transform ?? "identity",
|
|
1024
|
+
grouping: [],
|
|
1025
|
+
panelId: "geo:root"
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1028
|
+
const result = compareReaderValues(operand(a), operand(b), {
|
|
1029
|
+
allowNegativeRelative: options.allowNegativeRelative
|
|
1030
|
+
});
|
|
1031
|
+
if (result.status === "unavailable")
|
|
1032
|
+
return [{ id: "difference", label: "B \u2212 A", explanation: result.message }];
|
|
1033
|
+
return [
|
|
1034
|
+
{
|
|
1035
|
+
id: "difference",
|
|
1036
|
+
label: "B \u2212 A",
|
|
1037
|
+
absolute: result.absolute,
|
|
1038
|
+
unit: result.unit,
|
|
1039
|
+
...options.comparison === "relative" ? result.relative === void 0 ? {
|
|
1040
|
+
explanation: result.relativeUnavailable === "zero-baseline" ? "Relative change is unavailable from zero." : "Relative change from a negative baseline requires explicit permission."
|
|
1041
|
+
} : { relative: `${(result.relative * 100).toFixed(1)}%` } : {}
|
|
1042
|
+
}
|
|
1043
|
+
];
|
|
1044
|
+
}
|
|
1045
|
+
const panel = createReaderPanel(
|
|
1046
|
+
options.panel ? options.panel.container : void 0,
|
|
1047
|
+
{
|
|
1048
|
+
clear: () => clear(),
|
|
1049
|
+
unpin: (slot) => {
|
|
1050
|
+
state.pinned = state.pinned.filter((_, position) => position !== (slot === "A" ? 0 : 1));
|
|
1051
|
+
state.announcement = `Removed ${slot}.`;
|
|
1052
|
+
paint();
|
|
1053
|
+
emit("user");
|
|
1054
|
+
},
|
|
1055
|
+
showOnMap: (id) => show(index.byObservation.get(id)),
|
|
1056
|
+
pinPreview: () => {
|
|
1057
|
+
if (state.preview) pin(state.preview.entity);
|
|
1058
|
+
}
|
|
1059
|
+
},
|
|
1060
|
+
container
|
|
1061
|
+
);
|
|
1062
|
+
const companion = createMapCompanion({
|
|
1063
|
+
preview: (record) => preview(record?.observation.entity ?? null),
|
|
1064
|
+
pin: (record) => pin(record.observation.entity),
|
|
1065
|
+
show
|
|
1066
|
+
});
|
|
1067
|
+
function show(record, requestedView) {
|
|
1068
|
+
if (destroyed || !record) return;
|
|
1069
|
+
record = index.byObservation.get(record.observation.observation);
|
|
1070
|
+
if (!record) return;
|
|
1071
|
+
if (record.viewIds?.length === 0) {
|
|
1072
|
+
state.announcement = `${record.name} is not visible in this projection. Choose a compatible projection or inset to show this place.`;
|
|
1073
|
+
paint();
|
|
1074
|
+
emit("user");
|
|
1075
|
+
return;
|
|
1076
|
+
}
|
|
1077
|
+
const views = instance.layout.views?.filter(
|
|
1078
|
+
(view) => record.observation.entity.kind === "feature" ? view.features.some((mark) => String(mark.id) === String(record.observation.entity.id)) : view.pointMarks.some((mark) => mark.data === record.observation.datum)
|
|
1079
|
+
) ?? [];
|
|
1080
|
+
if (requestedView && !views.some((view) => view.viewId === requestedView))
|
|
1081
|
+
requestedView = void 0;
|
|
1082
|
+
const preferredView = activatedViews.get(record.observation.observation);
|
|
1083
|
+
const viewId = requestedView ?? (views.some((view) => view.viewId === preferredView) ? preferredView : void 0) ?? (views.length === 1 ? views[0].viewId : void 0);
|
|
1084
|
+
if (!viewId && views.length > 1) {
|
|
1085
|
+
chooser?.remove();
|
|
1086
|
+
chooser = document.createElement("div");
|
|
1087
|
+
chooser.className = "oc-map-overlap-chooser";
|
|
1088
|
+
chooser.setAttribute("role", "group");
|
|
1089
|
+
chooser.setAttribute("aria-label", `Choose a view for ${record.name}`);
|
|
1090
|
+
for (const view of views) {
|
|
1091
|
+
const button = document.createElement("button");
|
|
1092
|
+
button.type = "button";
|
|
1093
|
+
button.textContent = `Show in ${view.coverage ?? view.viewId}`;
|
|
1094
|
+
button.addEventListener("click", () => {
|
|
1095
|
+
show(index.byObservation.get(record.observation.observation), view.viewId);
|
|
1096
|
+
chooser?.remove();
|
|
1097
|
+
chooser = void 0;
|
|
1098
|
+
});
|
|
1099
|
+
chooser.append(button);
|
|
1100
|
+
}
|
|
1101
|
+
container.append(chooser);
|
|
1102
|
+
chooser.querySelector("button")?.focus();
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
if (record.observation.entity.kind === "feature")
|
|
1106
|
+
instance.zoomTo(record.observation.entity.id, { viewId });
|
|
1107
|
+
else {
|
|
1108
|
+
const point = views.find((view) => view.viewId === viewId)?.pointMarks.find((mark) => mark.data === record.observation.datum) ?? record.point;
|
|
1109
|
+
if (point)
|
|
1110
|
+
instance.fitBounds(
|
|
1111
|
+
{
|
|
1112
|
+
x: point.cx - point.r,
|
|
1113
|
+
y: point.cy - point.r,
|
|
1114
|
+
width: point.r * 2,
|
|
1115
|
+
height: point.r * 2,
|
|
1116
|
+
padding: 24
|
|
1117
|
+
},
|
|
1118
|
+
{ viewId }
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
function paint() {
|
|
1123
|
+
if (destroyed) return;
|
|
1124
|
+
const pinned = state.pinned.flatMap((pin2, position) => {
|
|
1125
|
+
const record = index.byObservation.get(pin2.observation);
|
|
1126
|
+
return record ? [
|
|
1127
|
+
{
|
|
1128
|
+
slot: position === 0 ? "A" : "B",
|
|
1129
|
+
record: panelRecord({ ...record, observation: pin2 })
|
|
1130
|
+
}
|
|
1131
|
+
] : [];
|
|
1132
|
+
});
|
|
1133
|
+
const previewRecord = state.preview ? index.byObservation.get(state.preview.observation) : void 0;
|
|
1134
|
+
panel.setTheme(instance.layout.theme);
|
|
1135
|
+
companion.setTheme(instance.layout.theme);
|
|
1136
|
+
panel.update({
|
|
1137
|
+
pinned,
|
|
1138
|
+
preview: previewRecord ? panelRecord(previewRecord) : void 0,
|
|
1139
|
+
comparisons: comparison(),
|
|
1140
|
+
announcement: state.announcement
|
|
1141
|
+
});
|
|
1142
|
+
const svg = container.querySelector("svg");
|
|
1143
|
+
if (svg)
|
|
1144
|
+
renderMapReaderOverlay(
|
|
1145
|
+
svg,
|
|
1146
|
+
instance.layout,
|
|
1147
|
+
instance.getCamera(),
|
|
1148
|
+
index,
|
|
1149
|
+
state,
|
|
1150
|
+
(viewId) => instance.getCamera(viewId)
|
|
1151
|
+
);
|
|
1152
|
+
}
|
|
1153
|
+
function emit(reason) {
|
|
1154
|
+
if (destroyed) return;
|
|
1155
|
+
onChange({ ...state, pinned: [...state.pinned] }, { reason });
|
|
1156
|
+
}
|
|
1157
|
+
function preview(ref) {
|
|
1158
|
+
if (destroyed || !lease?.active) return;
|
|
1159
|
+
const next = ref ? byRef(ref)?.observation ?? null : null;
|
|
1160
|
+
if (state.preview?.observation === next?.observation) return;
|
|
1161
|
+
state.preview = next;
|
|
1162
|
+
paint();
|
|
1163
|
+
emit("user");
|
|
1164
|
+
}
|
|
1165
|
+
function pin(ref) {
|
|
1166
|
+
if (destroyed || !lease?.active) return;
|
|
1167
|
+
const record = byRef(ref);
|
|
1168
|
+
if (!record) return;
|
|
1169
|
+
if (record.identityStatus === "ambiguous") {
|
|
1170
|
+
state.announcement = "This identity is ambiguous; supply a unique key before pinning.";
|
|
1171
|
+
paint();
|
|
1172
|
+
emit("user");
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
const existing = state.pinned.findIndex(
|
|
1176
|
+
(pin2) => pin2.observation === record.observation.observation
|
|
1177
|
+
);
|
|
1178
|
+
if (existing >= 0) state.pinned = state.pinned.filter((_, index2) => index2 !== existing);
|
|
1179
|
+
else
|
|
1180
|
+
state.pinned = state.pinned.length < 2 ? [...state.pinned, record.observation] : [state.pinned[0], record.observation];
|
|
1181
|
+
state.preview = record.observation;
|
|
1182
|
+
state.announcement = `${record.name} ${existing >= 0 ? "removed" : "pinned"}.`;
|
|
1183
|
+
paint();
|
|
1184
|
+
emit("user");
|
|
1185
|
+
}
|
|
1186
|
+
function clear() {
|
|
1187
|
+
if (destroyed) return;
|
|
1188
|
+
state = { preview: null, pinned: [], announcement: "Selection dismissed." };
|
|
1189
|
+
paint();
|
|
1190
|
+
emit("user");
|
|
1191
|
+
}
|
|
1192
|
+
function update(next, visibleKeys) {
|
|
1193
|
+
if (destroyed) return;
|
|
1194
|
+
chooser?.remove();
|
|
1195
|
+
chooser = void 0;
|
|
1196
|
+
const filtered = visibility !== visibleKeys;
|
|
1197
|
+
const replaced = spec.data !== next.data || spec.points?.data !== next.points?.data || spec.encoding.key?.field !== next.encoding.key?.field || spec.points?.key?.field !== next.points?.key?.field;
|
|
1198
|
+
spec = next;
|
|
1199
|
+
visibility = visibleKeys;
|
|
1200
|
+
const prior = state.pinned;
|
|
1201
|
+
const priorPreview = state.preview;
|
|
1202
|
+
index = buildGeoReaderIndex(spec, instance.layout);
|
|
1203
|
+
for (const [key2, viewId] of activatedViews)
|
|
1204
|
+
if (!index.byObservation.get(key2)?.viewIds?.includes(viewId)) activatedViews.delete(key2);
|
|
1205
|
+
for (const diagnostic of index.diagnostics) options.onDiagnostic?.(diagnostic);
|
|
1206
|
+
for (const record of index.records)
|
|
1207
|
+
record.observation.state = visibility && !visibility.has(record.observation.observation) ? "filtered" : "visible";
|
|
1208
|
+
state.pinned = prior.flatMap((pin2) => {
|
|
1209
|
+
const record = index.byObservation.get(pin2.observation);
|
|
1210
|
+
if (!record || record.identityStatus === "ambiguous" || !pin2.persistent && replaced)
|
|
1211
|
+
return [];
|
|
1212
|
+
return [
|
|
1213
|
+
{
|
|
1214
|
+
...record.observation,
|
|
1215
|
+
...JSON.stringify(pin2.datum) !== JSON.stringify(record.observation.datum) ? { previous: pin2.datum } : {}
|
|
1216
|
+
}
|
|
1217
|
+
];
|
|
1218
|
+
});
|
|
1219
|
+
state.preview = !replaced && !filtered && priorPreview ? index.byObservation.get(priorPreview.observation)?.observation ?? null : null;
|
|
1220
|
+
state.announcement = state.pinned.length < prior.length ? "Selection cleared: an observation was removed or its identity changed." : "";
|
|
1221
|
+
rect = void 0;
|
|
1222
|
+
companion.update(index);
|
|
1223
|
+
paint();
|
|
1224
|
+
emit(filtered ? "filter" : "data");
|
|
1225
|
+
}
|
|
1226
|
+
const svgRect = () => rect ??= container.querySelector("svg")?.getBoundingClientRect();
|
|
1227
|
+
function hits(event) {
|
|
1228
|
+
const bounds = svgRect();
|
|
1229
|
+
if (!bounds || !bounds.width || !bounds.height) return [];
|
|
1230
|
+
const fullLayout = instance.layout;
|
|
1231
|
+
const targetView = event.target instanceof Element ? event.target.closest("[data-geo-view-id]")?.getAttribute("data-geo-view-id") : null;
|
|
1232
|
+
const view = fullLayout.views?.find((view2) => view2.viewId === targetView);
|
|
1233
|
+
const layout = view ? geoViewMapLayout(fullLayout, view) : fullLayout;
|
|
1234
|
+
const x = (event.clientX - bounds.left) * layout.width / bounds.width - layout.area.x;
|
|
1235
|
+
const y = (event.clientY - bounds.top) * layout.height / bounds.height - layout.area.y;
|
|
1236
|
+
const affine = mapLabelAffine(instance.getCamera(view?.viewId), layout);
|
|
1237
|
+
const points = index.records.filter((record) => {
|
|
1238
|
+
if (record.observation.entity.kind !== "point") return false;
|
|
1239
|
+
const point = view ? view.pointMarks.find((mark) => mark.data === record.observation.datum) : record.point;
|
|
1240
|
+
if (!point) return false;
|
|
1241
|
+
return Math.hypot(x - point.cx * affine.k - affine.tx, y - point.cy * affine.k - affine.ty) <= point.r;
|
|
1242
|
+
});
|
|
1243
|
+
if (view)
|
|
1244
|
+
for (const record of points) activatedViews.set(record.observation.observation, view.viewId);
|
|
1245
|
+
if (points.length) return points;
|
|
1246
|
+
const element = event.target instanceof Element ? event.target.closest("[data-feature-id]") : null;
|
|
1247
|
+
const id = element?.getAttribute("data-feature-id");
|
|
1248
|
+
const features = id === null || id === void 0 ? [] : index.records.filter(
|
|
1249
|
+
(record) => record.observation.entity.kind === "feature" && String(record.observation.entity.id) === id
|
|
1250
|
+
);
|
|
1251
|
+
if (view)
|
|
1252
|
+
for (const record of features)
|
|
1253
|
+
activatedViews.set(record.observation.observation, view.viewId);
|
|
1254
|
+
return features;
|
|
1255
|
+
}
|
|
1256
|
+
let chooser;
|
|
1257
|
+
function choose(records) {
|
|
1258
|
+
chooser?.remove();
|
|
1259
|
+
if (records.length === 1) {
|
|
1260
|
+
pin(records[0].observation.entity);
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
if (!records.length) return;
|
|
1264
|
+
chooser = document.createElement("div");
|
|
1265
|
+
chooser.className = "oc-map-overlap-chooser";
|
|
1266
|
+
chooser.setAttribute("role", "group");
|
|
1267
|
+
chooser.setAttribute("aria-label", "Overlapping places");
|
|
1268
|
+
for (const record of records) {
|
|
1269
|
+
const button = document.createElement("button");
|
|
1270
|
+
button.type = "button";
|
|
1271
|
+
button.textContent = `${record.name}: ${record.valueLabel}`;
|
|
1272
|
+
button.addEventListener("click", () => {
|
|
1273
|
+
pin(record.observation.entity);
|
|
1274
|
+
chooser?.remove();
|
|
1275
|
+
chooser = void 0;
|
|
1276
|
+
});
|
|
1277
|
+
chooser.append(button);
|
|
1278
|
+
}
|
|
1279
|
+
chooser.addEventListener("keydown", (event) => {
|
|
1280
|
+
const buttons = [...chooser.querySelectorAll("button")];
|
|
1281
|
+
const i = buttons.indexOf(event.target);
|
|
1282
|
+
if (event.key === "Escape") {
|
|
1283
|
+
chooser?.remove();
|
|
1284
|
+
chooser = void 0;
|
|
1285
|
+
} else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
1286
|
+
buttons[(i + (event.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length]?.focus();
|
|
1287
|
+
event.preventDefault();
|
|
1288
|
+
}
|
|
1289
|
+
});
|
|
1290
|
+
container.append(chooser);
|
|
1291
|
+
}
|
|
1292
|
+
const move = (event) => {
|
|
1293
|
+
if (!lease?.active || gesturing || readerEventExcluded(event.target) || event.pointerType === "touch")
|
|
1294
|
+
return;
|
|
1295
|
+
pendingMove = event;
|
|
1296
|
+
if (previewFrame !== void 0) return;
|
|
1297
|
+
previewFrame = requestAnimationFrame(() => {
|
|
1298
|
+
previewFrame = void 0;
|
|
1299
|
+
const event2 = pendingMove;
|
|
1300
|
+
pendingMove = void 0;
|
|
1301
|
+
if (event2 && !gesturing && lease?.active) {
|
|
1302
|
+
const record = hits(event2)[0];
|
|
1303
|
+
preview(record?.observation.entity ?? null);
|
|
1304
|
+
}
|
|
1305
|
+
});
|
|
1306
|
+
};
|
|
1307
|
+
const down = (event) => {
|
|
1308
|
+
if (!lease?.active || readerEventExcluded(event.target)) return;
|
|
1309
|
+
pointer = { x: event.clientX, y: event.clientY, id: event.pointerId };
|
|
1310
|
+
};
|
|
1311
|
+
const up = (event) => {
|
|
1312
|
+
const start = pointer;
|
|
1313
|
+
pointer = void 0;
|
|
1314
|
+
if (!start || !lease?.active || gesturing || start.id !== event.pointerId || readerEventExcluded(event.target) || Math.hypot(start.x - event.clientX, start.y - event.clientY) > 4)
|
|
1315
|
+
return;
|
|
1316
|
+
choose(hits(event));
|
|
1317
|
+
};
|
|
1318
|
+
const cancel = () => {
|
|
1319
|
+
pointer = void 0;
|
|
1320
|
+
rect = void 0;
|
|
1321
|
+
pendingMove = void 0;
|
|
1322
|
+
if (previewFrame !== void 0) {
|
|
1323
|
+
cancelAnimationFrame(previewFrame);
|
|
1324
|
+
previewFrame = void 0;
|
|
1325
|
+
}
|
|
1326
|
+
};
|
|
1327
|
+
const leave = () => {
|
|
1328
|
+
if (pointer) return;
|
|
1329
|
+
preview(null);
|
|
1330
|
+
};
|
|
1331
|
+
const key = (event) => {
|
|
1332
|
+
if (!lease?.active || event.target !== inspectionTarget) return;
|
|
1333
|
+
const rows = index.records;
|
|
1334
|
+
if (event.key === "Escape") {
|
|
1335
|
+
clear();
|
|
1336
|
+
event.preventDefault();
|
|
1337
|
+
} else if (event.key === "ArrowRight" || event.key === "ArrowLeft" || event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
1338
|
+
if (!rows.length) return;
|
|
1339
|
+
keyboardIndex = (keyboardIndex + (event.key === "ArrowLeft" || event.key === "ArrowUp" ? -1 : 1) + rows.length) % rows.length;
|
|
1340
|
+
preview(rows[keyboardIndex].observation.entity);
|
|
1341
|
+
event.preventDefault();
|
|
1342
|
+
} else if (event.key === "Enter" || event.key === " ") {
|
|
1343
|
+
if (state.preview) pin(state.preview.entity);
|
|
1344
|
+
else if (rows.length) preview(rows[0].observation.entity);
|
|
1345
|
+
event.preventDefault();
|
|
1346
|
+
}
|
|
1347
|
+
};
|
|
1348
|
+
container.addEventListener("pointermove", move);
|
|
1349
|
+
container.addEventListener("pointerdown", down);
|
|
1350
|
+
container.addEventListener("pointerup", up);
|
|
1351
|
+
container.addEventListener("pointercancel", cancel);
|
|
1352
|
+
container.addEventListener("pointerleave", leave);
|
|
1353
|
+
container.addEventListener("keydown", key);
|
|
1354
|
+
window.addEventListener("scroll", cancel, true);
|
|
1355
|
+
const unsubscribe = lease?.subscribe((event) => {
|
|
1356
|
+
if (event.kind === "destroy") destroy();
|
|
1357
|
+
else if (event.kind === "layout") {
|
|
1358
|
+
const latest = lease.getSpec();
|
|
1359
|
+
update(latest && "type" in latest && latest.type === "map" ? latest : spec, visibility);
|
|
1360
|
+
} else if (event.kind === "camera") {
|
|
1361
|
+
const svg = container.querySelector("svg");
|
|
1362
|
+
if (svg)
|
|
1363
|
+
renderMapReaderOverlay(
|
|
1364
|
+
svg,
|
|
1365
|
+
instance.layout,
|
|
1366
|
+
instance.getCamera(),
|
|
1367
|
+
index,
|
|
1368
|
+
state,
|
|
1369
|
+
(viewId) => instance.getCamera(viewId)
|
|
1370
|
+
);
|
|
1371
|
+
} else if (event.kind === "gesture-start") {
|
|
1372
|
+
gesturing = true;
|
|
1373
|
+
cancel();
|
|
1374
|
+
} else if (event.kind === "gesture-end") gesturing = false;
|
|
1375
|
+
});
|
|
1376
|
+
function setOptions(next) {
|
|
1377
|
+
if (destroyed) return;
|
|
1378
|
+
const revisionChanged = options.revision !== next.revision;
|
|
1379
|
+
const bindingChanged = JSON.stringify(options.binding) !== JSON.stringify(next.binding);
|
|
1380
|
+
options = next;
|
|
1381
|
+
if (revisionChanged || bindingChanged) {
|
|
1382
|
+
state.pinned = state.pinned.filter((pin2) => !bindingChanged && pin2.persistent);
|
|
1383
|
+
state.preview = null;
|
|
1384
|
+
state.announcement = "Selection reconciled with the current data binding.";
|
|
1385
|
+
}
|
|
1386
|
+
panel.setContainer(options.panel ? options.panel.container : void 0);
|
|
1387
|
+
companion.setOptions(options.companion || void 0);
|
|
1388
|
+
companion.update(index);
|
|
1389
|
+
paint();
|
|
1390
|
+
}
|
|
1391
|
+
function destroy() {
|
|
1392
|
+
if (destroyed) return;
|
|
1393
|
+
destroyed = true;
|
|
1394
|
+
cancel();
|
|
1395
|
+
inspectionTarget.remove();
|
|
1396
|
+
unsubscribe?.();
|
|
1397
|
+
lease?.release();
|
|
1398
|
+
panel.destroy();
|
|
1399
|
+
companion.destroy();
|
|
1400
|
+
chooser?.remove();
|
|
1401
|
+
container.querySelector("[data-oc-reader-overlay]")?.remove();
|
|
1402
|
+
container.removeEventListener("pointermove", move);
|
|
1403
|
+
container.removeEventListener("pointerdown", down);
|
|
1404
|
+
container.removeEventListener("pointerup", up);
|
|
1405
|
+
container.removeEventListener("pointercancel", cancel);
|
|
1406
|
+
container.removeEventListener("pointerleave", leave);
|
|
1407
|
+
container.removeEventListener("keydown", key);
|
|
1408
|
+
window.removeEventListener("scroll", cancel, true);
|
|
1409
|
+
}
|
|
1410
|
+
setOptions(options);
|
|
1411
|
+
for (const diagnostic of index.diagnostics) options.onDiagnostic?.(diagnostic);
|
|
1412
|
+
return {
|
|
1413
|
+
get selection() {
|
|
1414
|
+
return { ...state, pinned: [...state.pinned] };
|
|
1415
|
+
},
|
|
1416
|
+
update,
|
|
1417
|
+
setOptions,
|
|
1418
|
+
pin,
|
|
1419
|
+
preview,
|
|
1420
|
+
clear,
|
|
1421
|
+
destroy,
|
|
1422
|
+
setSelection(next, setOptions2 = {}) {
|
|
1423
|
+
if (destroyed) return;
|
|
1424
|
+
state = {
|
|
1425
|
+
...next,
|
|
1426
|
+
pinned: next.pinned.flatMap((pin2) => {
|
|
1427
|
+
const record = index.byObservation.get(pin2.observation);
|
|
1428
|
+
return record && record.identityStatus !== "ambiguous" ? [record.observation] : [];
|
|
1429
|
+
}).slice(0, 2),
|
|
1430
|
+
preview: next.preview ? index.byObservation.get(next.preview.observation)?.observation ?? null : null
|
|
1431
|
+
};
|
|
1432
|
+
paint();
|
|
1433
|
+
if (setOptions2.notify === true) emit("programmatic");
|
|
1434
|
+
}
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
// src/observation-reader.ts
|
|
639
1439
|
import {
|
|
640
1440
|
observationKey,
|
|
641
1441
|
reconcileReaderSelection,
|
|
642
1442
|
resolveObservationIdentities
|
|
643
1443
|
} from "@opendata-ai/openchart-engine";
|
|
1444
|
+
|
|
1445
|
+
// src/reader-view-model.ts
|
|
1446
|
+
import { buildD3Formatter, formatNumber } from "@opendata-ai/openchart-core";
|
|
1447
|
+
import { compareReaderValues as compareReaderValues2, normalizeSpec } from "@opendata-ai/openchart-engine";
|
|
1448
|
+
var number = (value) => typeof value === "number" ? value : typeof value === "string" && value.trim() ? Number(value) : NaN;
|
|
1449
|
+
var string = (value) => value === null || value === void 0 ? "No observation" : String(value);
|
|
1450
|
+
var semanticEncodings = /* @__PURE__ */ new WeakMap();
|
|
1451
|
+
function observationEncoding(spec) {
|
|
1452
|
+
if (!spec) return void 0;
|
|
1453
|
+
let cached = semanticEncodings.get(spec);
|
|
1454
|
+
if (!cached || cached.data !== spec.data || cached.encoding !== spec.encoding) {
|
|
1455
|
+
cached = {
|
|
1456
|
+
data: spec.data,
|
|
1457
|
+
encoding: spec.encoding,
|
|
1458
|
+
normalized: normalizeSpec(spec).encoding
|
|
1459
|
+
};
|
|
1460
|
+
semanticEncodings.set(spec, cached);
|
|
1461
|
+
}
|
|
1462
|
+
return cached.normalized;
|
|
1463
|
+
}
|
|
1464
|
+
function observationPanelModel(state, spec, options) {
|
|
1465
|
+
const encoding = observationEncoding(spec);
|
|
1466
|
+
const horizontal = encoding?.x?.type === "quantitative" && encoding?.y?.type !== "quantitative";
|
|
1467
|
+
const category = horizontal ? encoding?.y : encoding?.x;
|
|
1468
|
+
const valueChannel = encoding?.y?.type === "quantitative" ? encoding.y : encoding?.x;
|
|
1469
|
+
const record = (observation) => {
|
|
1470
|
+
const data = observation.datum;
|
|
1471
|
+
const value = valueChannel;
|
|
1472
|
+
return {
|
|
1473
|
+
id: observation.observation,
|
|
1474
|
+
name: category && "field" in category ? string(data[category.field]) : observation.observation,
|
|
1475
|
+
valueLabel: value && "field" in value ? (() => {
|
|
1476
|
+
const raw = data[value.field], numeric = number(raw);
|
|
1477
|
+
const explicit = value.format || (typeof value.axis === "object" && value.axis !== null ? value.axis.format : void 0);
|
|
1478
|
+
const formatter = buildD3Formatter(explicit) || formatNumber;
|
|
1479
|
+
return Number.isFinite(numeric) ? formatter(numeric) : string(raw);
|
|
1480
|
+
})() : "",
|
|
1481
|
+
unit: value && "field" in value ? spec?.fields?.[value.field]?.unit : void 0,
|
|
1482
|
+
state: observation.state,
|
|
1483
|
+
fields: Object.entries(data).map(([label, value2]) => ({ label, value: string(value2) }))
|
|
1484
|
+
};
|
|
1485
|
+
};
|
|
1486
|
+
const comparisons = [];
|
|
1487
|
+
const pair = state.pinned.length === 2 ? state.pinned : state.pinned.length === 1 && state.preview && state.preview.observation !== state.pinned[0].observation ? [state.pinned[0], state.preview] : [];
|
|
1488
|
+
if (pair.length === 2 && options.comparison && options.comparison !== "values" && valueChannel && "field" in valueChannel) {
|
|
1489
|
+
const field = valueChannel.field, metadata = spec?.fields?.[field];
|
|
1490
|
+
const operand = (item) => ({
|
|
1491
|
+
value: number(item.datum[field]),
|
|
1492
|
+
metadata,
|
|
1493
|
+
binding: {
|
|
1494
|
+
dataset: options.binding?.dataset ?? "chart",
|
|
1495
|
+
transform: JSON.stringify(spec?.transform ?? []),
|
|
1496
|
+
grouping: [item.series ?? ""],
|
|
1497
|
+
panelId: spec?.encoding.facet && "field" in spec.encoding.facet ? JSON.stringify(item.datum[spec.encoding.facet.field]) : "root"
|
|
1498
|
+
}
|
|
1499
|
+
});
|
|
1500
|
+
const result = compareReaderValues2(operand(pair[0]), operand(pair[1]), {
|
|
1501
|
+
allowNegativeRelative: options.allowNegativeRelative
|
|
1502
|
+
});
|
|
1503
|
+
if (result.status === "unavailable")
|
|
1504
|
+
comparisons.push({ id: field, label: "B minus A", explanation: result.message });
|
|
1505
|
+
else
|
|
1506
|
+
comparisons.push({
|
|
1507
|
+
id: field,
|
|
1508
|
+
label: "B minus A",
|
|
1509
|
+
absolute: result.absolute,
|
|
1510
|
+
unit: result.unit,
|
|
1511
|
+
...options.comparison === "relative" ? {
|
|
1512
|
+
relative: result.relative === void 0 ? void 0 : `${(result.relative * 100).toLocaleString(void 0, { maximumFractionDigits: 2 })}%`,
|
|
1513
|
+
explanation: result.relativeUnavailable ? `Relative change unavailable: ${result.relativeUnavailable.replaceAll("-", " ")}.` : void 0
|
|
1514
|
+
} : {}
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
return {
|
|
1518
|
+
preview: state.preview ? record(state.preview) : void 0,
|
|
1519
|
+
pinned: state.pinned.map((item, index) => ({
|
|
1520
|
+
slot: index === 0 ? "A" : "B",
|
|
1521
|
+
record: record(item)
|
|
1522
|
+
})),
|
|
1523
|
+
comparisons,
|
|
1524
|
+
announcement: state.announcement
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1527
|
+
function sliceRecord(slice) {
|
|
1528
|
+
const panel = slice.panel.facet ? slice.panel.facet.values.map((value) => string(value.value)).join(", ") : "";
|
|
1529
|
+
return {
|
|
1530
|
+
id: slice.key,
|
|
1531
|
+
name: slice.xLabel,
|
|
1532
|
+
domainLabel: panel ? `Panel: ${panel}` : void 0,
|
|
1533
|
+
valueLabel: `${slice.members.length} series`,
|
|
1534
|
+
state: slice.state,
|
|
1535
|
+
fields: slice.members.map((member) => ({
|
|
1536
|
+
label: member.series.metadata?.unit ? `${member.series.label} (${member.series.metadata.unit})` : member.series.label,
|
|
1537
|
+
value: member.status === "no-observation" ? "No observation" : member.status === "ambiguous" ? "Ambiguous observations" : member.formattedValue ?? string(member.value),
|
|
1538
|
+
status: member.status === "no-observation" ? "missing" : member.status === "ambiguous" ? "ambiguous" : "value"
|
|
1539
|
+
}))
|
|
1540
|
+
};
|
|
1541
|
+
}
|
|
1542
|
+
function slicePanelModel(state, options) {
|
|
1543
|
+
const pair = state.pinned.length === 2 ? state.pinned : state.pinned.length === 1 && state.preview && state.preview.key !== state.pinned[0].key ? [state.pinned[0], state.preview] : [];
|
|
1544
|
+
const comparisons = [];
|
|
1545
|
+
if (pair.length === 2 && options.comparison && options.comparison !== "values") {
|
|
1546
|
+
const [a, b] = pair;
|
|
1547
|
+
for (const member of a.members) {
|
|
1548
|
+
const after = b.members.find((item) => item.series.key === member.series.key);
|
|
1549
|
+
if (!after) continue;
|
|
1550
|
+
if (member.status !== "observed" || after.status !== "observed" || member.valueStatus !== "finite" || after.valueStatus !== "finite") {
|
|
1551
|
+
comparisons.push({
|
|
1552
|
+
id: member.series.key,
|
|
1553
|
+
label: member.series.label,
|
|
1554
|
+
explanation: "Change unavailable: no unique finite observation at both dates."
|
|
1555
|
+
});
|
|
1556
|
+
continue;
|
|
1557
|
+
}
|
|
1558
|
+
const operand = (slice, value, metadata) => ({
|
|
1559
|
+
value,
|
|
1560
|
+
metadata,
|
|
1561
|
+
binding: {
|
|
1562
|
+
dataset: slice.binding.dataset,
|
|
1563
|
+
transform: slice.binding.transform,
|
|
1564
|
+
grouping: slice.binding.grouping,
|
|
1565
|
+
panelId: slice.panel.id
|
|
1566
|
+
}
|
|
1567
|
+
});
|
|
1568
|
+
const result = compareReaderValues2(
|
|
1569
|
+
operand(a, member.value, member.series.metadata),
|
|
1570
|
+
operand(b, after.value, after.series.metadata),
|
|
1571
|
+
{ allowNegativeRelative: options.allowNegativeRelative }
|
|
1572
|
+
);
|
|
1573
|
+
if (result.status === "unavailable")
|
|
1574
|
+
comparisons.push({
|
|
1575
|
+
id: member.series.key,
|
|
1576
|
+
label: member.series.label,
|
|
1577
|
+
explanation: result.message
|
|
1578
|
+
});
|
|
1579
|
+
else
|
|
1580
|
+
comparisons.push({
|
|
1581
|
+
id: member.series.key,
|
|
1582
|
+
label: `${member.series.label}: B minus A`,
|
|
1583
|
+
absolute: result.absolute,
|
|
1584
|
+
unit: result.unit,
|
|
1585
|
+
...options.comparison === "relative" ? {
|
|
1586
|
+
relative: result.relative === void 0 ? void 0 : `${(result.relative * 100).toLocaleString(void 0, { maximumFractionDigits: 2 })}%`,
|
|
1587
|
+
explanation: result.relativeUnavailable ? `Relative change unavailable: ${result.relativeUnavailable.replaceAll("-", " ")}.` : void 0
|
|
1588
|
+
} : {}
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
return {
|
|
1593
|
+
preview: state.preview ? sliceRecord(state.preview) : void 0,
|
|
1594
|
+
pinned: state.pinned.map((slice, index) => ({
|
|
1595
|
+
slot: index === 0 ? "A" : "B",
|
|
1596
|
+
record: sliceRecord(slice)
|
|
1597
|
+
})),
|
|
1598
|
+
comparisons,
|
|
1599
|
+
announcement: state.announcement
|
|
1600
|
+
};
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
// src/observation-reader.ts
|
|
644
1604
|
var LAYER_UNSUPPORTED = "Reader inspection is unavailable for layered charts: observations cannot be bound to a single dataset.";
|
|
645
1605
|
var seriesField = (spec) => spec.encoding.color && "field" in spec.encoding.color ? spec.encoding.color.field : void 0;
|
|
646
|
-
function
|
|
1606
|
+
function createObservationReader(container, instance, initial, onChange, initialOptions = {}) {
|
|
647
1607
|
let spec = "layer" in initial ? null : initial;
|
|
1608
|
+
let options = initialOptions;
|
|
1609
|
+
let destroyed = false;
|
|
1610
|
+
const originalTabindex = container.getAttribute("tabindex");
|
|
1611
|
+
const originalLabel = container.getAttribute("aria-label");
|
|
1612
|
+
const originalRole = container.getAttribute("aria-roledescription");
|
|
1613
|
+
const owner = {};
|
|
1614
|
+
let lease = acquireReaderHost(instance, owner);
|
|
1615
|
+
if ("layer" in initial) lease?.release();
|
|
1616
|
+
else if (!instance.layout.a11y.hidden && lease?.active) {
|
|
1617
|
+
container.setAttribute("tabindex", "0");
|
|
1618
|
+
container.setAttribute("aria-label", instance.layout.a11y.altText);
|
|
1619
|
+
container.setAttribute("aria-roledescription", "chart");
|
|
1620
|
+
}
|
|
1621
|
+
const active = () => !destroyed && !!lease?.active;
|
|
1622
|
+
let bounds = container.querySelector("svg")?.getBoundingClientRect() ?? container.getBoundingClientRect();
|
|
1623
|
+
const refreshBounds = () => {
|
|
1624
|
+
const svgBounds = container.querySelector("svg")?.getBoundingClientRect();
|
|
1625
|
+
bounds = svgBounds?.width && svgBounds.height ? svgBounds : container.getBoundingClientRect();
|
|
1626
|
+
};
|
|
1627
|
+
refreshBounds();
|
|
1628
|
+
const usableBounds = () => bounds;
|
|
648
1629
|
let state = {
|
|
649
1630
|
preview: null,
|
|
650
1631
|
pinned: [],
|
|
651
|
-
announcement: spec ? "" : LAYER_UNSUPPORTED
|
|
1632
|
+
announcement: spec ? lease ? "" : "Inspection is unavailable because another reader owns this chart." : LAYER_UNSUPPORTED
|
|
652
1633
|
};
|
|
653
1634
|
let keyboardIndex = -1;
|
|
654
1635
|
let settleFrame = null;
|
|
1636
|
+
let pointerFrame = null;
|
|
1637
|
+
let pointerEvent = null;
|
|
655
1638
|
let visibilityOverride;
|
|
656
1639
|
let identities = spec ? resolveObservationIdentities(spec.data, spec.encoding.key?.field, seriesField(spec)) : [];
|
|
657
|
-
|
|
1640
|
+
let panel;
|
|
1641
|
+
let emitted = "";
|
|
1642
|
+
const paint = () => {
|
|
1643
|
+
panel?.setTheme(instance.layout.theme);
|
|
1644
|
+
panel?.update(observationPanelModel(state, spec, options));
|
|
1645
|
+
};
|
|
1646
|
+
const emit = (reason = "user") => {
|
|
1647
|
+
if (destroyed) return;
|
|
1648
|
+
paint();
|
|
1649
|
+
const signature = JSON.stringify(state);
|
|
1650
|
+
if (signature === emitted) return;
|
|
1651
|
+
emitted = signature;
|
|
1652
|
+
onChange({ ...state, pinned: [...state.pinned] }, { reason });
|
|
1653
|
+
};
|
|
658
1654
|
const visibleData = (layout = instance.sampleLayout()) => {
|
|
659
|
-
const rows = layout.marks.flatMap((mark) => {
|
|
1655
|
+
const rows = (layout.facet ? layout.facet.panels.flatMap((panel2) => panel2.marks) : layout.marks).flatMap((mark) => {
|
|
660
1656
|
if ("opacity" in mark && mark.opacity === 0) return [];
|
|
661
1657
|
if (mark.type === "line" || mark.type === "area")
|
|
662
1658
|
return (mark.dataPoints ?? []).map((point, index) => ({
|
|
@@ -708,7 +1704,14 @@ function createReader(container, instance, initial, onChange) {
|
|
|
708
1704
|
if (matches.length > 1) return null;
|
|
709
1705
|
if (matches.length === 1) index = matches[0];
|
|
710
1706
|
}
|
|
711
|
-
if (index >= 0)
|
|
1707
|
+
if (index >= 0) {
|
|
1708
|
+
const source = spec.data[index];
|
|
1709
|
+
const candidates = visibleData(instance.layout).filter(
|
|
1710
|
+
(row) => keyField ? observationKey(row.datum[keyField]) === identities[index].observation : Object.keys(source).every((field) => row.datum[field] === source[field])
|
|
1711
|
+
);
|
|
1712
|
+
const destination = candidates.length === 1 ? candidates[0].datum : datum;
|
|
1713
|
+
return { ...identities[index], datum: destination, state: "visible" };
|
|
1714
|
+
}
|
|
712
1715
|
const group = visibleData(instance.layout).find(
|
|
713
1716
|
(row) => row.datum === datum || JSON.stringify(row.datum) === JSON.stringify(datum)
|
|
714
1717
|
);
|
|
@@ -738,6 +1741,7 @@ function createReader(container, instance, initial, onChange) {
|
|
|
738
1741
|
emit();
|
|
739
1742
|
};
|
|
740
1743
|
const clear = () => {
|
|
1744
|
+
cancel();
|
|
741
1745
|
state = {
|
|
742
1746
|
preview: null,
|
|
743
1747
|
pinned: [],
|
|
@@ -749,11 +1753,11 @@ function createReader(container, instance, initial, onChange) {
|
|
|
749
1753
|
if (!spec) return null;
|
|
750
1754
|
const svg = container.querySelector("svg");
|
|
751
1755
|
if (!svg) return null;
|
|
752
|
-
const
|
|
753
|
-
if (!
|
|
1756
|
+
const bounds2 = usableBounds();
|
|
1757
|
+
if (!bounds2.width || !bounds2.height) return null;
|
|
754
1758
|
const layout = instance.sampleLayout();
|
|
755
|
-
const x = (event.clientX -
|
|
756
|
-
const y = (event.clientY -
|
|
1759
|
+
const x = (event.clientX - bounds2.left) * layout.dimensions.width / bounds2.width;
|
|
1760
|
+
const y = (event.clientY - bounds2.top) * layout.dimensions.height / bounds2.height;
|
|
757
1761
|
if (x < layout.area.x || x > layout.area.x + layout.area.width || y < layout.area.y || y > layout.area.y + layout.area.height)
|
|
758
1762
|
return null;
|
|
759
1763
|
let nearest = null;
|
|
@@ -765,7 +1769,7 @@ function createReader(container, instance, initial, onChange) {
|
|
|
765
1769
|
nearest = datum;
|
|
766
1770
|
}
|
|
767
1771
|
};
|
|
768
|
-
for (const mark of layout.marks) {
|
|
1772
|
+
for (const mark of layout.facet ? layout.facet.panels.flatMap((panel2) => panel2.marks) : layout.marks) {
|
|
769
1773
|
if ("opacity" in mark && mark.opacity === 0) continue;
|
|
770
1774
|
if (mark.type === "point") consider(mark.data, x - mark.cx, y - mark.cy);
|
|
771
1775
|
else if (mark.type === "rect")
|
|
@@ -790,26 +1794,29 @@ function createReader(container, instance, initial, onChange) {
|
|
|
790
1794
|
return nearest;
|
|
791
1795
|
}
|
|
792
1796
|
const preview = (event) => {
|
|
793
|
-
if (event.pointerType === "touch") return;
|
|
1797
|
+
if (!active() || readerEventExcluded(event.target) || event.pointerType === "touch") return;
|
|
794
1798
|
const datum = hit(event);
|
|
795
1799
|
state = { ...state, preview: datum ? resolve(datum) : null };
|
|
796
1800
|
emit();
|
|
797
1801
|
};
|
|
798
1802
|
const click = (event) => {
|
|
1803
|
+
if (!active() || readerEventExcluded(event.target) || cancelledTap) return;
|
|
799
1804
|
const datum = hit(event);
|
|
800
1805
|
if (datum) pin(datum);
|
|
801
1806
|
};
|
|
802
1807
|
const leave = () => {
|
|
1808
|
+
cancel();
|
|
803
1809
|
state = { ...state, preview: null };
|
|
804
1810
|
emit();
|
|
805
1811
|
};
|
|
806
1812
|
const keydown = (event) => {
|
|
807
|
-
if (!spec) return;
|
|
1813
|
+
if (!spec || !active() || readerEventExcluded(event.target)) return;
|
|
808
1814
|
if (event.target.closest(
|
|
809
1815
|
"input, textarea, select, button, [contenteditable=true]"
|
|
810
1816
|
))
|
|
811
1817
|
return;
|
|
812
1818
|
if (event.key === "Escape") {
|
|
1819
|
+
event.preventDefault();
|
|
813
1820
|
clear();
|
|
814
1821
|
return;
|
|
815
1822
|
}
|
|
@@ -828,29 +1835,95 @@ function createReader(container, instance, initial, onChange) {
|
|
|
828
1835
|
state = { ...state, preview: resolve(rows[keyboardIndex].datum) };
|
|
829
1836
|
emit();
|
|
830
1837
|
};
|
|
831
|
-
|
|
1838
|
+
let press = null;
|
|
1839
|
+
let cancelledTap = false;
|
|
1840
|
+
const down = (event) => {
|
|
1841
|
+
cancelledTap = false;
|
|
1842
|
+
if (!active() || readerEventExcluded(event.target) || !event.isPrimary && event.pointerType === "touch") {
|
|
1843
|
+
press = null;
|
|
1844
|
+
cancelledTap = true;
|
|
1845
|
+
return;
|
|
1846
|
+
}
|
|
1847
|
+
press = { x: event.clientX, y: event.clientY, id: event.pointerId };
|
|
1848
|
+
};
|
|
1849
|
+
const move = (event) => {
|
|
1850
|
+
if (press && Math.hypot(event.clientX - press.x, event.clientY - press.y) > 5)
|
|
1851
|
+
cancelledTap = true;
|
|
1852
|
+
pointerEvent = event;
|
|
1853
|
+
if (pointerFrame === null)
|
|
1854
|
+
pointerFrame = requestAnimationFrame(() => {
|
|
1855
|
+
pointerFrame = null;
|
|
1856
|
+
if (pointerEvent && !destroyed) preview(pointerEvent);
|
|
1857
|
+
pointerEvent = null;
|
|
1858
|
+
});
|
|
1859
|
+
};
|
|
1860
|
+
const cancel = () => {
|
|
1861
|
+
press = null;
|
|
1862
|
+
cancelledTap = true;
|
|
1863
|
+
if (pointerFrame !== null) cancelAnimationFrame(pointerFrame);
|
|
1864
|
+
pointerFrame = null;
|
|
1865
|
+
pointerEvent = null;
|
|
1866
|
+
};
|
|
1867
|
+
const scroll = () => {
|
|
1868
|
+
refreshBounds();
|
|
1869
|
+
cancel();
|
|
1870
|
+
};
|
|
1871
|
+
container.addEventListener("pointerdown", down);
|
|
1872
|
+
container.addEventListener("pointercancel", cancel);
|
|
1873
|
+
window.addEventListener("scroll", scroll, true);
|
|
1874
|
+
window.addEventListener("resize", refreshBounds);
|
|
1875
|
+
container.addEventListener("pointermove", move);
|
|
832
1876
|
container.addEventListener("pointerleave", leave);
|
|
833
1877
|
container.addEventListener("click", click);
|
|
834
|
-
container.addEventListener("keydown", keydown
|
|
1878
|
+
container.addEventListener("keydown", keydown);
|
|
1879
|
+
panel = createReaderPanel(
|
|
1880
|
+
options.panel ? options.panel.container : void 0,
|
|
1881
|
+
{
|
|
1882
|
+
clear,
|
|
1883
|
+
unpin(slot) {
|
|
1884
|
+
const index = slot === "A" ? 0 : 1;
|
|
1885
|
+
state = {
|
|
1886
|
+
...state,
|
|
1887
|
+
pinned: state.pinned.filter((_, i) => i !== index),
|
|
1888
|
+
announcement: "Observation unpinned."
|
|
1889
|
+
};
|
|
1890
|
+
emit();
|
|
1891
|
+
},
|
|
1892
|
+
pinPreview() {
|
|
1893
|
+
if (state.preview) pin(state.preview.datum);
|
|
1894
|
+
}
|
|
1895
|
+
},
|
|
1896
|
+
container
|
|
1897
|
+
);
|
|
1898
|
+
paint();
|
|
835
1899
|
if (!spec) emit();
|
|
836
|
-
|
|
1900
|
+
let unsubscribe;
|
|
1901
|
+
const controller = {
|
|
837
1902
|
get selection() {
|
|
838
1903
|
return state;
|
|
839
1904
|
},
|
|
840
1905
|
pin,
|
|
841
1906
|
clear,
|
|
842
1907
|
update(next, visibleKeys) {
|
|
1908
|
+
if (destroyed) return;
|
|
1909
|
+
refreshBounds();
|
|
1910
|
+
if (lease?.active && !instance.layout.a11y.hidden) {
|
|
1911
|
+
container.setAttribute("aria-label", instance.layout.a11y.altText);
|
|
1912
|
+
container.setAttribute("aria-roledescription", "chart");
|
|
1913
|
+
container.setAttribute("tabindex", "0");
|
|
1914
|
+
}
|
|
843
1915
|
if (settleFrame !== null) {
|
|
844
1916
|
cancelAnimationFrame(settleFrame);
|
|
845
1917
|
settleFrame = null;
|
|
846
1918
|
}
|
|
847
1919
|
visibilityOverride = visibleKeys;
|
|
848
1920
|
if ("layer" in next) {
|
|
1921
|
+
lease?.release();
|
|
849
1922
|
spec = null;
|
|
850
1923
|
identities = [];
|
|
851
1924
|
keyboardIndex = -1;
|
|
852
1925
|
state = { preview: null, pinned: [], announcement: LAYER_UNSUPPORTED };
|
|
853
|
-
emit();
|
|
1926
|
+
emit(visibilityOverride ? "filter" : "data");
|
|
854
1927
|
return;
|
|
855
1928
|
}
|
|
856
1929
|
const meaningChanged = !spec || JSON.stringify([next.mark, next.encoding, next.transform]) !== JSON.stringify([spec.mark, spec.encoding, spec.transform]);
|
|
@@ -863,7 +1936,16 @@ function createReader(container, instance, initial, onChange) {
|
|
|
863
1936
|
};
|
|
864
1937
|
}
|
|
865
1938
|
const replaced = !spec || next.data !== spec.data || next.encoding.key?.field !== spec.encoding.key?.field;
|
|
866
|
-
const resuming = !spec;
|
|
1939
|
+
const resuming = !spec || !lease;
|
|
1940
|
+
if (resuming) {
|
|
1941
|
+
lease = acquireReaderHost(instance, owner);
|
|
1942
|
+
subscribeLease();
|
|
1943
|
+
if (lease?.active && !instance.layout.a11y.hidden) {
|
|
1944
|
+
container.setAttribute("tabindex", "0");
|
|
1945
|
+
container.setAttribute("aria-label", instance.layout.a11y.altText);
|
|
1946
|
+
container.setAttribute("aria-roledescription", "chart");
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
867
1949
|
spec = next;
|
|
868
1950
|
identities = resolveObservationIdentities(
|
|
869
1951
|
spec.data,
|
|
@@ -889,9 +1971,16 @@ function createReader(container, instance, initial, onChange) {
|
|
|
889
1971
|
state: effectiveVisibility.has(item.observation) ? "visible" : "filtered"
|
|
890
1972
|
}))
|
|
891
1973
|
};
|
|
892
|
-
|
|
1974
|
+
state.pinned = state.pinned.map(
|
|
1975
|
+
(item) => item.state === "visible" && item.kind !== "aggregate" ? { ...item, datum: resolve(item.datum)?.datum ?? item.datum } : item
|
|
1976
|
+
);
|
|
1977
|
+
if (resuming)
|
|
1978
|
+
state = {
|
|
1979
|
+
...state,
|
|
1980
|
+
announcement: lease?.active ? "" : "Inspection is unavailable because another reader owns this chart."
|
|
1981
|
+
};
|
|
893
1982
|
if (replaced || meaningChanged) keyboardIndex = -1;
|
|
894
|
-
emit();
|
|
1983
|
+
emit(visibilityOverride ? "filter" : "data");
|
|
895
1984
|
const settle = () => {
|
|
896
1985
|
settleFrame = null;
|
|
897
1986
|
if (!state.pinned.some((item) => item.previous)) return;
|
|
@@ -900,16 +1989,594 @@ function createReader(container, instance, initial, onChange) {
|
|
|
900
1989
|
return;
|
|
901
1990
|
}
|
|
902
1991
|
state = { ...state, pinned: state.pinned.map(({ previous: _previous, ...item }) => item) };
|
|
903
|
-
emit();
|
|
1992
|
+
emit(visibilityOverride ? "filter" : "data");
|
|
904
1993
|
};
|
|
905
1994
|
settleFrame = requestAnimationFrame(settle);
|
|
906
1995
|
},
|
|
1996
|
+
setSelection(next, settings) {
|
|
1997
|
+
if (destroyed || JSON.stringify(next) === JSON.stringify(state)) return;
|
|
1998
|
+
state = { ...next, pinned: [...next.pinned].slice(-2) };
|
|
1999
|
+
if (settings?.notify) emit("programmatic");
|
|
2000
|
+
else {
|
|
2001
|
+
emitted = JSON.stringify(state);
|
|
2002
|
+
paint();
|
|
2003
|
+
}
|
|
2004
|
+
},
|
|
2005
|
+
setOptions(next) {
|
|
2006
|
+
if (destroyed) return;
|
|
2007
|
+
const revisionChanged = options.revision !== next.revision;
|
|
2008
|
+
const bindingChanged = JSON.stringify(options.binding) !== JSON.stringify(next.binding);
|
|
2009
|
+
if (revisionChanged || bindingChanged)
|
|
2010
|
+
state = {
|
|
2011
|
+
...state,
|
|
2012
|
+
preview: null,
|
|
2013
|
+
pinned: state.pinned.filter((pin2) => !bindingChanged && pin2.persistent),
|
|
2014
|
+
announcement: "Selection reconciled with the current data binding."
|
|
2015
|
+
};
|
|
2016
|
+
options = next;
|
|
2017
|
+
panel?.setContainer(options.panel ? options.panel.container : void 0);
|
|
2018
|
+
paint();
|
|
2019
|
+
},
|
|
907
2020
|
destroy() {
|
|
2021
|
+
if (destroyed) return;
|
|
2022
|
+
destroyed = true;
|
|
2023
|
+
cancel();
|
|
2024
|
+
unsubscribe?.();
|
|
2025
|
+
if (originalTabindex === null) container.removeAttribute("tabindex");
|
|
2026
|
+
else container.setAttribute("tabindex", originalTabindex);
|
|
2027
|
+
if (originalLabel === null) container.removeAttribute("aria-label");
|
|
2028
|
+
else container.setAttribute("aria-label", originalLabel);
|
|
2029
|
+
if (originalRole === null) container.removeAttribute("aria-roledescription");
|
|
2030
|
+
else container.setAttribute("aria-roledescription", originalRole);
|
|
2031
|
+
lease?.release();
|
|
2032
|
+
panel?.destroy();
|
|
2033
|
+
window.removeEventListener("scroll", scroll, true);
|
|
2034
|
+
window.removeEventListener("resize", refreshBounds);
|
|
2035
|
+
container.removeEventListener("pointerdown", down);
|
|
2036
|
+
container.removeEventListener("pointercancel", cancel);
|
|
908
2037
|
if (settleFrame !== null) cancelAnimationFrame(settleFrame);
|
|
909
|
-
container.removeEventListener("pointermove",
|
|
2038
|
+
container.removeEventListener("pointermove", move);
|
|
910
2039
|
container.removeEventListener("pointerleave", leave);
|
|
911
2040
|
container.removeEventListener("click", click);
|
|
912
|
-
container.removeEventListener("keydown", keydown
|
|
2041
|
+
container.removeEventListener("keydown", keydown);
|
|
2042
|
+
}
|
|
2043
|
+
};
|
|
2044
|
+
function subscribeLease() {
|
|
2045
|
+
unsubscribe?.();
|
|
2046
|
+
unsubscribe = lease?.subscribe((event) => {
|
|
2047
|
+
if (event.kind === "destroy") {
|
|
2048
|
+
controller.destroy();
|
|
2049
|
+
return;
|
|
2050
|
+
}
|
|
2051
|
+
if (event.kind === "gesture-start" || event.kind === "editing") {
|
|
2052
|
+
cancel();
|
|
2053
|
+
return;
|
|
2054
|
+
}
|
|
2055
|
+
if (event.kind === "layout") {
|
|
2056
|
+
const next = lease?.getSpec();
|
|
2057
|
+
controller.update(next && !("type" in next) ? next : spec ?? initial);
|
|
2058
|
+
}
|
|
2059
|
+
});
|
|
2060
|
+
}
|
|
2061
|
+
subscribeLease();
|
|
2062
|
+
return controller;
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
// src/slice-reader.ts
|
|
2066
|
+
import {
|
|
2067
|
+
buildReaderSliceIndex,
|
|
2068
|
+
nearestReaderSlice,
|
|
2069
|
+
readerSliceByKey,
|
|
2070
|
+
reconcileReaderSlices
|
|
2071
|
+
} from "@opendata-ai/openchart-engine";
|
|
2072
|
+
var NS2 = "http://www.w3.org/2000/svg";
|
|
2073
|
+
function createSliceReader(container, instance, initial, onChange, initialOptions) {
|
|
2074
|
+
let options = initialOptions, spec = initial, revision = 0, destroyed = false;
|
|
2075
|
+
let index = buildReaderSliceIndex(spec, instance.layout, {
|
|
2076
|
+
revision: options.revision ?? String(revision),
|
|
2077
|
+
binding: options.binding
|
|
2078
|
+
});
|
|
2079
|
+
let state = {
|
|
2080
|
+
kind: "slices",
|
|
2081
|
+
preview: null,
|
|
2082
|
+
pinned: [],
|
|
2083
|
+
announcement: index.supported ? "" : index.diagnostics.map((d) => d.message).join(" ")
|
|
2084
|
+
};
|
|
2085
|
+
let lease = null;
|
|
2086
|
+
const owner = {};
|
|
2087
|
+
let unsubscribe;
|
|
2088
|
+
let guide = null;
|
|
2089
|
+
let pointerFrame = null;
|
|
2090
|
+
let pendingPointer = null;
|
|
2091
|
+
let lastEmitted = "";
|
|
2092
|
+
let cancelledTap = false;
|
|
2093
|
+
let press = null;
|
|
2094
|
+
let bounds = container.querySelector("svg")?.getBoundingClientRect() ?? container.getBoundingClientRect();
|
|
2095
|
+
const refreshBounds = () => {
|
|
2096
|
+
const svg = container.querySelector("svg");
|
|
2097
|
+
const candidate = svg?.getBoundingClientRect();
|
|
2098
|
+
bounds = candidate?.width && candidate.height ? candidate : container.getBoundingClientRect();
|
|
2099
|
+
};
|
|
2100
|
+
refreshBounds();
|
|
2101
|
+
const active = () => !destroyed && index.supported && !instance.layout.a11y.hidden && !!lease?.active;
|
|
2102
|
+
const sourceSpec = () => {
|
|
2103
|
+
const current = lease?.getSpec();
|
|
2104
|
+
return current && !("type" in current) ? current : spec;
|
|
2105
|
+
};
|
|
2106
|
+
const cancel = () => {
|
|
2107
|
+
press = null;
|
|
2108
|
+
cancelledTap = true;
|
|
2109
|
+
pendingPointer = null;
|
|
2110
|
+
if (pointerFrame !== null) cancelAnimationFrame(pointerFrame);
|
|
2111
|
+
pointerFrame = null;
|
|
2112
|
+
};
|
|
2113
|
+
const candidates = () => index.panels.flatMap((panel2) => panel2.slices);
|
|
2114
|
+
function guides() {
|
|
2115
|
+
guide?.remove();
|
|
2116
|
+
guide = null;
|
|
2117
|
+
const svg = container.querySelector("svg");
|
|
2118
|
+
if (!svg || !index.supported) return;
|
|
2119
|
+
guide = document.createElementNS(NS2, "g");
|
|
2120
|
+
guide.setAttribute("class", "oc-reader-guides");
|
|
2121
|
+
guide.setAttribute("pointer-events", "none");
|
|
2122
|
+
guide.setAttribute("aria-hidden", "true");
|
|
2123
|
+
const slices = [
|
|
2124
|
+
...state.pinned,
|
|
2125
|
+
...state.preview && !state.pinned.some((pin2) => pin2.key === state.preview.key) ? [state.preview] : []
|
|
2126
|
+
];
|
|
2127
|
+
for (const slice of slices) {
|
|
2128
|
+
const panel2 = index.panels.find((panel3) => panel3.scope.id === slice.panel.id);
|
|
2129
|
+
const position = panel2?.slices.findIndex((item) => item.key === slice.key) ?? -1;
|
|
2130
|
+
if (!panel2 || position < 0 || slice.state === "filtered") continue;
|
|
2131
|
+
const x = panel2.positions[position];
|
|
2132
|
+
const line = document.createElementNS(NS2, "line");
|
|
2133
|
+
for (const [key2, value] of Object.entries({
|
|
2134
|
+
x1: x,
|
|
2135
|
+
x2: x,
|
|
2136
|
+
y1: panel2.area.y,
|
|
2137
|
+
y2: panel2.area.y + panel2.area.height,
|
|
2138
|
+
stroke: instance.layout.theme.colors.text,
|
|
2139
|
+
"stroke-opacity": state.pinned.some((pin2) => pin2.key === slice.key) ? 0.55 : 0.25,
|
|
2140
|
+
"stroke-width": 1
|
|
2141
|
+
}))
|
|
2142
|
+
line.setAttribute(key2, String(value));
|
|
2143
|
+
line.setAttribute("data-reader-slice", slice.key);
|
|
2144
|
+
guide.appendChild(line);
|
|
2145
|
+
const pinIndex = state.pinned.findIndex((pin2) => pin2.key === slice.key);
|
|
2146
|
+
if (pinIndex >= 0) {
|
|
2147
|
+
const label = document.createElementNS(NS2, "text");
|
|
2148
|
+
label.textContent = pinIndex === 0 ? "A" : "B";
|
|
2149
|
+
for (const [key2, value] of Object.entries({
|
|
2150
|
+
x: x > panel2.area.x + panel2.area.width - 20 ? x - 4 : x + 4,
|
|
2151
|
+
"text-anchor": x > panel2.area.x + panel2.area.width - 20 ? "end" : "start",
|
|
2152
|
+
y: panel2.area.y + instance.layout.theme.fonts.sizes.small,
|
|
2153
|
+
"font-family": instance.layout.theme.fonts.family,
|
|
2154
|
+
"font-size": instance.layout.theme.fonts.sizes.small,
|
|
2155
|
+
"font-weight": 600,
|
|
2156
|
+
fill: instance.layout.theme.colors.text
|
|
2157
|
+
}))
|
|
2158
|
+
label.setAttribute(key2, String(value));
|
|
2159
|
+
guide.appendChild(label);
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
svg.appendChild(guide);
|
|
2163
|
+
}
|
|
2164
|
+
const panel = createReaderPanel(
|
|
2165
|
+
options.panel ? options.panel.container : void 0,
|
|
2166
|
+
{
|
|
2167
|
+
clear: () => clear(),
|
|
2168
|
+
unpin(slot) {
|
|
2169
|
+
const i = slot === "A" ? 0 : 1;
|
|
2170
|
+
state = {
|
|
2171
|
+
...state,
|
|
2172
|
+
pinned: state.pinned.filter((_, index2) => index2 !== i),
|
|
2173
|
+
announcement: "Date unpinned."
|
|
2174
|
+
};
|
|
2175
|
+
emit("user");
|
|
2176
|
+
},
|
|
2177
|
+
pinPreview() {
|
|
2178
|
+
if (state.preview) pin(state.preview);
|
|
2179
|
+
}
|
|
2180
|
+
},
|
|
2181
|
+
container
|
|
2182
|
+
);
|
|
2183
|
+
const paint = () => {
|
|
2184
|
+
panel.setTheme(instance.layout.theme);
|
|
2185
|
+
panel.update(slicePanelModel(state, options));
|
|
2186
|
+
guides();
|
|
2187
|
+
};
|
|
2188
|
+
const emit = (reason) => {
|
|
2189
|
+
if (destroyed) return;
|
|
2190
|
+
paint();
|
|
2191
|
+
const next = JSON.stringify(state);
|
|
2192
|
+
if (next === lastEmitted) return;
|
|
2193
|
+
lastEmitted = next;
|
|
2194
|
+
onChange({ ...state, pinned: [...state.pinned] }, { reason });
|
|
2195
|
+
};
|
|
2196
|
+
function pin(slice) {
|
|
2197
|
+
if (destroyed) return;
|
|
2198
|
+
const current = readerSliceByKey(index, slice.key);
|
|
2199
|
+
if (!current) return;
|
|
2200
|
+
const existing = state.pinned.findIndex((pin2) => pin2.key === current.key);
|
|
2201
|
+
const pinned = existing >= 0 ? state.pinned.filter((pin2) => pin2.key !== current.key) : state.pinned.length < 2 ? [...state.pinned, current] : [state.pinned[0], current];
|
|
2202
|
+
state = {
|
|
2203
|
+
...state,
|
|
2204
|
+
preview: current,
|
|
2205
|
+
pinned,
|
|
2206
|
+
announcement: existing >= 0 ? `${current.xLabel} unpinned.` : `${current.xLabel} pinned as ${pinned.length === 1 ? "A" : "B"}.`
|
|
2207
|
+
};
|
|
2208
|
+
emit("user");
|
|
2209
|
+
}
|
|
2210
|
+
function clear() {
|
|
2211
|
+
if (destroyed) return;
|
|
2212
|
+
cancel();
|
|
2213
|
+
state = { kind: "slices", preview: null, pinned: [], announcement: "Selection dismissed." };
|
|
2214
|
+
emit("user");
|
|
2215
|
+
}
|
|
2216
|
+
function hit(clientX, clientY) {
|
|
2217
|
+
if (!bounds.width || !bounds.height) return null;
|
|
2218
|
+
const x = (clientX - bounds.left) * instance.layout.dimensions.width / bounds.width, y = (clientY - bounds.top) * instance.layout.dimensions.height / bounds.height;
|
|
2219
|
+
const panel2 = index.panels.find(
|
|
2220
|
+
(panel3) => x >= panel3.area.x && x <= panel3.area.x + panel3.area.width && y >= panel3.area.y && y <= panel3.area.y + panel3.area.height
|
|
2221
|
+
);
|
|
2222
|
+
return panel2 ? nearestReaderSlice(index, x, panel2.scope.id) : null;
|
|
2223
|
+
}
|
|
2224
|
+
const preview = (event) => {
|
|
2225
|
+
if (press && Math.hypot(event.clientX - press.x, event.clientY - press.y) > 5)
|
|
2226
|
+
cancelledTap = true;
|
|
2227
|
+
if (!active() || readerEventExcluded(event.target) || event.pointerType === "touch" || event.buttons)
|
|
2228
|
+
return;
|
|
2229
|
+
pendingPointer = { x: event.clientX, y: event.clientY };
|
|
2230
|
+
if (pointerFrame !== null) return;
|
|
2231
|
+
pointerFrame = requestAnimationFrame(() => {
|
|
2232
|
+
pointerFrame = null;
|
|
2233
|
+
if (!active() || !pendingPointer) return;
|
|
2234
|
+
const value = pendingPointer;
|
|
2235
|
+
pendingPointer = null;
|
|
2236
|
+
state = { ...state, preview: hit(value.x, value.y) };
|
|
2237
|
+
emit("user");
|
|
2238
|
+
});
|
|
2239
|
+
};
|
|
2240
|
+
const leave = () => {
|
|
2241
|
+
if (!active() || press?.touch) return;
|
|
2242
|
+
cancel();
|
|
2243
|
+
state = { ...state, preview: null };
|
|
2244
|
+
emit("user");
|
|
2245
|
+
};
|
|
2246
|
+
const down = (event) => {
|
|
2247
|
+
cancelledTap = false;
|
|
2248
|
+
if (!active() || readerEventExcluded(event.target) || press && press.id !== event.pointerId) {
|
|
2249
|
+
cancel();
|
|
2250
|
+
return;
|
|
2251
|
+
}
|
|
2252
|
+
press = {
|
|
2253
|
+
x: event.clientX,
|
|
2254
|
+
y: event.clientY,
|
|
2255
|
+
id: event.pointerId,
|
|
2256
|
+
touch: event.pointerType === "touch"
|
|
2257
|
+
};
|
|
2258
|
+
};
|
|
2259
|
+
const up = (event) => {
|
|
2260
|
+
if (!press) return;
|
|
2261
|
+
if (press.touch && !cancelledTap && active() && !readerEventExcluded(event.target)) {
|
|
2262
|
+
const slice = hit(event.clientX, event.clientY);
|
|
2263
|
+
if (slice) pin(slice);
|
|
2264
|
+
cancelledTap = true;
|
|
2265
|
+
}
|
|
2266
|
+
press = null;
|
|
2267
|
+
};
|
|
2268
|
+
const click = (event) => {
|
|
2269
|
+
if (!active() || cancelledTap || readerEventExcluded(event.target)) return;
|
|
2270
|
+
const slice = hit(event.clientX, event.clientY);
|
|
2271
|
+
if (slice) pin(slice);
|
|
2272
|
+
};
|
|
2273
|
+
const key = (event) => {
|
|
2274
|
+
if (!active() || readerEventExcluded(event.target)) return;
|
|
2275
|
+
if (event.key === "Escape") {
|
|
2276
|
+
event.preventDefault();
|
|
2277
|
+
clear();
|
|
2278
|
+
return;
|
|
2279
|
+
}
|
|
2280
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
2281
|
+
event.preventDefault();
|
|
2282
|
+
if (state.preview) pin(state.preview);
|
|
2283
|
+
else {
|
|
2284
|
+
const first = candidates()[0];
|
|
2285
|
+
if (first) {
|
|
2286
|
+
state = { ...state, preview: first };
|
|
2287
|
+
pin(first);
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
return;
|
|
2291
|
+
}
|
|
2292
|
+
if (!["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) return;
|
|
2293
|
+
const rows = candidates();
|
|
2294
|
+
if (!rows.length) return;
|
|
2295
|
+
event.preventDefault();
|
|
2296
|
+
const backwards = event.key === "ArrowLeft" || event.key === "ArrowUp";
|
|
2297
|
+
const current = rows.findIndex((row) => row.key === state.preview?.key);
|
|
2298
|
+
const next = current < 0 ? backwards ? rows.length - 1 : 0 : (current + (backwards ? -1 : 1) + rows.length) % rows.length;
|
|
2299
|
+
state = { ...state, preview: rows[next] };
|
|
2300
|
+
emit("user");
|
|
2301
|
+
};
|
|
2302
|
+
const blur = (event) => {
|
|
2303
|
+
if (event.relatedTarget instanceof Node && container.contains(event.relatedTarget)) return;
|
|
2304
|
+
leave();
|
|
2305
|
+
};
|
|
2306
|
+
const scroll = () => {
|
|
2307
|
+
refreshBounds();
|
|
2308
|
+
cancel();
|
|
2309
|
+
};
|
|
2310
|
+
for (const [name, listener] of [
|
|
2311
|
+
["pointermove", preview],
|
|
2312
|
+
["pointerdown", down],
|
|
2313
|
+
["pointerup", up],
|
|
2314
|
+
["pointercancel", cancel],
|
|
2315
|
+
["pointerleave", leave],
|
|
2316
|
+
["click", click],
|
|
2317
|
+
["keydown", key],
|
|
2318
|
+
["focusout", blur]
|
|
2319
|
+
])
|
|
2320
|
+
container.addEventListener(name, listener);
|
|
2321
|
+
window.addEventListener("scroll", scroll, true);
|
|
2322
|
+
window.addEventListener("resize", refreshBounds);
|
|
2323
|
+
const originalTabindex = container.getAttribute("tabindex");
|
|
2324
|
+
const originalLabel = container.getAttribute("aria-label");
|
|
2325
|
+
const originalRole = container.getAttribute("aria-roledescription");
|
|
2326
|
+
function acquire() {
|
|
2327
|
+
if (!index.supported) {
|
|
2328
|
+
lease?.release();
|
|
2329
|
+
return;
|
|
2330
|
+
}
|
|
2331
|
+
if (!lease?.active) {
|
|
2332
|
+
lease?.release();
|
|
2333
|
+
lease = acquireReaderHost(instance, owner);
|
|
2334
|
+
}
|
|
2335
|
+
if (!instance.layout.a11y.hidden && lease?.active) {
|
|
2336
|
+
container.setAttribute("tabindex", "0");
|
|
2337
|
+
container.setAttribute("aria-label", instance.layout.a11y.altText);
|
|
2338
|
+
container.setAttribute("aria-roledescription", "chart");
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
function diagnostics() {
|
|
2342
|
+
for (const diagnostic of index.diagnostics) options.onDiagnostic?.(diagnostic);
|
|
2343
|
+
}
|
|
2344
|
+
const controller = {
|
|
2345
|
+
get selection() {
|
|
2346
|
+
return state;
|
|
2347
|
+
},
|
|
2348
|
+
pin,
|
|
2349
|
+
clear,
|
|
2350
|
+
update(next, visibleKeys) {
|
|
2351
|
+
if (destroyed) return;
|
|
2352
|
+
const previousState = state;
|
|
2353
|
+
const dataReplaced = "layer" in spec || "layer" in next || next.data !== spec.data;
|
|
2354
|
+
spec = next;
|
|
2355
|
+
if (dataReplaced) revision++;
|
|
2356
|
+
const nextIndex = buildReaderSliceIndex(spec, instance.layout, {
|
|
2357
|
+
revision: options.revision ?? String(revision),
|
|
2358
|
+
binding: options.binding
|
|
2359
|
+
});
|
|
2360
|
+
state = reconcileReaderSlices(state, nextIndex, {
|
|
2361
|
+
dataReplaced,
|
|
2362
|
+
visibleObservationKeys: visibleKeys
|
|
2363
|
+
});
|
|
2364
|
+
if (!dataReplaced && !state.announcement) {
|
|
2365
|
+
const preview2 = previousState.preview ? readerSliceByKey(nextIndex, previousState.preview.key) : null;
|
|
2366
|
+
state = {
|
|
2367
|
+
...state,
|
|
2368
|
+
announcement: previousState.announcement,
|
|
2369
|
+
preview: preview2?.state === "visible" ? preview2 : null
|
|
2370
|
+
};
|
|
2371
|
+
}
|
|
2372
|
+
index = nextIndex;
|
|
2373
|
+
refreshBounds();
|
|
2374
|
+
acquire();
|
|
2375
|
+
diagnostics();
|
|
2376
|
+
const visibilityChanged = previousState.pinned.some(
|
|
2377
|
+
(before) => state.pinned.find((after) => after.key === before.key)?.state !== before.state
|
|
2378
|
+
);
|
|
2379
|
+
emit(visibleKeys || !dataReplaced && visibilityChanged ? "filter" : "data");
|
|
2380
|
+
},
|
|
2381
|
+
setSelection(next, settings) {
|
|
2382
|
+
if (destroyed || JSON.stringify(state) === JSON.stringify(next)) return;
|
|
2383
|
+
state = { ...next, kind: "slices", pinned: next.pinned.slice(0, 2) };
|
|
2384
|
+
if (settings?.notify) emit("programmatic");
|
|
2385
|
+
else {
|
|
2386
|
+
lastEmitted = JSON.stringify(state);
|
|
2387
|
+
paint();
|
|
2388
|
+
}
|
|
2389
|
+
},
|
|
2390
|
+
setOptions(next) {
|
|
2391
|
+
if (destroyed) return;
|
|
2392
|
+
const revisionChanged = next.revision !== options.revision;
|
|
2393
|
+
const bindingChanged = JSON.stringify(next.binding) !== JSON.stringify(options.binding);
|
|
2394
|
+
const rebuild = revisionChanged || bindingChanged;
|
|
2395
|
+
if (rebuild)
|
|
2396
|
+
state = {
|
|
2397
|
+
...state,
|
|
2398
|
+
preview: null,
|
|
2399
|
+
pinned: state.pinned.filter((pin2) => !bindingChanged && pin2.persistent),
|
|
2400
|
+
announcement: "Selection reconciled with the current data binding."
|
|
2401
|
+
};
|
|
2402
|
+
options = next;
|
|
2403
|
+
panel.setContainer(options.panel ? options.panel.container : void 0);
|
|
2404
|
+
if (rebuild) controller.update(spec);
|
|
2405
|
+
else paint();
|
|
2406
|
+
},
|
|
2407
|
+
destroy() {
|
|
2408
|
+
if (destroyed) return;
|
|
2409
|
+
destroyed = true;
|
|
2410
|
+
cancel();
|
|
2411
|
+
unsubscribe?.();
|
|
2412
|
+
if (originalLabel === null) container.removeAttribute("aria-label");
|
|
2413
|
+
else container.setAttribute("aria-label", originalLabel);
|
|
2414
|
+
if (originalRole === null) container.removeAttribute("aria-roledescription");
|
|
2415
|
+
else container.setAttribute("aria-roledescription", originalRole);
|
|
2416
|
+
lease?.release();
|
|
2417
|
+
panel.destroy();
|
|
2418
|
+
guide?.remove();
|
|
2419
|
+
for (const [name, listener] of [
|
|
2420
|
+
["pointermove", preview],
|
|
2421
|
+
["pointerdown", down],
|
|
2422
|
+
["pointerup", up],
|
|
2423
|
+
["pointercancel", cancel],
|
|
2424
|
+
["pointerleave", leave],
|
|
2425
|
+
["click", click],
|
|
2426
|
+
["keydown", key],
|
|
2427
|
+
["focusout", blur]
|
|
2428
|
+
])
|
|
2429
|
+
container.removeEventListener(name, listener);
|
|
2430
|
+
window.removeEventListener("scroll", scroll, true);
|
|
2431
|
+
window.removeEventListener("resize", refreshBounds);
|
|
2432
|
+
if (originalTabindex === null) container.removeAttribute("tabindex");
|
|
2433
|
+
else container.setAttribute("tabindex", originalTabindex);
|
|
2434
|
+
}
|
|
2435
|
+
};
|
|
2436
|
+
acquire();
|
|
2437
|
+
const subscriptionLease = lease ?? acquireReaderHost(instance, owner);
|
|
2438
|
+
if (!index.supported) subscriptionLease?.release();
|
|
2439
|
+
unsubscribe = subscriptionLease?.subscribe((event) => {
|
|
2440
|
+
if (event.kind === "destroy") controller.destroy();
|
|
2441
|
+
else if (event.kind === "gesture-start" || event.kind === "editing") cancel();
|
|
2442
|
+
else if (event.kind === "layout") controller.update(sourceSpec());
|
|
2443
|
+
});
|
|
2444
|
+
diagnostics();
|
|
2445
|
+
paint();
|
|
2446
|
+
if (!index.supported) emit("data");
|
|
2447
|
+
return controller;
|
|
2448
|
+
}
|
|
2449
|
+
|
|
2450
|
+
// src/reader.ts
|
|
2451
|
+
function createReader(container, instance, spec, onChange, options = {}) {
|
|
2452
|
+
return options.mode === "slices" ? createSliceReader(
|
|
2453
|
+
container,
|
|
2454
|
+
instance,
|
|
2455
|
+
spec,
|
|
2456
|
+
onChange,
|
|
2457
|
+
options
|
|
2458
|
+
) : createObservationReader(
|
|
2459
|
+
container,
|
|
2460
|
+
instance,
|
|
2461
|
+
spec,
|
|
2462
|
+
onChange,
|
|
2463
|
+
options
|
|
2464
|
+
);
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
// src/reader-binding.ts
|
|
2468
|
+
function createChartReaderBinding() {
|
|
2469
|
+
let controller, lastInstance, lastSpec, mode;
|
|
2470
|
+
let retained;
|
|
2471
|
+
let callback;
|
|
2472
|
+
const detach = () => {
|
|
2473
|
+
if (controller) {
|
|
2474
|
+
retained = controller.selection;
|
|
2475
|
+
controller.destroy();
|
|
2476
|
+
controller = void 0;
|
|
2477
|
+
}
|
|
2478
|
+
lastInstance = void 0;
|
|
2479
|
+
};
|
|
2480
|
+
return {
|
|
2481
|
+
sync(container, instance, spec, options, selection, onChange) {
|
|
2482
|
+
callback = onChange;
|
|
2483
|
+
if (!options) {
|
|
2484
|
+
detach();
|
|
2485
|
+
retained = void 0;
|
|
2486
|
+
mode = void 0;
|
|
2487
|
+
lastSpec = spec;
|
|
2488
|
+
return;
|
|
2489
|
+
}
|
|
2490
|
+
const nextMode = options.mode ?? "observations";
|
|
2491
|
+
if (instance !== lastInstance || nextMode !== mode) {
|
|
2492
|
+
const oldMode = mode;
|
|
2493
|
+
detach();
|
|
2494
|
+
if (nextMode !== oldMode) retained = void 0;
|
|
2495
|
+
mode = nextMode;
|
|
2496
|
+
lastInstance = instance;
|
|
2497
|
+
controller = nextMode === "slices" ? createReader(
|
|
2498
|
+
container,
|
|
2499
|
+
instance,
|
|
2500
|
+
lastSpec ?? spec,
|
|
2501
|
+
(state, meta) => callback?.(state, meta),
|
|
2502
|
+
{ ...options, mode: "slices" }
|
|
2503
|
+
) : createReader(
|
|
2504
|
+
container,
|
|
2505
|
+
instance,
|
|
2506
|
+
lastSpec ?? spec,
|
|
2507
|
+
(state, meta) => callback?.(state, meta),
|
|
2508
|
+
{ ...options, mode: "observations" }
|
|
2509
|
+
);
|
|
2510
|
+
if (retained) {
|
|
2511
|
+
if ("kind" in retained && nextMode === "slices")
|
|
2512
|
+
controller.setSelection(retained);
|
|
2513
|
+
else if (!("kind" in retained) && nextMode === "observations")
|
|
2514
|
+
controller.setSelection(retained);
|
|
2515
|
+
}
|
|
2516
|
+
if (lastSpec && lastSpec !== spec) controller.update(spec);
|
|
2517
|
+
}
|
|
2518
|
+
lastSpec = spec;
|
|
2519
|
+
controller.setOptions(options);
|
|
2520
|
+
if (selection) {
|
|
2521
|
+
if ("kind" in selection && mode === "slices")
|
|
2522
|
+
controller.setSelection(selection);
|
|
2523
|
+
else if (!("kind" in selection) && mode === "observations")
|
|
2524
|
+
controller.setSelection(selection);
|
|
2525
|
+
}
|
|
2526
|
+
},
|
|
2527
|
+
detach,
|
|
2528
|
+
destroy() {
|
|
2529
|
+
detach();
|
|
2530
|
+
retained = void 0;
|
|
2531
|
+
lastSpec = void 0;
|
|
2532
|
+
mode = void 0;
|
|
2533
|
+
callback = void 0;
|
|
2534
|
+
}
|
|
2535
|
+
};
|
|
2536
|
+
}
|
|
2537
|
+
function createGeoReaderBinding() {
|
|
2538
|
+
let controller, lastInstance, lastSpec, retained;
|
|
2539
|
+
let callback;
|
|
2540
|
+
const detach = () => {
|
|
2541
|
+
if (controller) {
|
|
2542
|
+
retained = controller.selection;
|
|
2543
|
+
controller.destroy();
|
|
2544
|
+
controller = void 0;
|
|
2545
|
+
}
|
|
2546
|
+
lastInstance = void 0;
|
|
2547
|
+
};
|
|
2548
|
+
return {
|
|
2549
|
+
sync(container, instance, spec, options, selection, onChange) {
|
|
2550
|
+
callback = onChange;
|
|
2551
|
+
if (!options) {
|
|
2552
|
+
detach();
|
|
2553
|
+
retained = void 0;
|
|
2554
|
+
lastSpec = spec;
|
|
2555
|
+
return;
|
|
2556
|
+
}
|
|
2557
|
+
if (instance !== lastInstance) {
|
|
2558
|
+
detach();
|
|
2559
|
+
lastInstance = instance;
|
|
2560
|
+
controller = createGeoReader(
|
|
2561
|
+
container,
|
|
2562
|
+
instance,
|
|
2563
|
+
lastSpec ?? spec,
|
|
2564
|
+
(state, meta) => callback?.(state, meta),
|
|
2565
|
+
options
|
|
2566
|
+
);
|
|
2567
|
+
if (retained) controller.setSelection(retained);
|
|
2568
|
+
if (lastSpec && lastSpec !== spec) controller.update(spec);
|
|
2569
|
+
}
|
|
2570
|
+
lastSpec = spec;
|
|
2571
|
+
controller.setOptions(options);
|
|
2572
|
+
if (selection) controller.setSelection(selection);
|
|
2573
|
+
},
|
|
2574
|
+
detach,
|
|
2575
|
+
destroy() {
|
|
2576
|
+
detach();
|
|
2577
|
+
retained = void 0;
|
|
2578
|
+
lastSpec = void 0;
|
|
2579
|
+
callback = void 0;
|
|
913
2580
|
}
|
|
914
2581
|
};
|
|
915
2582
|
}
|
|
@@ -1263,6 +2930,7 @@ function renderCell(cell) {
|
|
|
1263
2930
|
case "flag":
|
|
1264
2931
|
return renderFlagCell(cell);
|
|
1265
2932
|
default:
|
|
2933
|
+
cell;
|
|
1266
2934
|
return renderTextCell(cell);
|
|
1267
2935
|
}
|
|
1268
2936
|
}
|
|
@@ -1271,16 +2939,9 @@ function renderCell(cell) {
|
|
|
1271
2939
|
import { compileSankey } from "@opendata-ai/openchart-engine";
|
|
1272
2940
|
|
|
1273
2941
|
// src/sankey-renderer.ts
|
|
1274
|
-
import {
|
|
1275
|
-
BRAND_FONT_SIZE,
|
|
1276
|
-
BRAND_MIN_WIDTH,
|
|
1277
|
-
estimateTextWidth,
|
|
1278
|
-
wrapText
|
|
1279
|
-
} from "@opendata-ai/openchart-core";
|
|
2942
|
+
import { estimateTextWidth, wrapText } from "@opendata-ai/openchart-core";
|
|
1280
2943
|
import { clampStaggerDelay } from "@opendata-ai/openchart-engine";
|
|
1281
2944
|
var SVG_NS2 = "http://www.w3.org/2000/svg";
|
|
1282
|
-
var XLINK_NS2 = "http://www.w3.org/1999/xlink";
|
|
1283
|
-
var BRAND_URL2 = "https://tryopendata.ai";
|
|
1284
2945
|
function createSVGElement2(tag) {
|
|
1285
2946
|
return document.createElementNS(SVG_NS2, tag);
|
|
1286
2947
|
}
|
|
@@ -1335,49 +2996,6 @@ function renderChrome2(parent, layout) {
|
|
|
1335
2996
|
}
|
|
1336
2997
|
parent.appendChild(g);
|
|
1337
2998
|
}
|
|
1338
|
-
function renderBrand(parent, layout) {
|
|
1339
|
-
if (layout.dimensions.width < BRAND_MIN_WIDTH) return;
|
|
1340
|
-
const { width } = layout.dimensions;
|
|
1341
|
-
const padding = layout.theme.spacing.padding;
|
|
1342
|
-
const rightEdge = width - padding;
|
|
1343
|
-
const fill = layout.theme.colors.axis;
|
|
1344
|
-
const bottomOffset = layout.area.y + layout.area.height;
|
|
1345
|
-
const { chrome } = layout;
|
|
1346
|
-
const firstBottom = chrome.source ?? chrome.byline ?? chrome.footer;
|
|
1347
|
-
const chromeY = firstBottom ? bottomOffset + firstBottom.y : bottomOffset + layout.theme.spacing.chartToFooter;
|
|
1348
|
-
const a = createSVGElement2("a");
|
|
1349
|
-
a.setAttribute("href", BRAND_URL2);
|
|
1350
|
-
a.setAttributeNS(XLINK_NS2, "xlink:href", BRAND_URL2);
|
|
1351
|
-
a.setAttribute("target", "_blank");
|
|
1352
|
-
a.setAttribute("rel", "noopener");
|
|
1353
|
-
a.setAttribute("class", "oc-chrome-ref");
|
|
1354
|
-
const BRAND_LARGE = 16;
|
|
1355
|
-
const text = createSVGElement2("text");
|
|
1356
|
-
setAttrs2(text, {
|
|
1357
|
-
x: rightEdge,
|
|
1358
|
-
y: chromeY + BRAND_LARGE,
|
|
1359
|
-
"dominant-baseline": "alphabetic",
|
|
1360
|
-
"text-anchor": "end",
|
|
1361
|
-
"font-family": layout.theme.fonts.family,
|
|
1362
|
-
"font-size": BRAND_FONT_SIZE,
|
|
1363
|
-
"fill-opacity": 0.55
|
|
1364
|
-
});
|
|
1365
|
-
text.style.setProperty("fill", fill);
|
|
1366
|
-
const trySpan = createSVGElement2("tspan");
|
|
1367
|
-
setAttrs2(trySpan, { "font-weight": 500 });
|
|
1368
|
-
trySpan.textContent = "try";
|
|
1369
|
-
const openDataSpan = createSVGElement2("tspan");
|
|
1370
|
-
setAttrs2(openDataSpan, { "font-weight": 600, "font-size": BRAND_LARGE });
|
|
1371
|
-
openDataSpan.textContent = "OpenData";
|
|
1372
|
-
const aiSpan = createSVGElement2("tspan");
|
|
1373
|
-
setAttrs2(aiSpan, { "font-weight": 500 });
|
|
1374
|
-
aiSpan.textContent = ".ai";
|
|
1375
|
-
text.appendChild(trySpan);
|
|
1376
|
-
text.appendChild(openDataSpan);
|
|
1377
|
-
text.appendChild(aiSpan);
|
|
1378
|
-
a.appendChild(text);
|
|
1379
|
-
parent.appendChild(a);
|
|
1380
|
-
}
|
|
1381
2999
|
function renderLegend(parent, legendLayout) {
|
|
1382
3000
|
if (!("entries" in legendLayout) || legendLayout.entries.length === 0) return;
|
|
1383
3001
|
const legend = legendLayout;
|
|
@@ -1615,6 +3233,7 @@ function renderSankeySVG(layout, animation) {
|
|
|
1615
3233
|
const animate = !!enterPhase;
|
|
1616
3234
|
const classes = animate ? "oc-chart oc-sankey oc-animate" : "oc-chart oc-sankey";
|
|
1617
3235
|
svg.setAttribute("class", classes);
|
|
3236
|
+
stampThemeProperties(svg, layout.theme);
|
|
1618
3237
|
if (enterPhase) {
|
|
1619
3238
|
const totalMarks = layout.nodes.length + layout.links.length;
|
|
1620
3239
|
const stagger = clampStaggerDelay(enterPhase.staggerDelay, totalMarks);
|
|
@@ -1645,9 +3264,12 @@ function renderSankeySVG(layout, animation) {
|
|
|
1645
3264
|
renderLabels(svg, layout.nodes, layout.measureText);
|
|
1646
3265
|
renderLegend(svg, layout.legend);
|
|
1647
3266
|
renderChrome2(svg, layout);
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
3267
|
+
renderAttributionBrand(
|
|
3268
|
+
svg,
|
|
3269
|
+
layout.chrome,
|
|
3270
|
+
layout.chrome.bottomAnchorY ?? layout.area.y + layout.area.height,
|
|
3271
|
+
layout.measureText
|
|
3272
|
+
);
|
|
1651
3273
|
return svg;
|
|
1652
3274
|
}
|
|
1653
3275
|
|
|
@@ -1970,6 +3592,12 @@ function createSankey(container, spec, options) {
|
|
|
1970
3592
|
}
|
|
1971
3593
|
const terminalSvg = renderSankeySVG(currentLayout);
|
|
1972
3594
|
const resolvedOptions = { theme: currentLayout.theme, ...exportOptions };
|
|
3595
|
+
prepareSettledExportTheme(
|
|
3596
|
+
svgElement,
|
|
3597
|
+
terminalSvg,
|
|
3598
|
+
resolvedOptions.theme,
|
|
3599
|
+
!!exportOptions?.theme
|
|
3600
|
+
);
|
|
1973
3601
|
switch (format) {
|
|
1974
3602
|
case "svg":
|
|
1975
3603
|
return exportSVG(terminalSvg);
|
|
@@ -2054,7 +3682,7 @@ function createSankey(container, spec, options) {
|
|
|
2054
3682
|
}
|
|
2055
3683
|
);
|
|
2056
3684
|
container.dataset.ocFontsState = fontsPending ? "pending" : "ready";
|
|
2057
|
-
if (options?.
|
|
3685
|
+
if (options?.autoResize !== false) {
|
|
2058
3686
|
disconnectResize = observeResize(container, () => {
|
|
2059
3687
|
resize();
|
|
2060
3688
|
});
|
|
@@ -2320,9 +3948,7 @@ import { BREAKPOINT_COMPACT_MAX } from "@opendata-ai/openchart-core";
|
|
|
2320
3948
|
import { compileTable } from "@opendata-ai/openchart-engine";
|
|
2321
3949
|
|
|
2322
3950
|
// src/table-renderer.ts
|
|
2323
|
-
import { BRAND_FONT_SIZE as BRAND_FONT_SIZE2, cssTokenDefault } from "@opendata-ai/openchart-core";
|
|
2324
3951
|
import { clampStaggerDelay as clampStaggerDelay2 } from "@opendata-ai/openchart-engine";
|
|
2325
|
-
var BRAND_URL3 = "https://tryopendata.ai";
|
|
2326
3952
|
function renderChromeBlock(layout, position) {
|
|
2327
3953
|
const chrome = layout.chrome;
|
|
2328
3954
|
if (position === "header") {
|
|
@@ -2493,28 +4119,8 @@ function renderTable(layout, container, opts) {
|
|
|
2493
4119
|
const wrapper = document.createElement("div");
|
|
2494
4120
|
wrapper.className = "oc-table-wrapper";
|
|
2495
4121
|
const { theme, chrome } = layout;
|
|
2496
|
-
if (theme)
|
|
2497
|
-
|
|
2498
|
-
const n = theme.colors.neutral;
|
|
2499
|
-
s.setProperty("--oc-bg", resolvedSurface(theme));
|
|
2500
|
-
s.setProperty("--oc-text", theme.colors.text);
|
|
2501
|
-
s.setProperty("--oc-text-secondary", n.secondary);
|
|
2502
|
-
s.setProperty("--oc-text-muted", n[600]);
|
|
2503
|
-
s.setProperty("--oc-text-faint", n.faint);
|
|
2504
|
-
s.setProperty("--oc-gray-100", n[100]);
|
|
2505
|
-
s.setProperty("--oc-gray-200", n[200]);
|
|
2506
|
-
s.setProperty("--oc-gray-300", n[300]);
|
|
2507
|
-
s.setProperty("--oc-gray-400", n[400]);
|
|
2508
|
-
s.setProperty("--oc-gridline", theme.colors.gridline);
|
|
2509
|
-
const stripWs = (c) => c.replace(/\s+/g, "");
|
|
2510
|
-
const defaultGridline = cssTokenDefault("--oc-gridline", theme.isDark ? "dark" : "light");
|
|
2511
|
-
const gridlineOverridden = stripWs(theme.colors.gridline) !== stripWs(defaultGridline);
|
|
2512
|
-
s.setProperty("--oc-border", gridlineOverridden ? theme.colors.gridline : n.border);
|
|
2513
|
-
s.setProperty("--oc-positive", theme.colors.positive);
|
|
2514
|
-
s.setProperty("--oc-negative", theme.colors.negative);
|
|
2515
|
-
s.setProperty("--oc-font-family", theme.fonts.family);
|
|
2516
|
-
s.fontFamily = theme.fonts.family;
|
|
2517
|
-
}
|
|
4122
|
+
if (theme) stampThemeProperties(wrapper, theme, "table");
|
|
4123
|
+
if (theme) wrapper.style.background = theme.colors.background;
|
|
2518
4124
|
{
|
|
2519
4125
|
const s = wrapper.style;
|
|
2520
4126
|
if (chrome.title) {
|
|
@@ -2537,9 +4143,6 @@ function renderTable(layout, container, opts) {
|
|
|
2537
4143
|
}
|
|
2538
4144
|
}
|
|
2539
4145
|
wrapper.classList.add(`oc-table--${layout.density}`);
|
|
2540
|
-
if (layout.density === "condensed") {
|
|
2541
|
-
wrapper.classList.add("oc-table--compact");
|
|
2542
|
-
}
|
|
2543
4146
|
if (layout.striped) {
|
|
2544
4147
|
wrapper.classList.add("oc-table--striped");
|
|
2545
4148
|
}
|
|
@@ -2580,12 +4183,6 @@ function renderTable(layout, container, opts) {
|
|
|
2580
4183
|
if (pagination) {
|
|
2581
4184
|
wrapper.appendChild(pagination);
|
|
2582
4185
|
}
|
|
2583
|
-
const footerRow = document.createElement("div");
|
|
2584
|
-
footerRow.className = "oc-table-footer-row";
|
|
2585
|
-
const footerChrome = renderChromeBlock(layout, "footer");
|
|
2586
|
-
if (footerChrome) {
|
|
2587
|
-
footerRow.appendChild(footerChrome);
|
|
2588
|
-
}
|
|
2589
4186
|
const liveRegion = document.createElement("div");
|
|
2590
4187
|
liveRegion.className = "oc-table-live-region oc-sr-only";
|
|
2591
4188
|
applySrOnlyStyles(liveRegion);
|
|
@@ -2593,22 +4190,8 @@ function renderTable(layout, container, opts) {
|
|
|
2593
4190
|
liveRegion.setAttribute("aria-atomic", "true");
|
|
2594
4191
|
liveRegion.setAttribute("role", "status");
|
|
2595
4192
|
wrapper.appendChild(liveRegion);
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
const brand = document.createElement("div");
|
|
2599
|
-
brand.className = "oc-table-ref";
|
|
2600
|
-
const brandLink = document.createElement("a");
|
|
2601
|
-
brandLink.href = BRAND_URL3;
|
|
2602
|
-
brandLink.target = "_blank";
|
|
2603
|
-
brandLink.rel = "noopener";
|
|
2604
|
-
brandLink.style.cssText = `font-size: ${BRAND_FONT_SIZE2}px; font-weight: 600; color: ${brandColor}; opacity: 0.55; text-decoration: none; font-family: ${theme ? theme.fonts.family : "sans-serif"};`;
|
|
2605
|
-
brandLink.textContent = "OpenData";
|
|
2606
|
-
brand.appendChild(brandLink);
|
|
2607
|
-
footerRow.appendChild(brand);
|
|
2608
|
-
}
|
|
2609
|
-
if (footerRow.childElementCount > 0) {
|
|
2610
|
-
wrapper.appendChild(footerRow);
|
|
2611
|
-
}
|
|
4193
|
+
const footer = createAttributionFooter(layout.chrome, layout.width ?? 600, layout.measureText);
|
|
4194
|
+
if (footer) wrapper.appendChild(footer);
|
|
2612
4195
|
if (opts?.animate && layout.animation?.enter) {
|
|
2613
4196
|
const enterPhase = layout.animation.enter;
|
|
2614
4197
|
const rowCount = layout.rows.length;
|
|
@@ -2747,7 +4330,7 @@ function createTable(container, spec, options) {
|
|
|
2747
4330
|
currentLayout,
|
|
2748
4331
|
compact && currentSpec.mobileLayout === "priority"
|
|
2749
4332
|
);
|
|
2750
|
-
if (currentSpec.density !== void 0
|
|
4333
|
+
if (currentSpec.density !== void 0) return;
|
|
2751
4334
|
const columns = currentLayout?.columns.length ?? 0;
|
|
2752
4335
|
const condensed = columns * MIN_READABLE_COLUMN_WIDTH > width || width < AUTO_CONDENSE_MAX_WIDTH;
|
|
2753
4336
|
wrapperElement.classList.toggle("oc-table--condensed", condensed);
|
|
@@ -3040,7 +4623,7 @@ function createTable(container, spec, options) {
|
|
|
3040
4623
|
container.classList.remove("oc-dark");
|
|
3041
4624
|
}
|
|
3042
4625
|
render();
|
|
3043
|
-
if (options?.
|
|
4626
|
+
if (options?.autoResize !== false) {
|
|
3044
4627
|
disconnectResize = observeResize(container, () => {
|
|
3045
4628
|
if (resizeDebounceTimer !== null) {
|
|
3046
4629
|
clearTimeout(resizeDebounceTimer);
|
|
@@ -3273,6 +4856,7 @@ function createTileMap(container, spec, options) {
|
|
|
3273
4856
|
if (!svgElement) return "";
|
|
3274
4857
|
const terminalSvg = renderTileMapSVG(currentLayout, { animate: false });
|
|
3275
4858
|
const resolvedOptions = { theme: currentLayout.theme, ...options_ };
|
|
4859
|
+
prepareSettledExportTheme(svgElement, terminalSvg, resolvedOptions.theme, !!options_?.theme);
|
|
3276
4860
|
switch (format) {
|
|
3277
4861
|
case "svg":
|
|
3278
4862
|
return exportSVG(terminalSvg);
|
|
@@ -3326,7 +4910,7 @@ function createTileMap(container, spec, options) {
|
|
|
3326
4910
|
}
|
|
3327
4911
|
);
|
|
3328
4912
|
container.dataset.ocFontsState = fontsPending ? "pending" : "ready";
|
|
3329
|
-
if (options?.
|
|
4913
|
+
if (options?.autoResize !== false) {
|
|
3330
4914
|
disconnectResize = observeResize(container, () => {
|
|
3331
4915
|
resize();
|
|
3332
4916
|
});
|
|
@@ -3349,7 +4933,10 @@ export {
|
|
|
3349
4933
|
attachKeyboardNav,
|
|
3350
4934
|
createBarList,
|
|
3351
4935
|
createChart,
|
|
4936
|
+
createChartReaderBinding,
|
|
3352
4937
|
createGeoMap,
|
|
4938
|
+
createGeoReader,
|
|
4939
|
+
createGeoReaderBinding,
|
|
3353
4940
|
createGraph,
|
|
3354
4941
|
createReader,
|
|
3355
4942
|
createSankey,
|
|
@@ -3379,6 +4966,7 @@ export {
|
|
|
3379
4966
|
renderImageCell,
|
|
3380
4967
|
renderSparklineCell,
|
|
3381
4968
|
renderTable,
|
|
3382
|
-
renderTextCell
|
|
4969
|
+
renderTextCell,
|
|
4970
|
+
resolveInteractionMode
|
|
3383
4971
|
};
|
|
3384
4972
|
//# sourceMappingURL=index.js.map
|