@toolbox-web/grid 1.26.1 → 1.27.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.
Files changed (114) hide show
  1. package/all.js +2 -2
  2. package/all.js.map +1 -1
  3. package/index.js +1 -1
  4. package/index.js.map +1 -1
  5. package/lib/core/grid.d.ts +64 -1
  6. package/lib/core/internal/diagnostics.d.ts +4 -3
  7. package/lib/core/internal/row-manager.d.ts +3 -1
  8. package/lib/core/plugin/base-plugin.d.ts +2 -2
  9. package/lib/core/types.d.ts +59 -3
  10. package/lib/features/registry.js.map +1 -1
  11. package/lib/plugins/clipboard/ClipboardPlugin.d.ts +1 -1
  12. package/lib/plugins/clipboard/index.js.map +1 -1
  13. package/lib/plugins/column-virtualization/index.js.map +1 -1
  14. package/lib/plugins/context-menu/ContextMenuPlugin.d.ts +24 -5
  15. package/lib/plugins/context-menu/index.js.map +1 -1
  16. package/lib/plugins/editing/EditingPlugin.d.ts +1 -1
  17. package/lib/plugins/editing/index.js.map +1 -1
  18. package/lib/plugins/export/ExportPlugin.d.ts +1 -1
  19. package/lib/plugins/export/index.js.map +1 -1
  20. package/lib/plugins/filtering/index.d.ts +2 -2
  21. package/lib/plugins/filtering/index.js +1 -1
  22. package/lib/plugins/filtering/index.js.map +1 -1
  23. package/lib/plugins/grouping-columns/GroupingColumnsPlugin.d.ts +2 -2
  24. package/lib/plugins/grouping-columns/grouping-columns.d.ts +18 -3
  25. package/lib/plugins/grouping-columns/index.d.ts +0 -1
  26. package/lib/plugins/grouping-columns/index.js +1 -1
  27. package/lib/plugins/grouping-columns/index.js.map +1 -1
  28. package/lib/plugins/grouping-columns/types.d.ts +1 -7
  29. package/lib/plugins/grouping-rows/index.d.ts +2 -1
  30. package/lib/plugins/grouping-rows/index.js +1 -1
  31. package/lib/plugins/grouping-rows/index.js.map +1 -1
  32. package/lib/plugins/master-detail/MasterDetailPlugin.d.ts +2 -0
  33. package/lib/plugins/master-detail/index.js +1 -1
  34. package/lib/plugins/master-detail/index.js.map +1 -1
  35. package/lib/plugins/master-detail/types.d.ts +20 -1
  36. package/lib/plugins/multi-sort/index.js.map +1 -1
  37. package/lib/plugins/pinned-columns/PinnedColumnsPlugin.d.ts +8 -1
  38. package/lib/plugins/pinned-columns/index.js +1 -1
  39. package/lib/plugins/pinned-columns/index.js.map +1 -1
  40. package/lib/plugins/pinned-columns/pinned-columns.d.ts +11 -1
  41. package/lib/plugins/pinned-rows/index.d.ts +1 -1
  42. package/lib/plugins/pinned-rows/index.js +1 -1
  43. package/lib/plugins/pinned-rows/index.js.map +1 -1
  44. package/lib/plugins/pinned-rows/types.d.ts +10 -3
  45. package/lib/plugins/pivot/PivotPlugin.d.ts +107 -1
  46. package/lib/plugins/pivot/index.d.ts +2 -1
  47. package/lib/plugins/pivot/index.js +1 -1
  48. package/lib/plugins/pivot/index.js.map +1 -1
  49. package/lib/plugins/print/index.js.map +1 -1
  50. package/lib/plugins/print/types.d.ts +0 -3
  51. package/lib/plugins/reorder-columns/ReorderPlugin.d.ts +19 -2
  52. package/lib/plugins/reorder-columns/index.js +1 -1
  53. package/lib/plugins/reorder-columns/index.js.map +1 -1
  54. package/lib/plugins/reorder-rows/index.d.ts +1 -1
  55. package/lib/plugins/reorder-rows/index.js.map +1 -1
  56. package/lib/plugins/responsive/ResponsivePlugin.d.ts +1 -1
  57. package/lib/plugins/responsive/index.js +1 -1
  58. package/lib/plugins/responsive/index.js.map +1 -1
  59. package/lib/plugins/selection/SelectionPlugin.d.ts +1 -1
  60. package/lib/plugins/selection/index.js +1 -1
  61. package/lib/plugins/selection/index.js.map +1 -1
  62. package/lib/plugins/selection/types.d.ts +3 -3
  63. package/lib/plugins/server-side/ServerSidePlugin.d.ts +6 -1
  64. package/lib/plugins/server-side/index.js +1 -1
  65. package/lib/plugins/server-side/index.js.map +1 -1
  66. package/lib/plugins/tree/TreePlugin.d.ts +116 -0
  67. package/lib/plugins/tree/index.d.ts +1 -1
  68. package/lib/plugins/tree/index.js +1 -1
  69. package/lib/plugins/tree/index.js.map +1 -1
  70. package/lib/plugins/tree/types.d.ts +16 -1
  71. package/lib/plugins/undo-redo/UndoRedoPlugin.d.ts +1 -1
  72. package/lib/plugins/undo-redo/index.js.map +1 -1
  73. package/lib/plugins/undo-redo/types.d.ts +15 -3
  74. package/lib/plugins/visibility/VisibilityPlugin.d.ts +18 -5
  75. package/lib/plugins/visibility/index.js +1 -1
  76. package/lib/plugins/visibility/index.js.map +1 -1
  77. package/package.json +1 -1
  78. package/public.d.ts +2 -4
  79. package/themes/dg-theme-material.css +16 -4
  80. package/umd/grid.all.umd.js +1 -1
  81. package/umd/grid.all.umd.js.map +1 -1
  82. package/umd/grid.umd.js +1 -1
  83. package/umd/grid.umd.js.map +1 -1
  84. package/umd/plugins/clipboard.umd.js.map +1 -1
  85. package/umd/plugins/context-menu.umd.js.map +1 -1
  86. package/umd/plugins/editing.umd.js.map +1 -1
  87. package/umd/plugins/export.umd.js.map +1 -1
  88. package/umd/plugins/filtering.umd.js +1 -1
  89. package/umd/plugins/filtering.umd.js.map +1 -1
  90. package/umd/plugins/grouping-columns.umd.js +1 -1
  91. package/umd/plugins/grouping-columns.umd.js.map +1 -1
  92. package/umd/plugins/grouping-rows.umd.js +1 -1
  93. package/umd/plugins/grouping-rows.umd.js.map +1 -1
  94. package/umd/plugins/master-detail.umd.js +1 -1
  95. package/umd/plugins/master-detail.umd.js.map +1 -1
  96. package/umd/plugins/pinned-columns.umd.js +1 -1
  97. package/umd/plugins/pinned-columns.umd.js.map +1 -1
  98. package/umd/plugins/pinned-rows.umd.js +1 -1
  99. package/umd/plugins/pinned-rows.umd.js.map +1 -1
  100. package/umd/plugins/pivot.umd.js +1 -1
  101. package/umd/plugins/pivot.umd.js.map +1 -1
  102. package/umd/plugins/reorder-columns.umd.js +1 -1
  103. package/umd/plugins/reorder-columns.umd.js.map +1 -1
  104. package/umd/plugins/responsive.umd.js +1 -1
  105. package/umd/plugins/responsive.umd.js.map +1 -1
  106. package/umd/plugins/selection.umd.js +1 -1
  107. package/umd/plugins/selection.umd.js.map +1 -1
  108. package/umd/plugins/server-side.umd.js +1 -1
  109. package/umd/plugins/server-side.umd.js.map +1 -1
  110. package/umd/plugins/tree.umd.js +1 -1
  111. package/umd/plugins/tree.umd.js.map +1 -1
  112. package/umd/plugins/undo-redo.umd.js.map +1 -1
  113. package/umd/plugins/visibility.umd.js +1 -1
  114. package/umd/plugins/visibility.umd.js.map +1 -1
@@ -66,17 +66,24 @@ export interface AggregationRowConfig {
66
66
  /** If true, row rendered as single spanning cell with label */
67
67
  fullWidth?: boolean;
68
68
  /**
69
- * Label for fullWidth mode. Can be a static string or a function that receives
69
+ * Row label. Can be a static string or a function that receives
70
70
  * the current rows and columns for dynamic content.
71
71
  *
72
+ * In **full-width mode** (`fullWidth: true`), the label is displayed inline before
73
+ * the aggregated values.
74
+ *
75
+ * In **per-column mode** (`fullWidth: false`, the default), the label renders as an
76
+ * overlay positioned at the left edge of the row, independent of column alignment.
77
+ * It does not truncate with column width.
78
+ *
72
79
  * @example Static label
73
80
  * ```ts
74
- * { fullWidth: true, label: 'Totals' }
81
+ * { label: 'Totals', aggregators: { price: 'sum' } }
75
82
  * ```
76
83
  *
77
84
  * @example Dynamic label
78
85
  * ```ts
79
- * { fullWidth: true, label: (rows) => `Total: ${rows.length} rows` }
86
+ * { label: (rows) => `Total: ${rows.length} rows` }
80
87
  * ```
81
88
  */
82
89
  label?: string | ((rows: unknown[], columns: ColumnConfig[]) => string);
@@ -1,4 +1,4 @@
1
- import { BaseGridPlugin } from '../../core/plugin/base-plugin';
1
+ import { BaseGridPlugin, PluginManifest } from '../../core/plugin/base-plugin';
2
2
  import { ColumnConfig, ToolPanelDefinition } from '../../core/types';
3
3
  import { PivotDataRow } from './pivot-engine';
4
4
  import { PivotConfig, PivotResult, PivotValueField } from './types';
@@ -82,6 +82,11 @@ import { PivotConfig, PivotResult, PivotValueField } from './types';
82
82
  * @internal Extends BaseGridPlugin
83
83
  */
84
84
  export declare class PivotPlugin extends BaseGridPlugin<PivotConfig> {
85
+ /**
86
+ * Plugin manifest declaring incompatibilities with other plugins.
87
+ * @internal
88
+ */
89
+ static readonly manifest: PluginManifest;
85
90
  /** @internal */
86
91
  readonly name = "pivot";
87
92
  /** @internal */
@@ -141,22 +146,123 @@ export declare class PivotPlugin extends BaseGridPlugin<PivotConfig> {
141
146
  */
142
147
  private cleanupGrandTotalFooter;
143
148
  toggle(key: string): void;
149
+ /**
150
+ * Expand a specific pivot group row, revealing its children.
151
+ *
152
+ * @param key - The pivot row key (hierarchical path, e.g. `'Engineering'` or `'Engineering||Frontend'`)
153
+ */
144
154
  expand(key: string): void;
155
+ /**
156
+ * Collapse a specific pivot group row, hiding its children.
157
+ *
158
+ * @param key - The pivot row key to collapse
159
+ */
145
160
  collapse(key: string): void;
161
+ /**
162
+ * Expand all pivot group rows, revealing the full hierarchy.
163
+ */
146
164
  expandAll(): void;
165
+ /**
166
+ * Collapse all pivot group rows, showing only top-level groups.
167
+ */
147
168
  collapseAll(): void;
148
169
  /**
149
170
  * Add all group keys from the current pivot result to expandedKeys.
150
171
  */
151
172
  private expandAllKeys;
173
+ /**
174
+ * Check whether a specific pivot group row is currently expanded.
175
+ *
176
+ * @param key - The pivot row key to check
177
+ * @returns `true` if the group is expanded
178
+ */
152
179
  isExpanded(key: string): boolean;
180
+ /**
181
+ * Enable pivot mode.
182
+ *
183
+ * Captures the original column set (if not already captured) and activates
184
+ * the pivot transformation. The grid will re-render with pivot columns and rows.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * const pivot = grid.getPluginByName('pivot');
189
+ * pivot.enablePivot();
190
+ * ```
191
+ */
153
192
  enablePivot(): void;
193
+ /**
194
+ * Disable pivot mode and restore the original grid columns.
195
+ *
196
+ * The grid reverts to its normal tabular layout with the original column definitions.
197
+ */
154
198
  disablePivot(): void;
199
+ /**
200
+ * Check whether pivot mode is currently active.
201
+ *
202
+ * @returns `true` if the grid is in pivot mode
203
+ */
155
204
  isPivotActive(): boolean;
205
+ /**
206
+ * Get the current pivot computation result.
207
+ *
208
+ * Returns the full {@link PivotResult} with rows, column keys, totals,
209
+ * and grand total. Returns `null` if pivot is inactive or not yet computed.
210
+ *
211
+ * @returns The computed pivot result, or `null`
212
+ */
156
213
  getPivotResult(): PivotResult | null;
214
+ /**
215
+ * Set the fields used for row grouping (vertical axis).
216
+ *
217
+ * Triggers a re-render with the new pivot structure.
218
+ *
219
+ * @param fields - Array of field names to group rows by
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * const pivot = grid.getPluginByName('pivot');
224
+ * pivot.setRowGroupFields(['region', 'department']);
225
+ * ```
226
+ */
157
227
  setRowGroupFields(fields: string[]): void;
228
+ /**
229
+ * Set the fields whose unique values become column headers (horizontal axis).
230
+ *
231
+ * Triggers a re-render with the new pivot column structure.
232
+ *
233
+ * @param fields - Array of field names for column grouping
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * const pivot = grid.getPluginByName('pivot');
238
+ * pivot.setColumnGroupFields(['quarter']);
239
+ * ```
240
+ */
158
241
  setColumnGroupFields(fields: string[]): void;
242
+ /**
243
+ * Set the value fields and their aggregation functions.
244
+ *
245
+ * Each value field defines which data field to aggregate and how.
246
+ * Triggers a re-render with the new aggregation.
247
+ *
248
+ * @param fields - Array of {@link PivotValueField} definitions
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * const pivot = grid.getPluginByName('pivot');
253
+ * pivot.setValueFields([
254
+ * { field: 'revenue', aggFunc: 'sum', header: 'Total Revenue' },
255
+ * { field: 'orders', aggFunc: 'count', header: '# Orders' },
256
+ * ]);
257
+ * ```
258
+ */
159
259
  setValueFields(fields: PivotValueField[]): void;
260
+ /**
261
+ * Force re-computation of the pivot result.
262
+ *
263
+ * Clears the cached pivot result and triggers a full re-render.
264
+ * Call this after changing the underlying row data.
265
+ */
160
266
  refresh(): void;
161
267
  /**
162
268
  * Show the pivot tool panel.
@@ -4,5 +4,6 @@
4
4
  *
5
5
  * @module Plugins/Pivot
6
6
  */
7
+ export type { PivotDataRow } from './pivot-engine';
7
8
  export { PivotPlugin } from './PivotPlugin';
8
- export type { AggFunc, PivotConfig, PivotResult, PivotValueField } from './types';
9
+ export type { AggFunc, PivotConfig, PivotResult, PivotRow, PivotValueField } from './types';
@@ -1,2 +1,2 @@
1
- function e(e,t){return`[tbw-grid${e?`#${e}`:""}${t?`:${t}`:""}]`}function t(t,i,o,r){return`${e(o,r)} ${t}: ${i}\n\n → More info: ${function(e){return`https://toolboxjs.com/grid/errors#${e.toLowerCase()}`}(t)}`}const i='<svg viewBox="0 0 16 16" width="12" height="12"><path fill="currentColor" d="M6 10.5a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/></svg>',o={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:i,filterActive:i,print:"🖨️"};class r{static dependencies;static manifest;aliases;version="undefined"!=typeof __GRID_VERSION__?__GRID_VERSION__:"dev";styles;cellRenderers;headerRenderers;cellEditors;grid;config;userConfig;#e;get defaultConfig(){return{}}constructor(e={}){this.userConfig=e}attach(e){this.#e?.abort(),this.#e=new AbortController,this.grid=e,this.config={...this.defaultConfig,...this.userConfig}}detach(){this.#e?.abort(),this.#e=void 0}getPlugin(e){return this.grid?.getPlugin(e)}emit(e,t){this.grid?.dispatchEvent?.(new CustomEvent(e,{detail:t,bubbles:!0}))}emitCancelable(e,t){const i=new CustomEvent(e,{detail:t,bubbles:!0,cancelable:!0});return this.grid?.dispatchEvent?.(i),i.defaultPrevented}on(e,t){this.grid?._pluginManager?.subscribe(this,e,t)}off(e){this.grid?._pluginManager?.unsubscribe(this,e)}emitPluginEvent(e,t){this.grid?._pluginManager?.emitPluginEvent(e,t)}requestRender(){this.grid?.requestRender?.()}requestColumnsRender(){this.grid?.requestColumnsRender?.()}requestRenderWithFocus(){this.grid?.requestRenderWithFocus?.()}requestAfterRender(){this.grid?.requestAfterRender?.()}requestVirtualRefresh(){this.grid?.requestVirtualRefresh?.()}get rows(){return this.grid?.rows??[]}get sourceRows(){return this.grid?.sourceRows??[]}get columns(){return this.grid?.columns??[]}get visibleColumns(){return this.grid?._visibleColumns??[]}get gridElement(){return this.grid?._hostElement}get disconnectSignal(){return this.#e?.signal??this.grid?.disconnectSignal}get gridIcons(){const e=this.grid?.gridConfig?.icons??{};return{...o,...e}}get isAnimationEnabled(){const e=this.grid?.effectiveConfig?.animation?.mode??"reduced-motion";if(!1===e||"off"===e)return!1;if(!0===e||"on"===e)return!0;const t=this.gridElement;if(t){return"0"!==getComputedStyle(t).getPropertyValue("--tbw-animation-enabled").trim()}return!0}get animationDuration(){const e=this.gridElement;if(e){const t=getComputedStyle(e).getPropertyValue("--tbw-animation-duration").trim(),i=parseInt(t,10);if(!isNaN(i))return i}return 200}resolveIcon(e,t){return void 0!==t?t:this.gridIcons[e]}setIcon(e,t){"string"==typeof t?e.innerHTML=t:t instanceof HTMLElement&&(e.innerHTML="",e.appendChild(t.cloneNode(!0)))}warn(i,o){void 0!==o?console.warn(t(i,o,this.gridElement.id,this.name)):console.warn(`${e(this.gridElement.id,this.name)} ${i}`)}throwDiagnostic(e,i){throw new Error(t(e,i,this.gridElement.id,this.name))}}const n={sum:(e,t)=>e.reduce((e,i)=>e+(Number(i[t])||0),0),avg:(e,t)=>{const i=e.reduce((e,i)=>e+(Number(i[t])||0),0);return e.length?i/e.length:0},count:e=>e.length,min:(e,t)=>e.length?Math.min(...e.map(e=>Number(e[t])||1/0)):0,max:(e,t)=>e.length?Math.max(...e.map(e=>Number(e[t])||-1/0)):0,first:(e,t)=>e[0]?.[t],last:(e,t)=>e[e.length-1]?.[t]},a=/* @__PURE__ */new Map,s={register(e,t){a.set(e,t)},unregister(e){a.delete(e)},get(e){if(void 0!==e)return"function"==typeof e?e:a.get(e)??n[e]},run(e,t,i,o){const r=this.get(e);return r?r(t,i,o):void 0},has:e=>a.has(e)||e in n,list:()=>[...Object.keys(n),...a.keys()]},l={sum:e=>e.reduce((e,t)=>e+t,0),avg:e=>e.length?e.reduce((e,t)=>e+t,0)/e.length:0,count:e=>e.length,min:e=>e.length?Math.min(...e):0,max:e=>e.length?Math.max(...e):0,first:e=>e[0]??0,last:e=>e[e.length-1]??0};s.register.bind(s),s.unregister.bind(s),s.get.bind(s),s.run.bind(s),s.list.bind(s);const d=function(e){return l[e]??l.sum};function c(e,t){return[...e,t].join("|")}function p(e,t){const i=t.rowGroupFields??[],o=t.columnGroupFields??[],r=t.valueFields??[],n=function(e,t){if(0===t.length)return["value"];const i=/* @__PURE__ */new Set;for(const o of e){const e=t.map(e=>String(o[e]??"")).join("|");i.add(e)}return[...i].sort()}(e,o),a=g(e,i,o,n,r,0,""),s=function(e,t,i){const o={};function r(e){for(const n of e)if(n.isGroup&&n.children?.length)n.children&&r(n.children);else for(const e of t)for(const t of i){const i=c([e],t.field);o[i]=(o[i]??0)+(n.values[i]??0)}}return r(e),o}(a,n,r);return{rows:a,columnKeys:n,totals:s,grandTotal:Object.values(s).reduce((e,t)=>e+t,0)}}function g(e,t,i,o,r,n,a){const s=[];if(0===t.length){const t=u(e,i,o,r),l=h(t);return s.push({rowKey:a||"all",rowLabel:a||"All",depth:n,values:t,total:l,isGroup:!1,rowCount:e.length}),s}const l=t[0],d=t.slice(1),c=d.length>0,p=function(e,t){const i=/* @__PURE__ */new Map;for(const o of e){const e=String(o[t]??""),r=i.get(e);r?r.push(o):i.set(e,[o])}return i}(e,l);for(const[v,b]of p){const e=a?`${a}|${v}`:v,t=u(b,i,o,r),l=h(t);let p;c&&(p=g(b,d,i,o,r,n+1,e)),s.push({rowKey:e,rowLabel:v||"(blank)",depth:n,values:t,total:l,isGroup:c,children:p,rowCount:b.length})}return s}function u(e,t,i,o){const r={};for(const n of i)for(const i of o){const o=(t.length>0?e.filter(e=>t.map(t=>String(e[t]??"")).join("|")===n):e).map(e=>Number(e[i.field])||0),a=d(i.aggFunc),s=o.length>0?a(o):null;r[c([n],i.field)]=s}return r}function h(e){let t=0;for(const i of Object.values(e))t+=i??0;return t}const v=["sum","avg","count","min","max","first","last"];function b(e,t,i,o){const r=new AbortController,n={config:t,callbacks:o,signal:r.signal},a=document.createElement("div");return a.className="tbw-pivot-panel",a.appendChild(f("Options",()=>function(e,t){const{config:i,callbacks:o,signal:r}=t,n=document.createElement("div");return n.className="tbw-pivot-options",n.appendChild(x("Enable Pivot View",e,e=>{o.onTogglePivot(e)},r)),n.appendChild(x("Show Row Totals",i.showTotals??!0,e=>{o.onOptionChange("showTotals",e)},r)),n.appendChild(x("Show Grand Total",i.showGrandTotal??!0,e=>{o.onOptionChange("showGrandTotal",e)},r)),n}(i,n))),a.appendChild(f("Row Groups",()=>m("rowGroups",n))),a.appendChild(f("Column Groups",()=>m("columnGroups",n))),a.appendChild(f("Values",()=>function(e){const{config:t,callbacks:i,signal:o}=e,r=document.createElement("div");r.className="tbw-pivot-drop-zone tbw-pivot-values-zone",r.setAttribute("data-zone","values");const n=t.valueFields??[];if(0===n.length){const e=document.createElement("div");e.className="tbw-pivot-placeholder",e.textContent="Drag numeric fields here for aggregation",r.appendChild(e)}else for(const a of n)r.appendChild(C(a,e));return r.addEventListener("dragover",e=>{e.preventDefault(),r.classList.add("drag-over")},{signal:o}),r.addEventListener("dragleave",()=>{r.classList.remove("drag-over")},{signal:o}),r.addEventListener("drop",e=>{e.preventDefault(),r.classList.remove("drag-over");const t=e.dataTransfer?.getData("text/plain");t&&i.onAddValueField(t,"sum")},{signal:o}),r}(n))),a.appendChild(f("Available Fields",()=>function(e){const{config:t,callbacks:i,signal:o}=e,r=document.createElement("div");r.className="tbw-pivot-available-fields";const n=i.getAvailableFields(),a=/* @__PURE__ */new Set([...t.rowGroupFields??[],...t.columnGroupFields??[],...t.valueFields?.map(e=>e.field)??[]]),s=n.filter(e=>!a.has(e.field));if(0===s.length){const e=document.createElement("div");e.className="tbw-pivot-placeholder",e.textContent="All fields are in use",r.appendChild(e)}else for(const l of s){const e=document.createElement("div");e.className="tbw-pivot-field-chip available",e.textContent=l.header,e.draggable=!0,e.title=`Drag to add "${l.field}" to a zone`,e.addEventListener("dragstart",t=>{t.dataTransfer?.setData("text/plain",l.field),e.classList.add("dragging")},{signal:o}),e.addEventListener("dragend",()=>{e.classList.remove("dragging")},{signal:o}),r.appendChild(e)}return r}(n))),e.appendChild(a),()=>{r.abort(),a.remove()}}function f(e,t){const i=document.createElement("div");i.className="tbw-pivot-section";const o=document.createElement("div");o.className="tbw-pivot-section-header",o.textContent=e;const r=document.createElement("div");return r.className="tbw-pivot-section-content",r.appendChild(t()),i.appendChild(o),i.appendChild(r),i}function m(e,t){const{config:i,callbacks:o,signal:r}=t,n=document.createElement("div");n.className="tbw-pivot-drop-zone",n.setAttribute("data-zone",e);const a="rowGroups"===e?i.rowGroupFields??[]:i.columnGroupFields??[];if(0===a.length){const e=document.createElement("div");e.className="tbw-pivot-placeholder",e.textContent="Drag fields here or click to add",n.appendChild(e)}else for(const s of a)n.appendChild(w(s,e,t));return n.addEventListener("dragover",e=>{e.preventDefault(),n.classList.add("drag-over")},{signal:r}),n.addEventListener("dragleave",()=>{n.classList.remove("drag-over")},{signal:r}),n.addEventListener("drop",t=>{t.preventDefault(),n.classList.remove("drag-over");const i=t.dataTransfer?.getData("text/plain");i&&o.onAddFieldToZone(i,e)},{signal:r}),n}function w(e,t,i){const{callbacks:o,signal:r}=i,n=document.createElement("div");n.className="tbw-pivot-field-chip",n.draggable=!0;const a=o.getAvailableFields().find(t=>t.field===e),s=document.createElement("span");s.className="tbw-pivot-chip-label",s.textContent=a?.header??e;const l=document.createElement("button");return l.className="tbw-pivot-chip-remove",l.innerHTML="×",l.title="Remove field",l.addEventListener("click",i=>{i.stopPropagation(),o.onRemoveFieldFromZone(e,t)},{signal:r}),n.appendChild(s),n.appendChild(l),n.addEventListener("dragstart",i=>{i.dataTransfer?.setData("text/plain",e),i.dataTransfer?.setData("source-zone",t),n.classList.add("dragging")},{signal:r}),n.addEventListener("dragend",()=>{n.classList.remove("dragging")},{signal:r}),n}function C(e,t){const{callbacks:i,signal:o}=t,r=document.createElement("div");r.className="tbw-pivot-field-chip tbw-pivot-value-chip";const n=i.getAvailableFields().find(t=>t.field===e.field),a=document.createElement("div");a.className="tbw-pivot-value-label-wrapper";const s=document.createElement("span");s.className="tbw-pivot-chip-label",s.textContent=n?.header??e.field;const l=document.createElement("select");l.className="tbw-pivot-agg-select",l.title="Aggregation function";for(const c of v){const t=document.createElement("option");t.value=c,t.textContent=c.toUpperCase(),t.selected=c===e.aggFunc,l.appendChild(t)}l.addEventListener("change",()=>{i.onUpdateValueAggFunc(e.field,l.value)},{signal:o});const d=document.createElement("button");return d.className="tbw-pivot-chip-remove",d.innerHTML="×",d.title="Remove value field",d.addEventListener("click",t=>{t.stopPropagation(),i.onRemoveValueField(e.field)},{signal:o}),a.appendChild(s),a.appendChild(l),r.appendChild(a),r.appendChild(d),r}function x(e,t,i,o){const r=document.createElement("label");r.className="tbw-pivot-checkbox";const n=document.createElement("input");n.type="checkbox",n.checked=t,n.addEventListener("change",()=>i(n.checked),{signal:o});const a=document.createElement("span");return a.textContent=e,r.appendChild(n),r.appendChild(a),r}class y extends r{name="pivot";styles='@layer tbw-plugins{.pivot-group-row{display:grid;grid-template-columns:var(--tbw-column-template);font-weight:600;background:var(--tbw-pivot-group-bg, var(--tbw-color-row-alt));min-height:var(--tbw-row-height);border-bottom:var(--tbw-row-divider)}.pivot-group-row:hover{background:var(--tbw-pivot-group-hover, var(--tbw-color-row-hover))}.pivot-leaf-row{display:grid;grid-template-columns:var(--tbw-column-template);background:var(--tbw-pivot-leaf-bg, var(--tbw-color-bg));min-height:var(--tbw-row-height);border-bottom:var(--tbw-row-divider)}.pivot-grand-total-row{display:grid;grid-template-columns:var(--tbw-column-template);font-weight:700;background:var(--tbw-pivot-grand-total-bg, var(--tbw-color-header-bg));min-height:var(--tbw-row-height);border-top:2px solid var(--tbw-color-border-strong)}.pivot-grand-total-row>.cell{display:flex;align-items:center;padding:var(--tbw-cell-padding);border-right:1px solid var(--tbw-color-border-cell);overflow:hidden;min-width:0}.pivot-grand-total-row>.cell:last-child{border-right:0}.pivot-grand-total-footer{position:sticky;bottom:0;z-index:var(--tbw-z-layer-pinned-rows, 20);background:var(--tbw-pivot-grand-total-bg, var(--tbw-color-header-bg));min-width:fit-content}.pivot-group-row>.cell,.pivot-leaf-row>.cell{display:flex;align-items:center;padding:var(--tbw-cell-padding);border-right:1px solid var(--tbw-color-border-cell);overflow:hidden;min-width:0}.pivot-group-row>.cell:last-child,.pivot-leaf-row>.cell:last-child{border-right:0}.pivot-toggle{display:inline-flex;align-items:center;justify-content:center;width:var(--tbw-pivot-toggle-size, var(--tbw-toggle-size, 1.25em));height:var(--tbw-pivot-toggle-size, var(--tbw-toggle-size, 1.25em));margin-right:var(--tbw-spacing-sm, .375em);border:none;background:transparent;cursor:pointer;color:var(--tbw-pivot-toggle-color, var(--tbw-color-fg-muted));border-radius:var(--tbw-border-radius);transition:background var(--tbw-transition-duration, .12s) var(--tbw-transition-ease, ease),color var(--tbw-transition-duration, .12s) var(--tbw-transition-ease, ease)}.pivot-toggle:hover{background:var(--tbw-pivot-toggle-hover-bg, var(--tbw-color-row-hover));color:var(--tbw-pivot-toggle-hover-color, var(--tbw-color-fg))}.pivot-toggle:focus{outline:var(--tbw-focus-outline);outline-offset:var(--tbw-focus-outline-offset)}.pivot-label{font-weight:inherit}.pivot-count{color:var(--tbw-pivot-count-color, var(--tbw-color-fg-muted));font-size:.9em;font-weight:400}.pivot-total-row{font-weight:700;border-top:2px solid var(--tbw-pivot-border, var(--tbw-color-border-strong))}[data-pivot-depth="1"]{--tbw-pivot-depth: 1}[data-pivot-depth="2"]{--tbw-pivot-depth: 2}[data-pivot-depth="3"]{--tbw-pivot-depth: 3}[data-pivot-depth="4"]{--tbw-pivot-depth: 4}.tbw-pivot-panel{display:flex;flex-direction:column;gap:var(--tbw-panel-padding, var(--tbw-spacing-lg, .75rem));padding:var(--tbw-panel-padding, var(--tbw-spacing-lg, .75rem));height:100%;overflow-y:auto;font-size:var(--tbw-font-size-sm, .8125rem)}.tbw-pivot-section{border:1px solid var(--tbw-pivot-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius);background:var(--tbw-pivot-section-bg, var(--tbw-color-bg))}.tbw-pivot-section-header{padding:var(--tbw-button-padding, var(--tbw-spacing-md, .5rem) var(--tbw-spacing-lg, .75rem));font-weight:600;background:var(--tbw-pivot-header-bg, var(--tbw-color-header-bg));border-bottom:1px solid var(--tbw-pivot-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius) var(--tbw-border-radius) 0 0}.tbw-pivot-section-content{padding:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem))}.tbw-pivot-toggle-wrapper{display:flex;align-items:center}.tbw-pivot-toggle-label{display:flex;align-items:center;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));cursor:pointer}.tbw-pivot-toggle-label input{width:var(--tbw-icon-size, 1rem);height:var(--tbw-icon-size, 1rem);cursor:pointer}.tbw-pivot-drop-zone{min-height:60px;padding:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));border:2px dashed var(--tbw-pivot-drop-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius);background:var(--tbw-pivot-drop-bg, var(--tbw-color-row-alt));display:flex;flex-wrap:wrap;gap:var(--tbw-spacing-sm, .375rem);align-content:flex-start;transition:all .15s ease}.tbw-pivot-drop-zone.drag-over{border-color:var(--tbw-color-accent);background:var(--tbw-pivot-drop-active, var(--tbw-focus-background))}.tbw-pivot-placeholder{color:var(--tbw-color-fg-muted);font-style:italic;padding:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));text-align:center;width:100%}.tbw-pivot-field-chip{display:inline-flex;align-items:center;gap:var(--tbw-spacing-sm, .375rem);padding:var(--tbw-button-padding-sm, var(--tbw-spacing-xs, .25rem) var(--tbw-spacing-md, .5rem));background:var(--tbw-pivot-chip-bg, var(--tbw-color-header-bg));border:1px solid var(--tbw-pivot-chip-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius);cursor:grab;font-size:var(--tbw-font-size-xs, .75rem);transition:all .15s ease}.tbw-pivot-field-chip:hover{background:var(--tbw-pivot-chip-hover, var(--tbw-color-row-hover));border-color:var(--tbw-color-accent)}.tbw-pivot-field-chip.available{background:var(--tbw-color-bg)}.tbw-pivot-field-chip.dragging{opacity:.5;cursor:grabbing}.tbw-pivot-chip-label{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:120px}.tbw-pivot-chip-remove{display:flex;align-items:center;justify-content:center;width:var(--tbw-icon-size, 1rem);height:var(--tbw-icon-size, 1rem);padding:0;border:none;background:transparent;color:var(--tbw-color-fg-muted);font-size:var(--tbw-font-size-sm, .875rem);font-weight:700;cursor:pointer;border-radius:50%;transition:all .15s ease}.tbw-pivot-chip-remove:hover{background:var(--tbw-pivot-chip-remove-hover-bg, var(--tbw-color-accent));color:var(--tbw-pivot-chip-remove-hover-fg, var(--tbw-color-accent-fg))}.tbw-pivot-value-chip{padding:var(--tbw-button-padding-sm, var(--tbw-spacing-xs, .25rem) var(--tbw-spacing-md, .5rem))}.tbw-pivot-value-label-wrapper{display:flex;align-items:center;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));flex:1;min-width:0}.tbw-pivot-agg-select{padding:var(--tbw-spacing-xs, .125rem) var(--tbw-spacing-xs, .25rem);font-size:var(--tbw-font-size-xs, .6875rem);border:1px solid var(--tbw-color-border);border-radius:var(--tbw-border-radius);background:var(--tbw-color-bg);cursor:pointer}.tbw-pivot-available-fields{display:flex;flex-wrap:wrap;gap:var(--tbw-spacing-sm, .375rem);min-height:40px}.tbw-pivot-options{display:flex;flex-direction:column;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem))}.tbw-pivot-checkbox{display:flex;align-items:center;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));cursor:pointer}.tbw-pivot-checkbox input{width:var(--tbw-icon-size-sm, .875rem);height:var(--tbw-icon-size-sm, .875rem);cursor:pointer}.pivot-group-row.tbw-pivot-slide-in,.pivot-leaf-row.tbw-pivot-slide-in{animation:tbw-pivot-slide-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}@keyframes tbw-pivot-slide-in{0%{opacity:0;transform:translate(-8px)}to{opacity:1;transform:translate(0)}}.pivot-group-row.tbw-pivot-fade-in,.pivot-leaf-row.tbw-pivot-fade-in{animation:tbw-pivot-fade-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}@keyframes tbw-pivot-fade-in{0%{opacity:0}to{opacity:1}}}';static PANEL_ID="pivot";get defaultConfig(){return{active:!0,showTotals:!0,showGrandTotal:!0,showToolPanel:!0,animation:"slide"}}isActive=!1;hasInitialized=!1;pivotResult=null;fieldHeaderMap=/* @__PURE__ */new Map;expandedKeys=/* @__PURE__ */new Set;defaultExpanded=!0;userHasToggledExpand=!1;originalColumns=[];panelContainer=null;grandTotalFooter=null;previousVisibleKeys=/* @__PURE__ */new Set;keysToAnimate=/* @__PURE__ */new Set;hasValidPivotConfig(){return(this.config.valueFields?.length??0)>0}get animationStyle(){return!!this.isAnimationEnabled&&(this.config.animation??"slide")}detach(){this.isActive=!1,this.hasInitialized=!1,this.pivotResult=null,this.fieldHeaderMap.clear(),this.originalColumns=[],this.panelContainer=null,this.cleanupGrandTotalFooter(),this.previousVisibleKeys.clear(),this.keysToAnimate.clear(),this.userHasToggledExpand=!1}getToolPanel(){if(!1!==(this.config?.showToolPanel??this.userConfig?.showToolPanel??!0))return{id:y.PANEL_ID,title:"Pivot",icon:"⊞",tooltip:"Configure pivot table",order:90,render:e=>this.renderPanel(e)}}processRows(e){if(!this.hasInitialized&&!1!==this.config.active&&this.hasValidPivotConfig()&&(this.hasInitialized=!0,this.isActive=!0),!this.isActive)return[...e];const t=function(e){const t=[];return e.rowGroupFields?.length||e.columnGroupFields?.length||t.push("At least one row or column group field is required"),e.valueFields?.length||t.push("At least one value field is required"),t}(this.config);if(t.length>0)return this.warn(`Config errors: ${t.join(", ")}`),[...e];this.buildFieldHeaderMap(),this.defaultExpanded=this.config.defaultExpanded??!0,this.pivotResult=p(e,this.config),0===this.expandedKeys.size&&this.defaultExpanded&&!this.userHasToggledExpand&&this.expandAllKeys();const i=this.config.indentWidth??20,o=function(e,t,i=!0){const o=[];function r(e){o.push(e);const n=t?t.has(e.rowKey):i;if(e.children&&n)for(const t of e.children)r(t)}for(const n of e)r(n);return o}(this.pivotResult.rows,this.expandedKeys,this.defaultExpanded).map(e=>({__pivotRowKey:e.rowKey,__pivotLabel:e.rowLabel,__pivotDepth:e.depth,__pivotIsGroup:e.isGroup,__pivotHasChildren:Boolean(e.children?.length),__pivotExpanded:this.expandedKeys.has(e.rowKey),__pivotRowCount:e.rowCount??0,__pivotIndent:e.depth*i,__pivotTotal:e.total,...e.values}));this.keysToAnimate.clear();const r=/* @__PURE__ */new Set;for(const n of o){const e=n.__pivotRowKey;r.add(e),!this.previousVisibleKeys.has(e)&&n.__pivotDepth>0&&this.keysToAnimate.add(e)}return this.previousVisibleKeys=r,o}processColumns(e){if(!this.isActive||!this.pivotResult)return[...e];const t=[],i=(this.config.rowGroupFields??[]).map(e=>this.fieldHeaderMap.get(e)??e).join(" / ");t.push({field:"__pivotLabel",header:i||"Group",width:200});for(const o of this.pivotResult.columnKeys)for(const e of this.config.valueFields??[]){const i=c([o],e.field),r=e.header||this.fieldHeaderMap.get(e.field)||e.field;t.push({field:i,header:`${o} - ${r} (${e.aggFunc})`,width:120,type:"number"})}return this.config.showTotals&&t.push({field:"__pivotTotal",header:"Total",width:100,type:"number"}),t}renderRow(e,t,i){const o=e;return o.__pivotRowKey&&o.__pivotHasChildren?function(e,t,i){return t.className="data-grid-row pivot-group-row",t.setAttribute("data-pivot-depth",String(e.__pivotDepth??0)),t.setAttribute("data-pivot-key",String(e.__pivotRowKey??"")),t.setAttribute("role","row"),t.innerHTML="",i.columns.forEach((o,r)=>{const n=document.createElement("div");if(n.className="cell",n.setAttribute("data-col",String(r)),n.setAttribute("data-row",String(i.rowIndex)),n.setAttribute("role","gridcell"),0===r){const t=Number(e.__pivotIndent)||0;n.style.paddingLeft=`${t}px`;const o=String(e.__pivotRowKey),r=document.createElement("button");r.type="button",r.className="pivot-toggle",r.setAttribute("aria-label",e.__pivotExpanded?"Collapse group":"Expand group"),i.setIcon(r,i.resolveIcon(e.__pivotExpanded?"collapse":"expand")),r.addEventListener("click",e=>{e.stopPropagation(),i.onToggle(o)}),n.appendChild(r);const a=document.createElement("span");a.className="pivot-label",a.textContent=String(e.__pivotLabel??""),n.appendChild(a);const s=document.createElement("span");s.className="pivot-count",s.textContent=` (${Number(e.__pivotRowCount)||0})`,n.appendChild(s)}else{const t=e[o.field];n.textContent=null!=t?String(t):""}t.appendChild(n)}),!0}(o,t,{columns:this.gridColumns,rowIndex:i,onToggle:e=>this.toggle(e),resolveIcon:e=>this.resolveIcon(e),setIcon:(e,t)=>this.setIcon(e,t)}):void 0!==o.__pivotRowKey&&this.isActive?function(e,t,i,o){return t.className="data-grid-row pivot-leaf-row",t.setAttribute("data-pivot-depth",String(e.__pivotDepth??0)),t.setAttribute("data-pivot-key",String(e.__pivotRowKey??"")),t.innerHTML="",i.forEach((i,r)=>{const n=document.createElement("div");if(n.className="cell",n.setAttribute("data-col",String(r)),n.setAttribute("data-row",String(o)),n.setAttribute("role","gridcell"),0===r){const t=Number(e.__pivotIndent)||0;n.style.paddingLeft=`${t+20}px`;const i=document.createElement("span");i.className="pivot-label",i.textContent=String(e.__pivotLabel??""),n.appendChild(i)}else{const t=e[i.field];n.textContent=null!=t?String(t):""}t.appendChild(n)}),!0}(o,t,this.gridColumns,i):(this.cleanupPivotStyling(t),!1)}cleanupPivotStyling(e){(e.classList.contains("pivot-group-row")||e.classList.contains("pivot-leaf-row")||e.classList.contains("pivot-grand-total-row"))&&(e.classList.remove("pivot-group-row","pivot-leaf-row","pivot-grand-total-row"),e.classList.add("data-grid-row"),e.removeAttribute("data-pivot-depth"),e.innerHTML="")}onKeyDown(e){if(" "!==e.key)return;if(!this.isActive)return;const t=this.grid._focusRow,i=this.rows[t];return i?.__pivotIsGroup&&i.__pivotHasChildren?(e.preventDefault(),this.toggle(i.__pivotRowKey),this.requestRenderWithFocus(),!0):void 0}afterRender(){this.isActive&&this.config.showGrandTotal&&this.pivotResult?this.renderGrandTotalFooter():this.cleanupGrandTotalFooter();const e=this.animationStyle;if(!1===e||0===this.keysToAnimate.size)return;const t=this.gridElement?.querySelector(".rows");if(!t)return;const i="fade"===e?"tbw-pivot-fade-in":"tbw-pivot-slide-in";for(const o of t.querySelectorAll(".pivot-group-row, .pivot-leaf-row")){const e=o.dataset.pivotKey;e&&this.keysToAnimate.has(e)&&(o.classList.add(i),o.addEventListener("animationend",()=>o.classList.remove(i),{once:!0}))}this.keysToAnimate.clear()}renderGrandTotalFooter(){if(!this.pivotResult)return;const e=this.gridElement;if(!e)return;const t=e.querySelector(".tbw-scroll-area")??e.querySelector(".tbw-grid-content")??e.children[0];if(!t)return;this.grandTotalFooter||(this.grandTotalFooter=document.createElement("div"),this.grandTotalFooter.className="pivot-grand-total-footer",t.appendChild(this.grandTotalFooter));const i={__pivotRowKey:"__grandTotal",__pivotLabel:"Grand Total",__pivotIsGrandTotal:!0,__pivotTotal:this.pivotResult.grandTotal,...this.pivotResult.totals};var o,r,n;o=i,r=this.grandTotalFooter,n=this.gridColumns,r.className="pivot-grand-total-row",r.setAttribute("role","presentation"),r.innerHTML="",n.forEach((e,t)=>{const i=document.createElement("div");if(i.className="cell",i.setAttribute("data-col",String(t)),0===t){const e=document.createElement("span");e.className="pivot-label",e.textContent="Grand Total",i.appendChild(e)}else{const t=o[e.field];i.textContent=null!=t?String(t):""}r.appendChild(i)})}cleanupGrandTotalFooter(){this.grandTotalFooter&&(this.grandTotalFooter.remove(),this.grandTotalFooter=null)}toggle(e){this.userHasToggledExpand=!0,this.expandedKeys.has(e)?this.expandedKeys.delete(e):this.expandedKeys.add(e),this.requestRender()}expand(e){this.userHasToggledExpand=!0,this.expandedKeys.add(e),this.requestRender()}collapse(e){this.userHasToggledExpand=!0,this.expandedKeys.delete(e),this.requestRender()}expandAll(){this.userHasToggledExpand=!0,this.expandAllKeys(),this.requestRender()}collapseAll(){this.userHasToggledExpand=!0,this.expandedKeys.clear(),this.requestRender()}expandAllKeys(){if(!this.pivotResult)return;const e=function(e){const t=[];function i(e){if(e.isGroup&&t.push(e.rowKey),e.children)for(const t of e.children)i(t)}for(const o of e)i(o);return t}(this.pivotResult.rows);for(const t of e)this.expandedKeys.add(t)}isExpanded(e){return this.expandedKeys.has(e)}enablePivot(){0===this.originalColumns.length&&this.captureOriginalColumns(),this.isActive=!0,this.requestRender()}disablePivot(){this.isActive=!1,this.pivotResult=null,this.requestRender()}isPivotActive(){return this.isActive}getPivotResult(){return this.pivotResult}setRowGroupFields(e){this.config.rowGroupFields=e,this.requestRender()}setColumnGroupFields(e){this.config.columnGroupFields=e,this.requestRender()}setValueFields(e){this.config.valueFields=e,this.requestRender()}refresh(){this.pivotResult=null,this.requestRender()}showPanel(){this.grid.openToolPanel(),this.grid.expandedToolPanelSections.includes(y.PANEL_ID)||this.grid.toggleToolPanelSection(y.PANEL_ID)}hidePanel(){this.grid.closeToolPanel()}togglePanel(){this.grid.isToolPanelOpen||this.grid.openToolPanel(),this.grid.toggleToolPanelSection(y.PANEL_ID)}isPanelVisible(){return this.grid.isToolPanelOpen&&this.grid.expandedToolPanelSections.includes(y.PANEL_ID)}get gridColumns(){return this.grid.columns??[]}refreshIfActive(){this.isActive&&this.refresh(),this.refreshPanel()}buildFieldHeaderMap(){const e=this.getAvailableFields();this.fieldHeaderMap.clear();for(const t of e)this.fieldHeaderMap.set(t.field,t.header)}getAvailableFields(){return this.originalColumns.length>0?this.originalColumns:this.captureOriginalColumns()}captureOriginalColumns(){try{const e=this.grid.getAllColumns?.()??this.grid.columns??[];return this.originalColumns=e.filter(e=>!e.field.startsWith("__pivot")).map(e=>({field:e.field,header:e.header??e.field})),this.originalColumns}catch{return[]}}renderPanel(e){this.panelContainer=e,0===this.originalColumns.length&&this.captureOriginalColumns();const t={onTogglePivot:e=>{e?this.enablePivot():this.disablePivot(),this.refreshPanel()},onAddFieldToZone:(e,t)=>this.addFieldToZone(e,t),onRemoveFieldFromZone:(e,t)=>this.removeFieldFromZone(e,t),onAddValueField:(e,t)=>this.addValueField(e,t),onRemoveValueField:e=>this.removeValueField(e),onUpdateValueAggFunc:(e,t)=>this.updateValueAggFunc(e,t),onOptionChange:(e,t)=>{this.config[e]=t,this.isActive&&this.refresh()},getAvailableFields:()=>this.getAvailableFields()};return b(e,this.config,this.isActive,t)}refreshPanel(){this.panelContainer&&(this.panelContainer.innerHTML="",this.renderPanel(this.panelContainer))}addFieldToZone(e,t){if("rowGroups"===t){const t=this.config.rowGroupFields??[];t.includes(e)||(this.config.rowGroupFields=[...t,e])}else{const t=this.config.columnGroupFields??[];t.includes(e)||(this.config.columnGroupFields=[...t,e])}this.removeFromOtherZones(e,t),this.refreshIfActive()}removeFieldFromZone(e,t){"rowGroups"===t?this.config.rowGroupFields=(this.config.rowGroupFields??[]).filter(t=>t!==e):this.config.columnGroupFields=(this.config.columnGroupFields??[]).filter(t=>t!==e),this.refreshIfActive()}removeFromOtherZones(e,t){"rowGroups"!==t&&(this.config.rowGroupFields=(this.config.rowGroupFields??[]).filter(t=>t!==e)),"columnGroups"!==t&&(this.config.columnGroupFields=(this.config.columnGroupFields??[]).filter(t=>t!==e)),"values"!==t&&(this.config.valueFields=(this.config.valueFields??[]).filter(t=>t.field!==e))}addValueField(e,t){const i=this.config.valueFields??[];i.some(t=>t.field===e)||(this.config.valueFields=[...i,{field:e,aggFunc:t}]),this.removeFromOtherZones(e,"values"),this.refreshIfActive()}removeValueField(e){this.config.valueFields=(this.config.valueFields??[]).filter(t=>t.field!==e),this.refreshIfActive()}updateValueAggFunc(e,t){const i=this.config.valueFields??[],o=i.findIndex(t=>t.field===e);o>=0&&(i[o]={...i[o],aggFunc:t},this.config.valueFields=[...i]),this.isActive&&this.refresh()}}export{y as PivotPlugin};
1
+ function e(e,t){return`[tbw-grid${e?`#${e}`:""}${t?`:${t}`:""}]`}function t(t,i,o,r){return`${e(o,r)} ${t}: ${i}\n\n → More info: ${function(e){return`https://toolboxjs.com/grid/errors#${e.toLowerCase()}`}(t)}`}const i='<svg viewBox="0 0 16 16" width="12" height="12"><path fill="currentColor" d="M6 10.5a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/></svg>',o={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:i,filterActive:i,print:"🖨️"};class r{static dependencies;static manifest;aliases;version="undefined"!=typeof __GRID_VERSION__?__GRID_VERSION__:"dev";styles;cellRenderers;headerRenderers;cellEditors;grid;config;userConfig;#e;get defaultConfig(){return{}}constructor(e={}){this.userConfig=e}attach(e){this.#e?.abort(),this.#e=new AbortController,this.grid=e,this.config={...this.defaultConfig,...this.userConfig}}detach(){this.#e?.abort(),this.#e=void 0}getPlugin(e){return this.grid?.getPlugin(e)}emit(e,t){this.grid?.dispatchEvent?.(new CustomEvent(e,{detail:t,bubbles:!0}))}emitCancelable(e,t){const i=new CustomEvent(e,{detail:t,bubbles:!0,cancelable:!0});return this.grid?.dispatchEvent?.(i),i.defaultPrevented}on(e,t){this.grid?._pluginManager?.subscribe(this,e,t)}off(e){this.grid?._pluginManager?.unsubscribe(this,e)}emitPluginEvent(e,t){this.grid?._pluginManager?.emitPluginEvent(e,t)}requestRender(){this.grid?.requestRender?.()}requestColumnsRender(){this.grid?.requestColumnsRender?.()}requestRenderWithFocus(){this.grid?.requestRenderWithFocus?.()}requestAfterRender(){this.grid?.requestAfterRender?.()}requestVirtualRefresh(){this.grid?.requestVirtualRefresh?.()}get rows(){return this.grid?.rows??[]}get sourceRows(){return this.grid?.sourceRows??[]}get columns(){return this.grid?.columns??[]}get visibleColumns(){return this.grid?._visibleColumns??[]}get gridElement(){return this.grid?._hostElement}get disconnectSignal(){return this.#e?.signal??this.grid?.disconnectSignal}get gridIcons(){const e=this.grid?.gridConfig?.icons??{};return{...o,...e}}get isAnimationEnabled(){const e=this.grid?.effectiveConfig?.animation?.mode??"reduced-motion";if(!1===e||"off"===e)return!1;if(!0===e||"on"===e)return!0;const t=this.gridElement;if(t){return"0"!==getComputedStyle(t).getPropertyValue("--tbw-animation-enabled").trim()}return!0}get animationDuration(){const e=this.gridElement;if(e){const t=getComputedStyle(e).getPropertyValue("--tbw-animation-duration").trim(),i=parseInt(t,10);if(!isNaN(i))return i}return 200}resolveIcon(e,t){return void 0!==t?t:this.gridIcons[e]}setIcon(e,t){"string"==typeof t?e.innerHTML=t:t instanceof HTMLElement&&(e.innerHTML="",e.appendChild(t.cloneNode(!0)))}warn(i,o){void 0!==o?console.warn(t(i,o,this.gridElement.id,this.name)):console.warn(`${e(this.gridElement.id,this.name)} ${i}`)}throwDiagnostic(e,i){throw new Error(t(e,i,this.gridElement.id,this.name))}}const n={sum:(e,t)=>e.reduce((e,i)=>e+(Number(i[t])||0),0),avg:(e,t)=>{const i=e.reduce((e,i)=>e+(Number(i[t])||0),0);return e.length?i/e.length:0},count:e=>e.length,min:(e,t)=>e.length?Math.min(...e.map(e=>Number(e[t])||1/0)):0,max:(e,t)=>e.length?Math.max(...e.map(e=>Number(e[t])||-1/0)):0,first:(e,t)=>e[0]?.[t],last:(e,t)=>e[e.length-1]?.[t]},a=/* @__PURE__ */new Map,s={register(e,t){a.set(e,t)},unregister(e){a.delete(e)},get(e){if(void 0!==e)return"function"==typeof e?e:a.get(e)??n[e]},run(e,t,i,o){const r=this.get(e);return r?r(t,i,o):void 0},has:e=>a.has(e)||e in n,list:()=>[...Object.keys(n),...a.keys()]},l={sum:e=>e.reduce((e,t)=>e+t,0),avg:e=>e.length?e.reduce((e,t)=>e+t,0)/e.length:0,count:e=>e.length,min:e=>e.length?Math.min(...e):0,max:e=>e.length?Math.max(...e):0,first:e=>e[0]??0,last:e=>e[e.length-1]??0};s.register.bind(s),s.unregister.bind(s),s.get.bind(s),s.run.bind(s),s.list.bind(s);const d=function(e){return l[e]??l.sum};function c(e,t){return[...e,t].join("|")}function p(e,t){const i=t.rowGroupFields??[],o=t.columnGroupFields??[],r=t.valueFields??[],n=function(e,t){if(0===t.length)return["value"];const i=/* @__PURE__ */new Set;for(const o of e){const e=t.map(e=>String(o[e]??"")).join("|");i.add(e)}return[...i].sort()}(e,o),a=g(e,i,o,n,r,0,""),s=function(e,t,i){const o={};function r(e){for(const n of e)if(n.isGroup&&n.children?.length)n.children&&r(n.children);else for(const e of t)for(const t of i){const i=c([e],t.field);o[i]=(o[i]??0)+(n.values[i]??0)}}return r(e),o}(a,n,r);return{rows:a,columnKeys:n,totals:s,grandTotal:Object.values(s).reduce((e,t)=>e+t,0)}}function g(e,t,i,o,r,n,a){const s=[];if(0===t.length){const t=u(e,i,o,r),l=h(t);return s.push({rowKey:a||"all",rowLabel:a||"All",depth:n,values:t,total:l,isGroup:!1,rowCount:e.length}),s}const l=t[0],d=t.slice(1),c=d.length>0,p=function(e,t){const i=/* @__PURE__ */new Map;for(const o of e){const e=String(o[t]??""),r=i.get(e);r?r.push(o):i.set(e,[o])}return i}(e,l);for(const[v,b]of p){const e=a?`${a}|${v}`:v,t=u(b,i,o,r),l=h(t);let p;c&&(p=g(b,d,i,o,r,n+1,e)),s.push({rowKey:e,rowLabel:v||"(blank)",depth:n,values:t,total:l,isGroup:c,children:p,rowCount:b.length})}return s}function u(e,t,i,o){const r={};for(const n of i)for(const i of o){const o=(t.length>0?e.filter(e=>t.map(t=>String(e[t]??"")).join("|")===n):e).map(e=>Number(e[i.field])||0),a=d(i.aggFunc),s=o.length>0?a(o):null;r[c([n],i.field)]=s}return r}function h(e){let t=0;for(const i of Object.values(e))t+=i??0;return t}const v=["sum","avg","count","min","max","first","last"];function b(e,t,i,o){const r=new AbortController,n={config:t,callbacks:o,signal:r.signal},a=document.createElement("div");return a.className="tbw-pivot-panel",a.appendChild(f("Options",()=>function(e,t){const{config:i,callbacks:o,signal:r}=t,n=document.createElement("div");return n.className="tbw-pivot-options",n.appendChild(x("Enable Pivot View",e,e=>{o.onTogglePivot(e)},r)),n.appendChild(x("Show Row Totals",i.showTotals??!0,e=>{o.onOptionChange("showTotals",e)},r)),n.appendChild(x("Show Grand Total",i.showGrandTotal??!0,e=>{o.onOptionChange("showGrandTotal",e)},r)),n}(i,n))),a.appendChild(f("Row Groups",()=>w("rowGroups",n))),a.appendChild(f("Column Groups",()=>w("columnGroups",n))),a.appendChild(f("Values",()=>function(e){const{config:t,callbacks:i,signal:o}=e,r=document.createElement("div");r.className="tbw-pivot-drop-zone tbw-pivot-values-zone",r.setAttribute("data-zone","values");const n=t.valueFields??[];if(0===n.length){const e=document.createElement("div");e.className="tbw-pivot-placeholder",e.textContent="Drag numeric fields here for aggregation",r.appendChild(e)}else for(const a of n)r.appendChild(C(a,e));return r.addEventListener("dragover",e=>{e.preventDefault(),r.classList.add("drag-over")},{signal:o}),r.addEventListener("dragleave",()=>{r.classList.remove("drag-over")},{signal:o}),r.addEventListener("drop",e=>{e.preventDefault(),r.classList.remove("drag-over");const t=e.dataTransfer?.getData("text/plain");t&&i.onAddValueField(t,"sum")},{signal:o}),r}(n))),a.appendChild(f("Available Fields",()=>function(e){const{config:t,callbacks:i,signal:o}=e,r=document.createElement("div");r.className="tbw-pivot-available-fields";const n=i.getAvailableFields(),a=/* @__PURE__ */new Set([...t.rowGroupFields??[],...t.columnGroupFields??[],...t.valueFields?.map(e=>e.field)??[]]),s=n.filter(e=>!a.has(e.field));if(0===s.length){const e=document.createElement("div");e.className="tbw-pivot-placeholder",e.textContent="All fields are in use",r.appendChild(e)}else for(const l of s){const e=document.createElement("div");e.className="tbw-pivot-field-chip available",e.textContent=l.header,e.draggable=!0,e.title=`Drag to add "${l.field}" to a zone`,e.addEventListener("dragstart",t=>{t.dataTransfer?.setData("text/plain",l.field),e.classList.add("dragging")},{signal:o}),e.addEventListener("dragend",()=>{e.classList.remove("dragging")},{signal:o}),r.appendChild(e)}return r}(n))),e.appendChild(a),()=>{r.abort(),a.remove()}}function f(e,t){const i=document.createElement("div");i.className="tbw-pivot-section";const o=document.createElement("div");o.className="tbw-pivot-section-header",o.textContent=e;const r=document.createElement("div");return r.className="tbw-pivot-section-content",r.appendChild(t()),i.appendChild(o),i.appendChild(r),i}function w(e,t){const{config:i,callbacks:o,signal:r}=t,n=document.createElement("div");n.className="tbw-pivot-drop-zone",n.setAttribute("data-zone",e);const a="rowGroups"===e?i.rowGroupFields??[]:i.columnGroupFields??[];if(0===a.length){const e=document.createElement("div");e.className="tbw-pivot-placeholder",e.textContent="Drag fields here or click to add",n.appendChild(e)}else for(const s of a)n.appendChild(m(s,e,t));return n.addEventListener("dragover",e=>{e.preventDefault(),n.classList.add("drag-over")},{signal:r}),n.addEventListener("dragleave",()=>{n.classList.remove("drag-over")},{signal:r}),n.addEventListener("drop",t=>{t.preventDefault(),n.classList.remove("drag-over");const i=t.dataTransfer?.getData("text/plain");i&&o.onAddFieldToZone(i,e)},{signal:r}),n}function m(e,t,i){const{callbacks:o,signal:r}=i,n=document.createElement("div");n.className="tbw-pivot-field-chip",n.draggable=!0;const a=o.getAvailableFields().find(t=>t.field===e),s=document.createElement("span");s.className="tbw-pivot-chip-label",s.textContent=a?.header??e;const l=document.createElement("button");return l.className="tbw-pivot-chip-remove",l.innerHTML="×",l.title="Remove field",l.addEventListener("click",i=>{i.stopPropagation(),o.onRemoveFieldFromZone(e,t)},{signal:r}),n.appendChild(s),n.appendChild(l),n.addEventListener("dragstart",i=>{i.dataTransfer?.setData("text/plain",e),i.dataTransfer?.setData("source-zone",t),n.classList.add("dragging")},{signal:r}),n.addEventListener("dragend",()=>{n.classList.remove("dragging")},{signal:r}),n}function C(e,t){const{callbacks:i,signal:o}=t,r=document.createElement("div");r.className="tbw-pivot-field-chip tbw-pivot-value-chip";const n=i.getAvailableFields().find(t=>t.field===e.field),a=document.createElement("div");a.className="tbw-pivot-value-label-wrapper";const s=document.createElement("span");s.className="tbw-pivot-chip-label",s.textContent=n?.header??e.field;const l=document.createElement("select");l.className="tbw-pivot-agg-select",l.title="Aggregation function";for(const c of v){const t=document.createElement("option");t.value=c,t.textContent=c.toUpperCase(),t.selected=c===e.aggFunc,l.appendChild(t)}l.addEventListener("change",()=>{i.onUpdateValueAggFunc(e.field,l.value)},{signal:o});const d=document.createElement("button");return d.className="tbw-pivot-chip-remove",d.innerHTML="×",d.title="Remove value field",d.addEventListener("click",t=>{t.stopPropagation(),i.onRemoveValueField(e.field)},{signal:o}),a.appendChild(s),a.appendChild(l),r.appendChild(a),r.appendChild(d),r}function x(e,t,i,o){const r=document.createElement("label");r.className="tbw-pivot-checkbox";const n=document.createElement("input");n.type="checkbox",n.checked=t,n.addEventListener("change",()=>i(n.checked),{signal:o});const a=document.createElement("span");return a.textContent=e,r.appendChild(n),r.appendChild(a),r}class y extends r{static manifest={incompatibleWith:[{name:"groupingRows",reason:"PivotPlugin creates its own aggregated row and column structure. Row grouping cannot be applied on top of pivot-generated rows."},{name:"tree",reason:"PivotPlugin replaces the entire row and column structure with aggregated pivot data. Tree hierarchy cannot coexist with pivot aggregation."},{name:"serverSide",reason:"PivotPlugin requires the full dataset to compute aggregations. ServerSidePlugin lazy-loads rows in blocks, so pivot aggregation cannot be performed client-side."}]};name="pivot";styles='@layer tbw-plugins{.pivot-group-row{display:grid;grid-template-columns:var(--tbw-column-template);font-weight:600;background:var(--tbw-pivot-group-bg, var(--tbw-color-row-alt));min-height:var(--tbw-row-height);border-bottom:var(--tbw-row-divider)}.pivot-group-row:hover{background:var(--tbw-pivot-group-hover, var(--tbw-color-row-hover))}.pivot-leaf-row{display:grid;grid-template-columns:var(--tbw-column-template);background:var(--tbw-pivot-leaf-bg, var(--tbw-color-bg));min-height:var(--tbw-row-height);border-bottom:var(--tbw-row-divider)}.pivot-grand-total-row{display:grid;grid-template-columns:var(--tbw-column-template);font-weight:700;background:var(--tbw-pivot-grand-total-bg, var(--tbw-color-header-bg));min-height:var(--tbw-row-height);border-top:2px solid var(--tbw-color-border-strong)}.pivot-grand-total-row>.cell{display:flex;align-items:center;padding:var(--tbw-cell-padding);border-right:1px solid var(--tbw-color-border-cell);overflow:hidden;min-width:0}.pivot-grand-total-row>.cell:last-child{border-right:0}.pivot-grand-total-footer{position:sticky;bottom:0;z-index:var(--tbw-z-layer-pinned-rows, 20);background:var(--tbw-pivot-grand-total-bg, var(--tbw-color-header-bg));min-width:fit-content}.pivot-group-row>.cell,.pivot-leaf-row>.cell{display:flex;align-items:center;padding:var(--tbw-cell-padding);border-right:1px solid var(--tbw-color-border-cell);overflow:hidden;min-width:0}.pivot-group-row>.cell:last-child,.pivot-leaf-row>.cell:last-child{border-right:0}.pivot-toggle{display:inline-flex;align-items:center;justify-content:center;width:var(--tbw-pivot-toggle-size, var(--tbw-toggle-size, 1.25em));height:var(--tbw-pivot-toggle-size, var(--tbw-toggle-size, 1.25em));margin-right:var(--tbw-spacing-sm, .375em);border:none;background:transparent;cursor:pointer;color:var(--tbw-pivot-toggle-color, var(--tbw-color-fg-muted));border-radius:var(--tbw-border-radius);transition:background var(--tbw-transition-duration, .12s) var(--tbw-transition-ease, ease),color var(--tbw-transition-duration, .12s) var(--tbw-transition-ease, ease)}.pivot-toggle:hover{background:var(--tbw-pivot-toggle-hover-bg, var(--tbw-color-row-hover));color:var(--tbw-pivot-toggle-hover-color, var(--tbw-color-fg))}.pivot-toggle:focus{outline:var(--tbw-focus-outline);outline-offset:var(--tbw-focus-outline-offset)}.pivot-label{font-weight:inherit}.pivot-count{color:var(--tbw-pivot-count-color, var(--tbw-color-fg-muted));font-size:.9em;font-weight:400}.pivot-total-row{font-weight:700;border-top:2px solid var(--tbw-pivot-border, var(--tbw-color-border-strong))}[data-pivot-depth="1"]{--tbw-pivot-depth: 1}[data-pivot-depth="2"]{--tbw-pivot-depth: 2}[data-pivot-depth="3"]{--tbw-pivot-depth: 3}[data-pivot-depth="4"]{--tbw-pivot-depth: 4}.tbw-pivot-panel{display:flex;flex-direction:column;gap:var(--tbw-panel-padding, var(--tbw-spacing-lg, .75rem));padding:var(--tbw-panel-padding, var(--tbw-spacing-lg, .75rem));height:100%;overflow-y:auto;font-size:var(--tbw-font-size-sm, .8125rem)}.tbw-pivot-section{border:1px solid var(--tbw-pivot-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius);background:var(--tbw-pivot-section-bg, var(--tbw-color-bg))}.tbw-pivot-section-header{padding:var(--tbw-button-padding, var(--tbw-spacing-md, .5rem) var(--tbw-spacing-lg, .75rem));font-weight:600;background:var(--tbw-pivot-header-bg, var(--tbw-color-header-bg));border-bottom:1px solid var(--tbw-pivot-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius) var(--tbw-border-radius) 0 0}.tbw-pivot-section-content{padding:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem))}.tbw-pivot-toggle-wrapper{display:flex;align-items:center}.tbw-pivot-toggle-label{display:flex;align-items:center;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));cursor:pointer}.tbw-pivot-toggle-label input{width:var(--tbw-icon-size, 1rem);height:var(--tbw-icon-size, 1rem);cursor:pointer}.tbw-pivot-drop-zone{min-height:60px;padding:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));border:2px dashed var(--tbw-pivot-drop-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius);background:var(--tbw-pivot-drop-bg, var(--tbw-color-row-alt));display:flex;flex-wrap:wrap;gap:var(--tbw-spacing-sm, .375rem);align-content:flex-start;transition:all .15s ease}.tbw-pivot-drop-zone.drag-over{border-color:var(--tbw-color-accent);background:var(--tbw-pivot-drop-active, var(--tbw-focus-background))}.tbw-pivot-placeholder{color:var(--tbw-color-fg-muted);font-style:italic;padding:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));text-align:center;width:100%}.tbw-pivot-field-chip{display:inline-flex;align-items:center;gap:var(--tbw-spacing-sm, .375rem);padding:var(--tbw-button-padding-sm, var(--tbw-spacing-xs, .25rem) var(--tbw-spacing-md, .5rem));background:var(--tbw-pivot-chip-bg, var(--tbw-color-header-bg));border:1px solid var(--tbw-pivot-chip-border, var(--tbw-color-border));border-radius:var(--tbw-border-radius);cursor:grab;font-size:var(--tbw-font-size-xs, .75rem);transition:all .15s ease}.tbw-pivot-field-chip:hover{background:var(--tbw-pivot-chip-hover, var(--tbw-color-row-hover));border-color:var(--tbw-color-accent)}.tbw-pivot-field-chip.available{background:var(--tbw-color-bg)}.tbw-pivot-field-chip.dragging{opacity:.5;cursor:grabbing}.tbw-pivot-chip-label{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:120px}.tbw-pivot-chip-remove{display:flex;align-items:center;justify-content:center;width:var(--tbw-icon-size, 1rem);height:var(--tbw-icon-size, 1rem);padding:0;border:none;background:transparent;color:var(--tbw-color-fg-muted);font-size:var(--tbw-font-size-sm, .875rem);font-weight:700;cursor:pointer;border-radius:50%;transition:all .15s ease}.tbw-pivot-chip-remove:hover{background:var(--tbw-pivot-chip-remove-hover-bg, var(--tbw-color-accent));color:var(--tbw-pivot-chip-remove-hover-fg, var(--tbw-color-accent-fg))}.tbw-pivot-value-chip{padding:var(--tbw-button-padding-sm, var(--tbw-spacing-xs, .25rem) var(--tbw-spacing-md, .5rem))}.tbw-pivot-value-label-wrapper{display:flex;align-items:center;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));flex:1;min-width:0}.tbw-pivot-agg-select{padding:var(--tbw-spacing-xs, .125rem) var(--tbw-spacing-xs, .25rem);font-size:var(--tbw-font-size-xs, .6875rem);border:1px solid var(--tbw-color-border);border-radius:var(--tbw-border-radius);background:var(--tbw-color-bg);cursor:pointer}.tbw-pivot-available-fields{display:flex;flex-wrap:wrap;gap:var(--tbw-spacing-sm, .375rem);min-height:40px}.tbw-pivot-options{display:flex;flex-direction:column;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem))}.tbw-pivot-checkbox{display:flex;align-items:center;gap:var(--tbw-panel-gap, var(--tbw-spacing-md, .5rem));cursor:pointer}.tbw-pivot-checkbox input{width:var(--tbw-icon-size-sm, .875rem);height:var(--tbw-icon-size-sm, .875rem);cursor:pointer}.pivot-group-row.tbw-pivot-slide-in,.pivot-leaf-row.tbw-pivot-slide-in{animation:tbw-pivot-slide-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}@keyframes tbw-pivot-slide-in{0%{opacity:0;transform:translate(-8px)}to{opacity:1;transform:translate(0)}}.pivot-group-row.tbw-pivot-fade-in,.pivot-leaf-row.tbw-pivot-fade-in{animation:tbw-pivot-fade-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}@keyframes tbw-pivot-fade-in{0%{opacity:0}to{opacity:1}}}';static PANEL_ID="pivot";get defaultConfig(){return{active:!0,showTotals:!0,showGrandTotal:!0,showToolPanel:!0,animation:"slide"}}isActive=!1;hasInitialized=!1;pivotResult=null;fieldHeaderMap=/* @__PURE__ */new Map;expandedKeys=/* @__PURE__ */new Set;defaultExpanded=!0;userHasToggledExpand=!1;originalColumns=[];panelContainer=null;grandTotalFooter=null;previousVisibleKeys=/* @__PURE__ */new Set;keysToAnimate=/* @__PURE__ */new Set;hasValidPivotConfig(){return(this.config.valueFields?.length??0)>0}get animationStyle(){return!!this.isAnimationEnabled&&(this.config.animation??"slide")}detach(){this.isActive=!1,this.hasInitialized=!1,this.pivotResult=null,this.fieldHeaderMap.clear(),this.originalColumns=[],this.panelContainer=null,this.cleanupGrandTotalFooter(),this.previousVisibleKeys.clear(),this.keysToAnimate.clear(),this.userHasToggledExpand=!1}getToolPanel(){if(!1!==(this.config?.showToolPanel??this.userConfig?.showToolPanel??!0))return{id:y.PANEL_ID,title:"Pivot",icon:"⊞",tooltip:"Configure pivot table",order:90,render:e=>this.renderPanel(e)}}processRows(e){if(!this.hasInitialized&&!1!==this.config.active&&this.hasValidPivotConfig()&&(this.hasInitialized=!0,this.isActive=!0),!this.isActive)return[...e];const t=function(e){const t=[];return e.rowGroupFields?.length||e.columnGroupFields?.length||t.push("At least one row or column group field is required"),e.valueFields?.length||t.push("At least one value field is required"),t}(this.config);if(t.length>0)return this.warn(`Config errors: ${t.join(", ")}`),[...e];this.buildFieldHeaderMap(),this.defaultExpanded=this.config.defaultExpanded??!0,this.pivotResult=p(e,this.config),0===this.expandedKeys.size&&this.defaultExpanded&&!this.userHasToggledExpand&&this.expandAllKeys();const i=this.config.indentWidth??20,o=function(e,t,i=!0){const o=[];function r(e){o.push(e);const n=t?t.has(e.rowKey):i;if(e.children&&n)for(const t of e.children)r(t)}for(const n of e)r(n);return o}(this.pivotResult.rows,this.expandedKeys,this.defaultExpanded).map(e=>({__pivotRowKey:e.rowKey,__pivotLabel:e.rowLabel,__pivotDepth:e.depth,__pivotIsGroup:e.isGroup,__pivotHasChildren:Boolean(e.children?.length),__pivotExpanded:this.expandedKeys.has(e.rowKey),__pivotRowCount:e.rowCount??0,__pivotIndent:e.depth*i,__pivotTotal:e.total,...e.values}));this.keysToAnimate.clear();const r=/* @__PURE__ */new Set;for(const n of o){const e=n.__pivotRowKey;r.add(e),!this.previousVisibleKeys.has(e)&&n.__pivotDepth>0&&this.keysToAnimate.add(e)}return this.previousVisibleKeys=r,o}processColumns(e){if(!this.isActive||!this.pivotResult)return[...e];const t=[],i=(this.config.rowGroupFields??[]).map(e=>this.fieldHeaderMap.get(e)??e).join(" / ");t.push({field:"__pivotLabel",header:i||"Group",width:200});for(const o of this.pivotResult.columnKeys)for(const e of this.config.valueFields??[]){const i=c([o],e.field),r=e.header||this.fieldHeaderMap.get(e.field)||e.field;t.push({field:i,header:`${o} - ${r} (${e.aggFunc})`,width:120,type:"number"})}return this.config.showTotals&&t.push({field:"__pivotTotal",header:"Total",width:100,type:"number"}),t}renderRow(e,t,i){const o=e;return o.__pivotRowKey&&o.__pivotHasChildren?function(e,t,i){return t.className="data-grid-row pivot-group-row",t.setAttribute("data-pivot-depth",String(e.__pivotDepth??0)),t.setAttribute("data-pivot-key",String(e.__pivotRowKey??"")),t.setAttribute("role","row"),t.innerHTML="",i.columns.forEach((o,r)=>{const n=document.createElement("div");if(n.className="cell",n.setAttribute("data-col",String(r)),n.setAttribute("data-row",String(i.rowIndex)),n.setAttribute("role","gridcell"),0===r){const t=Number(e.__pivotIndent)||0;n.style.paddingLeft=`${t}px`;const o=String(e.__pivotRowKey),r=document.createElement("button");r.type="button",r.className="pivot-toggle",r.setAttribute("aria-label",e.__pivotExpanded?"Collapse group":"Expand group"),i.setIcon(r,i.resolveIcon(e.__pivotExpanded?"collapse":"expand")),r.addEventListener("click",e=>{e.stopPropagation(),i.onToggle(o)}),n.appendChild(r);const a=document.createElement("span");a.className="pivot-label",a.textContent=String(e.__pivotLabel??""),n.appendChild(a);const s=document.createElement("span");s.className="pivot-count",s.textContent=` (${Number(e.__pivotRowCount)||0})`,n.appendChild(s)}else{const t=e[o.field];n.textContent=null!=t?String(t):""}t.appendChild(n)}),!0}(o,t,{columns:this.gridColumns,rowIndex:i,onToggle:e=>this.toggle(e),resolveIcon:e=>this.resolveIcon(e),setIcon:(e,t)=>this.setIcon(e,t)}):void 0!==o.__pivotRowKey&&this.isActive?function(e,t,i,o){return t.className="data-grid-row pivot-leaf-row",t.setAttribute("data-pivot-depth",String(e.__pivotDepth??0)),t.setAttribute("data-pivot-key",String(e.__pivotRowKey??"")),t.innerHTML="",i.forEach((i,r)=>{const n=document.createElement("div");if(n.className="cell",n.setAttribute("data-col",String(r)),n.setAttribute("data-row",String(o)),n.setAttribute("role","gridcell"),0===r){const t=Number(e.__pivotIndent)||0;n.style.paddingLeft=`${t+20}px`;const i=document.createElement("span");i.className="pivot-label",i.textContent=String(e.__pivotLabel??""),n.appendChild(i)}else{const t=e[i.field];n.textContent=null!=t?String(t):""}t.appendChild(n)}),!0}(o,t,this.gridColumns,i):(this.cleanupPivotStyling(t),!1)}cleanupPivotStyling(e){(e.classList.contains("pivot-group-row")||e.classList.contains("pivot-leaf-row")||e.classList.contains("pivot-grand-total-row"))&&(e.classList.remove("pivot-group-row","pivot-leaf-row","pivot-grand-total-row"),e.classList.add("data-grid-row"),e.removeAttribute("data-pivot-depth"),e.innerHTML="")}onKeyDown(e){if(" "!==e.key)return;if(!this.isActive)return;const t=this.grid._focusRow,i=this.rows[t];return i?.__pivotIsGroup&&i.__pivotHasChildren?(e.preventDefault(),this.toggle(i.__pivotRowKey),this.requestRenderWithFocus(),!0):void 0}afterRender(){this.isActive&&this.config.showGrandTotal&&this.pivotResult?this.renderGrandTotalFooter():this.cleanupGrandTotalFooter();const e=this.animationStyle;if(!1===e||0===this.keysToAnimate.size)return;const t=this.gridElement?.querySelector(".rows");if(!t)return;const i="fade"===e?"tbw-pivot-fade-in":"tbw-pivot-slide-in";for(const o of t.querySelectorAll(".pivot-group-row, .pivot-leaf-row")){const e=o.dataset.pivotKey;e&&this.keysToAnimate.has(e)&&(o.classList.add(i),o.addEventListener("animationend",()=>o.classList.remove(i),{once:!0}))}this.keysToAnimate.clear()}renderGrandTotalFooter(){if(!this.pivotResult)return;const e=this.gridElement;if(!e)return;const t=e.querySelector(".tbw-scroll-area")??e.querySelector(".tbw-grid-content")??e.querySelector(".tbw-grid-root");if(!t)return;this.grandTotalFooter||(this.grandTotalFooter=document.createElement("div"),this.grandTotalFooter.className="pivot-grand-total-footer",t.appendChild(this.grandTotalFooter));const i={__pivotRowKey:"__grandTotal",__pivotLabel:"Grand Total",__pivotIsGrandTotal:!0,__pivotTotal:this.pivotResult.grandTotal,...this.pivotResult.totals};var o,r,n;o=i,r=this.grandTotalFooter,n=this.gridColumns,r.className="pivot-grand-total-row",r.setAttribute("role","presentation"),r.innerHTML="",n.forEach((e,t)=>{const i=document.createElement("div");if(i.className="cell",i.setAttribute("data-col",String(t)),0===t){const e=document.createElement("span");e.className="pivot-label",e.textContent="Grand Total",i.appendChild(e)}else{const t=o[e.field];i.textContent=null!=t?String(t):""}r.appendChild(i)})}cleanupGrandTotalFooter(){this.grandTotalFooter&&(this.grandTotalFooter.remove(),this.grandTotalFooter=null)}toggle(e){this.userHasToggledExpand=!0,this.expandedKeys.has(e)?this.expandedKeys.delete(e):this.expandedKeys.add(e),this.requestRender()}expand(e){this.userHasToggledExpand=!0,this.expandedKeys.add(e),this.requestRender()}collapse(e){this.userHasToggledExpand=!0,this.expandedKeys.delete(e),this.requestRender()}expandAll(){this.userHasToggledExpand=!0,this.expandAllKeys(),this.requestRender()}collapseAll(){this.userHasToggledExpand=!0,this.expandedKeys.clear(),this.requestRender()}expandAllKeys(){if(!this.pivotResult)return;const e=function(e){const t=[];function i(e){if(e.isGroup&&t.push(e.rowKey),e.children)for(const t of e.children)i(t)}for(const o of e)i(o);return t}(this.pivotResult.rows);for(const t of e)this.expandedKeys.add(t)}isExpanded(e){return this.expandedKeys.has(e)}enablePivot(){0===this.originalColumns.length&&this.captureOriginalColumns(),this.isActive=!0,this.requestRender()}disablePivot(){this.isActive=!1,this.pivotResult=null,this.requestRender()}isPivotActive(){return this.isActive}getPivotResult(){return this.pivotResult}setRowGroupFields(e){this.config.rowGroupFields=e,this.requestRender()}setColumnGroupFields(e){this.config.columnGroupFields=e,this.requestRender()}setValueFields(e){this.config.valueFields=e,this.requestRender()}refresh(){this.pivotResult=null,this.requestRender()}showPanel(){this.grid.openToolPanel(),this.grid.expandedToolPanelSections.includes(y.PANEL_ID)||this.grid.toggleToolPanelSection(y.PANEL_ID)}hidePanel(){this.grid.closeToolPanel()}togglePanel(){this.grid.isToolPanelOpen||this.grid.openToolPanel(),this.grid.toggleToolPanelSection(y.PANEL_ID)}isPanelVisible(){return this.grid.isToolPanelOpen&&this.grid.expandedToolPanelSections.includes(y.PANEL_ID)}get gridColumns(){return this.grid.columns??[]}refreshIfActive(){this.isActive&&this.refresh(),this.refreshPanel()}buildFieldHeaderMap(){const e=this.getAvailableFields();this.fieldHeaderMap.clear();for(const t of e)this.fieldHeaderMap.set(t.field,t.header)}getAvailableFields(){return this.originalColumns.length>0?this.originalColumns:this.captureOriginalColumns()}captureOriginalColumns(){try{const e=this.grid.getAllColumns?.()??this.grid.columns??[];return this.originalColumns=e.filter(e=>!e.field.startsWith("__pivot")).map(e=>({field:e.field,header:e.header??e.field})),this.originalColumns}catch{return[]}}renderPanel(e){this.panelContainer=e,0===this.originalColumns.length&&this.captureOriginalColumns();const t={onTogglePivot:e=>{e?this.enablePivot():this.disablePivot(),this.refreshPanel()},onAddFieldToZone:(e,t)=>this.addFieldToZone(e,t),onRemoveFieldFromZone:(e,t)=>this.removeFieldFromZone(e,t),onAddValueField:(e,t)=>this.addValueField(e,t),onRemoveValueField:e=>this.removeValueField(e),onUpdateValueAggFunc:(e,t)=>this.updateValueAggFunc(e,t),onOptionChange:(e,t)=>{this.config[e]=t,this.isActive&&this.refresh()},getAvailableFields:()=>this.getAvailableFields()};return b(e,this.config,this.isActive,t)}refreshPanel(){this.panelContainer&&(this.panelContainer.innerHTML="",this.renderPanel(this.panelContainer))}addFieldToZone(e,t){if("rowGroups"===t){const t=this.config.rowGroupFields??[];t.includes(e)||(this.config.rowGroupFields=[...t,e])}else{const t=this.config.columnGroupFields??[];t.includes(e)||(this.config.columnGroupFields=[...t,e])}this.removeFromOtherZones(e,t),this.refreshIfActive()}removeFieldFromZone(e,t){"rowGroups"===t?this.config.rowGroupFields=(this.config.rowGroupFields??[]).filter(t=>t!==e):this.config.columnGroupFields=(this.config.columnGroupFields??[]).filter(t=>t!==e),this.refreshIfActive()}removeFromOtherZones(e,t){"rowGroups"!==t&&(this.config.rowGroupFields=(this.config.rowGroupFields??[]).filter(t=>t!==e)),"columnGroups"!==t&&(this.config.columnGroupFields=(this.config.columnGroupFields??[]).filter(t=>t!==e)),"values"!==t&&(this.config.valueFields=(this.config.valueFields??[]).filter(t=>t.field!==e))}addValueField(e,t){const i=this.config.valueFields??[];i.some(t=>t.field===e)||(this.config.valueFields=[...i,{field:e,aggFunc:t}]),this.removeFromOtherZones(e,"values"),this.refreshIfActive()}removeValueField(e){this.config.valueFields=(this.config.valueFields??[]).filter(t=>t.field!==e),this.refreshIfActive()}updateValueAggFunc(e,t){const i=this.config.valueFields??[],o=i.findIndex(t=>t.field===e);o>=0&&(i[o]={...i[o],aggFunc:t},this.config.valueFields=[...i]),this.isActive&&this.refresh()}}export{y as PivotPlugin};
2
2
  //# sourceMappingURL=index.js.map