pivotkit-react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +300 -0
- package/LICENSE +21 -0
- package/README.md +327 -2
- package/dist/chunks/chunk-C32TYSHG.js +441 -0
- package/dist/chunks/chunk-ILHFIV2P.js +784 -0
- package/dist/chunks/chunk-KKJO36W5.js +1833 -0
- package/dist/chunks/chunk-XYP47DJ3.js +123 -0
- package/dist/echarts.js +262 -0
- package/dist/index.js +3390 -0
- package/dist/labels.js +12 -0
- package/dist/menus.js +1085 -0
- package/dist/styles.css +2447 -0
- package/package.json +70 -4
- package/types/PivotReport.d.ts +20 -0
- package/types/all-labels.d.ts +5 -0
- package/types/chart-decorate.d.ts +16 -0
- package/types/chart-options.d.ts +23 -0
- package/types/chart-summary.d.ts +12 -0
- package/types/chart-tooltip.d.ts +38 -0
- package/types/chart-types.d.ts +152 -0
- package/types/chart.d.ts +39 -0
- package/types/chip-menus.d.ts +32 -0
- package/types/clear-input.d.ts +20 -0
- package/types/dnd-moves.d.ts +45 -0
- package/types/dnd.d.ts +101 -0
- package/types/dom.d.ts +52 -0
- package/types/drill-export.d.ts +42 -0
- package/types/drill-panel.d.ts +39 -0
- package/types/drill.d.ts +62 -0
- package/types/echarts.d.ts +4 -0
- package/types/export-run.d.ts +59 -0
- package/types/field-info.d.ts +34 -0
- package/types/fields.d.ts +32 -0
- package/types/files.d.ts +27 -0
- package/types/filter.d.ts +21 -0
- package/types/icons.d.ts +76 -0
- package/types/index.d.ts +536 -0
- package/types/info.d.ts +34 -0
- package/types/iso.d.ts +2 -0
- package/types/labels.d.ts +721 -0
- package/types/lazy-menus.d.ts +16 -0
- package/types/menu-labels.d.ts +255 -0
- package/types/menus.d.ts +7 -0
- package/types/names.d.ts +80 -0
- package/types/numbers.d.ts +13 -0
- package/types/overflow.d.ts +59 -0
- package/types/place.d.ts +43 -0
- package/types/popover.d.ts +89 -0
- package/types/rove.d.ts +20 -0
- package/types/section.d.ts +42 -0
- package/types/share.d.ts +123 -0
- package/types/skeleton.d.ts +9 -0
- package/types/storage.d.ts +24 -0
- package/types/table.d.ts +23 -0
- package/types/templates.d.ts +40 -0
- package/types/theme.d.ts +24 -0
- package/types/tip-list.d.ts +20 -0
- package/types/tips.d.ts +298 -0
- package/types/toasts.d.ts +26 -0
- package/types/toolbar-panels.d.ts +90 -0
- package/types/toolbar.d.ts +5 -0
- package/types/use-big-result.d.ts +29 -0
- package/types/use-export.d.ts +7 -0
- package/types/use-theme.d.ts +11 -0
- package/types/use-view-history.d.ts +34 -0
- package/types/version.d.ts +2 -0
- package/types/view-history.d.ts +89 -0
- package/types/view-ops.d.ts +344 -0
- package/types/view-store.d.ts +52 -0
- package/types/virtual.d.ts +44 -0
- package/types/zones.d.ts +42 -0
|
@@ -0,0 +1,1833 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CHART_TYPES,
|
|
3
|
+
MANY_SERIES,
|
|
4
|
+
SLICE_TYPES,
|
|
5
|
+
chartPlan
|
|
6
|
+
} from "./chunk-XYP47DJ3.js";
|
|
7
|
+
import {
|
|
8
|
+
fill
|
|
9
|
+
} from "./chunk-ILHFIV2P.js";
|
|
10
|
+
|
|
11
|
+
// src/names.js
|
|
12
|
+
import { fieldLabel } from "pivotkit-core";
|
|
13
|
+
function valueLabel(spec, labels) {
|
|
14
|
+
if (spec.label) {
|
|
15
|
+
return spec.label;
|
|
16
|
+
}
|
|
17
|
+
const core = labels.core;
|
|
18
|
+
let base;
|
|
19
|
+
if (!spec.field) {
|
|
20
|
+
base = core.countRows;
|
|
21
|
+
} else {
|
|
22
|
+
const agg = Object.hasOwn(core.aggregators, spec.agg) ? core.aggregators[spec.agg] : spec.agg;
|
|
23
|
+
base = fill(core.valueName, { agg, field: fieldLabel(spec.field, core) });
|
|
24
|
+
}
|
|
25
|
+
if (!spec.showAs || !core.showAs || !core.showAsName) {
|
|
26
|
+
return base;
|
|
27
|
+
}
|
|
28
|
+
const showAs = Object.hasOwn(core.showAs, spec.showAs.type) ? core.showAs[spec.showAs.type] : spec.showAs.type;
|
|
29
|
+
return fill(core.showAsName, { showAs, value: spec.field && spec.agg === "sum" ? fieldLabel(spec.field, core) : base });
|
|
30
|
+
}
|
|
31
|
+
function refLabel(view, ref, labels) {
|
|
32
|
+
if (!ref) {
|
|
33
|
+
return labels.core.countRows;
|
|
34
|
+
}
|
|
35
|
+
const sameField = (v) => ref.field === null || v.field === null || v.field === void 0 ? (v.field ?? null) === ref.field : v.field.toLowerCase() === ref.field.toLowerCase();
|
|
36
|
+
const spec = (view.values ?? []).find((v) => v.agg === ref.agg && sameField(v));
|
|
37
|
+
return valueLabel({ ...spec ?? ref, showAs: null }, labels);
|
|
38
|
+
}
|
|
39
|
+
var OP_SIGNS = Object.freeze({ "=": "=", "!=": "\u2260", ">": ">", ">=": "\u2265", "<": "<", "<=": "\u2264" });
|
|
40
|
+
function valueConditionText(view, f, labels, locale) {
|
|
41
|
+
const number = (x) => typeof x === "number" ? x.toLocaleString(locale, { maximumFractionDigits: 6 }) : "";
|
|
42
|
+
const value = refLabel(view, f.by ?? (view.values?.[0] ? { field: view.values[0].field ?? null, agg: view.values[0].agg } : null), labels);
|
|
43
|
+
if (f.op === "between" || f.op === "notBetween") {
|
|
44
|
+
const [from, to] = typeof f.value2 === "number" && f.value2 < f.value ? [f.value2, f.value] : [f.value, f.value2];
|
|
45
|
+
return fill(labels.valueConditions[f.op], { value, from: number(from), to: number(to) });
|
|
46
|
+
}
|
|
47
|
+
return fill(labels.valueConditions.compare, { value, op: Object.hasOwn(OP_SIGNS, f.op) ? OP_SIGNS[f.op] : f.op, number: number(f.value) });
|
|
48
|
+
}
|
|
49
|
+
function groupingName(source, unit, labels, size) {
|
|
50
|
+
const inName = labels.unitInName && Object.hasOwn(labels.unitInName, unit) ? labels.unitInName[unit] : labels.units[unit];
|
|
51
|
+
return unit === "bin" ? fill(labels.binsName, { field: source, size }) : fill(labels.derivedName, { field: source, unit: inName ?? unit });
|
|
52
|
+
}
|
|
53
|
+
var quickZone = (type) => type === "number" ? "values" : "rows";
|
|
54
|
+
function fieldAction(field, type, zones, labels) {
|
|
55
|
+
const zone = quickZone(type);
|
|
56
|
+
const kind = zone === "values" ? "add" : zones.includes(zone) ? null : zones.some((z) => z === "cols" || z === "filters") ? "move" : "add";
|
|
57
|
+
const name2 = zones.length ? `${field}, ${fill(labels.inZone, { zone: zones.map((z) => labels.zones[z] ?? z).join(", ") })}` : field;
|
|
58
|
+
const description = kind === null ? "" : fill(kind === "move" ? labels.moveTo : labels.addTo, { zone: labels.zones[zone] });
|
|
59
|
+
return { zone, kind, name: name2, description };
|
|
60
|
+
}
|
|
61
|
+
var SUMMARY_ORDER = Object.freeze(["rows", "cols", "values", "filters"]);
|
|
62
|
+
function layoutSummary(entries, labels) {
|
|
63
|
+
return SUMMARY_ORDER.filter((zone) => entries[zone] && entries[zone].length).map((zone) => ({ zone, title: labels.zones[zone], fields: entries[zone].map((e) => e.text).join(", ") }));
|
|
64
|
+
}
|
|
65
|
+
function filterText(view, field, state, labels, locale) {
|
|
66
|
+
const same2 = (f) => typeof f.field === "string" && f.field.toLowerCase() === field.toLowerCase();
|
|
67
|
+
const parts = [];
|
|
68
|
+
if (state && state.summary) {
|
|
69
|
+
parts.push(fill(labels.filterSummary, { shown: state.summary.shown.toLocaleString(locale), total: state.summary.total.toLocaleString(locale) }));
|
|
70
|
+
}
|
|
71
|
+
for (const f of view.filters.filter(same2)) {
|
|
72
|
+
if (f.type === "label") {
|
|
73
|
+
const op = Object.hasOwn(labels.labelOps, f.op) ? labels.labelOps[f.op] : f.op;
|
|
74
|
+
parts.push(`${op} \u201C${f.value}\u201D`);
|
|
75
|
+
} else if (f.type === "value") {
|
|
76
|
+
parts.push(valueConditionText(view, f, labels, locale));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return parts.length ? fill(labels.filterIs, { filter: parts.join(", ") }) : null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/drill.js
|
|
83
|
+
import { cellRows, formatter, isFormat } from "pivotkit-core";
|
|
84
|
+
var DRILL_LIMIT = 1e3;
|
|
85
|
+
function decodeRow(encoded, i) {
|
|
86
|
+
const row = {};
|
|
87
|
+
for (const column of encoded.columns) {
|
|
88
|
+
const code = column.codes[i];
|
|
89
|
+
const v = code < 0 ? null : column.dict[code];
|
|
90
|
+
const value = column.type === "date" && typeof v === "number" ? new Date(v).toISOString().replace(/(T00:00:00)?\.000Z$/, "").replace(/:00$/, "") : v;
|
|
91
|
+
Object.defineProperty(row, column.name, { value, enumerable: true, writable: true, configurable: true });
|
|
92
|
+
}
|
|
93
|
+
return row;
|
|
94
|
+
}
|
|
95
|
+
function drillRows(result, { rowPath = [], colPath = [], data = null, limit = DRILL_LIMIT }) {
|
|
96
|
+
const all = cellRows(result, { row: rowPath, col: colPath });
|
|
97
|
+
const indices = all.slice(0, Math.max(0, limit));
|
|
98
|
+
const rows = new Array(indices.length);
|
|
99
|
+
for (let k = 0; k < indices.length; k++) {
|
|
100
|
+
rows[k] = data ? data[indices[k]] : decodeRow(result.encoded, indices[k]);
|
|
101
|
+
}
|
|
102
|
+
return { count: all.length, rows, indices };
|
|
103
|
+
}
|
|
104
|
+
var DATE_FORMATS = /* @__PURE__ */ new Set(["date", "datetime", "time"]);
|
|
105
|
+
function columnText(column, { locale, labels, format }) {
|
|
106
|
+
const spec = typeof format === "string" && isFormat(format) ? format.trim().toLowerCase() : null;
|
|
107
|
+
if (column.type === "date") {
|
|
108
|
+
const f = formatter(spec && DATE_FORMATS.has(spec.split(":")[0]) ? spec : column.dateOnly ? "date" : "datetime", { locale });
|
|
109
|
+
return { text: f, numeric: false };
|
|
110
|
+
}
|
|
111
|
+
if (column.type === "number") {
|
|
112
|
+
const nf = new Intl.NumberFormat(locale, { maximumFractionDigits: 6 });
|
|
113
|
+
const f = spec && !DATE_FORMATS.has(spec.split(":")[0]) ? formatter(spec, { locale }) : (v) => nf.format(
|
|
114
|
+
/** @type {number} */
|
|
115
|
+
v
|
|
116
|
+
);
|
|
117
|
+
return { text: f, numeric: spec !== "text" };
|
|
118
|
+
}
|
|
119
|
+
if (column.type === "boolean") {
|
|
120
|
+
return { text: (v) => v ? labels.true : labels.false, numeric: false };
|
|
121
|
+
}
|
|
122
|
+
return { text: (v) => String(v), numeric: false };
|
|
123
|
+
}
|
|
124
|
+
function drillTable(encoded, indices, { locale, labels, formatOf = () => void 0 }) {
|
|
125
|
+
const cols = encoded.columns.map((column) => ({ column, ...columnText(column, { locale, labels, format: formatOf(column.name) }) }));
|
|
126
|
+
const rows = Array.from(
|
|
127
|
+
indices,
|
|
128
|
+
(i) => cols.map(({ column, text }) => {
|
|
129
|
+
const code = column.codes[i];
|
|
130
|
+
const v = code < 0 ? null : column.dict[code];
|
|
131
|
+
return v === null || v === void 0 ? "" : text(v);
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
return { columns: cols.map((c) => ({ name: c.column.name, numeric: c.numeric })), rows };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/icons.jsx
|
|
138
|
+
import React from "react";
|
|
139
|
+
var PATHS = {
|
|
140
|
+
grip: "M9 6h.01M9 12h.01M9 18h.01M15 6h.01M15 12h.01M15 18h.01",
|
|
141
|
+
text: "M4 18 8.5 6h1L14 18M5.6 14h6.8M17 11.5c.6-.7 1.4-1 2.3-1 1.5 0 2.2.9 2.2 2.3V18m0-3.4c-.8-.4-1.6-.5-2.5-.5-1.4 0-2.3.7-2.3 1.9 0 1.1.8 1.8 2 1.8 1.2 0 2.1-.6 2.8-1.6",
|
|
142
|
+
number: "M9 4 7 20M17 4l-2 16M4 9h16M3 15h16",
|
|
143
|
+
date: "M7 3v3M17 3v3M4 8h16M5 5h14a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1ZM8 12h2M14 12h2M8 16h2M14 16h2",
|
|
144
|
+
boolean: "M8 7h8a5 5 0 0 1 0 10H8A5 5 0 0 1 8 7ZM16 14a2 2 0 1 0 0-4 2 2 0 0 0 0 4Z",
|
|
145
|
+
search: "M11 18a7 7 0 1 0 0-14 7 7 0 0 0 0 14ZM20 20l-4-4",
|
|
146
|
+
x: "M6 6l12 12M18 6 6 18",
|
|
147
|
+
chevronDown: "m6 9 6 6 6-6",
|
|
148
|
+
chevronRight: "m9 6 6 6-6 6",
|
|
149
|
+
undo: "M9 14 4 9l5-5M4 9h10.5a5.5 5.5 0 0 1 0 11H11",
|
|
150
|
+
redo: "m15 14 5-5-5-5M20 9H9.5a5.5 5.5 0 0 0 0 11H13",
|
|
151
|
+
reset: "M3 12a9 9 0 1 0 3-6.7L3 8M3 3v5h5",
|
|
152
|
+
table: "M4 5h16v14H4zM4 10h16M4 15h16M10 5v14",
|
|
153
|
+
chart: "M4 20h16M7 16v-5M12 16V6M17 16v-8",
|
|
154
|
+
stacked: "M4 20h16M7 16v-3M7 13v-3M12 16v-4M12 12V6M17 16v-2M17 14V9",
|
|
155
|
+
horizontal: "M4 4v16M8 7h7M8 12h11M8 17h5",
|
|
156
|
+
line: "M4 18 9 12l4 3 7-9M4 20h16",
|
|
157
|
+
area: "M4 18 9 11l4 3 7-8v14H4Z",
|
|
158
|
+
pie: "M12 3a9 9 0 1 0 9 9h-9Z M15 3.5A9 9 0 0 1 20.5 9H15Z",
|
|
159
|
+
donut: "M12 3a9 9 0 1 0 9 9h-4a5 5 0 1 1-5-5Z M14.5 3.4A9 9 0 0 1 20.6 9.5h-4.1a5 5 0 0 0-2-2Z",
|
|
160
|
+
heatmap: "M4 4h7v7H4zM13 4h7v7h-7zM4 13h7v7H4zM13 13h7v7h-7z",
|
|
161
|
+
download: "M12 4v11M7 10l5 5 5-5M5 20h14",
|
|
162
|
+
bookmark: "M7 4h10a1 1 0 0 1 1 1v15l-6-4-6 4V5a1 1 0 0 1 1-1Z",
|
|
163
|
+
sliders: "M4 7h10M18 7h2M4 17h4M12 17h8M16 5v4M10 15v4",
|
|
164
|
+
filter: "M4 5h16l-6 7.5V19l-4-2v-4.5Z",
|
|
165
|
+
more: "M12 6h.01M12 12h.01M12 18h.01",
|
|
166
|
+
plus: "M12 5v14M5 12h14",
|
|
167
|
+
check: "m5 12 5 5 9-10",
|
|
168
|
+
sortAsc: "M7 4v16M3 8l4-4 4 4M14 6h7M14 12h5M14 18h3",
|
|
169
|
+
sortDesc: "M7 4v16M3 16l4 4 4-4M14 6h3M14 12h5M14 18h7",
|
|
170
|
+
panel: "M4 5h16v14H4zM9 5v14",
|
|
171
|
+
layout: "M4 5h7v6H4zM13 5h7v6h-7zM4 13h7v6H4zM13 13h7v6h-7z",
|
|
172
|
+
trash: "M5 7h14M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3",
|
|
173
|
+
save: "M5 4h11l3 3v13H5zM8 4v5h7V4M8 20v-6h8v6",
|
|
174
|
+
sparkle: "M12 4l1.8 4.6L18 10l-4.2 1.4L12 16l-1.8-4.6L6 10l4.2-1.4Z",
|
|
175
|
+
rows: "M4 6h16M4 12h16M4 18h16",
|
|
176
|
+
cols: "M6 4v16M12 4v16M18 4v16",
|
|
177
|
+
sigma: "M18 5H6l6 7-6 7h12",
|
|
178
|
+
info: "M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18ZM12 11v5M12 8h.01",
|
|
179
|
+
image: "M4 5h16v14H4zM4 16l5-5 4 4 3-3 4 4M15 9h.01",
|
|
180
|
+
file: "M6 3h8l4 4v14H6zM14 3v4h4",
|
|
181
|
+
swap: "M7 4 3 8l4 4M3 8h14M17 20l4-4-4-4M21 16H7",
|
|
182
|
+
top: "M4 20v-6h5v6M9 20V8h6v12M15 20v-9h5v9M3 20h18",
|
|
183
|
+
link: "M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1",
|
|
184
|
+
copy: "M8 8h11v12H8zM5 16V4h11",
|
|
185
|
+
upload: "M12 16V5M7 10l5-5 5 5M5 20h14",
|
|
186
|
+
drag: "M12 3v18M3 12h18M12 3l-3 3M12 3l3 3M12 21l-3-3M12 21l3-3M3 12l3-3M3 12l3 3M21 12l-3-3M21 12l-3 3",
|
|
187
|
+
enlarge: "M14 4h6v6M10 20H4v-6M20 4l-7 7M4 20l7-7",
|
|
188
|
+
fullScreen: "M3 8V3h5M21 8V3h-5M3 16v5h5M21 16v5h-5"
|
|
189
|
+
};
|
|
190
|
+
function Icon({ name: name2, size = 16, className }) {
|
|
191
|
+
return /* @__PURE__ */ React.createElement("svg", { className: className ? `pk-icon ${className}` : "pk-icon", width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.8", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", focusable: "false" }, /* @__PURE__ */ React.createElement("path", { d: PATHS[name2] ?? PATHS.info }));
|
|
192
|
+
}
|
|
193
|
+
var TYPE_ICON = { string: "text", number: "number", date: "date", boolean: "boolean", null: "text" };
|
|
194
|
+
var ZONE_ICON = { rows: "rows", cols: "cols", values: "sigma", filters: "filter" };
|
|
195
|
+
|
|
196
|
+
// src/clear-input.jsx
|
|
197
|
+
import React2, { useRef } from "react";
|
|
198
|
+
function emptyField(el) {
|
|
199
|
+
el.focus({ preventScroll: true });
|
|
200
|
+
let done;
|
|
201
|
+
try {
|
|
202
|
+
el.select();
|
|
203
|
+
const doc = (
|
|
204
|
+
/** @type {Document & { execCommand?: (cmd: string) => boolean }} */
|
|
205
|
+
el.ownerDocument
|
|
206
|
+
);
|
|
207
|
+
done = typeof doc.execCommand === "function" && doc.execCommand("delete") && el.value === "";
|
|
208
|
+
} catch {
|
|
209
|
+
done = false;
|
|
210
|
+
}
|
|
211
|
+
if (!done) {
|
|
212
|
+
const proto = el instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;
|
|
213
|
+
Object.getOwnPropertyDescriptor(proto, "value")?.set?.call(el, "");
|
|
214
|
+
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function ClearInput({ value, onChange, onClear, clearLabel, multiline = false, className, inputRef, ...rest }) {
|
|
218
|
+
const own = useRef(
|
|
219
|
+
/** @type {HTMLInputElement | HTMLTextAreaElement | null} */
|
|
220
|
+
null
|
|
221
|
+
);
|
|
222
|
+
const ref = inputRef ?? own;
|
|
223
|
+
const clear = () => {
|
|
224
|
+
if (ref.current) {
|
|
225
|
+
emptyField(ref.current);
|
|
226
|
+
}
|
|
227
|
+
onClear?.();
|
|
228
|
+
};
|
|
229
|
+
const onKeyDown = (e) => {
|
|
230
|
+
rest.onKeyDown?.(e);
|
|
231
|
+
if (e.key === "Escape" && value !== "" && !e.defaultPrevented) {
|
|
232
|
+
e.preventDefault();
|
|
233
|
+
e.stopPropagation();
|
|
234
|
+
clear();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const Tag = multiline ? "textarea" : "input";
|
|
238
|
+
return /* @__PURE__ */ React2.createElement("span", { className: `pk-clear-input${multiline ? " pk-clear-multi" : ""}${className ? ` ${className}` : ""}` }, /* @__PURE__ */ React2.createElement(Tag, { ...rest, ref, value, onChange: (e) => onChange(e.target.value), onKeyDown }), value !== "" ? /* @__PURE__ */ React2.createElement(
|
|
239
|
+
"button",
|
|
240
|
+
{
|
|
241
|
+
type: "button",
|
|
242
|
+
className: "pk-clear-x",
|
|
243
|
+
"aria-label": clearLabel,
|
|
244
|
+
title: clearLabel,
|
|
245
|
+
onPointerDown: (e) => e.preventDefault(),
|
|
246
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
247
|
+
onClick: clear,
|
|
248
|
+
"data-testid": "clear-input"
|
|
249
|
+
},
|
|
250
|
+
/* @__PURE__ */ React2.createElement(Icon, { name: "x", size: 14 })
|
|
251
|
+
) : null);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// src/dom.js
|
|
255
|
+
import { useEffect, useRef as useRef2 } from "react";
|
|
256
|
+
var FOCUSABLE = "button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], [tabindex]:not([tabindex='-1'])";
|
|
257
|
+
function rootOf(node) {
|
|
258
|
+
const root = node && typeof node.getRootNode === "function" ? node.getRootNode() : null;
|
|
259
|
+
if (root && (root.nodeType === 9 || root.nodeType === 11)) {
|
|
260
|
+
return (
|
|
261
|
+
/** @type {Document | ShadowRoot} */
|
|
262
|
+
root
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
return typeof document !== "undefined" ? document : null;
|
|
266
|
+
}
|
|
267
|
+
function activeIn(node) {
|
|
268
|
+
return rootOf(node)?.activeElement ?? null;
|
|
269
|
+
}
|
|
270
|
+
function eventWithin(event, elements) {
|
|
271
|
+
const path = typeof event.composedPath === "function" ? event.composedPath() : [];
|
|
272
|
+
return elements.some((el) => el && (path.includes(el) || event.target instanceof Node && el.contains(event.target)));
|
|
273
|
+
}
|
|
274
|
+
function tabbables(container) {
|
|
275
|
+
return (
|
|
276
|
+
/** @type {HTMLElement[]} */
|
|
277
|
+
[...container.querySelectorAll(FOCUSABLE)].filter((el) => !el.closest("[inert]") && el.getClientRects().length > 0)
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
function trapTab(event, container) {
|
|
281
|
+
if (event.key !== "Tab" || !container) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const list = tabbables(container);
|
|
285
|
+
if (list.length === 0) {
|
|
286
|
+
event.preventDefault();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const first = list[0];
|
|
290
|
+
const last = list[list.length - 1];
|
|
291
|
+
const active = activeIn(container);
|
|
292
|
+
if (event.shiftKey && (active === first || !container.contains(active))) {
|
|
293
|
+
event.preventDefault();
|
|
294
|
+
last.focus();
|
|
295
|
+
} else if (!event.shiftKey && (active === last || !container.contains(active))) {
|
|
296
|
+
event.preventDefault();
|
|
297
|
+
first.focus();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
var layers = [];
|
|
301
|
+
function useEscapeLayer(open, onClose) {
|
|
302
|
+
const close = useRef2(onClose);
|
|
303
|
+
useEffect(() => {
|
|
304
|
+
close.current = onClose;
|
|
305
|
+
});
|
|
306
|
+
useEffect(() => {
|
|
307
|
+
if (!open || typeof document === "undefined") {
|
|
308
|
+
return void 0;
|
|
309
|
+
}
|
|
310
|
+
const token = {};
|
|
311
|
+
layers.push(token);
|
|
312
|
+
const onKey = (e) => {
|
|
313
|
+
if (e.key === "Escape" && layers[layers.length - 1] === token && !e.defaultPrevented) {
|
|
314
|
+
e.preventDefault();
|
|
315
|
+
close.current();
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
document.addEventListener("keydown", onKey);
|
|
319
|
+
return () => {
|
|
320
|
+
document.removeEventListener("keydown", onKey);
|
|
321
|
+
layers.splice(layers.indexOf(token), 1);
|
|
322
|
+
};
|
|
323
|
+
}, [open]);
|
|
324
|
+
}
|
|
325
|
+
function useModal(ref, open, onClose, opener) {
|
|
326
|
+
useEscapeLayer(open, onClose);
|
|
327
|
+
useEffect(() => {
|
|
328
|
+
const el = ref.current;
|
|
329
|
+
if (!open || !el) {
|
|
330
|
+
return void 0;
|
|
331
|
+
}
|
|
332
|
+
const was = activeIn(el);
|
|
333
|
+
const plain = !was || was === el.ownerDocument.body || was.classList.contains("pk-root");
|
|
334
|
+
const back = plain && opener ? el.closest(".pk-root")?.querySelector(opener) ?? was : was;
|
|
335
|
+
const t = requestAnimationFrame(() => {
|
|
336
|
+
const first = (
|
|
337
|
+
/** @type {HTMLElement | null} */
|
|
338
|
+
el.querySelector("[data-autofocus]") ?? tabbables(el)[0] ?? el
|
|
339
|
+
);
|
|
340
|
+
first.focus({ preventScroll: true });
|
|
341
|
+
});
|
|
342
|
+
return () => {
|
|
343
|
+
cancelAnimationFrame(t);
|
|
344
|
+
const target = (
|
|
345
|
+
/** @type {HTMLElement | null} */
|
|
346
|
+
back
|
|
347
|
+
);
|
|
348
|
+
const now = activeIn(el);
|
|
349
|
+
if (target && target.isConnected && (!now || el.contains(now) || now === el.ownerDocument.body)) {
|
|
350
|
+
target.focus({ preventScroll: true });
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}, [ref, open, opener]);
|
|
354
|
+
return { onKeyDown: (e) => trapTab(e, ref.current) };
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// src/iso.js
|
|
358
|
+
import { useEffect as useEffect2, useLayoutEffect } from "react";
|
|
359
|
+
var useIsoLayoutEffect = typeof window !== "undefined" && typeof document !== "undefined" ? useLayoutEffect : useEffect2;
|
|
360
|
+
|
|
361
|
+
// src/popover.jsx
|
|
362
|
+
import React3, { createContext, useCallback, useContext, useEffect as useEffect3, useId, useRef as useRef3, useState } from "react";
|
|
363
|
+
import { createPortal } from "react-dom";
|
|
364
|
+
|
|
365
|
+
// src/place.js
|
|
366
|
+
var MARGIN = 8;
|
|
367
|
+
var GAP = 6;
|
|
368
|
+
function placePopover(anchor, size, viewport, align = "start", { fill: fill2 = false } = {}) {
|
|
369
|
+
const below = viewport.height - anchor.bottom - GAP - MARGIN;
|
|
370
|
+
const above = anchor.top - GAP - MARGIN;
|
|
371
|
+
const wanted = align === "end" ? anchor.right - size.width : anchor.left;
|
|
372
|
+
const left = Math.max(MARGIN, Math.min(wanted, viewport.width - size.width - MARGIN));
|
|
373
|
+
const most = Math.max(0, viewport.height - 2 * MARGIN);
|
|
374
|
+
if (fill2 && size.height > below && size.height > above) {
|
|
375
|
+
const height2 = Math.min(size.height, most);
|
|
376
|
+
const side2 = below >= above ? "below" : "above";
|
|
377
|
+
const near = side2 === "below" ? anchor.bottom + GAP : anchor.top - GAP - height2;
|
|
378
|
+
return { top: Math.max(MARGIN, Math.min(near, viewport.height - MARGIN - height2)), left, maxHeight: most, side: side2 };
|
|
379
|
+
}
|
|
380
|
+
const side = size.height <= below || below >= above ? "below" : "above";
|
|
381
|
+
const room = side === "below" ? below : above;
|
|
382
|
+
const height = Math.min(size.height, room);
|
|
383
|
+
const top = side === "below" ? anchor.bottom + GAP : anchor.top - GAP - height;
|
|
384
|
+
return { top: Math.max(MARGIN, top), left, maxHeight: Math.max(120, room), side };
|
|
385
|
+
}
|
|
386
|
+
function fromOrigin(place, origin) {
|
|
387
|
+
return { ...place, top: place.top - origin.top, left: place.left - origin.left };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// src/popover.jsx
|
|
391
|
+
var LayerContext = createContext({ layer: (
|
|
392
|
+
/** @type {HTMLElement | null} */
|
|
393
|
+
null
|
|
394
|
+
), size: "wide" });
|
|
395
|
+
var OPTION = "[data-pk-option]:not([disabled])";
|
|
396
|
+
function wheelScrollsInside(path, popover, dx, dy) {
|
|
397
|
+
for (const node of path) {
|
|
398
|
+
if (!(node instanceof Element)) {
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
const style = node.ownerDocument.defaultView?.getComputedStyle(node);
|
|
402
|
+
const scrolls = (overflow) => overflow === "auto" || overflow === "scroll";
|
|
403
|
+
if (style && scrolls(style.overflowY) && (dy < 0 && node.scrollTop > 0 || dy > 0 && node.scrollTop + node.clientHeight < node.scrollHeight - 1)) {
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
if (style && scrolls(style.overflowX) && (dx < 0 && node.scrollLeft > 0 || dx > 0 && node.scrollLeft + node.clientWidth < node.scrollWidth - 1)) {
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
if (node === popover) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
function Popover({ open, anchor, onClose, label, role = "dialog", align = "start", className, id, passive = false, fill: fill2 = false, onPointerEnter, onPointerLeave, children }) {
|
|
416
|
+
const { layer, size } = useContext(LayerContext);
|
|
417
|
+
const ref = useRef3(
|
|
418
|
+
/** @type {HTMLDivElement | null} */
|
|
419
|
+
null
|
|
420
|
+
);
|
|
421
|
+
const [pos, setPos] = useState(
|
|
422
|
+
/** @type {null | { top: number, left: number, maxHeight: number, side: string }} */
|
|
423
|
+
null
|
|
424
|
+
);
|
|
425
|
+
const sheet = size === "narrow" && !passive;
|
|
426
|
+
useIsoLayoutEffect(() => {
|
|
427
|
+
if (!open) {
|
|
428
|
+
setPos(null);
|
|
429
|
+
return void 0;
|
|
430
|
+
}
|
|
431
|
+
const place = () => {
|
|
432
|
+
const el2 = ref.current;
|
|
433
|
+
const a = anchor.current;
|
|
434
|
+
if (!el2 || !a || sheet) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const r = a.getBoundingClientRect();
|
|
438
|
+
const place2 = placePopover(r, { width: el2.offsetWidth, height: el2.scrollHeight }, { width: window.innerWidth, height: window.innerHeight }, align, { fill: fill2 });
|
|
439
|
+
const probe = layer?.querySelector(".pk-origin");
|
|
440
|
+
setPos(probe ? fromOrigin(place2, probe.getBoundingClientRect()) : place2);
|
|
441
|
+
};
|
|
442
|
+
place();
|
|
443
|
+
const el = ref.current;
|
|
444
|
+
const ro = typeof ResizeObserver === "function" && el ? new ResizeObserver(place) : null;
|
|
445
|
+
if (ro && el) {
|
|
446
|
+
ro.observe(el);
|
|
447
|
+
}
|
|
448
|
+
window.addEventListener("resize", place);
|
|
449
|
+
window.addEventListener("scroll", place, true);
|
|
450
|
+
return () => {
|
|
451
|
+
ro?.disconnect();
|
|
452
|
+
window.removeEventListener("resize", place);
|
|
453
|
+
window.removeEventListener("scroll", place, true);
|
|
454
|
+
};
|
|
455
|
+
}, [open, anchor, align, sheet, layer, fill2]);
|
|
456
|
+
useEscapeLayer(open, onClose);
|
|
457
|
+
const closeRef = useRef3(onClose);
|
|
458
|
+
useEffect3(() => {
|
|
459
|
+
closeRef.current = onClose;
|
|
460
|
+
});
|
|
461
|
+
useEffect3(() => {
|
|
462
|
+
if (!open || passive) {
|
|
463
|
+
return void 0;
|
|
464
|
+
}
|
|
465
|
+
const el = ref.current;
|
|
466
|
+
const shown = (o) => o.getClientRects().length > 0;
|
|
467
|
+
const first = el?.querySelector("[data-autofocus]") ?? [...el?.querySelectorAll(OPTION) ?? []].find(shown) ?? [...el?.querySelectorAll(FOCUSABLE) ?? []].find(shown);
|
|
468
|
+
const t = requestAnimationFrame(() => (
|
|
469
|
+
/** @type {HTMLElement | null} */
|
|
470
|
+
first?.focus({ preventScroll: true })
|
|
471
|
+
));
|
|
472
|
+
const onDown = (e) => {
|
|
473
|
+
if (!eventWithin(e, [el, anchor.current])) {
|
|
474
|
+
closeRef.current();
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
const doc = el?.ownerDocument ?? document;
|
|
478
|
+
doc.addEventListener("pointerdown", onDown, true);
|
|
479
|
+
const onWheel = (e) => {
|
|
480
|
+
if (el && !e.ctrlKey && !wheelScrollsInside(e.composedPath(), el, e.deltaX, e.deltaY)) {
|
|
481
|
+
e.preventDefault();
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
el?.addEventListener("wheel", onWheel, { passive: false });
|
|
485
|
+
const button = anchor.current;
|
|
486
|
+
return () => {
|
|
487
|
+
cancelAnimationFrame(t);
|
|
488
|
+
doc.removeEventListener("pointerdown", onDown, true);
|
|
489
|
+
el?.removeEventListener("wheel", onWheel);
|
|
490
|
+
const now = activeIn(button);
|
|
491
|
+
if (button && button.isConnected && (!now || now === doc.body || el?.contains(now) || !now.isConnected)) {
|
|
492
|
+
button.focus({ preventScroll: true });
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
}, [open, anchor, passive]);
|
|
496
|
+
if (!open || !layer) {
|
|
497
|
+
return null;
|
|
498
|
+
}
|
|
499
|
+
const onKeyDown = (e) => {
|
|
500
|
+
if (role === "menu" && e.key === "Tab") {
|
|
501
|
+
e.preventDefault();
|
|
502
|
+
onClose();
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (sheet) {
|
|
506
|
+
trapTab(e, ref.current);
|
|
507
|
+
}
|
|
508
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp" || e.key === "Home" || e.key === "End") {
|
|
509
|
+
const options = (
|
|
510
|
+
/** @type {HTMLElement[]} */
|
|
511
|
+
[...ref.current?.querySelectorAll(OPTION) ?? []].filter((o) => o.getClientRects().length > 0)
|
|
512
|
+
);
|
|
513
|
+
if (options.length === 0 || e.target instanceof HTMLInputElement && e.target.type !== "checkbox") {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
e.preventDefault();
|
|
517
|
+
const at = options.indexOf(
|
|
518
|
+
/** @type {HTMLElement} */
|
|
519
|
+
activeIn(ref.current)
|
|
520
|
+
);
|
|
521
|
+
const next = e.key === "Home" ? 0 : e.key === "End" ? options.length - 1 : (at + (e.key === "ArrowDown" ? 1 : -1) + options.length) % options.length;
|
|
522
|
+
options[next].focus();
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
const onBlur = (e) => {
|
|
526
|
+
const to = (
|
|
527
|
+
/** @type {Node | null} */
|
|
528
|
+
e.relatedTarget
|
|
529
|
+
);
|
|
530
|
+
if (to && !ref.current?.contains(to) && !anchor.current?.contains(to)) {
|
|
531
|
+
onClose();
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
const style = sheet ? void 0 : pos ? { top: pos.top, left: pos.left, maxHeight: pos.maxHeight } : { top: 0, left: 0, visibility: (
|
|
535
|
+
/** @type {const} */
|
|
536
|
+
"hidden"
|
|
537
|
+
) };
|
|
538
|
+
return createPortal(
|
|
539
|
+
/* @__PURE__ */ React3.createElement(React3.Fragment, null, sheet ? /* @__PURE__ */ React3.createElement("div", { className: "pk-scrim", onClick: onClose, "aria-hidden": "true" }) : null, /* @__PURE__ */ React3.createElement(
|
|
540
|
+
"div",
|
|
541
|
+
{
|
|
542
|
+
ref,
|
|
543
|
+
id,
|
|
544
|
+
role,
|
|
545
|
+
"aria-label": passive ? void 0 : label,
|
|
546
|
+
"aria-modal": sheet && role === "dialog" ? true : void 0,
|
|
547
|
+
className: `pk-popover${sheet ? " pk-sheet" : ""}${pos?.side === "above" ? " pk-above" : ""}${className ? ` ${className}` : ""}`,
|
|
548
|
+
style,
|
|
549
|
+
onKeyDown,
|
|
550
|
+
onBlur: sheet || passive ? void 0 : onBlur,
|
|
551
|
+
onPointerEnter,
|
|
552
|
+
onPointerLeave,
|
|
553
|
+
tabIndex: passive ? void 0 : -1
|
|
554
|
+
},
|
|
555
|
+
children
|
|
556
|
+
)),
|
|
557
|
+
layer
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
function MenuButton({ label, icon, text, showText = true, className, role = "menu", align, popoverClass, disabled, title, children, testId, buttonRef, name: name2, tabIndex, slot, fill: fill2 }) {
|
|
561
|
+
const [open, setOpen] = useState(false);
|
|
562
|
+
const own = useRef3(
|
|
563
|
+
/** @type {HTMLButtonElement | null} */
|
|
564
|
+
null
|
|
565
|
+
);
|
|
566
|
+
const ref = buttonRef ?? own;
|
|
567
|
+
const id = useId();
|
|
568
|
+
const close = useCallback(() => setOpen(false), []);
|
|
569
|
+
return /* @__PURE__ */ React3.createElement(React3.Fragment, null, /* @__PURE__ */ React3.createElement(
|
|
570
|
+
"button",
|
|
571
|
+
{
|
|
572
|
+
ref,
|
|
573
|
+
type: "button",
|
|
574
|
+
className: className ?? "pk-btn",
|
|
575
|
+
"aria-haspopup": role === "menu" ? "menu" : "dialog",
|
|
576
|
+
"aria-expanded": open,
|
|
577
|
+
"aria-controls": open ? id : void 0,
|
|
578
|
+
"aria-label": name2 ?? (showText && text ? void 0 : label),
|
|
579
|
+
title: title ?? (showText && text ? void 0 : label),
|
|
580
|
+
disabled,
|
|
581
|
+
tabIndex,
|
|
582
|
+
"data-pk-slot": slot,
|
|
583
|
+
"data-testid": testId,
|
|
584
|
+
onClick: () => setOpen((o) => !o)
|
|
585
|
+
},
|
|
586
|
+
icon ? /* @__PURE__ */ React3.createElement(Icon, { name: (
|
|
587
|
+
/** @type {any} */
|
|
588
|
+
icon
|
|
589
|
+
) }) : null,
|
|
590
|
+
showText && text ? /* @__PURE__ */ React3.createElement("span", { className: "pk-btn-text" }, text) : null,
|
|
591
|
+
showText && text ? /* @__PURE__ */ React3.createElement(Icon, { name: "chevronDown", size: 14, className: "pk-caret" }) : null
|
|
592
|
+
), /* @__PURE__ */ React3.createElement(Popover, { open, anchor: ref, onClose: close, label, role, align, className: popoverClass, id, fill: fill2 }, children(close)));
|
|
593
|
+
}
|
|
594
|
+
function Option({ onSelect, icon, checked, children, hint, reason, inMenu = false, disabled, danger, tone, testId }) {
|
|
595
|
+
const second = disabled && reason ? reason : hint;
|
|
596
|
+
const role = inMenu ? checked === void 0 ? "menuitem" : "menuitemradio" : void 0;
|
|
597
|
+
return /* @__PURE__ */ React3.createElement(
|
|
598
|
+
"button",
|
|
599
|
+
{
|
|
600
|
+
type: "button",
|
|
601
|
+
role,
|
|
602
|
+
"aria-checked": inMenu && checked !== void 0 ? checked : void 0,
|
|
603
|
+
"aria-pressed": !inMenu && checked !== void 0 ? checked : void 0,
|
|
604
|
+
className: `pk-option${checked ? " pk-checked" : ""}${danger ? " pk-danger" : ""}${tone === "accent" ? " pk-accent" : ""}`,
|
|
605
|
+
"data-pk-option": "",
|
|
606
|
+
"data-testid": testId,
|
|
607
|
+
disabled,
|
|
608
|
+
onClick: onSelect
|
|
609
|
+
},
|
|
610
|
+
/* @__PURE__ */ React3.createElement("span", { className: "pk-option-icon" }, checked ? /* @__PURE__ */ React3.createElement(Icon, { name: "check" }) : icon ? /* @__PURE__ */ React3.createElement(Icon, { name: (
|
|
611
|
+
/** @type {any} */
|
|
612
|
+
icon
|
|
613
|
+
) }) : null),
|
|
614
|
+
/* @__PURE__ */ React3.createElement("span", { className: "pk-option-text" }, children, second ? /* @__PURE__ */ React3.createElement("span", { className: "pk-option-hint" }, second) : null)
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// src/info.jsx
|
|
619
|
+
import React4, { useCallback as useCallback2, useEffect as useEffect4, useId as useId2, useRef as useRef4, useState as useState2 } from "react";
|
|
620
|
+
var LEAVE_DELAY = 250;
|
|
621
|
+
function FieldDescription({ info, labels, link = true, className = "pk-field-desc" }) {
|
|
622
|
+
return /* @__PURE__ */ React4.createElement("p", { className }, /* @__PURE__ */ React4.createElement("span", null, info.description), link && info.href ? /* @__PURE__ */ React4.createElement(React4.Fragment, null, " ", /* @__PURE__ */ React4.createElement("a", { href: info.href, target: "_blank", rel: "noopener noreferrer", className: "pk-learn-more" }, labels.learnMore)) : null);
|
|
623
|
+
}
|
|
624
|
+
function InfoButton({ info, name: name2, labels, className, testId = "field-info", label: named, tabIndex, slot }) {
|
|
625
|
+
const [mode, setMode] = useState2(
|
|
626
|
+
/** @type {null | "peek" | "open"} */
|
|
627
|
+
null
|
|
628
|
+
);
|
|
629
|
+
const ref = useRef4(
|
|
630
|
+
/** @type {HTMLButtonElement | null} */
|
|
631
|
+
null
|
|
632
|
+
);
|
|
633
|
+
const quiet = useRef4(false);
|
|
634
|
+
const leaving = useRef4(
|
|
635
|
+
/** @type {ReturnType<typeof setTimeout> | null} */
|
|
636
|
+
null
|
|
637
|
+
);
|
|
638
|
+
const id = useId2();
|
|
639
|
+
const textId = useId2();
|
|
640
|
+
const stay = () => {
|
|
641
|
+
if (leaving.current) {
|
|
642
|
+
clearTimeout(leaving.current);
|
|
643
|
+
leaving.current = null;
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
const leaveSoon = () => {
|
|
647
|
+
stay();
|
|
648
|
+
leaving.current = setTimeout(() => setMode((m) => m === "peek" ? null : m), LEAVE_DELAY);
|
|
649
|
+
};
|
|
650
|
+
useEffect4(() => stay, []);
|
|
651
|
+
const close = useCallback2(() => {
|
|
652
|
+
quiet.current = true;
|
|
653
|
+
setMode(null);
|
|
654
|
+
requestAnimationFrame(() => {
|
|
655
|
+
quiet.current = false;
|
|
656
|
+
});
|
|
657
|
+
}, []);
|
|
658
|
+
const label = named ?? fill(labels.aboutField, { field: name2 });
|
|
659
|
+
return /* @__PURE__ */ React4.createElement(React4.Fragment, null, /* @__PURE__ */ React4.createElement(
|
|
660
|
+
"button",
|
|
661
|
+
{
|
|
662
|
+
ref,
|
|
663
|
+
type: "button",
|
|
664
|
+
className: `pk-info${className ? ` ${className}` : ""}`,
|
|
665
|
+
"aria-label": label,
|
|
666
|
+
"aria-describedby": textId,
|
|
667
|
+
"aria-expanded": mode === "open",
|
|
668
|
+
"aria-controls": mode ? id : void 0,
|
|
669
|
+
"aria-haspopup": "dialog",
|
|
670
|
+
tabIndex,
|
|
671
|
+
"data-pk-slot": slot,
|
|
672
|
+
"data-testid": testId,
|
|
673
|
+
onPointerEnter: (e) => {
|
|
674
|
+
if (e.pointerType === "mouse") {
|
|
675
|
+
stay();
|
|
676
|
+
setMode((m) => m ?? "peek");
|
|
677
|
+
}
|
|
678
|
+
},
|
|
679
|
+
onPointerLeave: (e) => {
|
|
680
|
+
if (e.pointerType === "mouse" && mode === "peek") {
|
|
681
|
+
leaveSoon();
|
|
682
|
+
}
|
|
683
|
+
},
|
|
684
|
+
onFocus: () => {
|
|
685
|
+
if (mode === null && !quiet.current) {
|
|
686
|
+
setMode("peek");
|
|
687
|
+
}
|
|
688
|
+
},
|
|
689
|
+
onBlur: (e) => {
|
|
690
|
+
const to = (
|
|
691
|
+
/** @type {Element | null} */
|
|
692
|
+
e.relatedTarget
|
|
693
|
+
);
|
|
694
|
+
if (mode === "peek" && !(to && to.closest?.(".pk-info-pop"))) {
|
|
695
|
+
setMode(null);
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
onClick: () => {
|
|
699
|
+
stay();
|
|
700
|
+
setMode(mode === "open" ? null : "open");
|
|
701
|
+
}
|
|
702
|
+
},
|
|
703
|
+
/* @__PURE__ */ React4.createElement(Icon, { name: "info", size: 14 })
|
|
704
|
+
), /* @__PURE__ */ React4.createElement("span", { id: textId, hidden: true }, info.description), /* @__PURE__ */ React4.createElement(
|
|
705
|
+
Popover,
|
|
706
|
+
{
|
|
707
|
+
key: mode ?? "closed",
|
|
708
|
+
open: mode !== null,
|
|
709
|
+
passive: mode === "peek",
|
|
710
|
+
anchor: ref,
|
|
711
|
+
onClose: close,
|
|
712
|
+
label,
|
|
713
|
+
role: mode === "peek" ? "tooltip" : "dialog",
|
|
714
|
+
className: "pk-info-pop",
|
|
715
|
+
id,
|
|
716
|
+
onPointerEnter: stay,
|
|
717
|
+
onPointerLeave: (e) => {
|
|
718
|
+
if (e.pointerType === "mouse" && mode === "peek") {
|
|
719
|
+
leaveSoon();
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
},
|
|
723
|
+
/* @__PURE__ */ React4.createElement(FieldDescription, { info, labels })
|
|
724
|
+
));
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// src/view-ops.js
|
|
728
|
+
import { LIMITS, aggregatorsFor, isFormat as isFormat2 } from "pivotkit-core";
|
|
729
|
+
var ZONES = Object.freeze(
|
|
730
|
+
/** @type {Zone[]} */
|
|
731
|
+
["filters", "cols", "rows", "values"]
|
|
732
|
+
);
|
|
733
|
+
var clone = (view) => JSON.parse(JSON.stringify(view));
|
|
734
|
+
var same = (a, b) => typeof a === "string" && typeof b === "string" && a.toLowerCase() === b.toLowerCase();
|
|
735
|
+
var clamp = (i, n) => Number.isInteger(i) ? Math.max(0, Math.min(i, n)) : n;
|
|
736
|
+
function filterZoneFields(view) {
|
|
737
|
+
const seen = /* @__PURE__ */ new Set();
|
|
738
|
+
const out = [];
|
|
739
|
+
for (const f of view.filters) {
|
|
740
|
+
const key = f.field.toLowerCase();
|
|
741
|
+
if (!seen.has(key) && !view.rows.some((r) => same(r, f.field)) && !view.cols.some((c) => same(c, f.field))) {
|
|
742
|
+
seen.add(key);
|
|
743
|
+
out.push(f.field);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
return out;
|
|
747
|
+
}
|
|
748
|
+
function axisOf(view, field) {
|
|
749
|
+
if (view.rows.some((r) => same(r, field))) {
|
|
750
|
+
return "rows";
|
|
751
|
+
}
|
|
752
|
+
if (view.cols.some((c) => same(c, field))) {
|
|
753
|
+
return "cols";
|
|
754
|
+
}
|
|
755
|
+
return null;
|
|
756
|
+
}
|
|
757
|
+
function defaultAgg(type, wanted) {
|
|
758
|
+
if (typeof wanted === "string" && aggregatorsFor(type ?? "string").includes(wanted)) {
|
|
759
|
+
return wanted;
|
|
760
|
+
}
|
|
761
|
+
return type === "number" && aggregatorsFor(type).includes("sum") ? "sum" : "count";
|
|
762
|
+
}
|
|
763
|
+
function tidy(view) {
|
|
764
|
+
const used = new Set([...view.rows, ...view.cols, ...view.values.map((v) => v.field), ...view.filters.map((f) => f.field)].filter(Boolean).map((n) => n.toLowerCase()));
|
|
765
|
+
view.derived = view.derived.filter((d) => used.has(d.name.toLowerCase()));
|
|
766
|
+
view.sort = view.sort.filter((s) => used.has(s.field.toLowerCase()));
|
|
767
|
+
return view;
|
|
768
|
+
}
|
|
769
|
+
function dropFromAxes(view, field) {
|
|
770
|
+
view.rows = view.rows.filter((r) => !same(r, field));
|
|
771
|
+
view.cols = view.cols.filter((c) => !same(c, field));
|
|
772
|
+
}
|
|
773
|
+
function resetCollapsed(view, before) {
|
|
774
|
+
for (const axis of ["rows", "cols"]) {
|
|
775
|
+
if (JSON.stringify(before[axis]) !== JSON.stringify(view[axis])) {
|
|
776
|
+
view.collapsed[axis] = [];
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
function zoneOverLimit(view, limits) {
|
|
781
|
+
const limit = (name2) => limits && Number.isInteger(limits[name2]) && limits[name2] > 0 ? limits[name2] : LIMITS[name2];
|
|
782
|
+
for (
|
|
783
|
+
const [zone, name2] of
|
|
784
|
+
/** @type {const} */
|
|
785
|
+
[["rows", "axisFields"], ["cols", "axisFields"], ["values", "values"]]
|
|
786
|
+
) {
|
|
787
|
+
const list = view && Array.isArray(view[zone]) ? view[zone] : [];
|
|
788
|
+
if (list.length > limit(name2)) {
|
|
789
|
+
return { zone, max: limit(name2) };
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return null;
|
|
793
|
+
}
|
|
794
|
+
function addField(view, field, zone, options = {}) {
|
|
795
|
+
if (options.dateUnit && zone !== "values") {
|
|
796
|
+
const name2 = options.groupName ?? `${field} (${options.dateUnit})`;
|
|
797
|
+
const placed = addField(removeField(view, name2), field, zone, { ...options, dateUnit: null, defaults: void 0 });
|
|
798
|
+
return groupField(placed, field, options.dateUnit, { name: name2 }).view;
|
|
799
|
+
}
|
|
800
|
+
const next = clone(view);
|
|
801
|
+
if (zone === "rows" || zone === "cols") {
|
|
802
|
+
dropFromAxes(next, field);
|
|
803
|
+
next[zone].splice(clamp(options.index, next[zone].length), 0, field);
|
|
804
|
+
const order = options.defaults?.order;
|
|
805
|
+
if (Array.isArray(order) && order.length > 0 && !next.sort.some((s) => same(s.field, field))) {
|
|
806
|
+
next.sort.push({ field, by: "custom", order: "asc", items: [...order] });
|
|
807
|
+
}
|
|
808
|
+
} else if (zone === "values") {
|
|
809
|
+
const wanted = options.defaults ?? {};
|
|
810
|
+
const format = typeof wanted.format === "string" && isFormat2(wanted.format) ? wanted.format : null;
|
|
811
|
+
next.values.splice(clamp(options.index, next.values.length), 0, { field, agg: defaultAgg(options.type, wanted.agg), format, label: null, showAs: null, conditional: null });
|
|
812
|
+
} else if (zone === "filters") {
|
|
813
|
+
dropFromAxes(next, field);
|
|
814
|
+
if (!next.filters.some((f) => same(f.field, field))) {
|
|
815
|
+
next.filters.push({ field, type: "exclude", items: [] });
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
resetCollapsed(next, view);
|
|
819
|
+
return tidy(next);
|
|
820
|
+
}
|
|
821
|
+
function removeAt(view, zone, index) {
|
|
822
|
+
const next = clone(view);
|
|
823
|
+
if (zone === "values") {
|
|
824
|
+
next.values.splice(index, 1);
|
|
825
|
+
} else {
|
|
826
|
+
const list = zone === "filters" ? filterZoneFields(view) : view[zone];
|
|
827
|
+
const field = list[index];
|
|
828
|
+
if (field === void 0) {
|
|
829
|
+
return view;
|
|
830
|
+
}
|
|
831
|
+
dropFromAxes(next, field);
|
|
832
|
+
next.filters = next.filters.filter((f) => !same(f.field, field));
|
|
833
|
+
}
|
|
834
|
+
resetCollapsed(next, view);
|
|
835
|
+
return tidy(next);
|
|
836
|
+
}
|
|
837
|
+
function removeField(view, field) {
|
|
838
|
+
if (!axisOf(view, field) && !filterZoneFields(view).some((f) => same(f, field))) {
|
|
839
|
+
return view;
|
|
840
|
+
}
|
|
841
|
+
const next = clone(view);
|
|
842
|
+
dropFromAxes(next, field);
|
|
843
|
+
next.filters = next.filters.filter((f) => !same(f.field, field));
|
|
844
|
+
resetCollapsed(next, view);
|
|
845
|
+
return tidy(next);
|
|
846
|
+
}
|
|
847
|
+
function fieldAt(view, zone, index) {
|
|
848
|
+
if (zone === "values") {
|
|
849
|
+
return view.values[index] ? view.values[index].field : void 0;
|
|
850
|
+
}
|
|
851
|
+
return (zone === "filters" ? filterZoneFields(view) : view[zone])[index];
|
|
852
|
+
}
|
|
853
|
+
function moveField(view, from, fromIndex, to, toIndex, options = {}) {
|
|
854
|
+
if (from === to) {
|
|
855
|
+
return reorder(view, from, fromIndex, toIndex);
|
|
856
|
+
}
|
|
857
|
+
const field = fieldAt(view, from, fromIndex);
|
|
858
|
+
if (field === void 0 || field === null) {
|
|
859
|
+
return view;
|
|
860
|
+
}
|
|
861
|
+
let next = clone(view);
|
|
862
|
+
if (from === "values") {
|
|
863
|
+
next.values.splice(fromIndex, 1);
|
|
864
|
+
} else if (to !== "filters") {
|
|
865
|
+
dropFromAxes(next, field);
|
|
866
|
+
if (to === "values") {
|
|
867
|
+
next.filters = next.filters.filter((f) => !same(f.field, field));
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
next = addField(next, field, to, { ...options, index: toIndex });
|
|
871
|
+
resetCollapsed(next, view);
|
|
872
|
+
return tidy(next);
|
|
873
|
+
}
|
|
874
|
+
function reorder(view, zone, from, to) {
|
|
875
|
+
const list = zone === "filters" ? null : zone === "values" ? view.values : view[zone];
|
|
876
|
+
if (!list || from === to || from < 0 || from >= list.length) {
|
|
877
|
+
return view;
|
|
878
|
+
}
|
|
879
|
+
const next = clone(view);
|
|
880
|
+
const target = next[zone];
|
|
881
|
+
const [item] = target.splice(from, 1);
|
|
882
|
+
target.splice(clamp(to, target.length), 0, item);
|
|
883
|
+
resetCollapsed(next, view);
|
|
884
|
+
return next;
|
|
885
|
+
}
|
|
886
|
+
var COUNTING = /* @__PURE__ */ new Set(["count", "countUnique", "listUnique"]);
|
|
887
|
+
var unitOf = (agg) => COUNTING.has(agg) ? "count" : "field";
|
|
888
|
+
function updateValue(view, index, patch, options = {}) {
|
|
889
|
+
const next = clone(view);
|
|
890
|
+
if (!next.values[index]) {
|
|
891
|
+
return view;
|
|
892
|
+
}
|
|
893
|
+
const before = view.values[index];
|
|
894
|
+
const value = { ...next.values[index], ...patch };
|
|
895
|
+
if (patch.agg && patch.agg !== before.agg) {
|
|
896
|
+
value.showAs = patch.showAs ?? null;
|
|
897
|
+
const wanted = options.defaults ?? {};
|
|
898
|
+
const hostFormat = typeof wanted.format === "string" && isFormat2(wanted.format) ? wanted.format : null;
|
|
899
|
+
const own = before.format && before.format !== hostFormat ? before.format : null;
|
|
900
|
+
if (patch.format !== void 0) {
|
|
901
|
+
value.format = patch.format;
|
|
902
|
+
} else if (own) {
|
|
903
|
+
value.format = unitOf(before.agg) === unitOf(patch.agg) ? own : null;
|
|
904
|
+
} else {
|
|
905
|
+
value.format = hostFormat && unitOf(typeof wanted.agg === "string" ? wanted.agg : "sum") === unitOf(patch.agg) ? hostFormat : null;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
next.values[index] = value;
|
|
909
|
+
return next;
|
|
910
|
+
}
|
|
911
|
+
function numberRange(encoded, field) {
|
|
912
|
+
const column = encoded.column(field);
|
|
913
|
+
if (!column || column.type !== "number") {
|
|
914
|
+
return null;
|
|
915
|
+
}
|
|
916
|
+
let min = Infinity;
|
|
917
|
+
let max = -Infinity;
|
|
918
|
+
for (const v of column.dict) {
|
|
919
|
+
if (typeof v === "number" && Number.isFinite(v)) {
|
|
920
|
+
min = Math.min(min, v);
|
|
921
|
+
max = Math.max(max, v);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
return min <= max ? { min, max } : null;
|
|
925
|
+
}
|
|
926
|
+
function binProblem(size, start, range) {
|
|
927
|
+
if (!range) {
|
|
928
|
+
return null;
|
|
929
|
+
}
|
|
930
|
+
for (const v of [range.min, range.max]) {
|
|
931
|
+
const k = Math.floor((v - start) / size);
|
|
932
|
+
const low = start + k * size;
|
|
933
|
+
const slack = size * 1e-9;
|
|
934
|
+
if (!Number.isSafeInteger(k) || low + size === low || low > v + slack || v >= low + size + slack) {
|
|
935
|
+
return "far";
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return null;
|
|
939
|
+
}
|
|
940
|
+
function groupField(view, field, unit, options = {}) {
|
|
941
|
+
const spec = view.derived.find((d) => same(d.name, field));
|
|
942
|
+
const source = spec ? spec.field : field;
|
|
943
|
+
const axis = axisOf(view, field);
|
|
944
|
+
const next = clone(view);
|
|
945
|
+
let name2 = source;
|
|
946
|
+
if (unit) {
|
|
947
|
+
name2 = options.name ?? `${source} (${unit})`;
|
|
948
|
+
const existing = next.derived.find((d) => same(d.name, name2));
|
|
949
|
+
const origin = options.origin ?? (spec?.by === "bin" ? spec.origin ?? 0 : 0);
|
|
950
|
+
const wanted = unit === "bin" ? { name: name2, field: source, by: unit, size: options.size, origin } : { name: name2, field: source, by: unit };
|
|
951
|
+
if (existing) {
|
|
952
|
+
Object.assign(existing, wanted);
|
|
953
|
+
} else {
|
|
954
|
+
next.derived.push(wanted);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
if (axis) {
|
|
958
|
+
const other = axis === "rows" ? "cols" : "rows";
|
|
959
|
+
if (!same(name2, field) && next[other].some((f) => same(f, name2))) {
|
|
960
|
+
next[axis] = next[axis].filter((f) => !same(f, field));
|
|
961
|
+
} else {
|
|
962
|
+
const seen = /* @__PURE__ */ new Set();
|
|
963
|
+
next[axis] = next[axis].map((f) => same(f, field) ? name2 : f).filter((f) => !seen.has(f.toLowerCase()) && seen.add(f.toLowerCase()));
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
if (!same(name2, field)) {
|
|
967
|
+
const inFilterZone = !axis && filterZoneFields(view).some((f) => same(f, field));
|
|
968
|
+
next.filters = next.filters.filter((f) => !same(f.field, field));
|
|
969
|
+
if (inFilterZone) {
|
|
970
|
+
next.filters.push({ field: name2, type: "exclude", items: [] });
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
next.sort = next.sort.map((s) => same(s.field, field) ? { ...s, field: name2 } : s);
|
|
974
|
+
resetCollapsed(next, view);
|
|
975
|
+
return { view: tidy(next), field: name2 };
|
|
976
|
+
}
|
|
977
|
+
function groupingOf(view, field) {
|
|
978
|
+
return view.derived.find((d) => same(d.name, field)) ?? null;
|
|
979
|
+
}
|
|
980
|
+
function sourceOf(view, field) {
|
|
981
|
+
const spec = groupingOf(view, field);
|
|
982
|
+
return spec ? spec.field : field;
|
|
983
|
+
}
|
|
984
|
+
function setSort(view, field, sort) {
|
|
985
|
+
const next = clone(view);
|
|
986
|
+
next.sort = next.sort.filter((s) => !same(s.field, field));
|
|
987
|
+
if (sort) {
|
|
988
|
+
next.sort.push({ field, ...sort });
|
|
989
|
+
}
|
|
990
|
+
return next;
|
|
991
|
+
}
|
|
992
|
+
function withHostOrders(view, orderOf) {
|
|
993
|
+
const missing = [...view.rows, ...view.cols].filter((field) => !view.derived.some((d) => same(d.name, field)) && !view.sort.some((s) => same(s.field, field)));
|
|
994
|
+
const added = missing.map((field) => ({ field, order: orderOf(field) })).filter(({ order }) => Array.isArray(order) && order.length > 0);
|
|
995
|
+
if (!added.length) {
|
|
996
|
+
return view;
|
|
997
|
+
}
|
|
998
|
+
const next = clone(view);
|
|
999
|
+
for (const { field, order } of added) {
|
|
1000
|
+
next.sort.push({ field, by: "custom", order: "asc", items: [.../** @type {unknown[]} */
|
|
1001
|
+
order] });
|
|
1002
|
+
}
|
|
1003
|
+
return next;
|
|
1004
|
+
}
|
|
1005
|
+
function sortOf(view, field) {
|
|
1006
|
+
return view.sort.find((s) => same(s.field, field)) ?? null;
|
|
1007
|
+
}
|
|
1008
|
+
function filtersOf(view, field, types) {
|
|
1009
|
+
return view.filters.filter((f) => same(f.field, field) && (!types || types.includes(f.type)));
|
|
1010
|
+
}
|
|
1011
|
+
function replaceFilters(view, field, types, entries) {
|
|
1012
|
+
const next = clone(view);
|
|
1013
|
+
const index = next.filters.findIndex((f) => same(f.field, field));
|
|
1014
|
+
const kept = next.filters.filter((f) => !(same(f.field, field) && types.includes(f.type)));
|
|
1015
|
+
const at = index === -1 ? kept.length : Math.min(index, kept.length);
|
|
1016
|
+
kept.splice(at, 0, ...entries);
|
|
1017
|
+
next.filters = kept;
|
|
1018
|
+
if (!axisOf(next, field) && filterZoneFields(view).some((f) => same(f, field)) && !kept.some((f) => same(f.field, field))) {
|
|
1019
|
+
next.filters.splice(at, 0, { field, type: "exclude", items: [] });
|
|
1020
|
+
}
|
|
1021
|
+
return next;
|
|
1022
|
+
}
|
|
1023
|
+
function replaceFilter(view, field, previous, entry) {
|
|
1024
|
+
const next = clone(view);
|
|
1025
|
+
const key = previous ? JSON.stringify(previous) : null;
|
|
1026
|
+
const at = key === null ? -1 : next.filters.findIndex((f) => same(f.field, field) && JSON.stringify(f) === key);
|
|
1027
|
+
if (at === -1) {
|
|
1028
|
+
if (!entry) {
|
|
1029
|
+
return next;
|
|
1030
|
+
}
|
|
1031
|
+
const last = next.filters.map((f) => same(f.field, field)).lastIndexOf(true);
|
|
1032
|
+
next.filters.splice(last + 1, 0, entry);
|
|
1033
|
+
return next;
|
|
1034
|
+
}
|
|
1035
|
+
if (entry) {
|
|
1036
|
+
next.filters[at] = entry;
|
|
1037
|
+
} else {
|
|
1038
|
+
next.filters.splice(at, 1);
|
|
1039
|
+
if (!axisOf(next, field) && filterZoneFields(view).some((f) => same(f, field)) && !next.filters.some((f) => same(f.field, field))) {
|
|
1040
|
+
next.filters.splice(at, 0, { field, type: "exclude", items: [] });
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
return next;
|
|
1044
|
+
}
|
|
1045
|
+
function setItemFilter(view, field, selected, all) {
|
|
1046
|
+
const keep = new Set(selected.map((v) => JSON.stringify(v)));
|
|
1047
|
+
const excluded = all.filter((v) => !keep.has(JSON.stringify(v)));
|
|
1048
|
+
return replaceFilters(view, field, ["include", "exclude"], excluded.length ? [{ field, type: "exclude", items: excluded }] : []);
|
|
1049
|
+
}
|
|
1050
|
+
function selectedItems(view, field, all) {
|
|
1051
|
+
let selected = all;
|
|
1052
|
+
for (const f of filtersOf(view, field, ["include", "exclude"])) {
|
|
1053
|
+
const listed = new Set(f.items.map((v) => JSON.stringify(v)));
|
|
1054
|
+
selected = selected.filter((v) => listed.has(JSON.stringify(v)) === (f.type === "include"));
|
|
1055
|
+
}
|
|
1056
|
+
return selected;
|
|
1057
|
+
}
|
|
1058
|
+
function setLabelFilter(view, field, condition) {
|
|
1059
|
+
return replaceFilters(view, field, ["label"], condition && condition.value !== "" ? [{ field, type: "label", op: condition.op, value: condition.value }] : []);
|
|
1060
|
+
}
|
|
1061
|
+
function setTopFilter(view, field, top, previous) {
|
|
1062
|
+
const entry = top ? { field, type: "top", count: top.count, bottom: Boolean(top.bottom), ...top.by ? { by: top.by } : {} } : null;
|
|
1063
|
+
if (previous) {
|
|
1064
|
+
return replaceFilter(view, field, previous, entry);
|
|
1065
|
+
}
|
|
1066
|
+
return replaceFilters(view, field, ["top"], entry ? [entry] : []);
|
|
1067
|
+
}
|
|
1068
|
+
function setValueFilter(view, field, condition, previous) {
|
|
1069
|
+
let entry = null;
|
|
1070
|
+
if (condition) {
|
|
1071
|
+
const { by, op, value, value2 } = condition;
|
|
1072
|
+
const between = op === "between" || op === "notBetween";
|
|
1073
|
+
entry = { field, type: "value", by: { field: by.field, agg: by.agg }, op, value, ...between ? { value2 } : {} };
|
|
1074
|
+
}
|
|
1075
|
+
if (previous) {
|
|
1076
|
+
return replaceFilter(view, field, previous, entry);
|
|
1077
|
+
}
|
|
1078
|
+
return replaceFilters(view, field, ["value"], entry ? [entry] : []);
|
|
1079
|
+
}
|
|
1080
|
+
function numericResult(spec, type) {
|
|
1081
|
+
return spec.agg !== "listUnique" && !(["first", "last"].includes(spec.agg) && type !== "number") && !(type === "date" && ["average", "median", "min", "max"].includes(spec.agg));
|
|
1082
|
+
}
|
|
1083
|
+
function withMeasure(choices, ref) {
|
|
1084
|
+
return ref && !choices.some((c) => sameRef(c, ref)) ? [...choices, { field: ref.field ?? null, agg: ref.agg }] : choices;
|
|
1085
|
+
}
|
|
1086
|
+
function sameRef(a, b) {
|
|
1087
|
+
return Boolean(a && b) && a.agg === b.agg && (a.field === null || b.field === null ? a.field === b.field : same(a.field, b.field));
|
|
1088
|
+
}
|
|
1089
|
+
function measureChoices(view, typeOf) {
|
|
1090
|
+
const out = [];
|
|
1091
|
+
for (const v of view.values) {
|
|
1092
|
+
const ref = { field: v.field ?? null, agg: v.agg };
|
|
1093
|
+
if (numericResult(v, v.field ? typeOf(v.field) ?? "string" : "number") && !out.some((r) => sameRef(r, ref))) {
|
|
1094
|
+
out.push(ref);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return out.length ? out : [{ field: null, agg: "count" }];
|
|
1098
|
+
}
|
|
1099
|
+
function removeFilters(view, field) {
|
|
1100
|
+
return replaceFilters(view, field, ["include", "exclude", "label", "value"], []);
|
|
1101
|
+
}
|
|
1102
|
+
function hasFilter(view, field) {
|
|
1103
|
+
return filtersOf(view, field, ["include", "exclude", "label", "value"]).some((f) => f.type !== "exclude" || f.items.length > 0);
|
|
1104
|
+
}
|
|
1105
|
+
function isFiltered(view, field) {
|
|
1106
|
+
return filtersOf(view, field).some((f) => f.type !== "exclude" || f.items.length > 0);
|
|
1107
|
+
}
|
|
1108
|
+
function toggleCollapsed(view, axis, path) {
|
|
1109
|
+
const next = clone(view);
|
|
1110
|
+
const key = JSON.stringify(path);
|
|
1111
|
+
const list = next.collapsed[axis];
|
|
1112
|
+
const at = list.findIndex((p) => JSON.stringify(p) === key);
|
|
1113
|
+
if (at === -1) {
|
|
1114
|
+
list.push(path);
|
|
1115
|
+
} else {
|
|
1116
|
+
list.splice(at, 1);
|
|
1117
|
+
}
|
|
1118
|
+
return next;
|
|
1119
|
+
}
|
|
1120
|
+
function rowFolding(view, outer) {
|
|
1121
|
+
const keys = new Set(view.collapsed.rows.map((p) => JSON.stringify(p)));
|
|
1122
|
+
const collapsed = outer.filter((p) => keys.has(JSON.stringify(p))).length;
|
|
1123
|
+
const open = view.rows.length < 2 ? 0 : outer.length - collapsed;
|
|
1124
|
+
const canExpand = view.collapsed.rows.length > 0;
|
|
1125
|
+
const canCollapse = open > 0;
|
|
1126
|
+
return {
|
|
1127
|
+
canExpand,
|
|
1128
|
+
canCollapse,
|
|
1129
|
+
collapsed: view.collapsed.rows.length,
|
|
1130
|
+
open,
|
|
1131
|
+
why: {
|
|
1132
|
+
expand: canExpand ? null : "expandNothing",
|
|
1133
|
+
collapse: canCollapse ? null : view.rows.length < 2 ? "collapseNeedsRows" : "collapseAllDone"
|
|
1134
|
+
}
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
function setCollapsed(view, axis, paths) {
|
|
1138
|
+
const next = clone(view);
|
|
1139
|
+
next.collapsed[axis] = paths ? paths : [];
|
|
1140
|
+
return next;
|
|
1141
|
+
}
|
|
1142
|
+
var TOTALS = Object.freeze(["grandTotalRow", "grandTotalColumn", "rowSubtotals", "colSubtotals"]);
|
|
1143
|
+
function totalNoEffect(view, key) {
|
|
1144
|
+
if (key === "grandTotalRow") {
|
|
1145
|
+
return view.rows.length >= 1 ? null : "needsRowField";
|
|
1146
|
+
}
|
|
1147
|
+
if (key === "grandTotalColumn") {
|
|
1148
|
+
return view.cols.length >= 1 ? null : "needsColField";
|
|
1149
|
+
}
|
|
1150
|
+
if (key === "rowSubtotals") {
|
|
1151
|
+
return view.rows.length >= 2 ? null : "needsRowFields";
|
|
1152
|
+
}
|
|
1153
|
+
return view.cols.length >= 2 ? null : "needsColFields";
|
|
1154
|
+
}
|
|
1155
|
+
function setTotal(view, key, on) {
|
|
1156
|
+
const next = clone(view);
|
|
1157
|
+
next.totals[key] = Boolean(on);
|
|
1158
|
+
return next;
|
|
1159
|
+
}
|
|
1160
|
+
function setDisplay(view, display) {
|
|
1161
|
+
const next = clone(view);
|
|
1162
|
+
next.display = display;
|
|
1163
|
+
return next;
|
|
1164
|
+
}
|
|
1165
|
+
function setChart(view, name2) {
|
|
1166
|
+
const next = clone(view);
|
|
1167
|
+
next.chart = { ...next.chart, ...CHART_TYPES[name2].chart };
|
|
1168
|
+
next.display = "chart";
|
|
1169
|
+
return next;
|
|
1170
|
+
}
|
|
1171
|
+
function setChartDisplay(view, patch) {
|
|
1172
|
+
const next = clone(view);
|
|
1173
|
+
next.chart = { ...next.chart, ...patch };
|
|
1174
|
+
return next;
|
|
1175
|
+
}
|
|
1176
|
+
function swapAxes(view) {
|
|
1177
|
+
const next = clone(view);
|
|
1178
|
+
[next.rows, next.cols] = [next.cols, next.rows];
|
|
1179
|
+
next.collapsed = { rows: next.collapsed.cols, cols: next.collapsed.rows };
|
|
1180
|
+
for (const v of next.values) {
|
|
1181
|
+
if (v.showAs && v.showAs.axis) {
|
|
1182
|
+
v.showAs = { ...v.showAs, axis: v.showAs.axis === "rows" ? "cols" : "rows" };
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
const swapped = { percentOfRowTotal: "percentOfColumnTotal", percentOfColumnTotal: "percentOfRowTotal", percentOfParentRow: "percentOfParentColumn", percentOfParentColumn: "percentOfParentRow" };
|
|
1186
|
+
for (const v of next.values) {
|
|
1187
|
+
if (v.showAs && Object.hasOwn(swapped, v.showAs.type)) {
|
|
1188
|
+
v.showAs = { ...v.showAs, type: swapped[v.showAs.type] };
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
return next;
|
|
1192
|
+
}
|
|
1193
|
+
function isEmptyView(view) {
|
|
1194
|
+
return view.rows.length === 0 && view.cols.length === 0 && view.values.length === 0;
|
|
1195
|
+
}
|
|
1196
|
+
function conditionKind(encoded, view, field) {
|
|
1197
|
+
const type = encoded.column(sourceOf(view, field))?.type ?? "string";
|
|
1198
|
+
return type === "date" || type === "boolean" ? null : type === "number" ? "number" : "text";
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// src/files.js
|
|
1202
|
+
var UNSAFE = /[<>:"/\\|?*\u0000-\u001F\u007F]+/g;
|
|
1203
|
+
var RESERVED = /^(con|prn|aux|nul|com\d|lpt\d)$/i;
|
|
1204
|
+
function safeFileName(name2) {
|
|
1205
|
+
const text = typeof name2 === "string" ? name2 : "";
|
|
1206
|
+
const cleaned = trimEnd(text.replace(UNSAFE, "-").replace(/^[\s.-]+/, "").slice(0, 120));
|
|
1207
|
+
return cleaned && !RESERVED.test(cleaned) ? cleaned : "pivot";
|
|
1208
|
+
}
|
|
1209
|
+
function trimEnd(text) {
|
|
1210
|
+
let end = text.length;
|
|
1211
|
+
while (end > 0 && (text[end - 1] === "." || /\s/.test(text[end - 1]))) {
|
|
1212
|
+
end--;
|
|
1213
|
+
}
|
|
1214
|
+
return text.slice(0, end);
|
|
1215
|
+
}
|
|
1216
|
+
function dataUrlToBlob(url) {
|
|
1217
|
+
const match = /^data:([^,;]*)((?:;[^,;]*)*?)(;base64)?,(.*)$/s.exec(url);
|
|
1218
|
+
if (!match) {
|
|
1219
|
+
throw new TypeError("not a data: URL");
|
|
1220
|
+
}
|
|
1221
|
+
const type = match[1] || "application/octet-stream";
|
|
1222
|
+
if (match[3]) {
|
|
1223
|
+
const bin = atob(match[4]);
|
|
1224
|
+
const bytes = new Uint8Array(bin.length);
|
|
1225
|
+
for (let i = 0; i < bin.length; i++) {
|
|
1226
|
+
bytes[i] = bin.charCodeAt(i);
|
|
1227
|
+
}
|
|
1228
|
+
return new Blob([bytes], { type });
|
|
1229
|
+
}
|
|
1230
|
+
return new Blob([decodeURIComponent(match[4])], { type });
|
|
1231
|
+
}
|
|
1232
|
+
function saveBlob(blob, name2, doc = document) {
|
|
1233
|
+
const url = URL.createObjectURL(blob);
|
|
1234
|
+
const a = doc.createElement("a");
|
|
1235
|
+
a.href = url;
|
|
1236
|
+
a.download = name2;
|
|
1237
|
+
a.rel = "noopener";
|
|
1238
|
+
doc.body.appendChild(a);
|
|
1239
|
+
a.click();
|
|
1240
|
+
a.remove();
|
|
1241
|
+
setTimeout(() => URL.revokeObjectURL(url), 1e4);
|
|
1242
|
+
}
|
|
1243
|
+
async function copyText(text, container) {
|
|
1244
|
+
try {
|
|
1245
|
+
if (typeof navigator !== "undefined" && navigator.clipboard && typeof navigator.clipboard.writeText === "function") {
|
|
1246
|
+
await navigator.clipboard.writeText(text);
|
|
1247
|
+
return true;
|
|
1248
|
+
}
|
|
1249
|
+
} catch {
|
|
1250
|
+
}
|
|
1251
|
+
if (!container) {
|
|
1252
|
+
return false;
|
|
1253
|
+
}
|
|
1254
|
+
const doc = container.ownerDocument;
|
|
1255
|
+
const field = doc.createElement("textarea");
|
|
1256
|
+
field.value = text;
|
|
1257
|
+
field.readOnly = true;
|
|
1258
|
+
field.className = "pk-sr";
|
|
1259
|
+
const before = (
|
|
1260
|
+
/** @type {HTMLElement | null} */
|
|
1261
|
+
container.getRootNode() instanceof ShadowRoot ? (
|
|
1262
|
+
/** @type {ShadowRoot} */
|
|
1263
|
+
container.getRootNode().activeElement
|
|
1264
|
+
) : doc.activeElement
|
|
1265
|
+
);
|
|
1266
|
+
container.appendChild(field);
|
|
1267
|
+
let ok;
|
|
1268
|
+
try {
|
|
1269
|
+
field.select();
|
|
1270
|
+
ok = typeof doc.execCommand === "function" && doc.execCommand("copy");
|
|
1271
|
+
} catch {
|
|
1272
|
+
ok = false;
|
|
1273
|
+
}
|
|
1274
|
+
field.remove();
|
|
1275
|
+
before?.focus?.({ preventScroll: true });
|
|
1276
|
+
return ok;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
// src/section.jsx
|
|
1280
|
+
import React5, { createContext as createContext2, useContext as useContext2, useId as useId3 } from "react";
|
|
1281
|
+
var HelpContext = createContext2(
|
|
1282
|
+
/** @type {Help} */
|
|
1283
|
+
{ explain: true, isOpen: () => true, setOpen: () => {
|
|
1284
|
+
}, labels: null }
|
|
1285
|
+
);
|
|
1286
|
+
function SettingInfo({ id, name: name2 }) {
|
|
1287
|
+
const { explain, labels } = useContext2(HelpContext);
|
|
1288
|
+
const text = labels && labels.settingInfo && Object.hasOwn(labels.settingInfo, id) ? labels.settingInfo[id] : null;
|
|
1289
|
+
if (!explain || !labels || !text) {
|
|
1290
|
+
return null;
|
|
1291
|
+
}
|
|
1292
|
+
return /* @__PURE__ */ React5.createElement(InfoButton, { info: { description: text }, name: name2, label: fill(labels.aboutSetting, { setting: name2 }), labels, className: "pk-setting-info", testId: `setting-info-${id}` });
|
|
1293
|
+
}
|
|
1294
|
+
function Section({ title, children, id, info }) {
|
|
1295
|
+
const titleId = useId3();
|
|
1296
|
+
const bodyId = useId3();
|
|
1297
|
+
const { isOpen, setOpen } = useContext2(HelpContext);
|
|
1298
|
+
const name2 = typeof title === "string" ? title : "";
|
|
1299
|
+
const infoButton = info ? /* @__PURE__ */ React5.createElement(SettingInfo, { id: info, name: name2 }) : null;
|
|
1300
|
+
if (!id) {
|
|
1301
|
+
return /* @__PURE__ */ React5.createElement("div", { className: "pk-section", role: "group", "aria-labelledby": titleId }, /* @__PURE__ */ React5.createElement("div", { className: "pk-section-head" }, /* @__PURE__ */ React5.createElement("div", { className: "pk-section-title", id: titleId }, title), infoButton), children);
|
|
1302
|
+
}
|
|
1303
|
+
const open = isOpen(id);
|
|
1304
|
+
return /* @__PURE__ */ React5.createElement("div", { className: `pk-section${open ? "" : " pk-folded"}`, role: "group", "aria-labelledby": titleId, "data-section": id }, /* @__PURE__ */ React5.createElement("div", { className: "pk-section-head" }, /* @__PURE__ */ React5.createElement("button", { type: "button", className: "pk-section-title pk-section-toggle", id: titleId, "aria-expanded": open, "aria-controls": bodyId, onClick: () => setOpen(id, !open), "data-testid": `section-${id}` }, /* @__PURE__ */ React5.createElement(Icon, { name: open ? "chevronDown" : "chevronRight", size: 14 }), /* @__PURE__ */ React5.createElement("span", null, title)), infoButton), /* @__PURE__ */ React5.createElement("div", { id: bodyId, className: "pk-section-body", hidden: !open }, children));
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
// src/storage.js
|
|
1308
|
+
function localStore() {
|
|
1309
|
+
try {
|
|
1310
|
+
return typeof window !== "undefined" && window.localStorage ? window.localStorage : null;
|
|
1311
|
+
} catch {
|
|
1312
|
+
return null;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
function readItem(name2, storage) {
|
|
1316
|
+
try {
|
|
1317
|
+
return storage ? storage.getItem(name2) : null;
|
|
1318
|
+
} catch {
|
|
1319
|
+
return null;
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
var listeners = /* @__PURE__ */ new Map();
|
|
1323
|
+
function subscribeItem(name2, callback) {
|
|
1324
|
+
const set = listeners.get(name2) ?? /* @__PURE__ */ new Set();
|
|
1325
|
+
set.add(callback);
|
|
1326
|
+
listeners.set(name2, set);
|
|
1327
|
+
const onStorage = (e) => {
|
|
1328
|
+
if (e.key === null || e.key === name2) {
|
|
1329
|
+
callback();
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
const win = typeof window !== "undefined" ? window : null;
|
|
1333
|
+
win?.addEventListener("storage", onStorage);
|
|
1334
|
+
return () => {
|
|
1335
|
+
set.delete(callback);
|
|
1336
|
+
if (set.size === 0) {
|
|
1337
|
+
listeners.delete(name2);
|
|
1338
|
+
}
|
|
1339
|
+
win?.removeEventListener("storage", onStorage);
|
|
1340
|
+
};
|
|
1341
|
+
}
|
|
1342
|
+
function writeItem(name2, storage, text) {
|
|
1343
|
+
let ok = false;
|
|
1344
|
+
if (storage) {
|
|
1345
|
+
try {
|
|
1346
|
+
if (text === null) {
|
|
1347
|
+
storage.removeItem(name2);
|
|
1348
|
+
} else {
|
|
1349
|
+
storage.setItem(name2, text);
|
|
1350
|
+
}
|
|
1351
|
+
ok = true;
|
|
1352
|
+
} catch {
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
for (const callback of [...listeners.get(name2) ?? []]) {
|
|
1356
|
+
callback();
|
|
1357
|
+
}
|
|
1358
|
+
return ok;
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
// src/tips.js
|
|
1362
|
+
import { CATALOG, fieldLabel as fieldLabel2, suggestDateUnit } from "pivotkit-core";
|
|
1363
|
+
var TIP_LIMIT = 1;
|
|
1364
|
+
var TIP_THRESHOLDS = Object.freeze({
|
|
1365
|
+
/** columns of an ungrouped date on Columns from which grouping it is suggested */
|
|
1366
|
+
dateColumns: 12,
|
|
1367
|
+
/** rows of an ungrouped date on Rows from which grouping it is suggested */
|
|
1368
|
+
dateRows: 60,
|
|
1369
|
+
/** columns from which swapping rows and columns is suggested (when there are fewer rows) */
|
|
1370
|
+
manyColumns: 20,
|
|
1371
|
+
/** changes of the view in this session after which saving it as a template is suggested */
|
|
1372
|
+
changesBeforeSave: 5,
|
|
1373
|
+
/** changes after the first drag during which the Undo and menu tip stays */
|
|
1374
|
+
afterDragChanges: 3,
|
|
1375
|
+
/** slices from which a pie or a donut suggests bars: a circle reads well up to about 7 parts */
|
|
1376
|
+
pieSlices: 7
|
|
1377
|
+
});
|
|
1378
|
+
var PERCENT_SHOW_AS = Object.freeze(["percentOfGrandTotal", "percentOfRowTotal", "percentOfColumnTotal", "percentOfParentRow", "percentOfParentColumn", "percentRunningTotal", "percentDifferenceFromPrevious"]);
|
|
1379
|
+
var name = (ctx, field) => fieldLabel2(field, { fields: ctx.fields });
|
|
1380
|
+
var derivedBy = (ctx, field) => ctx.view.derived.find((d) => d.name.toLowerCase() === String(field).toLowerCase())?.by ?? "";
|
|
1381
|
+
var isRawDate = (ctx, field) => ctx.typeOf(field) === "date" && sourceOf(ctx.view, field).toLowerCase() === String(field).toLowerCase();
|
|
1382
|
+
var table = (ctx) => !ctx.failed && !ctx.empty && ctx.view.display === "table";
|
|
1383
|
+
var chart = (ctx) => !ctx.failed && ctx.view.display === "chart" && ctx.series !== null && ctx.view.values.length > 0;
|
|
1384
|
+
var seriesOf = (ctx) => (
|
|
1385
|
+
/** @type {NonNullable<TipContext["series"]>} */
|
|
1386
|
+
ctx.series
|
|
1387
|
+
);
|
|
1388
|
+
var axisChart = (ctx) => chart(ctx) && CHART_TYPES[ctx.chartPlan.name].axis;
|
|
1389
|
+
var lines = (ctx) => ["line", "area"].includes(ctx.chartPlan.name);
|
|
1390
|
+
var sliceChart = (ctx) => chart(ctx) && SLICE_TYPES.includes(ctx.chartPlan.name);
|
|
1391
|
+
var dateOn = (ctx, axis) => ctx.view[axis].some((f) => ctx.typeOf(f) === "date");
|
|
1392
|
+
var datesAcross = (ctx) => axisChart(ctx) && !ctx.chartPlan.swap && lines(ctx) && dateOn(ctx, "cols") && !dateOn(ctx, "rows");
|
|
1393
|
+
var TIPS = Object.freeze([
|
|
1394
|
+
{
|
|
1395
|
+
// a filter that leaves nothing: say so instead of an empty table
|
|
1396
|
+
id: "filterEmpty",
|
|
1397
|
+
kind: "notice",
|
|
1398
|
+
icon: "filter",
|
|
1399
|
+
action: "removeFilters",
|
|
1400
|
+
when: (ctx) => !ctx.failed && !ctx.empty && ctx.dataRows > 0 && ctx.matched === 0 && ctx.view.filters.some((f) => isFiltered(ctx.view, f.field)) ? {} : null
|
|
1401
|
+
},
|
|
1402
|
+
{
|
|
1403
|
+
// top N changes what totals mean
|
|
1404
|
+
id: "top",
|
|
1405
|
+
kind: "notice",
|
|
1406
|
+
icon: "top",
|
|
1407
|
+
when: (ctx) => {
|
|
1408
|
+
if (ctx.failed || ctx.empty) {
|
|
1409
|
+
return null;
|
|
1410
|
+
}
|
|
1411
|
+
for (const field of [...ctx.view.rows, ...ctx.view.cols]) {
|
|
1412
|
+
const top = filtersOf(ctx.view, field, ["top"])[0];
|
|
1413
|
+
if (top) {
|
|
1414
|
+
return { field: name(ctx, field), count: top.count, bottom: Boolean(top.bottom) };
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
return null;
|
|
1418
|
+
}
|
|
1419
|
+
},
|
|
1420
|
+
{
|
|
1421
|
+
id: "chartCapped",
|
|
1422
|
+
kind: "notice",
|
|
1423
|
+
when: (ctx) => chart(ctx) && seriesOf(ctx).categoryCount > seriesOf(ctx).categories.length ? { shown: seriesOf(ctx).categories.length, total: seriesOf(ctx).categoryCount } : null
|
|
1424
|
+
},
|
|
1425
|
+
{
|
|
1426
|
+
// "% of row total": what 100 % is
|
|
1427
|
+
id: "showAs",
|
|
1428
|
+
icon: "sigma",
|
|
1429
|
+
when: (ctx) => {
|
|
1430
|
+
if (ctx.failed || ctx.empty) {
|
|
1431
|
+
return null;
|
|
1432
|
+
}
|
|
1433
|
+
const spec = ctx.view.values.find((v) => v.showAs && PERCENT_SHOW_AS.includes(v.showAs.type));
|
|
1434
|
+
return spec ? { type: spec.showAs.type } : null;
|
|
1435
|
+
}
|
|
1436
|
+
},
|
|
1437
|
+
{
|
|
1438
|
+
// a type the user chose for data over time: lines show it (automatic draws them)
|
|
1439
|
+
id: "chartOverTime",
|
|
1440
|
+
icon: "line",
|
|
1441
|
+
action: "auto",
|
|
1442
|
+
when: (ctx) => {
|
|
1443
|
+
if (!chart(ctx) || ctx.chartPlan.auto || lines(ctx) || ctx.chartPlan.name === "heatmap") {
|
|
1444
|
+
return null;
|
|
1445
|
+
}
|
|
1446
|
+
if (chartPlan({ ...ctx.view, chart: { ...ctx.view.chart, type: "auto" } }, ctx.encoded).name !== "line") {
|
|
1447
|
+
return null;
|
|
1448
|
+
}
|
|
1449
|
+
const field = [...ctx.view.rows, ...ctx.view.cols].find((f) => ctx.typeOf(f) === "date" && !CATALOG.cyclicUnits.includes(derivedBy(ctx, f)));
|
|
1450
|
+
return field ? { field: name(ctx, field) } : null;
|
|
1451
|
+
}
|
|
1452
|
+
},
|
|
1453
|
+
{
|
|
1454
|
+
// a pie of many slices: the small ones cannot be told apart
|
|
1455
|
+
id: "pieSlices",
|
|
1456
|
+
icon: "chart",
|
|
1457
|
+
action: "bars",
|
|
1458
|
+
when: (ctx) => sliceChart(ctx) && seriesOf(ctx).categories.length > TIP_THRESHOLDS.pieSlices ? { count: seriesOf(ctx).categories.length, max: TIP_THRESHOLDS.pieSlices } : null
|
|
1459
|
+
},
|
|
1460
|
+
{
|
|
1461
|
+
// a pie draws one slice per row: the columns are added up
|
|
1462
|
+
id: "pieColumns",
|
|
1463
|
+
icon: "info",
|
|
1464
|
+
action: "bars",
|
|
1465
|
+
when: (ctx) => sliceChart(ctx) && ctx.view.cols.length > 0 ? { fields: ctx.view.cols.map((f) => name(ctx, f)).join(", ") } : null
|
|
1466
|
+
},
|
|
1467
|
+
{
|
|
1468
|
+
id: "chartDates",
|
|
1469
|
+
action: "swap",
|
|
1470
|
+
when: (ctx) => datesAcross(ctx) ? {} : null
|
|
1471
|
+
},
|
|
1472
|
+
{
|
|
1473
|
+
// a line or an area over categories without an order (countries, teams) draws a trend
|
|
1474
|
+
// between things that follow no line: bars fit better. Dates, number ranges and a custom
|
|
1475
|
+
// order (seasons) are an order
|
|
1476
|
+
id: "chartTrend",
|
|
1477
|
+
icon: "chart",
|
|
1478
|
+
action: "bars",
|
|
1479
|
+
when: (ctx) => {
|
|
1480
|
+
if (!axisChart(ctx) || ctx.chartPlan.auto || datesAcross(ctx) || !lines(ctx) || ctx.view.rows.length === 0) {
|
|
1481
|
+
return null;
|
|
1482
|
+
}
|
|
1483
|
+
const field = ctx.view.rows[ctx.view.rows.length - 1];
|
|
1484
|
+
const ordered = ["date", "number"].includes(ctx.typeOf(field)) || ctx.view.sort.some((s) => s.by === "custom" && String(s.field).toLowerCase() === String(field).toLowerCase());
|
|
1485
|
+
return ordered ? null : {};
|
|
1486
|
+
}
|
|
1487
|
+
},
|
|
1488
|
+
{
|
|
1489
|
+
id: "chartSeries",
|
|
1490
|
+
action: "swap",
|
|
1491
|
+
// the dates tip says the same (swap) more precisely; lines over time drawn automatically
|
|
1492
|
+
// would draw the same lines after a swap
|
|
1493
|
+
when: (ctx) => axisChart(ctx) && !datesAcross(ctx) && !(ctx.chartPlan.auto && lines(ctx)) && seriesOf(ctx).series.length > MANY_SERIES ? { count: seriesOf(ctx).series.length } : null
|
|
1494
|
+
},
|
|
1495
|
+
{
|
|
1496
|
+
// a text field in Values is counted: say so, and what a sum needs
|
|
1497
|
+
id: "textValue",
|
|
1498
|
+
icon: "sigma",
|
|
1499
|
+
when: (ctx) => {
|
|
1500
|
+
if (ctx.failed || ctx.empty) {
|
|
1501
|
+
return null;
|
|
1502
|
+
}
|
|
1503
|
+
const spec = ctx.view.values.find((v) => v.field && !v.label && ["string", "boolean"].includes(ctx.typeOf(v.field)));
|
|
1504
|
+
return spec ? { field: name(ctx, spec.field) } : null;
|
|
1505
|
+
}
|
|
1506
|
+
},
|
|
1507
|
+
{
|
|
1508
|
+
// a date without grouping makes a column per day (or per instant)
|
|
1509
|
+
id: "dateUngrouped",
|
|
1510
|
+
icon: "date",
|
|
1511
|
+
action: "groupDate",
|
|
1512
|
+
when: (ctx) => {
|
|
1513
|
+
if (!table(ctx)) {
|
|
1514
|
+
return null;
|
|
1515
|
+
}
|
|
1516
|
+
const onCols = ctx.colLeaves > TIP_THRESHOLDS.dateColumns ? ctx.view.cols.find((f) => isRawDate(ctx, f)) : void 0;
|
|
1517
|
+
const field = onCols ?? (ctx.rowLeaves > TIP_THRESHOLDS.dateRows ? ctx.view.rows.find((f) => isRawDate(ctx, f)) : void 0);
|
|
1518
|
+
if (!field) {
|
|
1519
|
+
return null;
|
|
1520
|
+
}
|
|
1521
|
+
const column = ctx.encoded.column(field);
|
|
1522
|
+
const suggested = suggestDateUnit(ctx.encoded, field);
|
|
1523
|
+
const unit = !suggested || suggested === "day" && column?.dateOnly ? "yearMonth" : suggested;
|
|
1524
|
+
return { field: name(ctx, field), source: field, unit };
|
|
1525
|
+
}
|
|
1526
|
+
},
|
|
1527
|
+
{
|
|
1528
|
+
id: "manyColumns",
|
|
1529
|
+
icon: "swap",
|
|
1530
|
+
action: "swap",
|
|
1531
|
+
when: (ctx) => table(ctx) && ctx.view.cols.length > 0 && ctx.colLeaves > TIP_THRESHOLDS.manyColumns && ctx.rowLeaves < ctx.colLeaves ? { count: ctx.colLeaves } : null
|
|
1532
|
+
},
|
|
1533
|
+
{
|
|
1534
|
+
// once after the first drag: Undo and the menus (before the next step: it goes after a few changes)
|
|
1535
|
+
id: "afterDrag",
|
|
1536
|
+
icon: "undo",
|
|
1537
|
+
when: (ctx) => !ctx.failed && ctx.dragAt !== null && ctx.changes - ctx.dragAt <= TIP_THRESHOLDS.afterDragChanges ? {} : null
|
|
1538
|
+
},
|
|
1539
|
+
{
|
|
1540
|
+
// rows without values show no numbers
|
|
1541
|
+
id: "values",
|
|
1542
|
+
icon: "sigma",
|
|
1543
|
+
when: (ctx) => table(ctx) && ctx.view.values.length === 0 ? {} : null
|
|
1544
|
+
},
|
|
1545
|
+
{
|
|
1546
|
+
id: "saveTemplate",
|
|
1547
|
+
icon: "bookmark",
|
|
1548
|
+
action: "saveTemplate",
|
|
1549
|
+
// after a save it waits for as many changes again
|
|
1550
|
+
when: (ctx) => ctx.canSave && !ctx.failed && !ctx.empty && ctx.changes - ctx.savedAt >= TIP_THRESHOLDS.changesBeforeSave ? {} : null
|
|
1551
|
+
},
|
|
1552
|
+
{
|
|
1553
|
+
// the empty report: every field has a menu, and Undo is there
|
|
1554
|
+
id: "menus",
|
|
1555
|
+
icon: "sparkle",
|
|
1556
|
+
when: (ctx) => !ctx.failed && ctx.empty && ctx.view.display === "table" && ctx.dragAt === null ? {} : null
|
|
1557
|
+
},
|
|
1558
|
+
{
|
|
1559
|
+
id: "cells",
|
|
1560
|
+
icon: "info",
|
|
1561
|
+
when: (ctx) => table(ctx) && ctx.tableShown && ctx.view.values.length > 0 && !ctx.drilled ? {} : null
|
|
1562
|
+
}
|
|
1563
|
+
]);
|
|
1564
|
+
var TIP_IDS = Object.freeze(TIPS.filter((t) => t.kind !== "notice").map((t) => t.id));
|
|
1565
|
+
var NOTICE_IDS = Object.freeze(TIPS.filter((t) => t.kind === "notice").map((t) => t.id));
|
|
1566
|
+
var ALL_TIPS = "*";
|
|
1567
|
+
var NO_EXPLANATIONS = "-explanations";
|
|
1568
|
+
var FIELDS_COLLAPSED = "-fields";
|
|
1569
|
+
var SWITCHES = Object.freeze([ALL_TIPS, NO_EXPLANATIONS, FIELDS_COLLAPSED]);
|
|
1570
|
+
function fieldsCollapsed(session, forced, dismissed) {
|
|
1571
|
+
return session ?? (forced === "collapsed" || forced === "open" ? forced === "collapsed" : dismissed.includes(FIELDS_COLLAPSED));
|
|
1572
|
+
}
|
|
1573
|
+
var SECTIONS = Object.freeze({
|
|
1574
|
+
// a value's menu
|
|
1575
|
+
summarize: true,
|
|
1576
|
+
showAs: true,
|
|
1577
|
+
format: false,
|
|
1578
|
+
highlight: false,
|
|
1579
|
+
name: false,
|
|
1580
|
+
valueFilters: false,
|
|
1581
|
+
move: false,
|
|
1582
|
+
// a field's menu on Rows or Columns
|
|
1583
|
+
sort: true,
|
|
1584
|
+
groupBy: true,
|
|
1585
|
+
bins: true,
|
|
1586
|
+
topN: true,
|
|
1587
|
+
filters: true,
|
|
1588
|
+
// the toolbar's menus
|
|
1589
|
+
totals: true,
|
|
1590
|
+
folding: true,
|
|
1591
|
+
help: true,
|
|
1592
|
+
// the Export menus (the toolbar's and the drill-down's)
|
|
1593
|
+
exportDownload: true,
|
|
1594
|
+
exportCopy: true,
|
|
1595
|
+
sharedViews: true,
|
|
1596
|
+
myViews: true,
|
|
1597
|
+
share: true,
|
|
1598
|
+
chartType: true,
|
|
1599
|
+
chartDisplay: true
|
|
1600
|
+
});
|
|
1601
|
+
function sectionOpen(id, dismissed) {
|
|
1602
|
+
if (dismissed.includes(`open:${id}`)) {
|
|
1603
|
+
return true;
|
|
1604
|
+
}
|
|
1605
|
+
if (dismissed.includes(`closed:${id}`)) {
|
|
1606
|
+
return false;
|
|
1607
|
+
}
|
|
1608
|
+
return Object.hasOwn(SECTIONS, id) ? SECTIONS[id] : true;
|
|
1609
|
+
}
|
|
1610
|
+
function withSection(dismissed, id, open) {
|
|
1611
|
+
const rest = dismissed.filter((d) => d !== `open:${id}` && d !== `closed:${id}`);
|
|
1612
|
+
const def = Object.hasOwn(SECTIONS, id) ? SECTIONS[id] : true;
|
|
1613
|
+
return open === def ? rest : [...rest, `${open ? "open" : "closed"}:${id}`];
|
|
1614
|
+
}
|
|
1615
|
+
var isSection = (id) => /^(open|closed):/.test(id) && Object.hasOwn(SECTIONS, id.slice(id.indexOf(":") + 1));
|
|
1616
|
+
var keptOnReset = (id) => SWITCHES.includes(id) || isSection(id);
|
|
1617
|
+
function helpSwitch(forced, dismissed, off = NO_EXPLANATIONS) {
|
|
1618
|
+
if (forced === "on" || forced === "off") {
|
|
1619
|
+
return { on: forced === "on", user: false };
|
|
1620
|
+
}
|
|
1621
|
+
return { on: !dismissed.includes(off), user: true };
|
|
1622
|
+
}
|
|
1623
|
+
var SHORTCUTS = Object.freeze([
|
|
1624
|
+
{ keys: ["Mod", "Z"], text: "undo" },
|
|
1625
|
+
{ keys: ["Mod", "Shift", "Z"], text: "redo" },
|
|
1626
|
+
{ keys: ["Arrow keys"], text: "move" },
|
|
1627
|
+
{ keys: ["Enter"], text: "open" },
|
|
1628
|
+
{ keys: ["Space"], text: "drag" },
|
|
1629
|
+
{ keys: ["Escape"], text: "close" },
|
|
1630
|
+
{ keys: ["Arrow keys"], text: "fields" },
|
|
1631
|
+
{ keys: ["Shift", "F10"], text: "fieldMenu" }
|
|
1632
|
+
]);
|
|
1633
|
+
function tipContext({ view, result, encoded, failed, empty, typeOf, fields, series = null, tableShown = false, drilled = false, changes = 0, dragAt = null, canSave = false, savedAt = 0, chartPlan: chartPlan2 = { name: "bar", auto: true, swap: false } }) {
|
|
1634
|
+
const leaves = (nodes, depth) => result && depth > 0 ? nodes.reduce((n, node) => n + (node.depth === depth ? 1 : 0), 0) : 1;
|
|
1635
|
+
return {
|
|
1636
|
+
view,
|
|
1637
|
+
empty,
|
|
1638
|
+
failed: failed || !result,
|
|
1639
|
+
typeOf,
|
|
1640
|
+
encoded,
|
|
1641
|
+
fields,
|
|
1642
|
+
rowLeaves: result ? leaves(result.rowNodes, result.rowFields.length) : 0,
|
|
1643
|
+
colLeaves: result ? leaves(result.colNodes, result.colFields.length) : 0,
|
|
1644
|
+
matched: result ? result.rows.length : 0,
|
|
1645
|
+
dataRows: encoded.length,
|
|
1646
|
+
series,
|
|
1647
|
+
tableShown,
|
|
1648
|
+
drilled,
|
|
1649
|
+
changes,
|
|
1650
|
+
dragAt,
|
|
1651
|
+
canSave,
|
|
1652
|
+
savedAt,
|
|
1653
|
+
chartPlan: chartPlan2
|
|
1654
|
+
};
|
|
1655
|
+
}
|
|
1656
|
+
function activeTips(ctx, { allowed, dismissed, limit = TIP_LIMIT }) {
|
|
1657
|
+
const gone = new Set(dismissed);
|
|
1658
|
+
const tipsOff = allowed === "off" || gone.has(ALL_TIPS);
|
|
1659
|
+
const out = [];
|
|
1660
|
+
let tips = 0;
|
|
1661
|
+
for (const entry of TIPS) {
|
|
1662
|
+
const notice = entry.kind === "notice";
|
|
1663
|
+
if (!notice && (tipsOff || tips >= limit || Array.isArray(allowed) && !allowed.includes(entry.id) || gone.has(entry.id))) {
|
|
1664
|
+
continue;
|
|
1665
|
+
}
|
|
1666
|
+
const params = entry.when(ctx);
|
|
1667
|
+
if (params) {
|
|
1668
|
+
out.push({ id: entry.id, params, entry });
|
|
1669
|
+
tips += notice ? 0 : 1;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
return out;
|
|
1673
|
+
}
|
|
1674
|
+
function tipText(id, params, labels, locale) {
|
|
1675
|
+
const texts = labels.tips;
|
|
1676
|
+
const values = Object.fromEntries(Object.entries(params).map(([k, v]) => [k, typeof v === "number" ? v.toLocaleString(locale) : v]));
|
|
1677
|
+
if (id === "showAs") {
|
|
1678
|
+
const per = texts.showAs;
|
|
1679
|
+
return Object.hasOwn(per, params.type) ? per[params.type] : "";
|
|
1680
|
+
}
|
|
1681
|
+
if (id === "top") {
|
|
1682
|
+
return fill(params.bottom ? texts.bottom : texts.top, values);
|
|
1683
|
+
}
|
|
1684
|
+
if (id === "dateUngrouped") {
|
|
1685
|
+
return fill(texts.dateUngrouped, { ...values, unit: labels.units[params.unit] ?? params.unit });
|
|
1686
|
+
}
|
|
1687
|
+
return Object.hasOwn(texts, id) && typeof texts[id] === "string" ? fill(texts[id], values) : "";
|
|
1688
|
+
}
|
|
1689
|
+
function tipActionText(entry, params, labels) {
|
|
1690
|
+
if (!entry.action) {
|
|
1691
|
+
return "";
|
|
1692
|
+
}
|
|
1693
|
+
const text = Object.hasOwn(labels.tipActions, entry.action) ? labels.tipActions[entry.action] : "";
|
|
1694
|
+
return fill(text, { unit: labels.units[params.unit] ?? params.unit ?? "" });
|
|
1695
|
+
}
|
|
1696
|
+
function allowedTips(value) {
|
|
1697
|
+
if (value === "off" || value === false) {
|
|
1698
|
+
return "off";
|
|
1699
|
+
}
|
|
1700
|
+
const list = typeof value === "string" && value.trim() !== "on" && value.trim() !== "" ? value.split(",") : Array.isArray(value) ? value : null;
|
|
1701
|
+
if (!list) {
|
|
1702
|
+
return "on";
|
|
1703
|
+
}
|
|
1704
|
+
return list.map((id) => String(id).trim()).filter((id) => TIP_IDS.includes(id));
|
|
1705
|
+
}
|
|
1706
|
+
function cleanDismissed(value) {
|
|
1707
|
+
return Array.isArray(value) ? [...new Set(value.filter((id) => typeof id === "string" && (SWITCHES.includes(id) || TIP_IDS.includes(id) || isSection(id))))] : [];
|
|
1708
|
+
}
|
|
1709
|
+
var storageName = (key) => `pivotkit:tips:${key}`;
|
|
1710
|
+
function readStored(key, storage) {
|
|
1711
|
+
return key ? readItem(storageName(key), storage) : null;
|
|
1712
|
+
}
|
|
1713
|
+
function parseStored(raw) {
|
|
1714
|
+
if (!raw) {
|
|
1715
|
+
return [];
|
|
1716
|
+
}
|
|
1717
|
+
try {
|
|
1718
|
+
return cleanDismissed(JSON.parse(raw));
|
|
1719
|
+
} catch {
|
|
1720
|
+
return [];
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
function subscribeStored(key, callback) {
|
|
1724
|
+
return key ? subscribeItem(storageName(key), callback) : () => {
|
|
1725
|
+
};
|
|
1726
|
+
}
|
|
1727
|
+
function writeDismissed(key, storage, dismissed) {
|
|
1728
|
+
if (key && storage) {
|
|
1729
|
+
writeItem(storageName(key), storage, dismissed.length ? JSON.stringify(dismissed) : null);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
function withoutFilters(view) {
|
|
1733
|
+
return { ...view, filters: filterZoneFields(view).map((field) => ({ field, type: "exclude", items: [] })) };
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
export {
|
|
1737
|
+
valueLabel,
|
|
1738
|
+
refLabel,
|
|
1739
|
+
valueConditionText,
|
|
1740
|
+
groupingName,
|
|
1741
|
+
fieldAction,
|
|
1742
|
+
layoutSummary,
|
|
1743
|
+
filterText,
|
|
1744
|
+
DRILL_LIMIT,
|
|
1745
|
+
decodeRow,
|
|
1746
|
+
drillRows,
|
|
1747
|
+
drillTable,
|
|
1748
|
+
Icon,
|
|
1749
|
+
TYPE_ICON,
|
|
1750
|
+
ZONE_ICON,
|
|
1751
|
+
ClearInput,
|
|
1752
|
+
rootOf,
|
|
1753
|
+
activeIn,
|
|
1754
|
+
useModal,
|
|
1755
|
+
useIsoLayoutEffect,
|
|
1756
|
+
LayerContext,
|
|
1757
|
+
Popover,
|
|
1758
|
+
MenuButton,
|
|
1759
|
+
Option,
|
|
1760
|
+
FieldDescription,
|
|
1761
|
+
InfoButton,
|
|
1762
|
+
ZONES,
|
|
1763
|
+
filterZoneFields,
|
|
1764
|
+
zoneOverLimit,
|
|
1765
|
+
addField,
|
|
1766
|
+
removeAt,
|
|
1767
|
+
moveField,
|
|
1768
|
+
reorder,
|
|
1769
|
+
updateValue,
|
|
1770
|
+
numberRange,
|
|
1771
|
+
binProblem,
|
|
1772
|
+
groupField,
|
|
1773
|
+
groupingOf,
|
|
1774
|
+
sourceOf,
|
|
1775
|
+
setSort,
|
|
1776
|
+
withHostOrders,
|
|
1777
|
+
sortOf,
|
|
1778
|
+
filtersOf,
|
|
1779
|
+
setItemFilter,
|
|
1780
|
+
selectedItems,
|
|
1781
|
+
setLabelFilter,
|
|
1782
|
+
setTopFilter,
|
|
1783
|
+
setValueFilter,
|
|
1784
|
+
numericResult,
|
|
1785
|
+
withMeasure,
|
|
1786
|
+
sameRef,
|
|
1787
|
+
measureChoices,
|
|
1788
|
+
removeFilters,
|
|
1789
|
+
hasFilter,
|
|
1790
|
+
toggleCollapsed,
|
|
1791
|
+
rowFolding,
|
|
1792
|
+
setCollapsed,
|
|
1793
|
+
TOTALS,
|
|
1794
|
+
totalNoEffect,
|
|
1795
|
+
setTotal,
|
|
1796
|
+
setDisplay,
|
|
1797
|
+
setChart,
|
|
1798
|
+
setChartDisplay,
|
|
1799
|
+
swapAxes,
|
|
1800
|
+
isEmptyView,
|
|
1801
|
+
conditionKind,
|
|
1802
|
+
safeFileName,
|
|
1803
|
+
dataUrlToBlob,
|
|
1804
|
+
saveBlob,
|
|
1805
|
+
copyText,
|
|
1806
|
+
HelpContext,
|
|
1807
|
+
SettingInfo,
|
|
1808
|
+
Section,
|
|
1809
|
+
localStore,
|
|
1810
|
+
readItem,
|
|
1811
|
+
subscribeItem,
|
|
1812
|
+
writeItem,
|
|
1813
|
+
ALL_TIPS,
|
|
1814
|
+
NO_EXPLANATIONS,
|
|
1815
|
+
FIELDS_COLLAPSED,
|
|
1816
|
+
fieldsCollapsed,
|
|
1817
|
+
sectionOpen,
|
|
1818
|
+
withSection,
|
|
1819
|
+
keptOnReset,
|
|
1820
|
+
helpSwitch,
|
|
1821
|
+
SHORTCUTS,
|
|
1822
|
+
tipContext,
|
|
1823
|
+
activeTips,
|
|
1824
|
+
tipText,
|
|
1825
|
+
tipActionText,
|
|
1826
|
+
allowedTips,
|
|
1827
|
+
cleanDismissed,
|
|
1828
|
+
readStored,
|
|
1829
|
+
parseStored,
|
|
1830
|
+
subscribeStored,
|
|
1831
|
+
writeDismissed,
|
|
1832
|
+
withoutFilters
|
|
1833
|
+
};
|