-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathMaps.js
More file actions
435 lines (435 loc) · 18.1 KB
/
Maps.js
File metadata and controls
435 lines (435 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* React integration.
* Copyright (c) 2026, Highsoft
*
* A valid license is required for using this software.
* See highcharts.com/license
*
* Built for Highcharts v.xx.
* Build stamp: 2026-02-19
*
*/
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React, { forwardRef, useEffect, useRef, useImperativeHandle, useMemo,
// @ts-ignore
} from "react";
import HC from "highcharts/esm/highmaps.src.js";
// Add data-hc-option to allowed attributes
if (HC.AST.allowedAttributes.indexOf("data-hc-option") === -1) {
HC.AST.allowedAttributes.push("data-hc-option");
}
export let Highcharts = HC;
export const Logger = {
logLevel: "silent",
log(...content) {
if (this.logLevel === "debug") {
console.log(...content);
}
},
};
/**
* Sets the global Highcharts reference.
*
* If no argument is provided, resets Highcharts to the default instance.
*/
export function setHighcharts(newHC) {
if (newHC === undefined) {
Highcharts = HC;
delete Highcharts.__provided;
return;
}
Highcharts = newHC;
Highcharts.__provided = true;
}
/**
* Returns the current global Highcharts reference.
*
*/
export function getHighcharts() {
return Highcharts;
}
const toArr = (thing) => (Array.isArray(thing) ? thing.flat() : [thing]);
function getChildProps(children, renderHTML = undefined) {
const optionsFromChildren = {};
/** Insert value into object by dot.notation path */
function objInsert(obj, path, value = null) {
const keys = path.split(".");
let current = obj;
for (let i = 0; i < keys.length - 1; i++) {
const k = keys[i];
if (!current[k])
current[k] = {};
current = current[k];
}
current[keys[keys.length - 1]] = value;
return obj;
}
function renderChildren(arr) {
return renderHTML
? renderHTML(arr)
: arr.filter((c) => typeof c === "string").join("");
}
/** Type guard for React elements with meta */
function isReactElementWithMeta(value) {
return (typeof value === "object" &&
value !== null &&
"$$typeof" in value &&
"props" in value);
}
function updateChildMeta(childMeta, parentMeta) {
return Object.assign(Object.assign({}, childMeta), { childOption: parentMeta.childOption
? `${parentMeta.childOption}.${childMeta.childOption}`
: childMeta.childOption, HCOption: parentMeta.HCOption
? `${parentMeta.HCOption}.${childMeta.HCOption}`
: childMeta.HCOption });
}
/** Handles the Series component passed as a child. */
function handleSeriesChild(child, series) {
var _a, _b, _c, _d;
if (!isReactElementWithMeta(child)) {
return;
}
const _e = (_a = child.props) !== null && _a !== void 0 ? _a : {}, { children, options, type, data } = _e, seriesObj = __rest(_e, ["children", "options", "type", "data"]);
series.push(Object.assign({
type: type !== null && type !== void 0 ? type : (_d = (_c = (_b = child.type) === null || _b === void 0 ? void 0 : _b._HCReact) === null || _c === void 0 ? void 0 : _c.HCOption) === null || _d === void 0 ? void 0 : _d.replace("series.", ""),
data: data || [],
}, seriesObj, options, children && getChildProps(children, renderHTML)));
}
function handleChildren(children, obj, meta) {
var _a, _b, _c;
if (!children)
return;
if (meta.childOption === "series") {
const series = [];
const childArray = Array.isArray(children) ? children : [children];
for (const child of childArray) {
handleSeriesChild(child, series);
}
if (series.length > 0) {
objInsert(obj, "series", series);
}
return;
}
if (Array.isArray(children) &&
children.some((c) => { var _a; return (_a = c === null || c === void 0 ? void 0 : c.props) === null || _a === void 0 ? void 0 : _a["data-hc-option"]; })) {
const lostChildren = [];
for (const child of children) {
const optKey = (_a = child === null || child === void 0 ? void 0 : child.props) === null || _a === void 0 ? void 0 : _a["data-hc-option"];
if (optKey) {
objInsert(obj, optKey, renderChildren([child]));
}
else {
lostChildren.push(child);
}
}
if (lostChildren.length && meta.childOption) {
objInsert(obj, meta.childOption, renderChildren(lostChildren));
}
return;
}
const nonOptionChildren = [];
if (Array.isArray(children)) {
for (const c of children) {
if ((_b = c === null || c === void 0 ? void 0 : c.type) === null || _b === void 0 ? void 0 : _b._HCReact) {
const childMeta = c.type._HCReact;
c.type._HCReact = updateChildMeta(childMeta, meta);
handleChild(c);
continue;
}
nonOptionChildren.push(c);
}
}
else {
const c = children;
if ((_c = c === null || c === void 0 ? void 0 : c.type) === null || _c === void 0 ? void 0 : _c._HCReact) {
const childMeta = c.type._HCReact;
c.type._HCReact = updateChildMeta(childMeta, meta);
handleChild(c);
}
else {
nonOptionChildren.push(c);
}
}
if (meta.childOption) {
const childrenToRender = nonOptionChildren.length > 0 ? nonOptionChildren : [children];
objInsert(obj, meta.childOption, renderChildren(childrenToRender));
}
}
function handleChild(child) {
var _a, _b, _c, _d;
var _e;
if (!child || typeof child !== "object")
return;
const meta = (_a = child.type) === null || _a === void 0 ? void 0 : _a._HCReact;
if (!(meta === null || meta === void 0 ? void 0 : meta.HCOption))
return;
const optionParent = ((_b = optionsFromChildren[_e = meta.HCOption]) !== null && _b !== void 0 ? _b : (optionsFromChildren[_e] = meta.isArrayType ? [] : {}));
const parentIsArray = Array.isArray(optionParent);
const insertInto = parentIsArray ? {} : optionParent;
const _f = (_c = child.props) !== null && _c !== void 0 ? _c : {}, { children: childChildren } = _f, props = __rest(_f, ["children"]);
if (meta.defaultOptions)
Object.assign(insertInto, meta.defaultOptions);
Object.assign(insertInto, props);
if (typeof childChildren === "string" && meta.childOption) {
objInsert(insertInto, meta.childOption, childChildren);
}
else if (Array.isArray(childChildren)) {
handleChildren(childChildren, insertInto, meta);
}
else if (isReactElementWithMeta(childChildren) && renderHTML) {
if (((_d = childChildren.props) === null || _d === void 0 ? void 0 : _d.children) &&
Object.keys(childChildren.props).length === 1) {
handleChildren(childChildren.props.children, insertInto, meta);
}
else if (meta.childOption) {
objInsert(insertInto, meta.childOption, renderHTML(childChildren));
}
}
if (parentIsArray) {
optionsFromChildren[meta.HCOption].push(insertInto);
}
}
if (Array.isArray(children)) {
children.flat().forEach((c) => handleChild(c));
}
else {
handleChild(children);
}
return optionsFromChildren;
}
function renderToHTML(el) {
// React prop names to their HTML attribute equivalents map.
const ATTR_MAP = {
className: "class",
htmlFor: "for",
tabIndex: "tabindex",
colSpan: "colspan",
rowSpan: "rowspan",
dateTime: "datetime",
readOnly: "readonly",
maxLength: "maxlength",
minLength: "minlength",
};
// HTML void elements that don't have closing tags.
const VOID_TAGS = new Set([
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr",
]);
// HTML entity escape map.
const ESC_MAP = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
// Escapes special HTML characters in text content.
function esc(s) {
return s.replace(/[&<>"']/g, (c) => ESC_MAP[c]);
}
// Escapes special characters in attribute values (only quotes & ampersands).
function escAttr(s) {
return s.replace(/["&]/g, (c) => ESC_MAP[c]);
}
// Maps React prop names to HTML attribute names.
function mapAttrName(k) {
const name = ATTR_MAP[k] || k;
// Handle data-* and aria-* attributes.
if (name.startsWith("data-") || name.startsWith("aria-")) {
return name.toLowerCase();
}
// Handle data-* and aria-* like camelCase props.
if (name.startsWith("data") || name.startsWith("aria")) {
return name.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
}
return name;
}
// Recursively renders an element and its children to HTML.
function render(el) {
var _a;
// Return empty string for null/undefined, booleans, and non-React elements.
if (el === null || el === undefined || typeof el === "boolean") {
return "";
}
// Return escaped string for strings.
if (typeof el === "string") {
return esc(el);
}
// Return stringified number for numbers.
if (typeof el === "number") {
return String(el);
}
// Return concatenated string for arrays.
if (Array.isArray(el)) {
return el.map(render).join("");
}
const element = el;
// Return empty string for unknown element types.
if (!(element === null || element === void 0 ? void 0 : element.$$typeof)) {
return "";
}
// Call function components with props and render the result.
if (typeof element.type === "function") {
return render(element.type(element.props || {}));
}
// Render fragment components.
if (element.type === Symbol.for("react.fragment")) {
return render((_a = element.props) === null || _a === void 0 ? void 0 : _a.children);
}
// Render DOM elements (HTML tags).
if (typeof element.type === "string") {
const _b = element.props || {}, { children, key, ref, dangerouslySetInnerHTML, suppressContentEditableWarning, suppressHydrationWarning } = _b, props = __rest(_b, ["children", "key", "ref", "dangerouslySetInnerHTML", "suppressContentEditableWarning", "suppressHydrationWarning"]);
const tag = element.type;
const attrs = [];
// Build attribute string from props.
for (const [k, v] of Object.entries(props)) {
// Skip null or undefined values.
if (v === null || v === undefined) {
continue;
}
// Skip event handlers (onClick, onChange, etc.).
if (k.startsWith("on") && typeof v === "function") {
continue;
}
// Only add boolean attributes if true.
if (typeof v === "boolean") {
if (v) {
attrs.push(mapAttrName(k));
}
continue;
}
// Convert style objects to CSS string.
if (k === "style" && typeof v === "object" && !Array.isArray(v)) {
const css = Object.entries(v)
.filter(([_, val]) => val != null)
.map(([p, val]) => {
const prop = p.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
return `${prop}:${val}`;
})
.join(";");
if (css)
attrs.push(`style="${escAttr(css)}"`);
continue;
}
// Map React names to HTML and handle data-/aria- attributes.
attrs.push(`${mapAttrName(k)}="${escAttr(String(v))}"`);
}
const attrsStr = attrs.length ? " " + attrs.join(" ") : "";
// Void elements are self-closing (no children, no closing tag).
if (VOID_TAGS.has(tag)) {
return `<${tag}${attrsStr}>`;
}
// Regular elements with content.
const content = typeof dangerouslySetInnerHTML === "object" &&
dangerouslySetInnerHTML !== null &&
"__html" in dangerouslySetInnerHTML &&
dangerouslySetInnerHTML.__html != null
? String(dangerouslySetInnerHTML.__html)
: render(children);
return `<${tag}${attrsStr}>${content}</${tag}>`;
}
return "";
}
return render(el);
}
// React v20+ notice: forwardRef will be removed
// https://react.dev/blog/2024/12/05/react-19#ref-as-a-prop
export const MapsChart = forwardRef(function MapsChart(props, ref) {
if (props.highcharts) {
setHighcharts(props.highcharts);
}
const renderHTML = useMemo(() => {
var _a;
return (_a = props.renderToHTML) !== null && _a !== void 0 ? _a : renderToHTML;
}, [props.renderToHTML]);
const chartConfig = useMemo(() => Highcharts.merge(Object.assign({
title: { text: props.title || undefined },
chart: { allowMutatingData: false },
}, props.options || {}), Object.assign({ series: props.children
? toArr(props.children)
.filter((c) => { var _a; return ((_a = c === null || c === void 0 ? void 0 : c.type) === null || _a === void 0 ? void 0 : _a.type) === "Series"; })
.map((c) => {
var _a, _b, _c;
const { children, type, id, className, data, options } = c.props;
return Highcharts.merge(Object.assign(Object.assign({ type: type !== null && type !== void 0 ? type : (_c = (_b = (_a = c.type) === null || _a === void 0 ? void 0 : _a._HCReact) === null || _b === void 0 ? void 0 : _b.HCOption) === null || _c === void 0 ? void 0 : _c.replace("series.", ""), data: data || [] }, (id && { id })), (className && { className })), Object.assign(Object.assign({}, (options || {})), getChildProps(children, renderHTML)));
})
: [] }, getChildProps(props.children, renderHTML)), props.options || {}), [props.children, props.options, renderHTML]);
const containerRef = useRef();
const chartRef = useRef();
useImperativeHandle(ref, () => ({
get chart() {
return chartRef.current;
},
get container() {
return containerRef.current;
},
}), []);
// Update the chart on render
useEffect(() => {
Logger.log(JSON.stringify(chartConfig, undefined, " "));
/** Append prop to chart config */
const appendProps = (config) => {
var _a;
var _b;
(_a = (_b = config.title).text) !== null && _a !== void 0 ? _a : (_b.text = props.title);
};
/** Append series to chart config */
const appendSeries = () => {
// config: HC.Options
if (props.children) {
const children = toArr(props.children).filter((c) => { var _a; return ((_a = c === null || c === void 0 ? void 0 : c.type) === null || _a === void 0 ? void 0 : _a.type) === "Series"; });
children.forEach((c, i) => {
var _a, _b, _c;
if (c.props) {
const _d = c.props, { children, type, options } = _d, otherProps = __rest(_d, ["children", "type", "options"]);
if (options) {
chartConfig.series[i] = Highcharts.merge(chartConfig.series[i], options);
}
chartConfig.series[i] = Highcharts.merge(chartConfig.series[i], Object.assign(Object.assign({ type: type !== null && type !== void 0 ? type : (_c = (_b = (_a = c === null || c === void 0 ? void 0 : c.type) === null || _a === void 0 ? void 0 : _a._HCReact) === null || _b === void 0 ? void 0 : _b.HCOption) === null || _c === void 0 ? void 0 : _c.replace("series.", "") }, getChildProps(c.props.children, renderHTML)), otherProps));
}
});
}
};
if (!chartRef.current) {
const HCConstructor = props.chartConstructor || "mapChart";
Logger.log("Creating chart using", HCConstructor, "constructor");
chartRef.current = getHighcharts()[HCConstructor](containerRef.current, chartConfig);
}
else {
Logger.log("Updating chart", JSON.parse(JSON.stringify(chartConfig)));
appendProps(chartConfig);
appendSeries(); // chartConfig
chartRef.current.update(Object.assign(Object.assign({}, chartConfig), getChildProps(props.children, renderHTML)), true, true);
}
}, [chartConfig, props.chartConstructor, props.children, renderHTML]);
return React.createElement("div", Object.assign({}, props.containerProps, { ref: containerRef }));
});
export function MapsSeries(props) {
return null;
}
MapsSeries.type = "Series";
MapsChart.Series = MapsSeries;
export default MapsChart;
//# sourceMappingURL=Maps.js.map