@toolbox-web/grid 0.0.1
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/all.d.ts +3518 -0
- package/all.js +3762 -0
- package/all.js.map +1 -0
- package/index.d.ts +2367 -0
- package/index.js +3105 -0
- package/index.js.map +1 -0
- package/lib/plugins/clipboard/index.js +365 -0
- package/lib/plugins/clipboard/index.js.map +1 -0
- package/lib/plugins/column-virtualization/index.js +255 -0
- package/lib/plugins/column-virtualization/index.js.map +1 -0
- package/lib/plugins/context-menu/index.js +341 -0
- package/lib/plugins/context-menu/index.js.map +1 -0
- package/lib/plugins/export/index.js +305 -0
- package/lib/plugins/export/index.js.map +1 -0
- package/lib/plugins/filtering/index.js +759 -0
- package/lib/plugins/filtering/index.js.map +1 -0
- package/lib/plugins/grouping-columns/index.js +283 -0
- package/lib/plugins/grouping-columns/index.js.map +1 -0
- package/lib/plugins/grouping-rows/index.js +494 -0
- package/lib/plugins/grouping-rows/index.js.map +1 -0
- package/lib/plugins/master-detail/index.js +303 -0
- package/lib/plugins/master-detail/index.js.map +1 -0
- package/lib/plugins/multi-sort/index.js +270 -0
- package/lib/plugins/multi-sort/index.js.map +1 -0
- package/lib/plugins/pinned-columns/index.js +221 -0
- package/lib/plugins/pinned-columns/index.js.map +1 -0
- package/lib/plugins/pinned-rows/index.js +459 -0
- package/lib/plugins/pinned-rows/index.js.map +1 -0
- package/lib/plugins/pivot/index.js +326 -0
- package/lib/plugins/pivot/index.js.map +1 -0
- package/lib/plugins/reorder/index.js +260 -0
- package/lib/plugins/reorder/index.js.map +1 -0
- package/lib/plugins/selection/index.js +426 -0
- package/lib/plugins/selection/index.js.map +1 -0
- package/lib/plugins/server-side/index.js +241 -0
- package/lib/plugins/server-side/index.js.map +1 -0
- package/lib/plugins/tree/index.js +383 -0
- package/lib/plugins/tree/index.js.map +1 -0
- package/lib/plugins/undo-redo/index.js +289 -0
- package/lib/plugins/undo-redo/index.js.map +1 -0
- package/lib/plugins/visibility/index.js +430 -0
- package/lib/plugins/visibility/index.js.map +1 -0
- package/package.json +53 -0
- package/themes/dg-theme-contrast.css +43 -0
- package/themes/dg-theme-large.css +54 -0
- package/themes/dg-theme-standard.css +19 -0
- package/themes/dg-theme-vibrant.css +16 -0
- package/umd/grid.all.umd.js +660 -0
- package/umd/grid.all.umd.js.map +1 -0
- package/umd/grid.umd.js +105 -0
- package/umd/grid.umd.js.map +1 -0
- package/umd/plugins/clipboard.umd.js +9 -0
- package/umd/plugins/clipboard.umd.js.map +1 -0
- package/umd/plugins/column-virtualization.umd.js +2 -0
- package/umd/plugins/column-virtualization.umd.js.map +1 -0
- package/umd/plugins/context-menu.umd.js +53 -0
- package/umd/plugins/context-menu.umd.js.map +1 -0
- package/umd/plugins/export.umd.js +14 -0
- package/umd/plugins/export.umd.js.map +1 -0
- package/umd/plugins/filtering.umd.js +175 -0
- package/umd/plugins/filtering.umd.js.map +1 -0
- package/umd/plugins/grouping-columns.umd.js +29 -0
- package/umd/plugins/grouping-columns.umd.js.map +1 -0
- package/umd/plugins/grouping-rows.umd.js +40 -0
- package/umd/plugins/grouping-rows.umd.js.map +1 -0
- package/umd/plugins/master-detail.umd.js +27 -0
- package/umd/plugins/master-detail.umd.js.map +1 -0
- package/umd/plugins/multi-sort.umd.js +26 -0
- package/umd/plugins/multi-sort.umd.js.map +1 -0
- package/umd/plugins/pinned-columns.umd.js +2 -0
- package/umd/plugins/pinned-columns.umd.js.map +1 -0
- package/umd/plugins/pinned-rows.umd.js +73 -0
- package/umd/plugins/pinned-rows.umd.js.map +1 -0
- package/umd/plugins/pivot.umd.js +8 -0
- package/umd/plugins/pivot.umd.js.map +1 -0
- package/umd/plugins/reorder.umd.js +31 -0
- package/umd/plugins/reorder.umd.js.map +1 -0
- package/umd/plugins/selection.umd.js +34 -0
- package/umd/plugins/selection.umd.js.map +1 -0
- package/umd/plugins/server-side.umd.js +2 -0
- package/umd/plugins/server-side.umd.js.map +1 -0
- package/umd/plugins/tree.umd.js +11 -0
- package/umd/plugins/tree.umd.js.map +1 -0
- package/umd/plugins/undo-redo.umd.js +2 -0
- package/umd/plugins/undo-redo.umd.js.map +1 -0
- package/umd/plugins/visibility.umd.js +94 -0
- package/umd/plugins/visibility.umd.js.map +1 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
class m {
|
|
2
|
+
/** Plugin version - override in subclass if needed */
|
|
3
|
+
version = "1.0.0";
|
|
4
|
+
/** CSS styles to inject into the grid's shadow DOM */
|
|
5
|
+
styles;
|
|
6
|
+
/** Custom cell renderers keyed by type name */
|
|
7
|
+
cellRenderers;
|
|
8
|
+
/** Custom header renderers keyed by type name */
|
|
9
|
+
headerRenderers;
|
|
10
|
+
/** Custom cell editors keyed by type name */
|
|
11
|
+
cellEditors;
|
|
12
|
+
/** The grid instance this plugin is attached to */
|
|
13
|
+
grid;
|
|
14
|
+
/** Plugin configuration - merged with defaults in attach() */
|
|
15
|
+
config;
|
|
16
|
+
/** User-provided configuration from constructor */
|
|
17
|
+
userConfig;
|
|
18
|
+
/**
|
|
19
|
+
* Default configuration - subclasses should override this getter.
|
|
20
|
+
* Note: This must be a getter (not property initializer) for proper inheritance
|
|
21
|
+
* since property initializers run after parent constructor.
|
|
22
|
+
*/
|
|
23
|
+
get defaultConfig() {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
constructor(e = {}) {
|
|
27
|
+
this.userConfig = e;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Called when the plugin is attached to a grid.
|
|
31
|
+
* Override to set up event listeners, initialize state, etc.
|
|
32
|
+
*/
|
|
33
|
+
attach(e) {
|
|
34
|
+
this.grid = e, this.config = { ...this.defaultConfig, ...this.userConfig };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Called when the plugin is detached from a grid.
|
|
38
|
+
* Override to clean up event listeners, timers, etc.
|
|
39
|
+
*/
|
|
40
|
+
detach() {
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get another plugin instance from the same grid.
|
|
44
|
+
* Use for inter-plugin communication.
|
|
45
|
+
*/
|
|
46
|
+
getPlugin(e) {
|
|
47
|
+
return this.grid?.getPlugin(e);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Emit a custom event from the grid.
|
|
51
|
+
*/
|
|
52
|
+
emit(e, o) {
|
|
53
|
+
this.grid?.dispatchEvent?.(new CustomEvent(e, { detail: o, bubbles: !0 }));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Request a re-render of the grid.
|
|
57
|
+
*/
|
|
58
|
+
requestRender() {
|
|
59
|
+
this.grid?.requestRender?.();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Request a lightweight style update without rebuilding DOM.
|
|
63
|
+
* Use this instead of requestRender() when only CSS classes need updating.
|
|
64
|
+
*/
|
|
65
|
+
requestAfterRender() {
|
|
66
|
+
this.grid?.requestAfterRender?.();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get the current rows from the grid.
|
|
70
|
+
*/
|
|
71
|
+
get rows() {
|
|
72
|
+
return this.grid?.rows ?? [];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get the original unfiltered/unprocessed rows from the grid.
|
|
76
|
+
* Use this when you need all source data regardless of active filters.
|
|
77
|
+
*/
|
|
78
|
+
get sourceRows() {
|
|
79
|
+
return this.grid?.sourceRows ?? [];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get the current columns from the grid.
|
|
83
|
+
*/
|
|
84
|
+
get columns() {
|
|
85
|
+
return this.grid?.columns ?? [];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get only visible columns from the grid (excludes hidden).
|
|
89
|
+
* Use this for rendering that needs to match the grid template.
|
|
90
|
+
*/
|
|
91
|
+
get visibleColumns() {
|
|
92
|
+
return this.grid?.visibleColumns ?? [];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the shadow root of the grid.
|
|
96
|
+
*/
|
|
97
|
+
get shadowRoot() {
|
|
98
|
+
return this.grid?.shadowRoot ?? null;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Log a warning message.
|
|
102
|
+
*/
|
|
103
|
+
warn(e) {
|
|
104
|
+
console.warn(`[tbw-grid:${this.name}] ${e}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function b(s) {
|
|
108
|
+
const e = [];
|
|
109
|
+
return !s.rowGroupFields?.length && !s.columnGroupFields?.length && e.push("At least one row or column group field is required"), s.valueFields?.length || e.push("At least one value field is required"), e;
|
|
110
|
+
}
|
|
111
|
+
function y(s) {
|
|
112
|
+
switch (s) {
|
|
113
|
+
case "sum":
|
|
114
|
+
return (e) => e.reduce((o, t) => o + t, 0);
|
|
115
|
+
case "avg":
|
|
116
|
+
return (e) => e.length ? e.reduce((o, t) => o + t, 0) / e.length : 0;
|
|
117
|
+
case "count":
|
|
118
|
+
return (e) => e.length;
|
|
119
|
+
case "min":
|
|
120
|
+
return (e) => e.length ? Math.min(...e) : 0;
|
|
121
|
+
case "max":
|
|
122
|
+
return (e) => e.length ? Math.max(...e) : 0;
|
|
123
|
+
case "first":
|
|
124
|
+
return (e) => e[0] ?? 0;
|
|
125
|
+
case "last":
|
|
126
|
+
return (e) => e[e.length - 1] ?? 0;
|
|
127
|
+
default:
|
|
128
|
+
return (e) => e.reduce((o, t) => o + t, 0);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function f(s, e) {
|
|
132
|
+
return [...s, e].join("|");
|
|
133
|
+
}
|
|
134
|
+
function F(s, e) {
|
|
135
|
+
const o = e.rowGroupFields ?? [], t = e.columnGroupFields ?? [], r = e.valueFields ?? [], i = G(s, t), n = C(s, o), u = P(n, t, i, r, 0), l = _(u, i, r), c = Object.values(l).reduce((d, a) => d + a, 0);
|
|
136
|
+
return {
|
|
137
|
+
rows: u,
|
|
138
|
+
columnKeys: i,
|
|
139
|
+
totals: l,
|
|
140
|
+
grandTotal: c
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function G(s, e) {
|
|
144
|
+
if (e.length === 0) return ["value"];
|
|
145
|
+
const o = /* @__PURE__ */ new Set();
|
|
146
|
+
for (const t of s) {
|
|
147
|
+
const r = e.map((i) => String(t[i] ?? "")).join("|");
|
|
148
|
+
o.add(r);
|
|
149
|
+
}
|
|
150
|
+
return [...o].sort();
|
|
151
|
+
}
|
|
152
|
+
function C(s, e) {
|
|
153
|
+
const o = /* @__PURE__ */ new Map();
|
|
154
|
+
for (const t of s) {
|
|
155
|
+
const r = e.map((n) => String(t[n] ?? "")).join("|");
|
|
156
|
+
o.has(r) || o.set(r, []);
|
|
157
|
+
const i = o.get(r);
|
|
158
|
+
i && i.push(t);
|
|
159
|
+
}
|
|
160
|
+
return o;
|
|
161
|
+
}
|
|
162
|
+
function P(s, e, o, t, r) {
|
|
163
|
+
const i = [];
|
|
164
|
+
for (const [n, u] of s) {
|
|
165
|
+
const l = {};
|
|
166
|
+
let c = 0;
|
|
167
|
+
for (const d of o)
|
|
168
|
+
for (const a of t) {
|
|
169
|
+
const p = (e.length > 0 ? u.filter((g) => e.map((R) => String(g[R] ?? "")).join("|") === d) : u).map((g) => Number(g[a.field]) || 0), v = y(a.aggFunc), h = p.length > 0 ? v(p) : null, w = f([d], a.field);
|
|
170
|
+
l[w] = h, h !== null && (c += h);
|
|
171
|
+
}
|
|
172
|
+
i.push({
|
|
173
|
+
rowKey: n,
|
|
174
|
+
rowLabel: n || "(blank)",
|
|
175
|
+
depth: r,
|
|
176
|
+
values: l,
|
|
177
|
+
total: c,
|
|
178
|
+
isGroup: !1
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return i;
|
|
182
|
+
}
|
|
183
|
+
function _(s, e, o) {
|
|
184
|
+
const t = {};
|
|
185
|
+
for (const r of e)
|
|
186
|
+
for (const i of o) {
|
|
187
|
+
const n = f([r], i.field);
|
|
188
|
+
t[n] = s.reduce((u, l) => u + (l.values[n] ?? 0), 0);
|
|
189
|
+
}
|
|
190
|
+
return t;
|
|
191
|
+
}
|
|
192
|
+
function q(s) {
|
|
193
|
+
const e = [];
|
|
194
|
+
function o(t) {
|
|
195
|
+
if (e.push(t), t.children)
|
|
196
|
+
for (const r of t.children)
|
|
197
|
+
o(r);
|
|
198
|
+
}
|
|
199
|
+
for (const t of s)
|
|
200
|
+
o(t);
|
|
201
|
+
return e;
|
|
202
|
+
}
|
|
203
|
+
class K extends m {
|
|
204
|
+
name = "pivot";
|
|
205
|
+
version = "1.0.0";
|
|
206
|
+
get defaultConfig() {
|
|
207
|
+
return {
|
|
208
|
+
enabled: !0,
|
|
209
|
+
showTotals: !0,
|
|
210
|
+
showGrandTotal: !0
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
// ===== Internal State =====
|
|
214
|
+
isActive = !1;
|
|
215
|
+
pivotResult = null;
|
|
216
|
+
columnHeaders = [];
|
|
217
|
+
rowHeaders = [];
|
|
218
|
+
// ===== Lifecycle =====
|
|
219
|
+
detach() {
|
|
220
|
+
this.isActive = !1, this.pivotResult = null, this.columnHeaders = [], this.rowHeaders = [];
|
|
221
|
+
}
|
|
222
|
+
// ===== Hooks =====
|
|
223
|
+
processRows(e) {
|
|
224
|
+
if (!this.config.enabled || !this.isActive)
|
|
225
|
+
return [...e];
|
|
226
|
+
const o = b(this.config);
|
|
227
|
+
return o.length > 0 ? (this.warn(`Config errors: ${o.join(", ")}`), [...e]) : (this.pivotResult = F(e, this.config), q(this.pivotResult.rows).map((t) => ({
|
|
228
|
+
__pivotRowKey: t.rowKey,
|
|
229
|
+
__pivotLabel: t.rowLabel,
|
|
230
|
+
__pivotDepth: t.depth,
|
|
231
|
+
__pivotIsGroup: t.isGroup,
|
|
232
|
+
__pivotTotal: t.total,
|
|
233
|
+
...t.values
|
|
234
|
+
})));
|
|
235
|
+
}
|
|
236
|
+
processColumns(e) {
|
|
237
|
+
if (!this.config.enabled || !this.isActive || !this.pivotResult)
|
|
238
|
+
return [...e];
|
|
239
|
+
const o = [];
|
|
240
|
+
o.push({
|
|
241
|
+
field: "__pivotLabel",
|
|
242
|
+
header: this.config.rowGroupFields?.join(" / ") ?? "Group",
|
|
243
|
+
width: 200
|
|
244
|
+
});
|
|
245
|
+
for (const t of this.pivotResult.columnKeys)
|
|
246
|
+
for (const r of this.config.valueFields ?? []) {
|
|
247
|
+
const i = f([t], r.field);
|
|
248
|
+
o.push({
|
|
249
|
+
field: i,
|
|
250
|
+
header: `${t} - ${r.header || r.field} (${r.aggFunc})`,
|
|
251
|
+
width: 120,
|
|
252
|
+
type: "number"
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
return this.config.showTotals && o.push({
|
|
256
|
+
field: "__pivotTotal",
|
|
257
|
+
header: "Total",
|
|
258
|
+
width: 100,
|
|
259
|
+
type: "number"
|
|
260
|
+
}), o;
|
|
261
|
+
}
|
|
262
|
+
// ===== Public API =====
|
|
263
|
+
/**
|
|
264
|
+
* Enable pivot mode.
|
|
265
|
+
*/
|
|
266
|
+
enablePivot() {
|
|
267
|
+
this.isActive = !0, this.requestRender();
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Disable pivot mode and return to normal grid view.
|
|
271
|
+
*/
|
|
272
|
+
disablePivot() {
|
|
273
|
+
this.isActive = !1, this.pivotResult = null, this.requestRender();
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Check if pivot mode is currently active.
|
|
277
|
+
*/
|
|
278
|
+
isPivotActive() {
|
|
279
|
+
return this.isActive;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Get the current pivot result.
|
|
283
|
+
*/
|
|
284
|
+
getPivotResult() {
|
|
285
|
+
return this.pivotResult;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Set the row group fields for pivoting.
|
|
289
|
+
* @param fields - Array of field names to group rows by
|
|
290
|
+
*/
|
|
291
|
+
setRowGroupFields(e) {
|
|
292
|
+
this.config.rowGroupFields = e, this.requestRender();
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Set the column group fields for pivoting.
|
|
296
|
+
* @param fields - Array of field names to create columns from
|
|
297
|
+
*/
|
|
298
|
+
setColumnGroupFields(e) {
|
|
299
|
+
this.config.columnGroupFields = e, this.requestRender();
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Set the value fields with aggregation functions.
|
|
303
|
+
* @param fields - Array of value field configurations
|
|
304
|
+
*/
|
|
305
|
+
setValueFields(e) {
|
|
306
|
+
this.config.valueFields = e, this.requestRender();
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Refresh the pivot by clearing cached results.
|
|
310
|
+
*/
|
|
311
|
+
refresh() {
|
|
312
|
+
this.pivotResult = null, this.requestRender();
|
|
313
|
+
}
|
|
314
|
+
// ===== Styles =====
|
|
315
|
+
styles = `
|
|
316
|
+
[data-pivot-depth="1"] { padding-left: 20px; }
|
|
317
|
+
[data-pivot-depth="2"] { padding-left: 40px; }
|
|
318
|
+
[data-pivot-depth="3"] { padding-left: 60px; }
|
|
319
|
+
.pivot-group-row { font-weight: bold; background: var(--tbw-pivot-group-bg, var(--tbw-color-panel-bg)); }
|
|
320
|
+
.pivot-total-row { font-weight: bold; border-top: 2px solid var(--tbw-pivot-border, var(--tbw-color-border-strong)); }
|
|
321
|
+
`;
|
|
322
|
+
}
|
|
323
|
+
export {
|
|
324
|
+
K as PivotPlugin
|
|
325
|
+
};
|
|
326
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../../../libs/grid/src/lib/core/plugin/base-plugin.ts","../../../../../../libs/grid/src/lib/plugins/pivot/pivot-model.ts","../../../../../../libs/grid/src/lib/plugins/pivot/pivot-engine.ts","../../../../../../libs/grid/src/lib/plugins/pivot/PivotPlugin.ts"],"sourcesContent":["/**\r\n * Base Grid Plugin Class\r\n *\r\n * All plugins extend this abstract class.\r\n * Plugins are instantiated per-grid and manage their own state.\r\n */\r\n\r\nimport type { ColumnConfig, ColumnState, HeaderContentDefinition, ToolPanelDefinition } from '../types';\r\n\r\n// Forward declare to avoid circular imports\r\nexport interface GridElement {\r\n shadowRoot: ShadowRoot | null;\r\n rows: any[];\r\n columns: ColumnConfig[];\r\n gridConfig: any;\r\n requestRender(): void;\r\n requestAfterRender(): void;\r\n forceLayout(): Promise<void>;\r\n getPlugin<T extends BaseGridPlugin>(PluginClass: new (...args: any[]) => T): T | undefined;\r\n getPluginByName(name: string): BaseGridPlugin | undefined;\r\n dispatchEvent(event: Event): boolean;\r\n}\r\n\r\n/**\r\n * Keyboard modifier flags\r\n */\r\nexport interface KeyboardModifiers {\r\n ctrl?: boolean;\r\n shift?: boolean;\r\n alt?: boolean;\r\n meta?: boolean;\r\n}\r\n\r\n/**\r\n * Cell coordinates\r\n */\r\nexport interface CellCoords {\r\n row: number;\r\n col: number;\r\n}\r\n\r\n/**\r\n * Cell click event\r\n */\r\nexport interface CellClickEvent {\r\n rowIndex: number;\r\n colIndex: number;\r\n field: string;\r\n value: any;\r\n row: any;\r\n cellEl: HTMLElement;\r\n originalEvent: MouseEvent;\r\n}\r\n\r\n/**\r\n * Row click event\r\n */\r\nexport interface RowClickEvent {\r\n rowIndex: number;\r\n row: any;\r\n rowEl: HTMLElement;\r\n originalEvent: MouseEvent;\r\n}\r\n\r\n/**\r\n * Header click event\r\n */\r\nexport interface HeaderClickEvent {\r\n colIndex: number;\r\n field: string;\r\n column: ColumnConfig;\r\n headerEl: HTMLElement;\r\n originalEvent: MouseEvent;\r\n}\r\n\r\n/**\r\n * Scroll event\r\n */\r\nexport interface ScrollEvent {\r\n scrollTop: number;\r\n scrollLeft: number;\r\n scrollHeight: number;\r\n scrollWidth: number;\r\n clientHeight: number;\r\n clientWidth: number;\r\n originalEvent?: Event;\r\n}\r\n\r\n/**\r\n * Cell mouse event (for drag operations, selection, etc.)\r\n */\r\nexport interface CellMouseEvent {\r\n /** Event type: mousedown, mousemove, or mouseup */\r\n type: 'mousedown' | 'mousemove' | 'mouseup';\r\n /** Row index, undefined if not over a data cell */\r\n rowIndex?: number;\r\n /** Column index, undefined if not over a cell */\r\n colIndex?: number;\r\n /** Field name, undefined if not over a cell */\r\n field?: string;\r\n /** Cell value, undefined if not over a data cell */\r\n value?: unknown;\r\n /** Row data object, undefined if not over a data row */\r\n row?: unknown;\r\n /** Column configuration, undefined if not over a column */\r\n column?: ColumnConfig;\r\n /** The cell element, undefined if not over a cell */\r\n cellElement?: HTMLElement;\r\n /** The row element, undefined if not over a row */\r\n rowElement?: HTMLElement;\r\n /** Whether the event is over a header cell */\r\n isHeader: boolean;\r\n /** Cell coordinates if over a valid data cell */\r\n cell?: CellCoords;\r\n /** The original mouse event */\r\n originalEvent: MouseEvent;\r\n}\r\n\r\n/**\r\n * Context menu parameters\r\n */\r\nexport interface ContextMenuParams {\r\n x: number;\r\n y: number;\r\n rowIndex?: number;\r\n colIndex?: number;\r\n field?: string;\r\n value?: any;\r\n row?: any;\r\n column?: ColumnConfig;\r\n isHeader?: boolean;\r\n}\r\n\r\n/**\r\n * Context menu item\r\n */\r\nexport interface ContextMenuItem {\r\n id: string;\r\n label: string;\r\n icon?: string;\r\n disabled?: boolean;\r\n separator?: boolean;\r\n children?: ContextMenuItem[];\r\n action?: (params: ContextMenuParams) => void;\r\n}\r\n\r\n/**\r\n * Cell render context for plugin cell renderers.\r\n * Provides full context including position and editing state.\r\n *\r\n * Note: This differs from the core `CellRenderContext` in types.ts which is\r\n * simpler and used for column view renderers. This version provides additional\r\n * context needed by plugins that register custom cell renderers.\r\n */\r\nexport interface PluginCellRenderContext {\r\n /** The cell value */\r\n value: any;\r\n /** The field/column key */\r\n field: string;\r\n /** The row data object */\r\n row: any;\r\n /** Row index in the data array */\r\n rowIndex: number;\r\n /** Column index */\r\n colIndex: number;\r\n /** Column configuration */\r\n column: ColumnConfig;\r\n /** Whether the cell is currently in edit mode */\r\n isEditing: boolean;\r\n}\r\n\r\n/**\r\n * Header render context for plugin header renderers.\r\n */\r\nexport interface PluginHeaderRenderContext {\r\n /** Column configuration */\r\n column: ColumnConfig;\r\n /** Column index */\r\n colIndex: number;\r\n}\r\n\r\n/**\r\n * Cell renderer function type for plugins.\r\n */\r\nexport type CellRenderer = (ctx: PluginCellRenderContext) => string | HTMLElement;\r\n\r\n/**\r\n * Header renderer function type for plugins.\r\n */\r\nexport type HeaderRenderer = (ctx: PluginHeaderRenderContext) => string | HTMLElement;\r\n\r\n/**\r\n * Cell editor interface for plugins.\r\n */\r\nexport interface CellEditor {\r\n create(ctx: PluginCellRenderContext, commitFn: (value: any) => void, cancelFn: () => void): HTMLElement;\r\n getValue?(element: HTMLElement): any;\r\n focus?(element: HTMLElement): void;\r\n}\r\n\r\n/**\r\n * Abstract base class for all grid plugins.\r\n *\r\n * @template TConfig - Configuration type for the plugin\r\n */\r\nexport abstract class BaseGridPlugin<TConfig = unknown> {\r\n /** Unique plugin identifier (derived from class name by default) */\r\n abstract readonly name: string;\r\n\r\n /** Plugin version - override in subclass if needed */\r\n readonly version: string = '1.0.0';\r\n\r\n /** CSS styles to inject into the grid's shadow DOM */\r\n readonly styles?: string;\r\n\r\n /** Custom cell renderers keyed by type name */\r\n readonly cellRenderers?: Record<string, CellRenderer>;\r\n\r\n /** Custom header renderers keyed by type name */\r\n readonly headerRenderers?: Record<string, HeaderRenderer>;\r\n\r\n /** Custom cell editors keyed by type name */\r\n readonly cellEditors?: Record<string, CellEditor>;\r\n\r\n /** The grid instance this plugin is attached to */\r\n protected grid!: GridElement;\r\n\r\n /** Plugin configuration - merged with defaults in attach() */\r\n protected config!: TConfig;\r\n\r\n /** User-provided configuration from constructor */\r\n private readonly userConfig: Partial<TConfig>;\r\n\r\n /**\r\n * Default configuration - subclasses should override this getter.\r\n * Note: This must be a getter (not property initializer) for proper inheritance\r\n * since property initializers run after parent constructor.\r\n */\r\n protected get defaultConfig(): Partial<TConfig> {\r\n return {};\r\n }\r\n\r\n constructor(config: Partial<TConfig> = {}) {\r\n this.userConfig = config;\r\n }\r\n\r\n /**\r\n * Called when the plugin is attached to a grid.\r\n * Override to set up event listeners, initialize state, etc.\r\n */\r\n attach(grid: GridElement): void {\r\n this.grid = grid;\r\n // Merge config here (after subclass construction is complete)\r\n this.config = { ...this.defaultConfig, ...this.userConfig } as TConfig;\r\n }\r\n\r\n /**\r\n * Called when the plugin is detached from a grid.\r\n * Override to clean up event listeners, timers, etc.\r\n */\r\n detach(): void {\r\n // Override in subclass\r\n }\r\n\r\n /**\r\n * Get another plugin instance from the same grid.\r\n * Use for inter-plugin communication.\r\n */\r\n protected getPlugin<T extends BaseGridPlugin>(PluginClass: new (...args: any[]) => T): T | undefined {\r\n return this.grid?.getPlugin(PluginClass);\r\n }\r\n\r\n /**\r\n * Emit a custom event from the grid.\r\n */\r\n protected emit<T>(eventName: string, detail: T): void {\r\n this.grid?.dispatchEvent?.(new CustomEvent(eventName, { detail, bubbles: true }));\r\n }\r\n\r\n /**\r\n * Request a re-render of the grid.\r\n */\r\n protected requestRender(): void {\r\n this.grid?.requestRender?.();\r\n }\r\n\r\n /**\r\n * Request a lightweight style update without rebuilding DOM.\r\n * Use this instead of requestRender() when only CSS classes need updating.\r\n */\r\n protected requestAfterRender(): void {\r\n this.grid?.requestAfterRender?.();\r\n }\r\n\r\n /**\r\n * Get the current rows from the grid.\r\n */\r\n protected get rows(): any[] {\r\n return this.grid?.rows ?? [];\r\n }\r\n\r\n /**\r\n * Get the original unfiltered/unprocessed rows from the grid.\r\n * Use this when you need all source data regardless of active filters.\r\n */\r\n protected get sourceRows(): any[] {\r\n return (this.grid as any)?.sourceRows ?? [];\r\n }\r\n\r\n /**\r\n * Get the current columns from the grid.\r\n */\r\n protected get columns(): ColumnConfig[] {\r\n return this.grid?.columns ?? [];\r\n }\r\n\r\n /**\r\n * Get only visible columns from the grid (excludes hidden).\r\n * Use this for rendering that needs to match the grid template.\r\n */\r\n protected get visibleColumns(): ColumnConfig[] {\r\n return (this.grid as any)?.visibleColumns ?? [];\r\n }\r\n\r\n /**\r\n * Get the shadow root of the grid.\r\n */\r\n protected get shadowRoot(): ShadowRoot | null {\r\n return this.grid?.shadowRoot ?? null;\r\n }\r\n\r\n /**\r\n * Log a warning message.\r\n */\r\n protected warn(message: string): void {\r\n console.warn(`[tbw-grid:${this.name}] ${message}`);\r\n }\r\n\r\n // ===== Lifecycle Hooks (override as needed) =====\r\n\r\n /**\r\n * Transform rows before rendering.\r\n * Called during each render cycle before rows are rendered to the DOM.\r\n * Use this to filter, sort, or add computed properties to rows.\r\n *\r\n * @param rows - The current rows array (readonly to encourage returning a new array)\r\n * @returns The modified rows array to render\r\n *\r\n * @example\r\n * ```ts\r\n * processRows(rows: readonly any[]): any[] {\r\n * // Filter out hidden rows\r\n * return rows.filter(row => !row._hidden);\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```ts\r\n * processRows(rows: readonly any[]): any[] {\r\n * // Add computed properties\r\n * return rows.map(row => ({\r\n * ...row,\r\n * _fullName: `${row.firstName} ${row.lastName}`\r\n * }));\r\n * }\r\n * ```\r\n */\r\n processRows?(rows: readonly any[]): any[];\r\n\r\n /**\r\n * Transform columns before rendering.\r\n * Called during each render cycle before column headers and cells are rendered.\r\n * Use this to add, remove, or modify column definitions.\r\n *\r\n * @param columns - The current columns array (readonly to encourage returning a new array)\r\n * @returns The modified columns array to render\r\n *\r\n * @example\r\n * ```ts\r\n * processColumns(columns: readonly ColumnConfig[]): ColumnConfig[] {\r\n * // Add a selection checkbox column\r\n * return [\r\n * { field: '_select', header: '', width: 40 },\r\n * ...columns\r\n * ];\r\n * }\r\n * ```\r\n */\r\n processColumns?(columns: readonly ColumnConfig[]): ColumnConfig[];\r\n\r\n /**\r\n * Called before each render cycle begins.\r\n * Use this to prepare state or cache values needed during rendering.\r\n *\r\n * @example\r\n * ```ts\r\n * beforeRender(): void {\r\n * this.visibleRowCount = this.calculateVisibleRows();\r\n * }\r\n * ```\r\n */\r\n beforeRender?(): void;\r\n\r\n /**\r\n * Called after each render cycle completes.\r\n * Use this for DOM manipulation, adding event listeners to rendered elements,\r\n * or applying visual effects like selection highlights.\r\n *\r\n * @example\r\n * ```ts\r\n * afterRender(): void {\r\n * // Apply selection styling to rendered rows\r\n * const rows = this.shadowRoot?.querySelectorAll('.data-row');\r\n * rows?.forEach((row, i) => {\r\n * row.classList.toggle('selected', this.selectedRows.has(i));\r\n * });\r\n * }\r\n * ```\r\n */\r\n afterRender?(): void;\r\n\r\n /**\r\n * Render a custom row, bypassing the default row rendering.\r\n * Use this for special row types like group headers, detail rows, or footers.\r\n *\r\n * @param row - The row data object\r\n * @param rowEl - The row DOM element to render into\r\n * @param rowIndex - The index of the row in the data array\r\n * @returns `true` if the plugin handled rendering (prevents default), `false`/`void` for default rendering\r\n *\r\n * @example\r\n * ```ts\r\n * renderRow(row: any, rowEl: HTMLElement, rowIndex: number): boolean | void {\r\n * if (row._isGroupHeader) {\r\n * rowEl.innerHTML = `<div class=\"group-header\">${row._groupLabel}</div>`;\r\n * return true; // Handled - skip default rendering\r\n * }\r\n * // Return void to let default rendering proceed\r\n * }\r\n * ```\r\n */\r\n renderRow?(row: any, rowEl: HTMLElement, rowIndex: number): boolean | void;\r\n\r\n // ===== Interaction Hooks (override as needed) =====\r\n\r\n /**\r\n * Handle keyboard events on the grid.\r\n * Called when a key is pressed while the grid or a cell has focus.\r\n *\r\n * @param event - The native KeyboardEvent\r\n * @returns `true` to prevent default behavior and stop propagation, `false`/`void` to allow default\r\n *\r\n * @example\r\n * ```ts\r\n * onKeyDown(event: KeyboardEvent): boolean | void {\r\n * // Handle Ctrl+A for select all\r\n * if (event.ctrlKey && event.key === 'a') {\r\n * this.selectAllRows();\r\n * return true; // Prevent default browser select-all\r\n * }\r\n * }\r\n * ```\r\n */\r\n onKeyDown?(event: KeyboardEvent): boolean | void;\r\n\r\n /**\r\n * Handle cell click events.\r\n * Called when a data cell is clicked (not headers).\r\n *\r\n * @param event - Cell click event with row/column context\r\n * @returns `true` to prevent default behavior and stop propagation, `false`/`void` to allow default\r\n *\r\n * @example\r\n * ```ts\r\n * onCellClick(event: CellClickEvent): boolean | void {\r\n * if (event.field === '_select') {\r\n * this.toggleRowSelection(event.rowIndex);\r\n * return true; // Handled\r\n * }\r\n * }\r\n * ```\r\n */\r\n onCellClick?(event: CellClickEvent): boolean | void;\r\n\r\n /**\r\n * Handle row click events.\r\n * Called when any part of a data row is clicked.\r\n * Note: This is called in addition to onCellClick, not instead of.\r\n *\r\n * @param event - Row click event with row context\r\n * @returns `true` to prevent default behavior and stop propagation, `false`/`void` to allow default\r\n *\r\n * @example\r\n * ```ts\r\n * onRowClick(event: RowClickEvent): boolean | void {\r\n * if (this.config.mode === 'row') {\r\n * this.selectRow(event.rowIndex, event.originalEvent);\r\n * return true;\r\n * }\r\n * }\r\n * ```\r\n */\r\n onRowClick?(event: RowClickEvent): boolean | void;\r\n\r\n /**\r\n * Handle header click events.\r\n * Called when a column header is clicked. Commonly used for sorting.\r\n *\r\n * @param event - Header click event with column context\r\n * @returns `true` to prevent default behavior and stop propagation, `false`/`void` to allow default\r\n *\r\n * @example\r\n * ```ts\r\n * onHeaderClick(event: HeaderClickEvent): boolean | void {\r\n * if (event.column.sortable !== false) {\r\n * this.toggleSort(event.field);\r\n * return true;\r\n * }\r\n * }\r\n * ```\r\n */\r\n onHeaderClick?(event: HeaderClickEvent): boolean | void;\r\n\r\n /**\r\n * Handle scroll events on the grid viewport.\r\n * Called during scrolling. Note: This may be called frequently; debounce if needed.\r\n *\r\n * @param event - Scroll event with scroll position and viewport dimensions\r\n *\r\n * @example\r\n * ```ts\r\n * onScroll(event: ScrollEvent): void {\r\n * // Update sticky column positions\r\n * this.updateStickyPositions(event.scrollLeft);\r\n * }\r\n * ```\r\n */\r\n onScroll?(event: ScrollEvent): void;\r\n\r\n /**\r\n * Handle cell mousedown events.\r\n * Used for initiating drag operations like range selection or column resize.\r\n *\r\n * @param event - Mouse event with cell context\r\n * @returns `true` to indicate drag started (prevents text selection), `false`/`void` otherwise\r\n *\r\n * @example\r\n * ```ts\r\n * onCellMouseDown(event: CellMouseEvent): boolean | void {\r\n * if (event.rowIndex !== undefined && this.config.mode === 'range') {\r\n * this.startDragSelection(event.rowIndex, event.colIndex);\r\n * return true; // Prevent text selection\r\n * }\r\n * }\r\n * ```\r\n */\r\n onCellMouseDown?(event: CellMouseEvent): boolean | void;\r\n\r\n /**\r\n * Handle cell mousemove events during drag operations.\r\n * Only called when a drag is in progress (after mousedown returned true).\r\n *\r\n * @param event - Mouse event with current cell context\r\n * @returns `true` to continue handling the drag, `false`/`void` otherwise\r\n *\r\n * @example\r\n * ```ts\r\n * onCellMouseMove(event: CellMouseEvent): boolean | void {\r\n * if (this.isDragging && event.rowIndex !== undefined) {\r\n * this.extendSelection(event.rowIndex, event.colIndex);\r\n * return true;\r\n * }\r\n * }\r\n * ```\r\n */\r\n onCellMouseMove?(event: CellMouseEvent): boolean | void;\r\n\r\n /**\r\n * Handle cell mouseup events to end drag operations.\r\n *\r\n * @param event - Mouse event with final cell context\r\n * @returns `true` if drag was finalized, `false`/`void` otherwise\r\n *\r\n * @example\r\n * ```ts\r\n * onCellMouseUp(event: CellMouseEvent): boolean | void {\r\n * if (this.isDragging) {\r\n * this.finalizeDragSelection();\r\n * this.isDragging = false;\r\n * return true;\r\n * }\r\n * }\r\n * ```\r\n */\r\n onCellMouseUp?(event: CellMouseEvent): boolean | void;\r\n\r\n /**\r\n * Provide context menu items when right-clicking on the grid.\r\n * Multiple plugins can contribute items; they are merged into a single menu.\r\n *\r\n * @param params - Context about where the menu was triggered (row, column, etc.)\r\n * @returns Array of menu items to display\r\n *\r\n * @example\r\n * ```ts\r\n * getContextMenuItems(params: ContextMenuParams): ContextMenuItem[] {\r\n * if (params.isHeader) {\r\n * return [\r\n * { id: 'sort-asc', label: 'Sort Ascending', action: () => this.sortAsc(params.field) },\r\n * { id: 'sort-desc', label: 'Sort Descending', action: () => this.sortDesc(params.field) },\r\n * ];\r\n * }\r\n * return [\r\n * { id: 'copy', label: 'Copy Cell', action: () => this.copyCell(params) },\r\n * ];\r\n * }\r\n * ```\r\n */\r\n getContextMenuItems?(params: ContextMenuParams): ContextMenuItem[];\r\n\r\n // ===== Column State Hooks (override as needed) =====\r\n\r\n /**\r\n * Contribute plugin-specific state for a column.\r\n * Called by the grid when collecting column state for serialization.\r\n * Plugins can add their own properties to the column state.\r\n *\r\n * @param field - The field name of the column\r\n * @returns Partial column state with plugin-specific properties, or undefined if no state to contribute\r\n *\r\n * @example\r\n * ```ts\r\n * getColumnState(field: string): Partial<ColumnState> | undefined {\r\n * const filterModel = this.filterModels.get(field);\r\n * if (filterModel) {\r\n * // Uses module augmentation to add filter property to ColumnState\r\n * return { filter: filterModel } as Partial<ColumnState>;\r\n * }\r\n * return undefined;\r\n * }\r\n * ```\r\n */\r\n getColumnState?(field: string): Partial<ColumnState> | undefined;\r\n\r\n /**\r\n * Apply plugin-specific state to a column.\r\n * Called by the grid when restoring column state from serialized data.\r\n * Plugins should restore their internal state based on the provided state.\r\n *\r\n * @param field - The field name of the column\r\n * @param state - The column state to apply (may contain plugin-specific properties)\r\n *\r\n * @example\r\n * ```ts\r\n * applyColumnState(field: string, state: ColumnState): void {\r\n * // Check for filter property added via module augmentation\r\n * const filter = (state as any).filter;\r\n * if (filter) {\r\n * this.filterModels.set(field, filter);\r\n * this.applyFilter();\r\n * }\r\n * }\r\n * ```\r\n */\r\n applyColumnState?(field: string, state: ColumnState): void;\r\n\r\n // ===== Shell Integration Hooks (override as needed) =====\r\n\r\n /**\r\n * Register a tool panel for this plugin.\r\n * Return undefined if plugin has no tool panel.\r\n * The shell will create a toolbar toggle button and render the panel content\r\n * when the user opens the panel.\r\n *\r\n * @returns Tool panel definition, or undefined if plugin has no panel\r\n *\r\n * @example\r\n * ```ts\r\n * getToolPanel(): ToolPanelDefinition | undefined {\r\n * return {\r\n * id: 'columns',\r\n * title: 'Columns',\r\n * icon: '☰',\r\n * tooltip: 'Show/hide columns',\r\n * order: 10,\r\n * render: (container) => {\r\n * this.renderColumnList(container);\r\n * return () => this.cleanup();\r\n * },\r\n * };\r\n * }\r\n * ```\r\n */\r\n getToolPanel?(): ToolPanelDefinition | undefined;\r\n\r\n /**\r\n * Register content for the shell header center section.\r\n * Return undefined if plugin has no header content.\r\n * Examples: search input, selection summary, status indicators.\r\n *\r\n * @returns Header content definition, or undefined if plugin has no header content\r\n *\r\n * @example\r\n * ```ts\r\n * getHeaderContent(): HeaderContentDefinition | undefined {\r\n * return {\r\n * id: 'quick-filter',\r\n * order: 10,\r\n * render: (container) => {\r\n * const input = document.createElement('input');\r\n * input.type = 'text';\r\n * input.placeholder = 'Search...';\r\n * input.addEventListener('input', this.handleInput);\r\n * container.appendChild(input);\r\n * return () => input.removeEventListener('input', this.handleInput);\r\n * },\r\n * };\r\n * }\r\n * ```\r\n */\r\n getHeaderContent?(): HeaderContentDefinition | undefined;\r\n}\r\n","import type { PivotConfig } from './types';\n\nexport function validatePivotConfig(config: PivotConfig): string[] {\n const errors: string[] = [];\n\n if (!config.rowGroupFields?.length && !config.columnGroupFields?.length) {\n errors.push('At least one row or column group field is required');\n }\n\n if (!config.valueFields?.length) {\n errors.push('At least one value field is required');\n }\n\n return errors;\n}\n\n/**\n * Get a value-based aggregator function for pivot operations.\n * This operates on pre-extracted numeric values (different from core row-based aggregators).\n */\nexport function getPivotAggregator(aggFunc: string): (values: number[]) => number {\n switch (aggFunc) {\n case 'sum':\n return (vals) => vals.reduce((a, b) => a + b, 0);\n case 'avg':\n return (vals) => (vals.length ? vals.reduce((a, b) => a + b, 0) / vals.length : 0);\n case 'count':\n return (vals) => vals.length;\n case 'min':\n return (vals) => (vals.length ? Math.min(...vals) : 0);\n case 'max':\n return (vals) => (vals.length ? Math.max(...vals) : 0);\n case 'first':\n return (vals) => vals[0] ?? 0;\n case 'last':\n return (vals) => vals[vals.length - 1] ?? 0;\n default:\n return (vals) => vals.reduce((a, b) => a + b, 0);\n }\n}\n\nexport function createValueKey(columnValues: string[], valueField: string): string {\n return [...columnValues, valueField].join('|');\n}\n","import type { PivotConfig, PivotResult, PivotRow, PivotValueField } from './types';\nimport { getPivotAggregator, createValueKey } from './pivot-model';\n\nexport type PivotDataRow = Record<string, unknown>;\n\nexport function buildPivot(rows: PivotDataRow[], config: PivotConfig): PivotResult {\n const rowGroupFields = config.rowGroupFields ?? [];\n const columnGroupFields = config.columnGroupFields ?? [];\n const valueFields = config.valueFields ?? [];\n\n // Get unique column combinations\n const columnKeys = getUniqueColumnKeys(rows, columnGroupFields);\n\n // Group rows by row group fields\n const groupedData = groupByFields(rows, rowGroupFields);\n\n // Build pivot rows\n const pivotRows = buildPivotRows(groupedData, columnGroupFields, columnKeys, valueFields, 0);\n\n // Calculate totals\n const totals = calculateTotals(pivotRows, columnKeys, valueFields);\n const grandTotal = Object.values(totals).reduce((a, b) => a + b, 0);\n\n return {\n rows: pivotRows,\n columnKeys,\n totals,\n grandTotal,\n };\n}\n\nexport function getUniqueColumnKeys(rows: PivotDataRow[], columnFields: string[]): string[] {\n if (columnFields.length === 0) return ['value'];\n\n const keys = new Set<string>();\n for (const row of rows) {\n const key = columnFields.map((f) => String(row[f] ?? '')).join('|');\n keys.add(key);\n }\n return [...keys].sort();\n}\n\nexport function groupByFields(rows: PivotDataRow[], fields: string[]): Map<string, PivotDataRow[]> {\n const groups = new Map<string, PivotDataRow[]>();\n\n for (const row of rows) {\n const key = fields.map((f) => String(row[f] ?? '')).join('|');\n if (!groups.has(key)) {\n groups.set(key, []);\n }\n const group = groups.get(key);\n if (group) group.push(row);\n }\n\n return groups;\n}\n\nexport function buildPivotRows(\n groupedData: Map<string, PivotDataRow[]>,\n columnFields: string[],\n columnKeys: string[],\n valueFields: PivotValueField[],\n depth: number\n): PivotRow[] {\n const result: PivotRow[] = [];\n\n for (const [rowKey, groupRows] of groupedData) {\n const values: Record<string, number | null> = {};\n let total = 0;\n\n for (const colKey of columnKeys) {\n for (const vf of valueFields) {\n const matchingRows =\n columnFields.length > 0\n ? groupRows.filter((r) => columnFields.map((f) => String(r[f] ?? '')).join('|') === colKey)\n : groupRows;\n\n const nums = matchingRows.map((r) => Number(r[vf.field]) || 0);\n const aggregator = getPivotAggregator(vf.aggFunc);\n const aggregatedResult = nums.length > 0 ? aggregator(nums) : null;\n\n const valueKey = createValueKey([colKey], vf.field);\n values[valueKey] = aggregatedResult;\n\n if (aggregatedResult !== null) total += aggregatedResult;\n }\n }\n\n result.push({\n rowKey,\n rowLabel: rowKey || '(blank)',\n depth,\n values,\n total,\n isGroup: false,\n });\n }\n\n return result;\n}\n\nexport function calculateTotals(\n pivotRows: PivotRow[],\n columnKeys: string[],\n valueFields: PivotValueField[]\n): Record<string, number> {\n const totals: Record<string, number> = {};\n\n for (const colKey of columnKeys) {\n for (const vf of valueFields) {\n const valueKey = createValueKey([colKey], vf.field);\n totals[valueKey] = pivotRows.reduce((sum, row) => {\n return sum + (row.values[valueKey] ?? 0);\n }, 0);\n }\n }\n\n return totals;\n}\n\nexport function flattenPivotRows(rows: PivotRow[]): PivotRow[] {\n const result: PivotRow[] = [];\n\n function flatten(row: PivotRow) {\n result.push(row);\n if (row.children) {\n for (const child of row.children) {\n flatten(child);\n }\n }\n }\n\n for (const row of rows) {\n flatten(row);\n }\n\n return result;\n}\n","/**\n * Pivot Plugin (Class-based)\n *\n * Provides pivot table functionality for tbw-grid.\n * Transforms flat data into grouped, aggregated pivot views.\n */\n\nimport { BaseGridPlugin } from '../../core/plugin/base-plugin';\nimport type { ColumnConfig } from '../../core/types';\nimport { buildPivot, flattenPivotRows, type PivotDataRow } from './pivot-engine';\nimport { createValueKey, validatePivotConfig } from './pivot-model';\nimport type { PivotConfig, PivotResult, PivotValueField } from './types';\n\n/**\n * Pivot Plugin for tbw-grid\n *\n * @example\n * ```ts\n * new PivotPlugin({\n * rowGroupFields: ['category'],\n * columnGroupFields: ['region'],\n * valueFields: [{ field: 'sales', aggFunc: 'sum' }]\n * })\n * ```\n */\nexport class PivotPlugin extends BaseGridPlugin<PivotConfig> {\n readonly name = 'pivot';\n override readonly version = '1.0.0';\n\n protected override get defaultConfig(): Partial<PivotConfig> {\n return {\n enabled: true,\n showTotals: true,\n showGrandTotal: true,\n };\n }\n\n // ===== Internal State =====\n private isActive = false;\n private pivotResult: PivotResult | null = null;\n private columnHeaders: string[] = [];\n private rowHeaders: string[] = [];\n\n // ===== Lifecycle =====\n\n override detach(): void {\n this.isActive = false;\n this.pivotResult = null;\n this.columnHeaders = [];\n this.rowHeaders = [];\n }\n\n // ===== Hooks =====\n\n override processRows(rows: readonly unknown[]): PivotDataRow[] {\n if (!this.config.enabled || !this.isActive) {\n return [...rows] as PivotDataRow[];\n }\n\n const errors = validatePivotConfig(this.config);\n if (errors.length > 0) {\n this.warn(`Config errors: ${errors.join(', ')}`);\n return [...rows] as PivotDataRow[];\n }\n\n // Build pivot\n this.pivotResult = buildPivot(rows as PivotDataRow[], this.config);\n\n // Return flattened pivot rows for rendering\n return flattenPivotRows(this.pivotResult.rows).map((pr) => ({\n __pivotRowKey: pr.rowKey,\n __pivotLabel: pr.rowLabel,\n __pivotDepth: pr.depth,\n __pivotIsGroup: pr.isGroup,\n __pivotTotal: pr.total,\n ...pr.values,\n }));\n }\n\n override processColumns(columns: readonly ColumnConfig[]): ColumnConfig[] {\n if (!this.config.enabled || !this.isActive || !this.pivotResult) {\n return [...columns];\n }\n\n const pivotColumns: ColumnConfig[] = [];\n\n // Row label column\n pivotColumns.push({\n field: '__pivotLabel',\n header: this.config.rowGroupFields?.join(' / ') ?? 'Group',\n width: 200,\n });\n\n // Value columns for each column key\n for (const colKey of this.pivotResult.columnKeys) {\n for (const vf of this.config.valueFields ?? []) {\n const valueKey = createValueKey([colKey], vf.field);\n pivotColumns.push({\n field: valueKey,\n header: `${colKey} - ${vf.header || vf.field} (${vf.aggFunc})`,\n width: 120,\n type: 'number',\n });\n }\n }\n\n // Totals column\n if (this.config.showTotals) {\n pivotColumns.push({\n field: '__pivotTotal',\n header: 'Total',\n width: 100,\n type: 'number',\n });\n }\n\n return pivotColumns;\n }\n\n // ===== Public API =====\n\n /**\n * Enable pivot mode.\n */\n enablePivot(): void {\n this.isActive = true;\n this.requestRender();\n }\n\n /**\n * Disable pivot mode and return to normal grid view.\n */\n disablePivot(): void {\n this.isActive = false;\n this.pivotResult = null;\n this.requestRender();\n }\n\n /**\n * Check if pivot mode is currently active.\n */\n isPivotActive(): boolean {\n return this.isActive;\n }\n\n /**\n * Get the current pivot result.\n */\n getPivotResult(): PivotResult | null {\n return this.pivotResult;\n }\n\n /**\n * Set the row group fields for pivoting.\n * @param fields - Array of field names to group rows by\n */\n setRowGroupFields(fields: string[]): void {\n this.config.rowGroupFields = fields;\n this.requestRender();\n }\n\n /**\n * Set the column group fields for pivoting.\n * @param fields - Array of field names to create columns from\n */\n setColumnGroupFields(fields: string[]): void {\n this.config.columnGroupFields = fields;\n this.requestRender();\n }\n\n /**\n * Set the value fields with aggregation functions.\n * @param fields - Array of value field configurations\n */\n setValueFields(fields: PivotValueField[]): void {\n this.config.valueFields = fields;\n this.requestRender();\n }\n\n /**\n * Refresh the pivot by clearing cached results.\n */\n refresh(): void {\n this.pivotResult = null;\n this.requestRender();\n }\n\n // ===== Styles =====\n\n override readonly styles = `\n [data-pivot-depth=\"1\"] { padding-left: 20px; }\n [data-pivot-depth=\"2\"] { padding-left: 40px; }\n [data-pivot-depth=\"3\"] { padding-left: 60px; }\n .pivot-group-row { font-weight: bold; background: var(--tbw-pivot-group-bg, var(--tbw-color-panel-bg)); }\n .pivot-total-row { font-weight: bold; border-top: 2px solid var(--tbw-pivot-border, var(--tbw-color-border-strong)); }\n `;\n}\n"],"names":["BaseGridPlugin","config","grid","PluginClass","eventName","detail","message","validatePivotConfig","errors","getPivotAggregator","aggFunc","vals","a","b","createValueKey","columnValues","valueField","buildPivot","rows","rowGroupFields","columnGroupFields","valueFields","columnKeys","getUniqueColumnKeys","groupedData","groupByFields","pivotRows","buildPivotRows","totals","calculateTotals","grandTotal","columnFields","keys","row","key","f","fields","groups","group","depth","result","rowKey","groupRows","values","total","colKey","vf","nums","r","aggregator","aggregatedResult","valueKey","sum","flattenPivotRows","flatten","child","PivotPlugin","pr","columns","pivotColumns"],"mappings":"AA6MO,MAAeA,EAAkC;AAAA;AAAA,EAK7C,UAAkB;AAAA;AAAA,EAGlB;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGC;AAAA;AAAA,EAGA;AAAA;AAAA,EAGO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOjB,IAAc,gBAAkC;AAC9C,WAAO,CAAA;AAAA,EACT;AAAA,EAEA,YAAYC,IAA2B,IAAI;AACzC,SAAK,aAAaA;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAOC,GAAyB;AAC9B,SAAK,OAAOA,GAEZ,KAAK,SAAS,EAAE,GAAG,KAAK,eAAe,GAAG,KAAK,WAAA;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAe;AAAA,EAEf;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,UAAoCC,GAAuD;AACnG,WAAO,KAAK,MAAM,UAAUA,CAAW;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA,EAKU,KAAQC,GAAmBC,GAAiB;AACpD,SAAK,MAAM,gBAAgB,IAAI,YAAYD,GAAW,EAAE,QAAAC,GAAQ,SAAS,GAAA,CAAM,CAAC;AAAA,EAClF;AAAA;AAAA;AAAA;AAAA,EAKU,gBAAsB;AAC9B,SAAK,MAAM,gBAAA;AAAA,EACb;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,qBAA2B;AACnC,SAAK,MAAM,qBAAA;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKA,IAAc,OAAc;AAC1B,WAAO,KAAK,MAAM,QAAQ,CAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA,E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BAAyB;AACvB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,iBAAqC;AACnC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,kBAAkBvB,GAAwB;AACxC,SAAK,OAAO,iBAAiBA,GAC7B,KAAK,cAAA;AAAA,EACP;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,qBAAqBA,GAAwB;AAC3C,SAAK,OAAO,oBAAoBA,GAChC,KAAK,cAAA;AAAA,EACP;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAeA,GAAiC;AAC9C,SAAK,OAAO,cAAcA,GAC1B,KAAK,cAAA;AAAA,EACP;AAAA;AAAA;AAAA;AAAA,EAKA,UAAgB;AACd,SAAK,cAAc,MACnB,KAAK,cAAA;AAAA,EACP;AAAA;AAAA,EAIkB,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAO7B;"}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
class b {
|
|
2
|
+
/** Plugin version - override in subclass if needed */
|
|
3
|
+
version = "1.0.0";
|
|
4
|
+
/** CSS styles to inject into the grid's shadow DOM */
|
|
5
|
+
styles;
|
|
6
|
+
/** Custom cell renderers keyed by type name */
|
|
7
|
+
cellRenderers;
|
|
8
|
+
/** Custom header renderers keyed by type name */
|
|
9
|
+
headerRenderers;
|
|
10
|
+
/** Custom cell editors keyed by type name */
|
|
11
|
+
cellEditors;
|
|
12
|
+
/** The grid instance this plugin is attached to */
|
|
13
|
+
grid;
|
|
14
|
+
/** Plugin configuration - merged with defaults in attach() */
|
|
15
|
+
config;
|
|
16
|
+
/** User-provided configuration from constructor */
|
|
17
|
+
userConfig;
|
|
18
|
+
/**
|
|
19
|
+
* Default configuration - subclasses should override this getter.
|
|
20
|
+
* Note: This must be a getter (not property initializer) for proper inheritance
|
|
21
|
+
* since property initializers run after parent constructor.
|
|
22
|
+
*/
|
|
23
|
+
get defaultConfig() {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
constructor(e = {}) {
|
|
27
|
+
this.userConfig = e;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Called when the plugin is attached to a grid.
|
|
31
|
+
* Override to set up event listeners, initialize state, etc.
|
|
32
|
+
*/
|
|
33
|
+
attach(e) {
|
|
34
|
+
this.grid = e, this.config = { ...this.defaultConfig, ...this.userConfig };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Called when the plugin is detached from a grid.
|
|
38
|
+
* Override to clean up event listeners, timers, etc.
|
|
39
|
+
*/
|
|
40
|
+
detach() {
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get another plugin instance from the same grid.
|
|
44
|
+
* Use for inter-plugin communication.
|
|
45
|
+
*/
|
|
46
|
+
getPlugin(e) {
|
|
47
|
+
return this.grid?.getPlugin(e);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Emit a custom event from the grid.
|
|
51
|
+
*/
|
|
52
|
+
emit(e, t) {
|
|
53
|
+
this.grid?.dispatchEvent?.(new CustomEvent(e, { detail: t, bubbles: !0 }));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Request a re-render of the grid.
|
|
57
|
+
*/
|
|
58
|
+
requestRender() {
|
|
59
|
+
this.grid?.requestRender?.();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Request a lightweight style update without rebuilding DOM.
|
|
63
|
+
* Use this instead of requestRender() when only CSS classes need updating.
|
|
64
|
+
*/
|
|
65
|
+
requestAfterRender() {
|
|
66
|
+
this.grid?.requestAfterRender?.();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get the current rows from the grid.
|
|
70
|
+
*/
|
|
71
|
+
get rows() {
|
|
72
|
+
return this.grid?.rows ?? [];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get the original unfiltered/unprocessed rows from the grid.
|
|
76
|
+
* Use this when you need all source data regardless of active filters.
|
|
77
|
+
*/
|
|
78
|
+
get sourceRows() {
|
|
79
|
+
return this.grid?.sourceRows ?? [];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get the current columns from the grid.
|
|
83
|
+
*/
|
|
84
|
+
get columns() {
|
|
85
|
+
return this.grid?.columns ?? [];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get only visible columns from the grid (excludes hidden).
|
|
89
|
+
* Use this for rendering that needs to match the grid template.
|
|
90
|
+
*/
|
|
91
|
+
get visibleColumns() {
|
|
92
|
+
return this.grid?.visibleColumns ?? [];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the shadow root of the grid.
|
|
96
|
+
*/
|
|
97
|
+
get shadowRoot() {
|
|
98
|
+
return this.grid?.shadowRoot ?? null;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Log a warning message.
|
|
102
|
+
*/
|
|
103
|
+
warn(e) {
|
|
104
|
+
console.warn(`[tbw-grid:${this.name}] ${e}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function p(i) {
|
|
108
|
+
const e = i.meta ?? {}, t = e.sticky;
|
|
109
|
+
return t === "left" || t === "right" ? !1 : e.lockPosition !== !0 && e.suppressMovable !== !0;
|
|
110
|
+
}
|
|
111
|
+
function h(i, e, t) {
|
|
112
|
+
if (e === t || e < 0 || e >= i.length || t < 0 || t > i.length) return i;
|
|
113
|
+
const d = [...i], [r] = d.splice(e, 1);
|
|
114
|
+
return d.splice(t, 0, r), d;
|
|
115
|
+
}
|
|
116
|
+
class v extends b {
|
|
117
|
+
name = "reorder";
|
|
118
|
+
version = "1.0.0";
|
|
119
|
+
get defaultConfig() {
|
|
120
|
+
return {
|
|
121
|
+
enabled: !0,
|
|
122
|
+
animation: !0,
|
|
123
|
+
animationDuration: 200
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// ===== Internal State =====
|
|
127
|
+
isDragging = !1;
|
|
128
|
+
draggedField = null;
|
|
129
|
+
draggedIndex = null;
|
|
130
|
+
dropIndex = null;
|
|
131
|
+
boundReorderRequestHandler = null;
|
|
132
|
+
// ===== Lifecycle =====
|
|
133
|
+
attach(e) {
|
|
134
|
+
super.attach(e), this.boundReorderRequestHandler = (t) => {
|
|
135
|
+
const d = t.detail;
|
|
136
|
+
d?.field && typeof d.toIndex == "number" && this.moveColumn(d.field, d.toIndex);
|
|
137
|
+
}, e.addEventListener("column-reorder-request", this.boundReorderRequestHandler);
|
|
138
|
+
}
|
|
139
|
+
detach() {
|
|
140
|
+
this.boundReorderRequestHandler && this.grid && (this.grid.removeEventListener(
|
|
141
|
+
"column-reorder-request",
|
|
142
|
+
this.boundReorderRequestHandler
|
|
143
|
+
), this.boundReorderRequestHandler = null), this.isDragging = !1, this.draggedField = null, this.draggedIndex = null, this.dropIndex = null;
|
|
144
|
+
}
|
|
145
|
+
// ===== Hooks =====
|
|
146
|
+
// Note: No processColumns hook needed - we directly update the grid's column order
|
|
147
|
+
afterRender() {
|
|
148
|
+
if (!this.config.enabled) return;
|
|
149
|
+
const e = this.shadowRoot;
|
|
150
|
+
if (!e) return;
|
|
151
|
+
e.querySelectorAll(".header-row > .cell").forEach((d) => {
|
|
152
|
+
const r = d, s = r.getAttribute("data-field");
|
|
153
|
+
if (!s) return;
|
|
154
|
+
const u = this.columns.find((n) => n.field === s);
|
|
155
|
+
if (!u || !p(u)) {
|
|
156
|
+
r.draggable = !1;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
r.draggable = !0, !r.getAttribute("data-dragstart-bound") && (r.setAttribute("data-dragstart-bound", "true"), r.addEventListener("dragstart", (n) => {
|
|
160
|
+
const o = this.getColumnOrder().indexOf(s);
|
|
161
|
+
this.isDragging = !0, this.draggedField = s, this.draggedIndex = o, n.dataTransfer && (n.dataTransfer.effectAllowed = "move", n.dataTransfer.setData("text/plain", s)), r.classList.add("dragging");
|
|
162
|
+
}), r.addEventListener("dragend", () => {
|
|
163
|
+
this.isDragging = !1, this.draggedField = null, this.draggedIndex = null, this.dropIndex = null, e.querySelectorAll(".header-row > .cell").forEach((n) => {
|
|
164
|
+
n.classList.remove("dragging", "drop-target", "drop-before", "drop-after");
|
|
165
|
+
});
|
|
166
|
+
}), r.addEventListener("dragover", (n) => {
|
|
167
|
+
if (n.preventDefault(), !this.isDragging || this.draggedField === s) return;
|
|
168
|
+
const a = r.getBoundingClientRect(), o = a.left + a.width / 2, g = this.getColumnOrder().indexOf(s);
|
|
169
|
+
this.dropIndex = n.clientX < o ? g : g + 1, r.classList.add("drop-target"), r.classList.toggle("drop-before", n.clientX < o), r.classList.toggle("drop-after", n.clientX >= o);
|
|
170
|
+
}), r.addEventListener("dragleave", () => {
|
|
171
|
+
r.classList.remove("drop-target", "drop-before", "drop-after");
|
|
172
|
+
}), r.addEventListener("drop", (n) => {
|
|
173
|
+
n.preventDefault();
|
|
174
|
+
const a = this.draggedField, o = this.draggedIndex, l = this.dropIndex;
|
|
175
|
+
if (!this.isDragging || a === null || o === null || l === null)
|
|
176
|
+
return;
|
|
177
|
+
const g = l > o ? l - 1 : l, f = this.getColumnOrder(), c = h(f, o, g), m = {
|
|
178
|
+
field: a,
|
|
179
|
+
fromIndex: o,
|
|
180
|
+
toIndex: g,
|
|
181
|
+
columnOrder: c
|
|
182
|
+
};
|
|
183
|
+
this.grid.setColumnOrder(c), this.emit("column-move", m), this.grid.requestStateChange?.();
|
|
184
|
+
}));
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
// ===== Public API =====
|
|
188
|
+
/**
|
|
189
|
+
* Get the current column order from the grid.
|
|
190
|
+
* @returns Array of field names in display order
|
|
191
|
+
*/
|
|
192
|
+
getColumnOrder() {
|
|
193
|
+
return this.grid.getColumnOrder();
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Move a column to a new position.
|
|
197
|
+
* @param field - The field name of the column to move
|
|
198
|
+
* @param toIndex - The target index
|
|
199
|
+
*/
|
|
200
|
+
moveColumn(e, t) {
|
|
201
|
+
const d = this.getColumnOrder(), r = d.indexOf(e);
|
|
202
|
+
if (r === -1) return;
|
|
203
|
+
const s = h(d, r, t);
|
|
204
|
+
this.grid.setColumnOrder(s), this.emit("column-move", {
|
|
205
|
+
field: e,
|
|
206
|
+
fromIndex: r,
|
|
207
|
+
toIndex: t,
|
|
208
|
+
columnOrder: s
|
|
209
|
+
}), this.grid.requestStateChange?.();
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Set a specific column order.
|
|
213
|
+
* @param order - Array of field names in desired order
|
|
214
|
+
*/
|
|
215
|
+
setColumnOrder(e) {
|
|
216
|
+
this.grid.setColumnOrder(e), this.grid.requestStateChange?.();
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Reset column order to the original configuration order.
|
|
220
|
+
*/
|
|
221
|
+
resetColumnOrder() {
|
|
222
|
+
const e = this.columns.map((t) => t.field);
|
|
223
|
+
this.grid.setColumnOrder(e), this.grid.requestStateChange?.();
|
|
224
|
+
}
|
|
225
|
+
// ===== Styles =====
|
|
226
|
+
styles = `
|
|
227
|
+
.header-row > .cell[draggable="true"] {
|
|
228
|
+
cursor: grab;
|
|
229
|
+
position: relative;
|
|
230
|
+
}
|
|
231
|
+
.header-row > .cell.dragging {
|
|
232
|
+
opacity: 0.5;
|
|
233
|
+
cursor: grabbing;
|
|
234
|
+
}
|
|
235
|
+
.header-row > .cell.drop-before::before {
|
|
236
|
+
content: '';
|
|
237
|
+
position: absolute;
|
|
238
|
+
left: 0;
|
|
239
|
+
top: 0;
|
|
240
|
+
bottom: 0;
|
|
241
|
+
width: 2px;
|
|
242
|
+
background: var(--tbw-reorder-indicator, var(--tbw-color-accent));
|
|
243
|
+
z-index: 1;
|
|
244
|
+
}
|
|
245
|
+
.header-row > .cell.drop-after::after {
|
|
246
|
+
content: '';
|
|
247
|
+
position: absolute;
|
|
248
|
+
right: 0;
|
|
249
|
+
top: 0;
|
|
250
|
+
bottom: 0;
|
|
251
|
+
width: 2px;
|
|
252
|
+
background: var(--tbw-reorder-indicator, var(--tbw-color-accent));
|
|
253
|
+
z-index: 1;
|
|
254
|
+
}
|
|
255
|
+
`;
|
|
256
|
+
}
|
|
257
|
+
export {
|
|
258
|
+
v as ReorderPlugin
|
|
259
|
+
};
|
|
260
|
+
//# sourceMappingURL=index.js.map
|