contour-ds 1.7.2 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +244 -326
- package/dist/agent-cookbook.json +960 -0
- package/dist/agent-examples.json +65 -0
- package/dist/agent-map-wiring.json +152 -0
- package/dist/agent-rules.json +16 -0
- package/dist/build-manifest.json +344 -126
- package/dist/contour-wc-content.esm.js +1939 -0
- package/dist/contour-wc-content.esm.min.js +1 -0
- package/dist/contour-wc-core.esm.js +3724 -0
- package/dist/contour-wc-core.esm.min.js +1 -0
- package/dist/contour-wc-dashboard.esm.js +6986 -0
- package/dist/contour-wc-dashboard.esm.min.js +1 -0
- package/dist/contour-wc-data.esm.js +6572 -0
- package/dist/contour-wc-data.esm.min.js +1 -0
- package/dist/contour-wc-docs.esm.js +976 -0
- package/dist/contour-wc-docs.esm.min.js +1 -0
- package/dist/contour-wc-feedback.esm.js +1742 -0
- package/dist/contour-wc-feedback.esm.min.js +1 -0
- package/dist/contour-wc-forms.esm.js +2437 -0
- package/dist/contour-wc-forms.esm.min.js +1 -0
- package/dist/contour-wc-interaction.esm.js +1343 -0
- package/dist/contour-wc-interaction.esm.min.js +1 -0
- package/dist/contour-wc-layout.esm.js +845 -0
- package/dist/contour-wc-layout.esm.min.js +1 -0
- package/dist/contour-wc-map.esm.js +2411 -0
- package/dist/contour-wc-map.esm.min.js +1 -0
- package/dist/contour-wc-misc.esm.js +945 -0
- package/dist/contour-wc-misc.esm.min.js +1 -0
- package/dist/contour-wc-navigation.esm.js +2842 -0
- package/dist/contour-wc-navigation.esm.min.js +1 -0
- package/dist/contour-wc-ui.esm.js +6519 -0
- package/dist/contour-wc-ui.esm.min.js +1 -0
- package/dist/contour-wc.esm.js +14435 -5723
- package/dist/contour-wc.esm.min.js +1 -1
- package/dist/contour-wc.js +14436 -5725
- package/dist/contour-wc.min.js +1 -1
- package/dist/contour.a11y.json +402 -0
- package/dist/contour.code-snippets +4534 -0
- package/dist/contour.components.json +4870 -1222
- package/dist/contour.css +6672 -4711
- package/dist/contour.d.ts +753 -1085
- package/dist/contour.esm.js +440 -18
- package/dist/contour.esm.min.js +1 -1
- package/dist/contour.html-data.json +11343 -0
- package/dist/contour.js +440 -18
- package/dist/contour.min.css +1 -1
- package/dist/contour.min.js +1 -1
- package/dist/contour.react.d.ts +422 -0
- package/dist/contour.single.html +17 -0
- package/dist/contour.single.min.css +1 -0
- package/dist/contour.themes.json +56 -12
- package/dist/contour.tokens.json +1318 -1053
- package/dist/contour.vue.d.ts +420 -0
- package/dist/contour.web-types.json +11703 -0
- package/dist/custom-elements.json +36120 -0
- package/dist/docs/actions.md +16 -6
- package/dist/docs/affordances.md +21 -85
- package/dist/docs/apireference.md +9 -5
- package/dist/docs/colour.md +10 -4
- package/dist/docs/componentindex.md +9 -22
- package/dist/docs/data.md +39 -58
- package/dist/docs/docs.md +12 -7
- package/dist/docs/feedback.md +30 -12
- package/dist/docs/field.md +28 -13
- package/dist/docs/forms.md +35 -19
- package/dist/docs/index.json +70 -61
- package/dist/docs/install.md +12 -7
- package/dist/docs/landing.md +7 -4
- package/dist/docs/mapengine.md +30 -8
- package/dist/docs/motion.md +12 -6
- package/dist/docs/navigation.md +31 -7
- package/dist/docs/overlays.md +32 -25
- package/dist/docs/overview.md +3 -2
- package/dist/docs/patterns.md +20 -10
- package/dist/docs/playground.md +11 -4
- package/dist/docs/principles.md +8 -6
- package/dist/docs/recipes.md +117 -13
- package/dist/docs/space.md +10 -6
- package/dist/docs/status.md +13 -14
- package/dist/docs/surfaces.md +17 -22
- package/dist/docs/theming.md +18 -32
- package/dist/docs/type.md +12 -5
- package/dist/docs/webcomponents.md +75 -33
- package/dist/hljs.css +1 -20
- package/dist/map-chrome.mapbox.css +68 -92
- package/dist/map-chrome.maplibre.css +68 -92
- package/dist/registry/items/contour-core.json +2 -2
- package/dist/registry/items/contour-data.json +2 -2
- package/dist/registry/items/contour-docs.json +2 -2
- package/dist/registry/items/contour-landing.json +2 -2
- package/dist/registry/registry.json +8 -8
- package/dist/surfaces/contour-core.min.css +1 -1
- package/dist/surfaces/contour-data.min.css +1 -1
- package/dist/surfaces/contour-docs.min.css +1 -1
- package/dist/surfaces/contour-landing.min.css +1 -1
- package/dist/svg/icons.svg +367 -0
- package/dist/wc/behaviours.mjs +222 -192
- package/dist/wc/content.mjs +486 -142
- package/dist/wc/core.mjs +917 -395
- package/dist/wc/dashboard.mjs +252 -25
- package/dist/wc/data-chart.mjs +1333 -0
- package/dist/wc/data-export.mjs +391 -0
- package/dist/wc/data-gauge.mjs +779 -0
- package/dist/wc/data-geo.mjs +696 -0
- package/dist/wc/data-glyphs.mjs +133 -0
- package/dist/wc/data-legend.mjs +431 -0
- package/dist/wc/data-readout.mjs +227 -0
- package/dist/wc/data-series.mjs +1004 -0
- package/dist/wc/data-table.mjs +751 -0
- package/dist/wc/data.mjs +15 -3707
- package/dist/wc/docs.mjs +113 -19
- package/dist/wc/feedback.mjs +320 -116
- package/dist/wc/forms-controls.mjs +635 -0
- package/dist/wc/forms-field.mjs +547 -0
- package/dist/wc/forms-groups.mjs +347 -0
- package/dist/wc/forms-input.mjs +292 -0
- package/dist/wc/forms.mjs +10 -1122
- package/dist/wc/glyph.mjs +204 -0
- package/dist/wc/index.mjs +16 -7
- package/dist/wc/interaction.mjs +322 -75
- package/dist/wc/layout.mjs +100 -29
- package/dist/wc/map.mjs +888 -285
- package/dist/wc/misc.mjs +137 -26
- package/dist/wc/navigation-menu.mjs +735 -0
- package/dist/wc/navigation-shell.mjs +495 -0
- package/dist/wc/navigation-tree.mjs +995 -0
- package/dist/wc/navigation.mjs +9 -1636
- package/dist/wc/registration.mjs +1 -8
- package/dist/wc/shadow.mjs +285 -113
- package/dist/wc/states.mjs +75 -78
- package/dist/wc/svg.mjs +209 -34
- package/dist/wc/ui-atoms.mjs +608 -0
- package/dist/wc/ui-data.mjs +1404 -0
- package/dist/wc/ui-feedback.mjs +419 -0
- package/dist/wc/ui-inputs.mjs +1570 -0
- package/dist/wc/ui-menus.mjs +307 -0
- package/dist/wc/ui-overlays.mjs +1605 -0
- package/integrations/README.md +25 -5
- package/integrations/ai/CLAUDE.md +6 -3
- package/integrations/ai/COPILOT.md +14 -0
- package/integrations/ai/GEMINI.md +14 -0
- package/integrations/ai/WINDSURF.md +14 -0
- package/integrations/ai/v0-prompt.md +13 -1
- package/integrations/frameworks/README.md +235 -0
- package/integrations/native/README.md +0 -3
- package/integrations/native/style-dictionary.config.json +1 -1
- package/integrations/tailwind/contour-theme.css +71 -13
- package/integrations/validate-markup.mjs +172 -0
- package/package.json +103 -39
- package/dist/wc/ui.mjs +0 -3560
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
import { setHTML, ContourBase, define, onDispose, formatValue, contourId, navKey, isRtl, cssLength , safeColor, warnBadJson, labelText, flagOn } from './core.mjs';
|
|
2
|
+
import { loadRemote, remoteData, renderStates, renderEmpty, wireStates, remoteInvalidate, watchViewport, parseArrayAttr, stateLabel } from './states.mjs';
|
|
3
|
+
import { donutSlices, barRects, smoothPath, polylinePoints, contourPaths } from './svg.mjs';
|
|
4
|
+
import { paintDonutGlyph, paintSparklineGlyph, paintPurityRing } from './glyph.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Table: web component wrapper.
|
|
8
|
+
* Attributes: `columns`, `rows`, `page`, `page-size`, `sort`, `sort-dir`, `virtual`, `title`, `selected`, `empty`, `loading`, `error`, `retry`, `skeleton`, `persist`.
|
|
9
|
+
*
|
|
10
|
+
* Controlled selection (R25-04, the write-half of the crossfilter contract;
|
|
11
|
+
* the identity half is R24-161). `selected` is a JSON array of ROW KEYS and
|
|
12
|
+
* drives the checkboxes from an external store; the `selected` JS property
|
|
13
|
+
* (array) is the same door and wins over the attribute while pushed.
|
|
14
|
+
*
|
|
15
|
+
* Keying — shared with R24-161's "key by row datum not view position":
|
|
16
|
+
* 1. an object row's `id` (or `key`) field;
|
|
17
|
+
* 2. the cell of a column declared `{key: true}` (stringified);
|
|
18
|
+
* 3. fallback: the row datum's canonical JSON (content identity — stable
|
|
19
|
+
* across sort/filter/virtual windowing; duplicate rows share a key, so
|
|
20
|
+
* declare a key column when the data repeats).
|
|
21
|
+
* Every rendered row carries its key as `data-row-key`.
|
|
22
|
+
*
|
|
23
|
+
* Events keep the legacy positional fields and add identity alongside:
|
|
24
|
+
* `contour:select` gains `key`/`index` on a checkbox change (`rows` stays the
|
|
25
|
+
* positional array), and `contour:filter-change`'s filter gains
|
|
26
|
+
* `selectedKeys` next to `selectedRows`. A consumer round-trips by writing
|
|
27
|
+
* `filter.selectedKeys` back to `selected`. The attribute is read only when
|
|
28
|
+
* it CHANGES — the component never reflects it back (an observed attribute
|
|
29
|
+
* a component writes to itself would re-enter render). Without a `selected`
|
|
30
|
+
* write the table keeps its legacy positional behaviour untouched.
|
|
31
|
+
*/
|
|
32
|
+
class ContourTable extends ContourBase {
|
|
33
|
+
static get updateMode() { return "keyed"; }
|
|
34
|
+
static get observed() { return ["columns", "rows", "page", "page-size", "sort", "sort-dir", "virtual", "title", "selected", "empty", "loading", "error", "retry", "skeleton", "persist", "density", "visible-columns", "cache-key", "lazy", "no-cache", "refresh", "src", "timeout"]; }
|
|
35
|
+
static get properties() { return { columns: { attribute: "columns" }, rows: { attribute: "rows" }, page: { attribute: "page", type: Number }, pageSize: { attribute: "page-size", type: Number } }; }
|
|
36
|
+
// R25-04: the property door. A push re-renders; a read returns the keys
|
|
37
|
+
// actually selected (identity), whichever mode the table is in.
|
|
38
|
+
get selected() { return this._selectionKeys(); }
|
|
39
|
+
set selected(value) {
|
|
40
|
+
this._pushedSelection = Array.isArray(value) ? value.map(String) : [];
|
|
41
|
+
this.requestRender();
|
|
42
|
+
}
|
|
43
|
+
_rowKey(row) {
|
|
44
|
+
if (row && typeof row === "object" && !Array.isArray(row)) {
|
|
45
|
+
if (row.id !== undefined && row.id !== null) return String(row.id);
|
|
46
|
+
if (row.key !== undefined && row.key !== null) return String(row.key);
|
|
47
|
+
}
|
|
48
|
+
const ki = this._keyCol;
|
|
49
|
+
if (ki !== null && ki !== undefined && ki >= 0 && row && row[ki] !== undefined && row[ki] !== null) {
|
|
50
|
+
const v = this._cellValue(row[ki]);
|
|
51
|
+
if (v !== "") return String(v);
|
|
52
|
+
}
|
|
53
|
+
return JSON.stringify(row === undefined ? null : row);
|
|
54
|
+
}
|
|
55
|
+
_selectionKeys() {
|
|
56
|
+
if (this._selectionControlled) return Array.from(this._selectedKeys || []);
|
|
57
|
+
const data = this._rowData || [];
|
|
58
|
+
return Array.from(this._selectedRows || []).map((i) => this._rowKey(data[i]));
|
|
59
|
+
}
|
|
60
|
+
// Controlled mode: the positional legacy field is DERIVED from the
|
|
61
|
+
// identity state, so `rows` and `selectedKeys` can never disagree.
|
|
62
|
+
_syncSelectionRows() {
|
|
63
|
+
const data = this._rowData || [];
|
|
64
|
+
const set = new Set();
|
|
65
|
+
data.forEach((r, i) => { if (this._selectedKeys.has(this._rowKey(r))) set.add(i); });
|
|
66
|
+
this._selectedRows = set;
|
|
67
|
+
}
|
|
68
|
+
_cellValue(cell) {
|
|
69
|
+
if (cell === null || cell === undefined) return "";
|
|
70
|
+
if (typeof cell === "object") return cell.value !== undefined ? cell.value : (cell.label !== undefined ? cell.label : JSON.stringify(cell));
|
|
71
|
+
return cell;
|
|
72
|
+
}
|
|
73
|
+
// Resolve a row's cell for a column: array rows positional, object rows keyed (matches contour-data-grid).
|
|
74
|
+
_cellFor(row, c, ci) {
|
|
75
|
+
if (row && typeof row === "object" && !Array.isArray(row)) {
|
|
76
|
+
const key = typeof c === "string" ? c : ((c && (c.key || c.label || c.title)) || "");
|
|
77
|
+
if (key && row[key] !== undefined) return row[key];
|
|
78
|
+
return row[ci];
|
|
79
|
+
}
|
|
80
|
+
return row ? row[ci] : undefined;
|
|
81
|
+
}
|
|
82
|
+
_compare(a, b, type) {
|
|
83
|
+
const av = this._cellValue(a);
|
|
84
|
+
const bv = this._cellValue(b);
|
|
85
|
+
if (type === "number" || type === "numeric") {
|
|
86
|
+
return (parseFloat(av) || 0) - (parseFloat(bv) || 0);
|
|
87
|
+
}
|
|
88
|
+
return String(av).localeCompare(String(bv), undefined, { numeric: true });
|
|
89
|
+
}
|
|
90
|
+
// R31-29: hide columns not listed in `visible-columns` (JSON index array).
|
|
91
|
+
_applyVisibleColumns(table) {
|
|
92
|
+
const raw = this.getAttribute("visible-columns");
|
|
93
|
+
if (!raw) return;
|
|
94
|
+
let vis = null;
|
|
95
|
+
try { const p = JSON.parse(raw); if (Array.isArray(p)) vis = new Set(p.map(Number)); } catch (e) { /* ignore */ }
|
|
96
|
+
if (!vis || !vis.size) return;
|
|
97
|
+
const show = (i) => vis.has(i);
|
|
98
|
+
table.querySelectorAll("thead tr th").forEach((th, i) => { th.hidden = !show(i); });
|
|
99
|
+
table.querySelectorAll("tbody tr").forEach((tr) => {
|
|
100
|
+
if (tr.children.length === 1 && tr.children[0].hasAttribute("colspan")) return; // spacer row
|
|
101
|
+
Array.from(tr.children).forEach((td, i) => { td.hidden = !show(i); });
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
render() {
|
|
105
|
+
if (!this.hasAttribute("role")) this.setAttribute("role", "region");
|
|
106
|
+
// An authored label names the table; "Table" is the fallback.
|
|
107
|
+
if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", this.getAttribute("title") || "Table");
|
|
108
|
+
wireStates(this);
|
|
109
|
+
loadRemote(this, "rows");
|
|
110
|
+
if (renderStates(this, "table")) return;
|
|
111
|
+
// The host is not a <table>, so it carries no table rule. The scroller the
|
|
112
|
+
// reflow contract requires is a real block element inside it.
|
|
113
|
+
const columns = this.getAttribute("columns");
|
|
114
|
+
const rows = remoteData(this, "rows");
|
|
115
|
+
// Slot composition: an authored table wins over the JSON attributes.
|
|
116
|
+
const authored = Array.from(this.children).filter((n) => !n.classList || !n.classList.contains("c-table-wrap"));
|
|
117
|
+
if (columns && rows && !authored.length) {
|
|
118
|
+
try {
|
|
119
|
+
const cols = JSON.parse(columns);
|
|
120
|
+
const data = JSON.parse(rows);
|
|
121
|
+
// Sync sort state from attributes on first render or external change.
|
|
122
|
+
const sortAttr = this.getAttribute("sort");
|
|
123
|
+
const dirAttr = this.getAttribute("sort-dir");
|
|
124
|
+
if (this._sortCol === undefined || sortAttr !== this._sortAttrLast || dirAttr !== this._sortDirAttrLast) {
|
|
125
|
+
this._sortAttrLast = sortAttr;
|
|
126
|
+
this._sortDirAttrLast = dirAttr;
|
|
127
|
+
if (sortAttr !== null) {
|
|
128
|
+
const idx = Number(sortAttr);
|
|
129
|
+
this._sortCol = Number.isFinite(idx) ? idx : cols.findIndex((c) => (typeof c === "object" && c && (c.label || c.title || c) === sortAttr));
|
|
130
|
+
if (this._sortCol < 0) this._sortCol = null;
|
|
131
|
+
} else {
|
|
132
|
+
this._sortCol = null;
|
|
133
|
+
}
|
|
134
|
+
this._sortDir = dirAttr === "descending" || dirAttr === "desc" ? -1 : 1;
|
|
135
|
+
}
|
|
136
|
+
// Apply sort.
|
|
137
|
+
let working = Array.isArray(data) ? data.slice() : [];
|
|
138
|
+
if (this._sortCol !== null && this._sortCol !== undefined && this._sortCol >= 0 && this._sortCol < cols.length) {
|
|
139
|
+
const col = this._sortCol;
|
|
140
|
+
const dir = this._sortDir || 1;
|
|
141
|
+
const colSpec = cols[col];
|
|
142
|
+
const type = (typeof colSpec === "object" && colSpec && colSpec.sortType) ? colSpec.sortType : "text";
|
|
143
|
+
// Resolve the sort cell by column key for object rows (matches _cellFor); positional read was a no-op.
|
|
144
|
+
working.sort((a, b) => this._compare(this._cellFor(a, colSpec, col), this._cellFor(b, colSpec, col), type) * dir);
|
|
145
|
+
}
|
|
146
|
+
// Column filters: narrow `working` by each active filter.
|
|
147
|
+
if (!this._filters) this._filters = {};
|
|
148
|
+
const activeFilters = Object.keys(this._filters).filter((k) => this._filters[k]);
|
|
149
|
+
if (activeFilters.length) {
|
|
150
|
+
working = working.filter((row) => activeFilters.every((k) => {
|
|
151
|
+
const ci = Number(k);
|
|
152
|
+
const q = String(this._filters[ci] || "").toLowerCase();
|
|
153
|
+
if (!q) return true;
|
|
154
|
+
// R28-37: key-resolved cell for object rows (positional row[ci] read undefined, filtering out all object rows).
|
|
155
|
+
return String(this._cellValue(this._cellFor(row, cols[ci], ci))).toLowerCase().includes(q);
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
// Pagination.
|
|
159
|
+
const pageSizeRaw = this.getAttribute("page-size");
|
|
160
|
+
const pageSize = pageSizeRaw !== null ? parseInt(pageSizeRaw, 10) : NaN;
|
|
161
|
+
const usePaging = Number.isFinite(pageSize) && pageSize > 0;
|
|
162
|
+
// Sync internal page state with external attribute changes.
|
|
163
|
+
let pageAttr = this.getAttribute("page");
|
|
164
|
+
if (this.getAttribute("persist") && this.id) {
|
|
165
|
+
const mode = this.getAttribute("persist");
|
|
166
|
+
let stored = null;
|
|
167
|
+
if (mode === "storage") { try { stored = localStorage.getItem("contour-table:" + this.id); } catch (e) {} }
|
|
168
|
+
else if (mode === "url") { try { stored = new URLSearchParams(window.location.search).get("c-table-" + this.id); } catch (e) {} }
|
|
169
|
+
if (stored !== null) pageAttr = stored;
|
|
170
|
+
}
|
|
171
|
+
if (this._pageInternal) {
|
|
172
|
+
this._pageInternal = false;
|
|
173
|
+
this._pageAttrLast = pageAttr;
|
|
174
|
+
} else if (pageAttr !== this._pageAttrLast) {
|
|
175
|
+
this._page = null;
|
|
176
|
+
this._pageAttrLast = pageAttr;
|
|
177
|
+
}
|
|
178
|
+
let page = this._page !== null ? this._page : (parseInt(pageAttr || "1", 10));
|
|
179
|
+
if (!Number.isFinite(page) || page < 1) page = 1;
|
|
180
|
+
let totalPages = 1;
|
|
181
|
+
let pageData = working;
|
|
182
|
+
if (usePaging) {
|
|
183
|
+
totalPages = Math.max(1, Math.ceil(working.length / pageSize));
|
|
184
|
+
if (page > totalPages) page = totalPages;
|
|
185
|
+
const start = (page - 1) * pageSize;
|
|
186
|
+
pageData = working.slice(start, start + pageSize);
|
|
187
|
+
}
|
|
188
|
+
this._page = page;
|
|
189
|
+
this._totalPages = totalPages;
|
|
190
|
+
this._pageSize = pageSize;
|
|
191
|
+
// Keep row objects in memory instead of serializing each into a data-row attribute: JSON duplicated data and bloated every row.
|
|
192
|
+
this._rowData = pageData;
|
|
193
|
+
// R25-04: controlled selection input. The key column is resolved from column specs; `selected` attribute is parsed only when it changes (internal changes emit events for the store to write back — reflecting the attribute would re-enter render).
|
|
194
|
+
this._keyCol = cols.findIndex((c) => typeof c === "object" && c !== null && c.key === true);
|
|
195
|
+
this._selectedRows = this._selectedRows || new Set();
|
|
196
|
+
this._selectedKeys = this._selectedKeys || new Set();
|
|
197
|
+
const selAttr = this.getAttribute("selected");
|
|
198
|
+
if (selAttr !== this._selectedAttrLast) {
|
|
199
|
+
this._selectedAttrLast = selAttr;
|
|
200
|
+
if (selAttr !== null) {
|
|
201
|
+
try {
|
|
202
|
+
const p = JSON.parse(selAttr);
|
|
203
|
+
if (Array.isArray(p)) { this._selectedKeys = new Set(p.map(String)); this._selectionControlled = true; }
|
|
204
|
+
else warnBadJson(this, "selected", new Error("not an array"));
|
|
205
|
+
} catch (e) { warnBadJson(this, "selected", e); }
|
|
206
|
+
} else if (this._pushedSelection === undefined) {
|
|
207
|
+
this._selectionControlled = false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (this._pushedSelection !== undefined) {
|
|
211
|
+
this._selectedKeys = new Set(this._pushedSelection);
|
|
212
|
+
this._selectionControlled = true;
|
|
213
|
+
}
|
|
214
|
+
if (this._selectionControlled) this._syncSelectionRows();
|
|
215
|
+
|
|
216
|
+
// The scroller must survive a re-render. Created through ensure() it was an ordinary generated node, so the base class dropped and rebuilt it on every render — including the render a scroll frame triggers. The replacement then had scrollTop 0, so the virtual window recomputed from the top and the table snapped back to row 0 on the first wheel tick (DW-5). keepNode() leaves it alive; its scroll offset is carried by the element instead of by a snapshot.
|
|
217
|
+
const wrap = this._virtualWrap || this.keepNode(
|
|
218
|
+
this.ensure(".c-table-wrap", () => {
|
|
219
|
+
const el = document.createElement("div");
|
|
220
|
+
el.className = "c-table-wrap";
|
|
221
|
+
// R24-04: scroller wrapper needs to be focusable and labelled
|
|
222
|
+
// so keyboard users can scroll it and screen readers announce it.
|
|
223
|
+
el.setAttribute("tabindex", "0");
|
|
224
|
+
el.setAttribute("role", "region");
|
|
225
|
+
el.setAttribute("aria-label", labelText(this, "table-data", "Table data"));
|
|
226
|
+
return el;
|
|
227
|
+
})
|
|
228
|
+
);
|
|
229
|
+
this._virtualWrap = wrap;
|
|
230
|
+
// Reuse the existing table when columns are unchanged, rebuilding only the body. This avoids O(n) DOM churn on row-only updates.
|
|
231
|
+
const colsKey = JSON.stringify(cols);
|
|
232
|
+
const table = (wrap.querySelector("table.c-table") && this._tableCols === colsKey)
|
|
233
|
+
? wrap.querySelector("table.c-table")
|
|
234
|
+
: (() => {
|
|
235
|
+
const t = document.createElement("table");
|
|
236
|
+
t.className = "c-table";
|
|
237
|
+
// A table needs a caption: the host's region label is outside the table, so a reader who enters it hears column headers with no statement of what the table is. Visually hidden keeps the block layout unchanged.
|
|
238
|
+
const captionEl = document.createElement("caption");
|
|
239
|
+
captionEl.className = "visually-hidden";
|
|
240
|
+
captionEl.textContent = this.getAttribute("title") || this.getAttribute("aria-label") || "Table";
|
|
241
|
+
t.appendChild(captionEl);
|
|
242
|
+
const thead = document.createElement("thead");
|
|
243
|
+
const tr = document.createElement("tr");
|
|
244
|
+
cols.forEach((c, i) => {
|
|
245
|
+
const colLabel = typeof c === "string" ? c : (c.label || c.title || c);
|
|
246
|
+
const th = document.createElement("th");
|
|
247
|
+
const sortable = (typeof c === "object" && c !== null && c.sort);
|
|
248
|
+
const isSelect = (typeof c === "object" && c !== null && c.select);
|
|
249
|
+
const isFilter = (typeof c === "object" && c !== null && c.filter);
|
|
250
|
+
const numeric = (typeof c === "object" && c !== null && (c.type === "number" || c.type === "numeric"));
|
|
251
|
+
if (numeric) th.classList.add("c-table__number");
|
|
252
|
+
if (isFilter) {
|
|
253
|
+
th.className = th.className.trim();
|
|
254
|
+
const inp = document.createElement("input");
|
|
255
|
+
inp.type = "search";
|
|
256
|
+
inp.className = "c-table__filter-input";
|
|
257
|
+
inp.placeholder = colLabel;
|
|
258
|
+
// A placeholder is not a label: a reader who tabs into the filter hears "edit text" with no column name. Name it.
|
|
259
|
+
inp.setAttribute("aria-label", labelText(this, "filter", "Filter") + " " + colLabel);
|
|
260
|
+
inp.dataset.filterIndex = String(i);
|
|
261
|
+
if (this._filters && this._filters[i]) inp.value = this._filters[i];
|
|
262
|
+
th.appendChild(inp);
|
|
263
|
+
} else if (isSelect) {
|
|
264
|
+
th.className = (th.className + " c-table__select").trim();
|
|
265
|
+
const cb = document.createElement("input");
|
|
266
|
+
cb.type = "checkbox";
|
|
267
|
+
cb.className = "c-table__select-all";
|
|
268
|
+
cb.setAttribute("aria-label", labelText(this, "select-all-rows", "Select all rows"));
|
|
269
|
+
// Stable identity for the base class's control-state restoration, so it never index-matches onto a row box.
|
|
270
|
+
cb.dataset.contourPreserveKey = "select-all";
|
|
271
|
+
th.appendChild(cb);
|
|
272
|
+
} else if (sortable) {
|
|
273
|
+
th.setAttribute("aria-sort", (this._sortCol === i)
|
|
274
|
+
? (this._sortDir === -1 ? "descending" : "ascending")
|
|
275
|
+
: "none");
|
|
276
|
+
const wrapEl = document.createElement("span");
|
|
277
|
+
wrapEl.className = "c-table__sort";
|
|
278
|
+
wrapEl.textContent = colLabel;
|
|
279
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
280
|
+
svg.setAttribute("aria-hidden", "true");
|
|
281
|
+
setHTML(svg, '<use href="#i-chevron-down" />');
|
|
282
|
+
wrapEl.appendChild(svg);
|
|
283
|
+
th.appendChild(wrapEl);
|
|
284
|
+
} else if (typeof c === "object" && c !== null && c.pinned) {
|
|
285
|
+
th.className = "c-table__pinned";
|
|
286
|
+
th.textContent = colLabel;
|
|
287
|
+
} else {
|
|
288
|
+
th.textContent = colLabel;
|
|
289
|
+
}
|
|
290
|
+
if (sortable) {
|
|
291
|
+
th.setAttribute("tabindex", "0");
|
|
292
|
+
th.setAttribute("role", "columnheader");
|
|
293
|
+
th.dataset.sortIndex = String(i);
|
|
294
|
+
}
|
|
295
|
+
tr.appendChild(th);
|
|
296
|
+
});
|
|
297
|
+
thead.appendChild(tr);
|
|
298
|
+
t.appendChild(thead);
|
|
299
|
+
t.appendChild(document.createElement("tbody"));
|
|
300
|
+
return t;
|
|
301
|
+
})();
|
|
302
|
+
this._tableCols = colsKey;
|
|
303
|
+
// R31-29: visible-columns (JSON array of column indices) hides the
|
|
304
|
+
// others. Applied as a post-render hide so the sort indices and the
|
|
305
|
+
// data model stay on the full column set.
|
|
306
|
+
this._applyVisibleColumns(table);
|
|
307
|
+
// Keep sort indicators in sync even when the table element is reused.
|
|
308
|
+
cols.forEach((c, i) => {
|
|
309
|
+
if (typeof c === "object" && c !== null && c.sort) {
|
|
310
|
+
const th = table.querySelector("thead tr th:nth-child(" + (i + 1) + ")");
|
|
311
|
+
if (th) th.setAttribute("aria-sort", (this._sortCol === i) ? (this._sortDir === -1 ? "descending" : "ascending") : "none");
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
const tbody = table.querySelector("tbody");
|
|
315
|
+
// Clearing the body clamps the scroller's scrollTop to 0 (the content is momentarily gone), so the virtual window would recompute from the top on the next scroll frame and the table would snap back to row 0 (DW-5). Carry the offset across the rebuild and restore it once the new rows are in. The scroll handler records what the reader asked for, because by the time render() runs the element may already have been clamped by the previous rebuild.
|
|
316
|
+
const wrapScrollTop = this._tablePendingScrollTop != null ? this._tablePendingScrollTop : wrap.scrollTop;
|
|
317
|
+
this._tablePendingScrollTop = null;
|
|
318
|
+
tbody.innerHTML = "";
|
|
319
|
+
const useVirtual = this.hasAttribute("virtual") && !usePaging && pageData.length > 100;
|
|
320
|
+
// One fallback matching ui.mjs: the two used to disagree (36 vs 32), so a table and a data grid on the same page computed different windows from the same token.
|
|
321
|
+
const rowHeight = this._measuredRowHeight || cssLength("--data-grid-row-height", 32);
|
|
322
|
+
const windowSize = 30;
|
|
323
|
+
const overscan = 10;
|
|
324
|
+
let windowStart = 0;
|
|
325
|
+
let visibleData = pageData;
|
|
326
|
+
if (useVirtual) {
|
|
327
|
+
const maxStart = Math.max(0, pageData.length - windowSize);
|
|
328
|
+
// Compute from wrapScrollTop, not wrap.scrollTop: clearing the body above already clamped the live scrollTop to 0, so reading it here would always yield windowStart 0 and the table would snap back to the first row on every scroll frame (DW-5).
|
|
329
|
+
windowStart = Math.min(maxStart, Math.max(0, Math.floor(wrapScrollTop / rowHeight) - overscan));
|
|
330
|
+
visibleData = pageData.slice(windowStart, windowStart + windowSize + overscan * 2);
|
|
331
|
+
table.setAttribute("aria-rowcount", String(pageData.length + 1));
|
|
332
|
+
// Record which window is on screen so the scroll handler can tell a scroll that moves the window from one that does not.
|
|
333
|
+
this._tableWindowStart = windowStart;
|
|
334
|
+
} else {
|
|
335
|
+
table.removeAttribute("aria-rowcount");
|
|
336
|
+
}
|
|
337
|
+
const addSpacer = (height) => {
|
|
338
|
+
const tr = document.createElement("tr");
|
|
339
|
+
tr.setAttribute("aria-hidden", "true");
|
|
340
|
+
const td = document.createElement("td");
|
|
341
|
+
td.colSpan = cols.length;
|
|
342
|
+
td.style.blockSize = `${height}px`;
|
|
343
|
+
td.style.padding = "0";
|
|
344
|
+
tr.appendChild(td);
|
|
345
|
+
tbody.appendChild(tr);
|
|
346
|
+
};
|
|
347
|
+
if (useVirtual) addSpacer(windowStart * rowHeight);
|
|
348
|
+
if (visibleData.length === 0) {
|
|
349
|
+
const tr = document.createElement("tr");
|
|
350
|
+
const td = document.createElement("td");
|
|
351
|
+
td.colSpan = cols.length;
|
|
352
|
+
td.textContent = this.getAttribute("empty") || "No data";
|
|
353
|
+
td.style.textAlign = "center";
|
|
354
|
+
td.style.color = "var(--color-text-muted)";
|
|
355
|
+
tr.appendChild(td);
|
|
356
|
+
tbody.appendChild(tr);
|
|
357
|
+
} else {
|
|
358
|
+
visibleData.forEach((row, ri) => {
|
|
359
|
+
const tr = document.createElement("tr");
|
|
360
|
+
tr.dataset.rowIndex = String(windowStart + ri);
|
|
361
|
+
// R25-04: the datum key rides the row so handlers and consumers can address identity, not view position.
|
|
362
|
+
const rowKey = this._rowKey(row);
|
|
363
|
+
tr.dataset.rowKey = rowKey;
|
|
364
|
+
// One tab stop per grid, not one per row: every row is -1 and the arrow keys move a roving tabindex. A 1,000-row table was 1,000 tab stops before this.
|
|
365
|
+
tr.setAttribute("tabindex", "-1");
|
|
366
|
+
cols.forEach((c, ci) => {
|
|
367
|
+
const isSelect = (typeof c === "object" && c !== null && c.select);
|
|
368
|
+
const numeric = (typeof c === "object" && c !== null && (c.type === "number" || c.type === "numeric"));
|
|
369
|
+
const td = document.createElement("td");
|
|
370
|
+
if (numeric) td.classList.add("c-table__number");
|
|
371
|
+
if (isSelect) {
|
|
372
|
+
const cb = document.createElement("input");
|
|
373
|
+
cb.type = "checkbox";
|
|
374
|
+
cb.className = "c-table__row-select";
|
|
375
|
+
cb.dataset.rowIndex = String(windowStart + ri);
|
|
376
|
+
// The base class preserves form-control state across a re-render; UNKEYED checkboxes restore by position, which would paint the pre-sort checked positions onto the new row order (and a virtual window's onto different rows). The preserve key makes that restoration identity-based: a row's checked state follows its datum, and a row newly scrolled into the window keeps the state render gave it.
|
|
377
|
+
cb.dataset.contourPreserveKey = rowKey;
|
|
378
|
+
// Controlled mode checks by KEY (survives sort/filter/window moves); legacy mode keeps the positional contract. The checkbox's checked state is the ARIA reflection — a plain <table> row must not carry aria-selected (grid-only state).
|
|
379
|
+
if (this._selectionControlled) {
|
|
380
|
+
if (this._selectedKeys.has(rowKey)) cb.checked = true;
|
|
381
|
+
} else if (this._selectedRows && this._selectedRows.has(windowStart + ri)) {
|
|
382
|
+
cb.checked = true;
|
|
383
|
+
}
|
|
384
|
+
td.appendChild(cb);
|
|
385
|
+
} else {
|
|
386
|
+
td.textContent = this._cellValue(this._cellFor(row, c, ci));
|
|
387
|
+
}
|
|
388
|
+
tr.appendChild(td);
|
|
389
|
+
});
|
|
390
|
+
tbody.appendChild(tr);
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
if (useVirtual) addSpacer(Math.max(0, pageData.length - windowStart - visibleData.length) * rowHeight);
|
|
394
|
+
// Keep exactly one row in the tab order: the first rendered row when none is active yet, so the grid is a single tab stop.
|
|
395
|
+
if (!tbody.querySelector('tr[tabindex="0"]')) {
|
|
396
|
+
const first = tbody.querySelector("tr[data-row-index]");
|
|
397
|
+
if (first) first.setAttribute("tabindex", "0");
|
|
398
|
+
}
|
|
399
|
+
// Measure a real row so the virtual window uses the rendered height, not a guess: the old fixed 36px disagreed with the measured 64.1px, so the spacers and the scroll math were both wrong.
|
|
400
|
+
if (!this._measuredRowHeight && !this._measuringRow) {
|
|
401
|
+
const firstRow = tbody.querySelector("tr[data-row-index]");
|
|
402
|
+
const h = firstRow ? firstRow.getBoundingClientRect().height : 0;
|
|
403
|
+
if (h > 0) {
|
|
404
|
+
this._measuredRowHeight = h;
|
|
405
|
+
if (Math.abs(h - rowHeight) > 1) {
|
|
406
|
+
this._measuringRow = true;
|
|
407
|
+
requestAnimationFrame(() => { this._measuringRow = false; this.render(); });
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (table.parentNode !== wrap) {
|
|
412
|
+
// Clear the contents, not the scroller: innerHTML = "" on the wrap would also reset its scrollTop, which is the offset the virtual window is computed from.
|
|
413
|
+
while (wrap.firstChild) wrap.removeChild(wrap.firstChild);
|
|
414
|
+
wrap.appendChild(table);
|
|
415
|
+
}
|
|
416
|
+
if (wrap.parentNode !== this) this.appendChild(wrap);
|
|
417
|
+
// Restore the scroll offset the body rebuild clamped away. Doing it here, before the browser has painted the empty body, is what keeps the virtual window anchored to the row the reader is looking at.
|
|
418
|
+
if (useVirtual && wrapScrollTop > 0 && wrap.scrollTop !== wrapScrollTop) {
|
|
419
|
+
// The scroll event this assignment emits is asynchronous — it fires after this task, by which time a try/finally flag has already been cleared. A timestamp is what survives the gap: the handler below ignores any scroll that arrives within a frame of a restore, because that scroll is the restore, not the reader.
|
|
420
|
+
this._tableScrollRestoredAt = (typeof performance !== "undefined" && performance.now) ? performance.now() : Date.now();
|
|
421
|
+
wrap.scrollTop = wrapScrollTop;
|
|
422
|
+
}
|
|
423
|
+
// Optional fixed-row DOM windowing for large datasets. Opt-in via `virtual`.
|
|
424
|
+
if (this.hasAttribute("virtual")) {
|
|
425
|
+
wrap.style.maxHeight = wrap.style.maxHeight || "24rem";
|
|
426
|
+
wrap.style.overflow = "auto";
|
|
427
|
+
// content-visibility: auto on the wrap collapses its scrollHeight to the contain-intrinsic-size placeholder (300px), so a virtual scroller has nothing to scroll through and the window never advances (DW-5). The wrap is the scroll target here, so it must render its contents; the windowing already keeps the DOM small.
|
|
428
|
+
wrap.style.contentVisibility = "visible";
|
|
429
|
+
table.classList.add("c-table--virtual");
|
|
430
|
+
} else {
|
|
431
|
+
wrap.style.maxHeight = "";
|
|
432
|
+
wrap.style.overflow = "";
|
|
433
|
+
wrap.style.contentVisibility = "";
|
|
434
|
+
table.classList.remove("c-table--virtual");
|
|
435
|
+
}
|
|
436
|
+
// Pagination footer.
|
|
437
|
+
if (usePaging) {
|
|
438
|
+
const footer = this.ensure(".c-table__pagination", () => { const el = document.createElement("div"); el.className = "c-table__pagination"; return el; });
|
|
439
|
+
footer.innerHTML = "";
|
|
440
|
+
const prev = document.createElement("button");
|
|
441
|
+
prev.className = "c-btn c-btn--ghost c-btn--sm";
|
|
442
|
+
prev.type = "button";
|
|
443
|
+
prev.textContent = labelText(this, "prev", "Previous");
|
|
444
|
+
prev.disabled = page <= 1;
|
|
445
|
+
prev.dataset.action = "prev";
|
|
446
|
+
const info = document.createElement("span");
|
|
447
|
+
info.textContent = `${labelText(this, "page", "Page")} ${page} ${labelText(this, "of", "of")} ${totalPages}`;
|
|
448
|
+
const next = document.createElement("button");
|
|
449
|
+
next.className = "c-btn c-btn--ghost c-btn--sm";
|
|
450
|
+
next.type = "button";
|
|
451
|
+
next.textContent = labelText(this, "next", "Next");
|
|
452
|
+
next.disabled = page >= totalPages;
|
|
453
|
+
next.dataset.action = "next";
|
|
454
|
+
footer.appendChild(prev);
|
|
455
|
+
footer.appendChild(info);
|
|
456
|
+
footer.appendChild(next);
|
|
457
|
+
} else {
|
|
458
|
+
const footer = this.querySelector(".c-table__pagination");
|
|
459
|
+
if (footer) footer.remove();
|
|
460
|
+
}
|
|
461
|
+
} catch (e) { warnBadJson(this, "attribute", e); }
|
|
462
|
+
} else if (!rows && !authored.length) {
|
|
463
|
+
renderEmpty(this, "table");
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
// R25-04: re-assert the selection AFTER the base's form-control restore. That restore preserves user-typed state across a re-render and runs after render(); for a CONTROLLED selection the store-driven state is the truth, so the rendered checks are re-applied here — enhance() runs last on both the connect and the re-render path. Idempotent and window-sized; the select-all box keeps the base's own restoration.
|
|
467
|
+
_applySelectionToDom() {
|
|
468
|
+
this.querySelectorAll(".c-table__row-select").forEach((cb) => {
|
|
469
|
+
const idx = Number(cb.dataset.rowIndex);
|
|
470
|
+
if (!Number.isFinite(idx)) return;
|
|
471
|
+
const row = this._rowData ? this._rowData[idx] : undefined;
|
|
472
|
+
const checked = this._selectionControlled
|
|
473
|
+
? this._selectedKeys.has(this._rowKey(row))
|
|
474
|
+
: !!(this._selectedRows && this._selectedRows.has(idx));
|
|
475
|
+
if (cb.checked !== checked) cb.checked = checked;
|
|
476
|
+
});
|
|
477
|
+
// R31-32: the select-all box is tri-state when only part of the rendered
|
|
478
|
+
// window is selected; without this it read as "nothing selected".
|
|
479
|
+
const all = this.querySelector(".c-table__select-all");
|
|
480
|
+
if (all) {
|
|
481
|
+
const boxes = Array.from(this.querySelectorAll(".c-table__row-select"));
|
|
482
|
+
const n = boxes.filter((cb) => cb.checked).length;
|
|
483
|
+
all.checked = boxes.length > 0 && n === boxes.length;
|
|
484
|
+
all.indeterminate = n > 0 && n < boxes.length;
|
|
485
|
+
// A windowed table selects the rendered page, not the dataset, so its
|
|
486
|
+
// control must say so — "Select all rows" would overstate the scope.
|
|
487
|
+
all.setAttribute("aria-label", flagOn(this, "virtual")
|
|
488
|
+
? labelText(this, "select-visible-rows", "Select visible rows")
|
|
489
|
+
: labelText(this, "select-all-rows", "Select all rows"));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
enhance() {
|
|
493
|
+
this._applySelectionToDom();
|
|
494
|
+
if (this._wiredTable) return;
|
|
495
|
+
this._wiredTable = true;
|
|
496
|
+
const handleSort = (th) => {
|
|
497
|
+
const idx = Number(th.dataset.sortIndex);
|
|
498
|
+
if (!Number.isFinite(idx)) return;
|
|
499
|
+
if (this._sortCol === idx) {
|
|
500
|
+
this._sortDir = (this._sortDir === 1) ? -1 : 1;
|
|
501
|
+
} else {
|
|
502
|
+
this._sortCol = idx;
|
|
503
|
+
this._sortDir = 1;
|
|
504
|
+
}
|
|
505
|
+
this.dispatchEvent(new CustomEvent("contour:sort", { bubbles: true, composed: true, detail: { column: idx, direction: this._sortDir === 1 ? "ascending" : "descending" } }));
|
|
506
|
+
this.dispatchEvent(new CustomEvent("contour:filter-change", { bubbles: true, composed: true, detail: { source: "table", filter: { sort: idx, sortDir: this._sortDir === 1 ? "ascending" : "descending" } } }));
|
|
507
|
+
this.requestRender();
|
|
508
|
+
};
|
|
509
|
+
this.addEventListener("click", (e) => {
|
|
510
|
+
const retryBtn = e.target && e.target.closest ? e.target.closest('button[data-action="retry"]') : null;
|
|
511
|
+
if (retryBtn && this.contains(retryBtn)) {
|
|
512
|
+
e.preventDefault();
|
|
513
|
+
this.dispatchEvent(new CustomEvent("contour:retry", { detail: { src: this.getAttribute("src") }, bubbles: true, composed: true }));
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
const th = e.target && e.target.closest ? e.target.closest("th[aria-sort]") : null;
|
|
517
|
+
if (th && this.contains(th)) {
|
|
518
|
+
e.preventDefault();
|
|
519
|
+
handleSort(th);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
const btn = e.target && e.target.closest ? e.target.closest(".c-table__pagination button") : null;
|
|
523
|
+
if (btn && this.contains(btn)) {
|
|
524
|
+
e.preventDefault();
|
|
525
|
+
const action = btn.dataset.action;
|
|
526
|
+
let page = this._page !== null ? this._page : (parseInt(this.getAttribute("page") || "1", 10));
|
|
527
|
+
if (!Number.isFinite(page) || page < 1) page = 1;
|
|
528
|
+
if (action === "prev" && page > 1) page -= 1;
|
|
529
|
+
if (action === "next") page += 1;
|
|
530
|
+
this._page = page;
|
|
531
|
+
this._pageAttrLast = String(page);
|
|
532
|
+
// Reflect page on host for consumers (D-04); marked internal so render preserves _page (B3-01).
|
|
533
|
+
this._pageInternal = true;
|
|
534
|
+
this.setAttribute("page", String(page));
|
|
535
|
+
this.dispatchEvent(new CustomEvent("contour:page", { bubbles: true, composed: true, detail: { page } }));
|
|
536
|
+
this.dispatchEvent(new CustomEvent("contour:filter-change", { bubbles: true, composed: true, detail: { source: "table", filter: { page } } }));
|
|
537
|
+
const persist = this.getAttribute("persist");
|
|
538
|
+
if (persist === "storage" && this.id) { try { localStorage.setItem("contour-table:" + this.id, String(page)); } catch (e) {} }
|
|
539
|
+
else if (persist === "url" && this.id) { try { const url = new URL(window.location.href); url.searchParams.set("c-table-" + this.id, String(page)); window.history.replaceState(null, "", url.toString()); } catch (e) {} }
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
this.addEventListener("keydown", (e) => {
|
|
543
|
+
if (e.key !== "Enter" && e.key !== " ") return;
|
|
544
|
+
const th = e.target && e.target.closest ? e.target.closest("th[aria-sort]") : null;
|
|
545
|
+
if (th && this.contains(th)) {
|
|
546
|
+
e.preventDefault();
|
|
547
|
+
handleSort(th);
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
// Column filter (F-09): re-render on filter input change.
|
|
551
|
+
this.addEventListener("input", (e) => {
|
|
552
|
+
const inp = e.target && e.target.closest ? e.target.closest(".c-table__filter-input") : null;
|
|
553
|
+
if (!inp || !this.contains(inp)) return;
|
|
554
|
+
const idx = Number(inp.dataset.filterIndex);
|
|
555
|
+
if (!Number.isFinite(idx)) return;
|
|
556
|
+
if (!this._filters) this._filters = {};
|
|
557
|
+
this._filters[idx] = inp.value;
|
|
558
|
+
this.requestRender();
|
|
559
|
+
this.dispatchEvent(new CustomEvent("contour:filter-change", { bubbles: true, composed: true, detail: { source: "table", filter: { column: idx, value: inp.value } } }));
|
|
560
|
+
});
|
|
561
|
+
this.addEventListener("scroll", (e) => {
|
|
562
|
+
if (!this.hasAttribute("virtual") || !e.target || !e.target.classList || !e.target.classList.contains("c-table-wrap")) return;
|
|
563
|
+
// Only re-render when computed window moves. Scroller emits per-pixel scroll; rebuilding 50 rows per event is wasteful and creates feedback loop (rebuild emits scroll). Window = scrollTop/rowHeight; compare that index.
|
|
564
|
+
const wrap = e.target;
|
|
565
|
+
const rowHeight = this._measuredRowHeight || cssLength("--data-grid-row-height", 32);
|
|
566
|
+
const maxStart = Math.max(0, (this._rowData ? this._rowData.length : 0) - 30);
|
|
567
|
+
const start = Math.min(maxStart, Math.max(0, Math.floor(wrap.scrollTop / rowHeight) - 10));
|
|
568
|
+
if (this._tableWindowStart === start) return;
|
|
569
|
+
if (this._tableVirtualRaf) return;
|
|
570
|
+
// Remember offset reader asked for; render clears body clamping scrollTop to 0 before render reads it — carry value here.
|
|
571
|
+
this._tablePendingScrollTop = wrap.scrollTop;
|
|
572
|
+
const run = () => { this._tableVirtualRaf = 0; this.requestRender(); };
|
|
573
|
+
this._tableVirtualRaf = typeof requestAnimationFrame === "function" ? requestAnimationFrame(run) : setTimeout(run, 16);
|
|
574
|
+
}, true);
|
|
575
|
+
// A pending virtual-scroll frame must not outlive its host: the callback calls requestRender() on a detached element. Cancel whichever handle the environment produced (rAF id or setTimeout id).
|
|
576
|
+
onDispose(this, () => {
|
|
577
|
+
if (this._tableVirtualRaf) {
|
|
578
|
+
if (typeof cancelAnimationFrame === "function") cancelAnimationFrame(this._tableVirtualRaf);
|
|
579
|
+
else clearTimeout(this._tableVirtualRaf);
|
|
580
|
+
this._tableVirtualRaf = 0;
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
// Row selection (F-09): track checked rows and emit contour:select.
|
|
584
|
+
// R25-04: in controlled mode the KEY set is the source of truth and the positional set is derived; in legacy mode the positional set stays the source of truth and keys are derived for the additive event fields.
|
|
585
|
+
this._selectedRows = this._selectedRows || new Set();
|
|
586
|
+
this.addEventListener("change", (e) => {
|
|
587
|
+
const cb = e.target && e.target.closest ? e.target.closest(".c-table__row-select") : null;
|
|
588
|
+
if (cb && this.contains(cb)) {
|
|
589
|
+
const idx = Number(cb.dataset.rowIndex);
|
|
590
|
+
if (!Number.isFinite(idx)) return;
|
|
591
|
+
const key = this._rowKey(this._rowData ? this._rowData[idx] : undefined);
|
|
592
|
+
if (this._selectionControlled) {
|
|
593
|
+
if (cb.checked) this._selectedKeys.add(key); else this._selectedKeys.delete(key);
|
|
594
|
+
this._syncSelectionRows();
|
|
595
|
+
} else {
|
|
596
|
+
if (cb.checked) this._selectedRows.add(idx); else this._selectedRows.delete(idx);
|
|
597
|
+
}
|
|
598
|
+
this._applySelectionToDom();
|
|
599
|
+
const rows = Array.from(this._selectedRows);
|
|
600
|
+
const keys = this._selectionKeys();
|
|
601
|
+
this.dispatchEvent(new CustomEvent("contour:select", { bubbles: true, composed: true, detail: { rows, key, index: idx } }));
|
|
602
|
+
this.dispatchEvent(new CustomEvent("contour:filter-change", { bubbles: true, composed: true, detail: { source: "table", filter: { selectedRows: rows, selectedKeys: keys } } }));
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
const all = e.target && e.target.closest ? e.target.closest(".c-table__select-all") : null;
|
|
606
|
+
if (all && this.contains(all)) {
|
|
607
|
+
const cbs = Array.from(this.querySelectorAll(".c-table__row-select"));
|
|
608
|
+
cbs.forEach((c) => { c.checked = all.checked; });
|
|
609
|
+
if (this._selectionControlled) {
|
|
610
|
+
// Select-all scope = rendered rows (R24-161); controlled mode adds/removes keys, keeps selections outside window.
|
|
611
|
+
cbs.forEach((c) => {
|
|
612
|
+
const i = Number(c.dataset.rowIndex);
|
|
613
|
+
const k = this._rowKey(this._rowData ? this._rowData[i] : undefined);
|
|
614
|
+
if (all.checked) this._selectedKeys.add(k); else this._selectedKeys.delete(k);
|
|
615
|
+
});
|
|
616
|
+
this._syncSelectionRows();
|
|
617
|
+
} else {
|
|
618
|
+
this._selectedRows = new Set(all.checked ? cbs.map((c) => Number(c.dataset.rowIndex)) : []);
|
|
619
|
+
}
|
|
620
|
+
all.indeterminate = false;
|
|
621
|
+
const rows = Array.from(this._selectedRows);
|
|
622
|
+
const keys = this._selectionKeys();
|
|
623
|
+
this.dispatchEvent(new CustomEvent("contour:select", { bubbles: true, composed: true, detail: { rows } }));
|
|
624
|
+
this.dispatchEvent(new CustomEvent("contour:filter-change", { bubbles: true, composed: true, detail: { source: "table", filter: { selectedRows: rows, selectedKeys: keys } } }));
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
// Map-linked table (CARTO-inspired): emit contour:hover on row hover so a consumer can highlight/zoom the corresponding feature on the map. Contour stays engine-agnostic: it only announces the row.
|
|
628
|
+
this.addEventListener("mouseover", (e) => {
|
|
629
|
+
const tr = e.target && e.target.closest ? e.target.closest("tbody tr") : null;
|
|
630
|
+
if (!tr || !this.contains(tr)) return;
|
|
631
|
+
const idx = Number(tr.dataset.rowIndex);
|
|
632
|
+
if (!Number.isFinite(idx)) return;
|
|
633
|
+
if (this._hoveredRow === idx) return;
|
|
634
|
+
this._hoveredRow = idx;
|
|
635
|
+
this.dispatchEvent(new CustomEvent("contour:hover", { bubbles: true, composed: true, detail: { row: idx } }));
|
|
636
|
+
});
|
|
637
|
+
this.addEventListener("mouseout", (e) => {
|
|
638
|
+
const tr = e.target && e.target.closest ? e.target.closest("tbody tr") : null;
|
|
639
|
+
if (!tr || !this.contains(tr)) return;
|
|
640
|
+
const related = e.relatedTarget && e.relatedTarget.closest ? e.relatedTarget.closest("tbody tr") : null;
|
|
641
|
+
if (related === tr) return;
|
|
642
|
+
this._hoveredRow = null;
|
|
643
|
+
this.dispatchEvent(new CustomEvent("contour:leave", { bubbles: true, composed: true, detail: {} }));
|
|
644
|
+
});
|
|
645
|
+
// Map-linked table (R16-17): click-to-zoom emits contour:zoom for engine-agnostic row highlighting.
|
|
646
|
+
this.addEventListener("click", (e) => {
|
|
647
|
+
if (e.target && e.target.closest && e.target.closest("input, button, select, textarea, a[href], label")) return;
|
|
648
|
+
const tr = e.target && e.target.closest ? e.target.closest("tbody tr") : null;
|
|
649
|
+
if (!tr || !this.contains(tr)) return;
|
|
650
|
+
const rawIdx = tr.dataset.rowIndex !== undefined ? tr.dataset.rowIndex : tr.dataset.row;
|
|
651
|
+
const idx = Number(rawIdx);
|
|
652
|
+
if (!Number.isFinite(idx)) return;
|
|
653
|
+
const rowData = (this._rowData && this._rowData[idx]) || null;
|
|
654
|
+
this.dispatchEvent(new CustomEvent("contour:zoom", { bubbles: true, composed: true, detail: { row: idx, index: idx, data: rowData } }));
|
|
655
|
+
});
|
|
656
|
+
this.addEventListener("keydown", (e) => {
|
|
657
|
+
const tr = e.target && e.target.closest ? e.target.closest("tbody tr") : null;
|
|
658
|
+
if (!tr || !this.contains(tr)) return;
|
|
659
|
+
const idx = Number(tr.dataset.rowIndex);
|
|
660
|
+
if (!Number.isFinite(idx)) return;
|
|
661
|
+
// Roving tabindex: arrows move the single tab stop, Home/End jump.
|
|
662
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp" || e.key === "Home" || e.key === "End") {
|
|
663
|
+
const rows = Array.from(this.querySelectorAll("tbody tr[data-row-index]"));
|
|
664
|
+
const at = rows.indexOf(tr);
|
|
665
|
+
let next = at;
|
|
666
|
+
if (e.key === "ArrowDown") next = Math.min(rows.length - 1, at + 1);
|
|
667
|
+
else if (e.key === "ArrowUp") next = Math.max(0, at - 1);
|
|
668
|
+
else if (e.key === "Home") next = 0;
|
|
669
|
+
else if (e.key === "End") next = rows.length - 1;
|
|
670
|
+
if (next !== at && rows[next]) {
|
|
671
|
+
e.preventDefault();
|
|
672
|
+
rows.forEach((r) => r.setAttribute("tabindex", "-1"));
|
|
673
|
+
rows[next].setAttribute("tabindex", "0");
|
|
674
|
+
try { rows[next].focus(); } catch (err) {}
|
|
675
|
+
}
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
if (e.key !== "Enter" && e.key !== " ") return;
|
|
679
|
+
e.preventDefault();
|
|
680
|
+
this.dispatchEvent(new CustomEvent("contour:activate", { bubbles: true, composed: true, detail: { row: idx } }));
|
|
681
|
+
});
|
|
682
|
+
// Cross-filter contract (R16-16): clear-all resets table sort/page/filter/selection state.
|
|
683
|
+
this.addEventListener("contour:clear-all", () => {
|
|
684
|
+
let changed = false;
|
|
685
|
+
if (this.hasAttribute("sort")) { this.removeAttribute("sort"); changed = true; }
|
|
686
|
+
if (this.hasAttribute("sort-dir")) { this.removeAttribute("sort-dir"); changed = true; }
|
|
687
|
+
if (this._sortCol !== null && this._sortCol !== undefined) { this._sortCol = null; this._sortDir = 1; changed = true; }
|
|
688
|
+
if (this.hasAttribute("page")) { this.removeAttribute("page"); changed = true; }
|
|
689
|
+
if (this._filters && Object.keys(this._filters).length) {
|
|
690
|
+
this._filters = {};
|
|
691
|
+
this.querySelectorAll(".c-table__filter-input").forEach((inp) => { inp.value = ""; });
|
|
692
|
+
changed = true;
|
|
693
|
+
}
|
|
694
|
+
if (this._selectedRows && this._selectedRows.size) { this._selectedRows.clear(); changed = true; }
|
|
695
|
+
if (!this._selectionControlled && this._selectedKeys && this._selectedKeys.size) { this._selectedKeys.clear(); changed = true; }
|
|
696
|
+
if (changed) {
|
|
697
|
+
this.dispatchEvent(new CustomEvent("contour:filter-change", { bubbles: true, composed: true, detail: { source: "table", filter: { sort: null, sortDir: null, page: null, filters: null } } }));
|
|
698
|
+
this.requestRender();
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
define("contour-table", ContourTable);
|
|
704
|
+
class ContourDataTable extends ContourTable {
|
|
705
|
+
render() {
|
|
706
|
+
super.render();
|
|
707
|
+
this.classList.add("c-data-table");
|
|
708
|
+
if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", labelText(this, "data-table", "Data table"));
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
define("contour-data-table", ContourDataTable);
|
|
712
|
+
// R31-130: column picker over the table `columns` contract. A list of
|
|
713
|
+
// checkboxes (one per column); toggling one emits contour:column-toggle
|
|
714
|
+
// {column,visible} so the table can show/hide that column. No table fork.
|
|
715
|
+
class ContourColumnPicker extends ContourBase {
|
|
716
|
+
static get observed() { return ["columns", "label"]; }
|
|
717
|
+
static get properties() { return { columns: { attribute: "columns" }, label: { attribute: "label" } }; }
|
|
718
|
+
render() {
|
|
719
|
+
this.classList.add("c-column-picker");
|
|
720
|
+
const btn = this.ensure(".c-column-picker__trigger", () => { const b = document.createElement("button"); b.type = "button"; b.className = "c-btn c-btn--sm c-column-picker__trigger"; return b; });
|
|
721
|
+
setHTML(btn, '<svg aria-hidden="true" viewBox="0 0 24 24"><use href="#i-sliders" /></svg>');
|
|
722
|
+
btn.appendChild(document.createTextNode(" " + (this.getAttribute("label") || labelText(this, "columns", "Columns"))));
|
|
723
|
+
const panel = this.ensure(".c-column-picker__panel", () => { const p = document.createElement("div"); p.className = "c-column-picker__panel"; return p; });
|
|
724
|
+
let cols = [];
|
|
725
|
+
try { const p = JSON.parse(this.getAttribute("columns") || "[]"); if (Array.isArray(p)) cols = p; } catch (e) { /* empty */ }
|
|
726
|
+
panel.innerHTML = "";
|
|
727
|
+
cols.forEach((c) => {
|
|
728
|
+
const name = typeof c === "string" ? c : (c.label || c.name || c);
|
|
729
|
+
const val = typeof c === "string" ? c : (c.value || c.name || c);
|
|
730
|
+
const item = document.createElement("label");
|
|
731
|
+
item.className = "c-column-picker__item";
|
|
732
|
+
const cb = document.createElement("input");
|
|
733
|
+
cb.type = "checkbox";
|
|
734
|
+
cb.checked = true;
|
|
735
|
+
cb.dataset.column = val;
|
|
736
|
+
item.appendChild(cb);
|
|
737
|
+
item.appendChild(document.createTextNode(" " + name));
|
|
738
|
+
panel.appendChild(item);
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
enhance() {
|
|
742
|
+
if (this._wiredColPicker) return;
|
|
743
|
+
this._wiredColPicker = true;
|
|
744
|
+
this.addEventListener("change", (e) => {
|
|
745
|
+
const cb = e.target && e.target.closest ? e.target.closest(".c-column-picker__item input") : null;
|
|
746
|
+
if (!cb || !this.contains(cb)) return;
|
|
747
|
+
this.dispatchEvent(new CustomEvent("contour:column-toggle", { bubbles: true, composed: true, detail: { column: cb.dataset.column, visible: cb.checked } }));
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
define("contour-column-picker", ContourColumnPicker);
|