@toolbox-web/grid 2.0.0-rc.2 → 2.0.0-rc.3

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 (64) 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/internal/diagnostics.d.ts +9 -1
  6. package/lib/features/registry.js.map +1 -1
  7. package/lib/plugins/clipboard/index.js.map +1 -1
  8. package/lib/plugins/column-virtualization/index.js.map +1 -1
  9. package/lib/plugins/context-menu/index.js.map +1 -1
  10. package/lib/plugins/editing/index.js.map +1 -1
  11. package/lib/plugins/export/index.js.map +1 -1
  12. package/lib/plugins/filtering/index.js +1 -1
  13. package/lib/plugins/filtering/index.js.map +1 -1
  14. package/lib/plugins/grouping-columns/index.js.map +1 -1
  15. package/lib/plugins/grouping-rows/GroupingRowsPlugin.d.ts +14 -22
  16. package/lib/plugins/grouping-rows/index.js +2 -2
  17. package/lib/plugins/grouping-rows/index.js.map +1 -1
  18. package/lib/plugins/grouping-rows/types.d.ts +7 -35
  19. package/lib/plugins/master-detail/MasterDetailPlugin.d.ts +30 -0
  20. package/lib/plugins/master-detail/index.js +1 -1
  21. package/lib/plugins/master-detail/index.js.map +1 -1
  22. package/lib/plugins/multi-sort/index.js.map +1 -1
  23. package/lib/plugins/pinned-columns/index.js.map +1 -1
  24. package/lib/plugins/pinned-rows/index.js.map +1 -1
  25. package/lib/plugins/pivot/index.js.map +1 -1
  26. package/lib/plugins/print/index.js.map +1 -1
  27. package/lib/plugins/reorder-columns/index.js.map +1 -1
  28. package/lib/plugins/reorder-rows/index.js.map +1 -1
  29. package/lib/plugins/responsive/index.js.map +1 -1
  30. package/lib/plugins/selection/index.js +1 -1
  31. package/lib/plugins/selection/index.js.map +1 -1
  32. package/lib/plugins/server-side/ServerSidePlugin.d.ts +48 -14
  33. package/lib/plugins/server-side/datasource-types.d.ts +189 -0
  34. package/lib/plugins/server-side/datasource.d.ts +5 -5
  35. package/lib/plugins/server-side/index.d.ts +1 -1
  36. package/lib/plugins/server-side/index.js +1 -1
  37. package/lib/plugins/server-side/index.js.map +1 -1
  38. package/lib/plugins/server-side/types.d.ts +8 -82
  39. package/lib/plugins/tooltip/index.js.map +1 -1
  40. package/lib/plugins/tree/TreePlugin.d.ts +10 -4
  41. package/lib/plugins/tree/index.js +1 -1
  42. package/lib/plugins/tree/index.js.map +1 -1
  43. package/lib/plugins/tree/tree-datasource.d.ts +13 -0
  44. package/lib/plugins/tree/types.d.ts +13 -0
  45. package/lib/plugins/undo-redo/index.js.map +1 -1
  46. package/lib/plugins/visibility/index.js.map +1 -1
  47. package/package.json +1 -1
  48. package/public.d.ts +3 -0
  49. package/umd/grid.all.umd.js +1 -1
  50. package/umd/grid.all.umd.js.map +1 -1
  51. package/umd/grid.umd.js +1 -1
  52. package/umd/grid.umd.js.map +1 -1
  53. package/umd/plugins/filtering.umd.js +1 -1
  54. package/umd/plugins/filtering.umd.js.map +1 -1
  55. package/umd/plugins/grouping-rows.umd.js +1 -1
  56. package/umd/plugins/grouping-rows.umd.js.map +1 -1
  57. package/umd/plugins/master-detail.umd.js +1 -1
  58. package/umd/plugins/master-detail.umd.js.map +1 -1
  59. package/umd/plugins/selection.umd.js +1 -1
  60. package/umd/plugins/selection.umd.js.map +1 -1
  61. package/umd/plugins/server-side.umd.js +1 -1
  62. package/umd/plugins/server-side.umd.js.map +1 -1
  63. package/umd/plugins/tree.umd.js +1 -1
  64. package/umd/plugins/tree.umd.js.map +1 -1
@@ -1,4 +1,4 @@
1
- import { BaseGridPlugin, CellClickEvent, HeaderClickEvent, PluginManifest, PluginQuery } from '../../core/plugin/base-plugin';
1
+ import { GridElement, BaseGridPlugin, CellClickEvent, HeaderClickEvent, PluginManifest, PluginQuery } from '../../core/plugin/base-plugin';
2
2
  import { ColumnConfig } from '../../core/types';
3
3
  import { FlattenedTreeRow, TreeConfig, TreeRow } from './types';
4
4
  /**
@@ -89,6 +89,10 @@ export declare class TreePlugin extends BaseGridPlugin<TreeConfig> {
89
89
  private previousVisibleKeys;
90
90
  private keysToAnimate;
91
91
  private sortState;
92
+ /** Cached original (unwrapped) renderer to prevent re-wrapping on repeated processColumns calls. */
93
+ private originalTreeColumnRenderer;
94
+ /** Field name of the column currently wrapped with tree decorations. */
95
+ private wrappedTreeColumnField;
92
96
  /** @internal */
93
97
  detach(): void;
94
98
  /**
@@ -96,6 +100,8 @@ export declare class TreePlugin extends BaseGridPlugin<TreeConfig> {
96
100
  * @internal
97
101
  */
98
102
  handleQuery(query: PluginQuery): unknown;
103
+ /** @internal */
104
+ attach(grid: GridElement): void;
99
105
  /**
100
106
  * Get expand/collapse animation style from plugin config.
101
107
  * Uses base class isAnimationEnabled to respect grid-level settings.
@@ -161,7 +167,7 @@ export declare class TreePlugin extends BaseGridPlugin<TreeConfig> {
161
167
  * Toggle the expanded state of a tree node.
162
168
  *
163
169
  * If the node is expanded it will be collapsed, and vice versa.
164
- * Emits a `tree-state-change` plugin event with the updated expanded keys.
170
+ * Emits a `tree-expand` event (broadcast to both DOM consumers and plugin bus).
165
171
  *
166
172
  * @param key - The unique key of the node to toggle (from {@link FlattenedTreeRow.key})
167
173
  *
@@ -176,7 +182,7 @@ export declare class TreePlugin extends BaseGridPlugin<TreeConfig> {
176
182
  * Expand all tree nodes recursively.
177
183
  *
178
184
  * Every node with children will be expanded, revealing the full tree hierarchy.
179
- * Emits a `tree-state-change` plugin event.
185
+ * Emits a `tree-expand` plugin event.
180
186
  *
181
187
  * @example
182
188
  * ```ts
@@ -189,7 +195,7 @@ export declare class TreePlugin extends BaseGridPlugin<TreeConfig> {
189
195
  * Collapse all tree nodes.
190
196
  *
191
197
  * Every node will be collapsed, showing only root-level rows.
192
- * Emits a `tree-state-change` plugin event.
198
+ * Emits a `tree-expand` plugin event.
193
199
  *
194
200
  * @example
195
201
  * ```ts
@@ -1,2 +1,2 @@
1
- const e="expanded",t="tree-toggle";function r(e,t){return`[tbw-grid${e?`#${e}`:""}${t?`:${t}`:""}]`}function n(e,t,n,i){return`${r(n,i)} ${e}: ${t}\n\n → More info: ${function(e){return`https://toolboxjs.com/grid/errors#${e.toLowerCase()}`}(e)}`}["__otorp__","__retteGenifed__","__retteSenifed__","rotcurtsnoc","wodniw","sihTlabolg","labolg","ssecorp","noitcnuF","tropmi","lave","tcelfeR","yxorP","rorrE","stnemugra","tnemucod","noitacol","eikooc","egarotSlacol","egarotSnoisses","BDdexedni","hctef","tseuqeRpttHLMX","tekcoSbeW","rekroW","rekroWderahS","rekroWecivreS","renepo","tnerap","pot","semarf","fles"].map(e=>e.split("").reverse().join(""));const i=/* @__PURE__ */new Set(["script","iframe","object","embed","form","input","button","textarea","select","link","meta","base","style","template","slot","portal","frame","frameset","applet","noscript","noembed","plaintext","xmp","listing"]),s=/^on\w+$/i,o=/* @__PURE__ */new Set(["href","src","action","formaction","data","srcdoc","xlink:href","poster","srcset"]),a=/^\s*(javascript|vbscript|data|blob):/i;function d(e){if(!e||"string"!=typeof e)return"";if(-1===e.indexOf("<"))return e;const t=document.createElement("template");return t.innerHTML=e,function(e){const t=[],r=e.querySelectorAll("*");for(const n of r){const e=n.tagName.toLowerCase();if(i.has(e)){t.push(n);continue}if("svg"===e||"http://www.w3.org/2000/svg"===n.namespaceURI){if(Array.from(n.attributes).some(e=>s.test(e.name)||"href"===e.name||"xlink:href"===e.name)){t.push(n);continue}}const r=[];for(const t of n.attributes){const e=t.name.toLowerCase();s.test(e)?r.push(t.name):(o.has(e)&&a.test(t.value)||"style"===e&&/expression\s*\(|javascript:|behavior\s*:/i.test(t.value))&&r.push(t.name)}r.forEach(e=>n.removeAttribute(e))}t.forEach(e=>e.remove())}(t.content),t.innerHTML}const l='<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>',c={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:l,filterActive:l,print:"🖨️"};class h{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 r=new CustomEvent(e,{detail:t,bubbles:!0,cancelable:!0});return this.grid?.dispatchEvent?.(r),r.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)}broadcast(e,t){this.emitPluginEvent(e,t),this.emit(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{...c,...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(),r=parseInt(t,10);if(!isNaN(r))return r}return 200}setIcon(e,t,r){e.dataset.icon=t.replace(/([A-Z])/g,"-$1").toLowerCase(),"collapse"===t?e.dataset.expanded="":"expand"===t&&delete e.dataset.expanded;const n=this.#t(t,r);void 0!==n?"string"==typeof n?e.innerHTML=d(n):n instanceof HTMLElement&&(e.innerHTML="",e.appendChild(n.cloneNode(!0))):e.innerHTML=""}#t(e,t){return void 0!==t?t:this.grid?.gridConfig?.icons?.[e]}updateSortIndicator(e,t){e.querySelector('[part~="sort-indicator"], .sort-indicator')?.remove();const r=document.createElement("span");r.setAttribute("part","sort-indicator"),r.className="sort-indicator",t?(e.setAttribute("aria-sort","asc"===t?"ascending":"descending"),e.setAttribute("data-sort",t),this.setIcon(r,"asc"===t?"sortAsc":"sortDesc")):(e.setAttribute("aria-sort","none"),e.removeAttribute("data-sort"),this.setIcon(r,"sortNone"));const n=e.querySelector(".tbw-filter-btn")??e.querySelector(".resize-handle");return n?e.insertBefore(r,n):e.appendChild(r),r}warn(e,t){void 0!==t?console.warn(n(e,t,this.gridElement.id,this.name)):console.warn(`${r(this.gridElement.id,this.name)} ${e}`)}throwDiagnostic(e,t){throw new Error(n(e,t,this.gridElement.id,this.name))}}function u(e,t,r){return void 0!==e.id?String(e.id):r?`${r}-${t}`:String(t)}function g(e,t){const r=new Set(e);return r.has(t)?r.delete(t):r.add(t),r}function p(e,t,r=null,n=0){const i=t.childrenField??"children",s=/* @__PURE__ */new Set;for(let o=0;o<e.length;o++){const a=e[o],d=u(a,o,r),l=a[i];if(Array.isArray(l)&&l.length>0){s.add(d);const e=p(l,t,d,n+1);for(const t of e)s.add(t)}}return s}function f(e,t,r,n=null,i=0){const s=r.childrenField??"children";for(let o=0;o<e.length;o++){const a=e[o],d=u(a,o,n);if(d===t)return[d];const l=a[s];if(Array.isArray(l)&&l.length>0){const e=f(l,t,r,d,i+1);if(e)return[d,...e]}}return null}function y(e,t,r,n){const i=f(e,t,r);if(!i)return n;const s=new Set(n);for(let o=0;o<i.length-1;o++)s.add(i[o]);return s}function m(e,t="children"){if(!Array.isArray(e)||0===e.length)return!1;for(const r of e){if(!r)continue;const e=r[t];if(Array.isArray(e)&&e.length>0)return!0}return!1}class w extends h{static manifest={modifiesRowStructure:!0,incompatibleWith:[{name:"groupingRows",reason:"Both plugins transform the entire row model. TreePlugin flattens nested hierarchies while GroupingRowsPlugin groups flat rows with synthetic headers. Use one approach per grid."},{name:"pivot",reason:"PivotPlugin replaces the entire row and column structure with aggregated pivot data. Tree hierarchy cannot coexist with pivot aggregation."},{name:"serverSide",reason:"TreePlugin requires the full hierarchy to flatten and manage expansion state. ServerSidePlugin lazy-loads rows in blocks and cannot provide nested children on demand."}],events:[{type:"tree-state-change",description:"Emitted when tree expansion state changes (toggle, expand all, collapse all)"}],queries:[{type:"canMoveRow",description:"Returns false for rows with children (parent nodes cannot be reordered)"}]};static dependencies=[{name:"multiSort",required:!1,reason:"Queries sort model for coordinated tree sorting"}];name="tree";styles="@layer tbw-plugins{tbw-grid .cell[data-field=__tbw_expander]{border-inline-end:none!important;padding:0;display:flex;align-items:center;justify-content:flex-start}tbw-grid .header-row .cell[data-field=__tbw_expander]{display:none}tbw-grid .header-row .cell[data-field=__tbw_expander]+.cell{grid-column:1 / 3}tbw-grid .tree-cell-wrapper{display:inline-flex;align-items:center;overflow:visible;padding-inline-start:calc(var(--tbw-tree-depth, 0) * var(--tbw-tree-indent-width, var(--tbw-tree-toggle-size, 1.25em)))}tbw-grid .tree-expander{display:flex;align-items:center;justify-content:flex-start;width:100%;height:100%;box-sizing:border-box}tbw-grid .tree-toggle{cursor:pointer;-webkit-user-select:none;user-select:none;display:inline-flex;align-items:center;justify-content:center;width:var(--tbw-tree-toggle-size, 1.25em);height:var(--tbw-tree-toggle-size, 1.25em);flex-shrink:0}tbw-grid .tree-toggle:hover{color:var(--tbw-tree-accent, var(--tbw-color-accent))}tbw-grid .tree-spacer{width:var(--tbw-tree-toggle-size, 1.25em);display:inline-block;flex-shrink:0}tbw-grid .data-grid-row.tbw-tree-slide-in{animation:tbw-tree-slide-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}tbw-grid .data-grid-row.tbw-tree-fade-in{animation:tbw-tree-fade-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}@keyframes tbw-tree-slide-in{0%{opacity:0;transform:translate(-8px)}to{opacity:1;transform:translate(0)}}@keyframes tbw-tree-fade-in{0%{opacity:0}to{opacity:1}}}";get defaultConfig(){return{childrenField:"children",autoDetect:!0,defaultExpanded:!1,indentWidth:20,showExpandIcons:!0,animation:"slide"}}expandedKeys=/* @__PURE__ */new Set;initialExpansionDone=!1;flattenedRows=[];rowKeyMap=/* @__PURE__ */new Map;previousVisibleKeys=/* @__PURE__ */new Set;keysToAnimate=/* @__PURE__ */new Set;sortState=null;detach(){this.expandedKeys.clear(),this.initialExpansionDone=!1,this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),this.keysToAnimate.clear(),this.sortState=null}handleQuery(e){if("canMoveRow"===e.type){const t=e.context,r=this.config.childrenField??"children",n=t?.[r];if(Array.isArray(n)&&n.length>0)return!1}}get animationStyle(){return!!this.isAnimationEnabled&&(this.config.animation??"slide")}detect(e){if(!this.config.autoDetect)return!1;const t=e,r=this.config.childrenField??function(e){if(!Array.isArray(e)||0===e.length)return null;const t=["children","items","nodes","subRows","nested"];for(const r of e)if(r&&"object"==typeof r)for(const e of t){const t=r[e];if(Array.isArray(t)&&t.length>0)return e}return null}(t)??"children";return m(t,r)}processRows(e){const t=e;if(!m(t,this.config.childrenField??"children"))return this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),[...e];let r=this.withStableKeys(t);const n=this.resolveEffectiveSortState();n&&(r=this.sortTree(r,n.field,n.direction)),this.config.defaultExpanded&&!this.initialExpansionDone&&(this.expandedKeys=p(r,this.config),this.initialExpansionDone=!0),this.flattenedRows=this.flattenTree(r,this.expandedKeys),this.rowKeyMap.clear(),this.keysToAnimate.clear();const i=/* @__PURE__ */new Set;for(const s of this.flattenedRows)this.rowKeyMap.set(s.key,s),i.add(s.key),!this.previousVisibleKeys.has(s.key)&&s.depth>0&&this.keysToAnimate.add(s.key);return this.previousVisibleKeys=i,this.flattenedRows.map(e=>({...e.data,__treeKey:e.key,__treeDepth:e.depth,__treeHasChildren:e.hasChildren,__treeExpanded:e.isExpanded}))}withStableKeys(e,t=null){const r=this.config.childrenField??"children";return e.map((e,n)=>{const i=e.__stableKey,s=void 0!==e.id?String(e.id):i??(t?`${t}-${n}`:String(n)),o=e[r],a=Array.isArray(o)&&o.length>0;return{...e,__stableKey:s,...a?{[r]:this.withStableKeys(o,s)}:{}}})}flattenTree(e,t,r=0){const n=this.config.childrenField??"children",i=[];for(const s of e){const e=s.__stableKey??String(s.id??"?"),o=s[n],a=Array.isArray(o)&&o.length>0,d=t.has(e);i.push({key:e,data:s,depth:r,hasChildren:a,isExpanded:d,parentKey:r>0&&e.substring(0,e.lastIndexOf("-"))||null}),a&&d&&i.push(...this.flattenTree(o,t,r+1))}return i}resolveEffectiveSortState(){const e=this.grid?.query?.("sort:get-model",null);if(Array.isArray(e)&&e.length>0){const t=e[0];if(Array.isArray(t)&&t.length>0)return{field:t[0].field,direction:"desc"===t[0].direction?-1:1}}return this.sortState}sortTree(e,t,r){const n=this.config.childrenField??"children";return[...e].sort((e,n)=>{const i=e[t],s=n[t];return null==i&&null==s?0:null==i?-1:null==s?1:i>s?r:i<s?-r:0}).map(e=>{const i=e[n];return Array.isArray(i)&&i.length>0?{...e,[n]:this.sortTree(i,t,r)}:e})}processColumns(r){if(0===this.flattenedRows.length)return[...r];const n=[...r];if(0===n.length)return n;const{treeColumn:i}=this.config;let s=0;if(i){const e=n.findIndex(e=>e.field===i);e>=0&&(s=e)}const o=n[s],a=o.viewRenderer,d=()=>this.config,l=this.setIcon.bind(this);return n[s]={...o,viewRenderer:r=>{const{row:n,value:i}=r,{showExpandIcons:s=!0,indentWidth:o}=d(),c=n,h=c.__treeDepth??0,u=document.createElement("span");if(u.className="tree-cell-wrapper",u.style.setProperty("--tbw-tree-depth",String(h)),void 0!==o&&u.style.setProperty("--tbw-tree-indent-width",`${o}px`),s)if(c.__treeHasChildren){const r=document.createElement("span");r.className=`${t}${c.__treeExpanded?` ${e}`:""}`,l(r,c.__treeExpanded?"collapse":"expand"),r.setAttribute("data-tree-key",String(c.__treeKey??"")),u.appendChild(r)}else{const e=document.createElement("span");e.className="tree-spacer",u.appendChild(e)}const g=document.createElement("span");if(g.className="tree-content",a){const e=a(r);e instanceof Node?g.appendChild(e):"string"==typeof e&&(g.innerHTML=e)}else g.textContent=null!=i?String(i):"";return u.appendChild(g),u}},n}onCellClick(e){const r=e.originalEvent?.target;if(!r?.classList.contains(t))return!1;const n=r.getAttribute("data-tree-key");if(!n)return!1;const i=this.rowKeyMap.get(n);return!!i&&(this.expandedKeys=g(this.expandedKeys,n),this.emit("tree-expand",{key:n,row:i.data,expanded:this.expandedKeys.has(n),depth:i.depth}),this.requestRender(),!0)}onKeyDown(e){if(" "!==e.key)return;const t=this.grid._focusRow,r=this.flattenedRows[t];return r?.hasChildren?(e.preventDefault(),this.expandedKeys=g(this.expandedKeys,r.key),this.emit("tree-expand",{key:r.key,row:r.data,expanded:this.expandedKeys.has(r.key),depth:r.depth}),this.requestRenderWithFocus(),!0):void 0}onHeaderClick(e){if(0===this.flattenedRows.length||!e.column.sortable)return!1;const t=this.grid?.query?.("sort:get-model",null);if(Array.isArray(t)&&t.length>0)return!1;const{field:r}=e.column;this.sortState&&this.sortState.field===r?1===this.sortState.direction?this.sortState={field:r,direction:-1}:this.sortState=null:this.sortState={field:r,direction:1};const n=this.grid;return void 0!==n._sortState&&(n._sortState=this.sortState?{...this.sortState}:null),this.broadcast("sort-change",{field:r,direction:this.sortState?.direction??0}),this.requestRender(),!0}afterRender(){const e=this.gridElement?.querySelector(".rows");if(!e)return;const t=this.animationStyle,r=!1!==t&&this.keysToAnimate.size>0,n="fade"===t?"tbw-tree-fade-in":"tbw-tree-slide-in";for(const i of e.querySelectorAll(".data-grid-row")){const e=i.querySelector(".cell[data-row]"),t=e?parseInt(e.getAttribute("data-row")??"-1",10):-1,s=this.flattenedRows[t];s?.hasChildren&&i.setAttribute("aria-expanded",String(s.isExpanded)),r&&s?.key&&this.keysToAnimate.has(s.key)&&(i.classList.add(n),i.addEventListener("animationend",()=>i.classList.remove(n),{once:!0}))}this.keysToAnimate.clear()}expand(e){this.expandedKeys.add(e),this.requestRender()}collapse(e){this.expandedKeys.delete(e),this.requestRender()}toggle(e){this.expandedKeys=g(this.expandedKeys,e),this.emitPluginEvent("tree-state-change",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}expandAll(){this.expandedKeys=p(this.rows,this.config),this.emitPluginEvent("tree-state-change",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}collapseAll(){this.expandedKeys=/* @__PURE__ */new Set,this.emitPluginEvent("tree-state-change",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}isExpanded(e){return this.expandedKeys.has(e)}getExpandedKeys(){return[...this.expandedKeys]}getFlattenedRows(){return[...this.flattenedRows]}getRowByKey(e){return this.rowKeyMap.get(e)?.data}expandToKey(e){this.expandedKeys=y(this.rows,e,this.config,this.expandedKeys),this.requestRender()}}export{w as TreePlugin};
1
+ const e="expanded",t="tree-toggle";function r(e,t){return`[tbw-grid${e?`#${e}`:""}${t?`:${t}`:""}]`}function n(e,t,n,i){return`${r(n,i)} ${e}: ${t}\n\n → More info: ${function(e){return`https://toolboxjs.com/grid/errors#${e.toLowerCase()}`}(e)}`}["__otorp__","__retteGenifed__","__retteSenifed__","rotcurtsnoc","wodniw","sihTlabolg","labolg","ssecorp","noitcnuF","tropmi","lave","tcelfeR","yxorP","rorrE","stnemugra","tnemucod","noitacol","eikooc","egarotSlacol","egarotSnoisses","BDdexedni","hctef","tseuqeRpttHLMX","tekcoSbeW","rekroW","rekroWderahS","rekroWecivreS","renepo","tnerap","pot","semarf","fles"].map(e=>e.split("").reverse().join(""));const i=/* @__PURE__ */new Set(["script","iframe","object","embed","form","input","button","textarea","select","link","meta","base","style","template","slot","portal","frame","frameset","applet","noscript","noembed","plaintext","xmp","listing"]),s=/^on\w+$/i,o=/* @__PURE__ */new Set(["href","src","action","formaction","data","srcdoc","xlink:href","poster","srcset"]),a=/^\s*(javascript|vbscript|data|blob):/i;function d(e){if(!e||"string"!=typeof e)return"";if(-1===e.indexOf("<"))return e;const t=document.createElement("template");return t.innerHTML=e,function(e){const t=[],r=e.querySelectorAll("*");for(const n of r){const e=n.tagName.toLowerCase();if(i.has(e)){t.push(n);continue}if("svg"===e||"http://www.w3.org/2000/svg"===n.namespaceURI){if(Array.from(n.attributes).some(e=>s.test(e.name)||"href"===e.name||"xlink:href"===e.name)){t.push(n);continue}}const r=[];for(const t of n.attributes){const e=t.name.toLowerCase();s.test(e)?r.push(t.name):(o.has(e)&&a.test(t.value)||"style"===e&&/expression\s*\(|javascript:|behavior\s*:/i.test(t.value))&&r.push(t.name)}r.forEach(e=>n.removeAttribute(e))}t.forEach(e=>e.remove())}(t.content),t.innerHTML}const l='<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>',c={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:l,filterActive:l,print:"🖨️"};class h{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 r=new CustomEvent(e,{detail:t,bubbles:!0,cancelable:!0});return this.grid?.dispatchEvent?.(r),r.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)}broadcast(e,t){this.emitPluginEvent(e,t),this.emit(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{...c,...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(),r=parseInt(t,10);if(!isNaN(r))return r}return 200}setIcon(e,t,r){e.dataset.icon=t.replace(/([A-Z])/g,"-$1").toLowerCase(),"collapse"===t?e.dataset.expanded="":"expand"===t&&delete e.dataset.expanded;const n=this.#t(t,r);void 0!==n?"string"==typeof n?e.innerHTML=d(n):n instanceof HTMLElement&&(e.innerHTML="",e.appendChild(n.cloneNode(!0))):e.innerHTML=""}#t(e,t){return void 0!==t?t:this.grid?.gridConfig?.icons?.[e]}updateSortIndicator(e,t){e.querySelector('[part~="sort-indicator"], .sort-indicator')?.remove();const r=document.createElement("span");r.setAttribute("part","sort-indicator"),r.className="sort-indicator",t?(e.setAttribute("aria-sort","asc"===t?"ascending":"descending"),e.setAttribute("data-sort",t),this.setIcon(r,"asc"===t?"sortAsc":"sortDesc")):(e.setAttribute("aria-sort","none"),e.removeAttribute("data-sort"),this.setIcon(r,"sortNone"));const n=e.querySelector(".tbw-filter-btn")??e.querySelector(".resize-handle");return n?e.insertBefore(r,n):e.appendChild(r),r}warn(e,t){void 0!==t?console.warn(n(e,t,this.gridElement.id,this.name)):console.warn(`${r(this.gridElement.id,this.name)} ${e}`)}throwDiagnostic(e,t){throw new Error(n(e,t,this.gridElement.id,this.name))}}function u(e,t,r){return void 0!==e.id?String(e.id):r?`${r}-${t}`:String(t)}function p(e,t){const r=new Set(e);return r.has(t)?r.delete(t):r.add(t),r}function g(e,t,r=null,n=0){const i=t.childrenField??"children",s=/* @__PURE__ */new Set;for(let o=0;o<e.length;o++){const a=e[o],d=u(a,o,r),l=a[i];if(Array.isArray(l)&&l.length>0){s.add(d);const e=g(l,t,d,n+1);for(const t of e)s.add(t)}}return s}function f(e,t,r,n=null,i=0){const s=r.childrenField??"children";for(let o=0;o<e.length;o++){const a=e[o],d=u(a,o,n);if(d===t)return[d];const l=a[s];if(Array.isArray(l)&&l.length>0){const e=f(l,t,r,d,i+1);if(e)return[d,...e]}}return null}function w(e,t,r,n){const i=f(e,t,r);if(!i)return n;const s=new Set(n);for(let o=0;o<i.length-1;o++)s.add(i[o]);return s}function m(e,t){const r=Math.min(t,e.length-1);if(r<0)return 0;let n=r;for(;n>0&&e[n].depth>0;)n--;let i=0;for(let s=0;s<=n;s++)0===e[s].depth&&i++;return i-1}function y(e,t="children"){if(!Array.isArray(e)||0===e.length)return!1;for(const r of e){if(!r)continue;const e=r[t];if(Array.isArray(e)&&e.length>0)return!0}return!1}class b extends h{static manifest={modifiesRowStructure:!0,hookPriority:{processRows:10},incompatibleWith:[{name:"groupingRows",reason:"Both plugins transform the entire row model. TreePlugin flattens nested hierarchies while GroupingRowsPlugin groups flat rows with synthetic headers. Use one approach per grid."},{name:"pivot",reason:"PivotPlugin replaces the entire row and column structure with aggregated pivot data. Tree hierarchy cannot coexist with pivot aggregation."}],events:[{type:"tree-expand",description:"Emitted when tree expansion state changes (toggle, expand all, collapse all). Broadcast to both DOM consumers and plugin bus."}],queries:[{type:"canMoveRow",description:"Returns false for rows with children (parent nodes cannot be reordered)"},{type:"datasource:viewport-mapping",description:"Translates flat viewport row indices to top-level node indices for ServerSide pagination."}]};static dependencies=[{name:"multiSort",required:!1,reason:"Queries sort model for coordinated tree sorting"},{name:"serverSide",required:!1,reason:"Consumes datasource events for lazy-loaded tree data"}];name="tree";styles='@layer tbw-plugins{tbw-grid .cell[data-field=__tbw_expander]{border-inline-end:none!important;padding:0;display:flex;align-items:center;justify-content:flex-start}tbw-grid .header-row .cell[data-field=__tbw_expander]{display:none}tbw-grid .header-row .cell[data-field=__tbw_expander]+.cell{grid-column:1 / 3}tbw-grid .tree-cell-wrapper{display:inline-flex;align-items:center;vertical-align:middle;overflow:visible;padding-inline-start:calc(var(--tbw-tree-depth, 0) * var(--tbw-tree-indent-width, var(--tbw-tree-toggle-size, 1.25em)))}tbw-grid .tree-expander{display:flex;align-items:center;justify-content:flex-start;width:100%;height:100%;box-sizing:border-box}tbw-grid .tree-toggle{cursor:pointer;-webkit-user-select:none;user-select:none;display:inline-flex;align-items:center;justify-content:center;width:var(--tbw-tree-toggle-size, 1.25em);height:var(--tbw-tree-toggle-size, 1.25em);flex-shrink:0}tbw-grid .tree-toggle:hover{color:var(--tbw-tree-accent, var(--tbw-color-accent))}tbw-grid .tree-spacer{width:var(--tbw-tree-toggle-size, 1.25em);height:var(--tbw-tree-toggle-size, 1.25em);display:inline-block;flex-shrink:0}tbw-grid .data-grid-row.tbw-tree-slide-in{animation:tbw-tree-slide-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}tbw-grid .data-grid-row.tbw-tree-fade-in{animation:tbw-tree-fade-in var(--tbw-animation-duration, .2s) var(--tbw-animation-easing, ease-out) forwards}@keyframes tbw-tree-slide-in{0%{opacity:0;transform:translate(-8px)}to{opacity:1;transform:translate(0)}}@keyframes tbw-tree-fade-in{0%{opacity:0}to{opacity:1}}tbw-grid.tbw-tree-loading .rows-viewport:after{content:"";display:block;height:2px;background:linear-gradient(90deg,transparent,var(--tbw-color-accent, #2563eb),transparent);animation:tbw-tree-loading-bar 1s ease-in-out infinite;position:sticky;bottom:0;left:0;right:0}@keyframes tbw-tree-loading-bar{0%{transform:translate(-100%)}to{transform:translate(100%)}}}';get defaultConfig(){return{childrenField:"children",autoDetect:!0,defaultExpanded:!1,indentWidth:20,showExpandIcons:!0,animation:"slide"}}expandedKeys=/* @__PURE__ */new Set;initialExpansionDone=!1;flattenedRows=[];rowKeyMap=/* @__PURE__ */new Map;previousVisibleKeys=/* @__PURE__ */new Set;keysToAnimate=/* @__PURE__ */new Set;sortState=null;originalTreeColumnRenderer;wrappedTreeColumnField;detach(){this.expandedKeys.clear(),this.initialExpansionDone=!1,this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),this.keysToAnimate.clear(),this.sortState=null,this.originalTreeColumnRenderer=void 0,this.wrappedTreeColumnField=void 0}handleQuery(e){if("canMoveRow"===e.type){const t=e.context,r=this.config.childrenField??"children",n=t?.[r];if(Array.isArray(n)&&n.length>0)return!1}if("datasource:viewport-mapping"===e.type){const{viewportStart:t,viewportEnd:r}=e.context;if(0===this.flattenedRows.length)return;return{startNode:m(this.flattenedRows,t),endNode:m(this.flattenedRows,r)+1,totalLoadedNodes:function(e){let t=0;for(const r of e)0===r.depth&&t++;return t}(this.flattenedRows)}}}attach(e){super.attach(e),this.on("datasource:data",e=>{const t=e;t.claimed||(t.claimed=!0)}),this.on("datasource:children",e=>{const t=e;if("tree"!==t.context?.source)return;t.claimed=!0;const r=t.context.parentNode;if(r){r[this.config.childrenField??"children"]=t.rows,this.requestRender()}})}get animationStyle(){return!!this.isAnimationEnabled&&(this.config.animation??"slide")}detect(e){if(!this.config.autoDetect)return!1;const t=e,r=this.config.childrenField??function(e){if(!Array.isArray(e)||0===e.length)return null;const t=["children","items","nodes","subRows","nested"];for(const r of e)if(r&&"object"==typeof r)for(const e of t){const t=r[e];if(Array.isArray(t)&&t.length>0)return e}return null}(t)??"children";return y(t,r)}processRows(e){const t=this.config.childrenField??"children",r=e;if(0===r.length||!y(r,t))return this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),[...e];let n=this.withStableKeys(r);const i=this.resolveEffectiveSortState();i&&(n=this.sortTree(n,i.field,i.direction)),this.config.defaultExpanded&&!this.initialExpansionDone&&(this.expandedKeys=g(n,this.config),this.initialExpansionDone=!0),this.flattenedRows=this.flattenTree(n,this.expandedKeys),this.rowKeyMap.clear(),this.keysToAnimate.clear();const s=/* @__PURE__ */new Set;for(const o of this.flattenedRows)this.rowKeyMap.set(o.key,o),s.add(o.key),!this.previousVisibleKeys.has(o.key)&&o.depth>0&&this.keysToAnimate.add(o.key);return this.previousVisibleKeys=s,this.flattenedRows.map(e=>({...e.data,__treeKey:e.key,__treeDepth:e.depth,__treeHasChildren:e.hasChildren,__treeExpanded:e.isExpanded}))}withStableKeys(e,t=null){const r=this.config.childrenField??"children";return e.map((e,n)=>{const i=e.__stableKey,s=void 0!==e.id?String(e.id):i??(t?`${t}-${n}`:String(n)),o=e[r],a=Array.isArray(o)&&o.length>0;return{...e,__stableKey:s,...a?{[r]:this.withStableKeys(o,s)}:{}}})}flattenTree(e,t,r=0){const n=this.config.childrenField??"children",i=[];for(const s of e){const e=s.__stableKey??String(s.id??"?"),o=s[n],a=Array.isArray(o)&&o.length>0,d=t.has(e);i.push({key:e,data:s,depth:r,hasChildren:a,isExpanded:d,parentKey:r>0&&e.substring(0,e.lastIndexOf("-"))||null}),a&&d&&i.push(...this.flattenTree(o,t,r+1))}return i}resolveEffectiveSortState(){const e=this.grid?.query?.("sort:get-model",null);if(Array.isArray(e)&&e.length>0){const t=e[0];if(Array.isArray(t)&&t.length>0)return{field:t[0].field,direction:"desc"===t[0].direction?-1:1}}return this.sortState}sortTree(e,t,r){const n=this.config.childrenField??"children";return[...e].sort((e,n)=>{const i=e[t],s=n[t];return null==i&&null==s?0:null==i?-1:null==s?1:i>s?r:i<s?-r:0}).map(e=>{const i=e[n];return Array.isArray(i)&&i.length>0?{...e,[n]:this.sortTree(i,t,r)}:e})}processColumns(r){if(0===this.flattenedRows.length)return[...r];const n=[...r];if(0===n.length)return n;const{treeColumn:i}=this.config;let s=0;if(i){const e=n.findIndex(e=>e.field===i);e>=0&&(s=e)}const o=n[s],a=o.field;this.wrappedTreeColumnField!==a&&(this.originalTreeColumnRenderer=o.viewRenderer,this.wrappedTreeColumnField=a);const d=this.originalTreeColumnRenderer,l=()=>this.config,c=this.setIcon.bind(this);return n[s]={...o,viewRenderer:r=>{const{row:n,value:i}=r,{showExpandIcons:s=!0,indentWidth:o}=l(),a=n,h=a.__treeDepth??0,u=document.createElement("span");if(u.className="tree-cell-wrapper",u.style.setProperty("--tbw-tree-depth",String(h)),void 0!==o&&u.style.setProperty("--tbw-tree-indent-width",`${o}px`),s)if(a.__treeHasChildren){const r=document.createElement("span");r.className=`${t}${a.__treeExpanded?` ${e}`:""}`,c(r,a.__treeExpanded?"collapse":"expand"),r.setAttribute("data-tree-key",String(a.__treeKey??"")),u.appendChild(r)}else{const e=document.createElement("span");e.className="tree-spacer",u.appendChild(e)}const p=document.createElement("span");if(p.className="tree-content",d){const e=d(r);e instanceof Node?p.appendChild(e):"string"==typeof e&&(p.innerHTML=e)}else p.textContent=null!=i?String(i):"";return u.appendChild(p),u}},n}onCellClick(e){const r=e.originalEvent?.target;if(!r?.classList.contains(t))return!1;const n=r.getAttribute("data-tree-key");if(!n)return!1;const i=this.rowKeyMap.get(n);return!!i&&(this.expandedKeys=p(this.expandedKeys,n),this.broadcast("tree-expand",{key:n,row:i.data,expanded:this.expandedKeys.has(n),depth:i.depth,expandedKeys:[...this.expandedKeys]}),this.requestRender(),!0)}onKeyDown(e){if(" "!==e.key)return;const t=this.grid._focusRow,r=this.flattenedRows[t];return r?.hasChildren?(e.preventDefault(),this.expandedKeys=p(this.expandedKeys,r.key),this.broadcast("tree-expand",{key:r.key,row:r.data,expanded:this.expandedKeys.has(r.key),depth:r.depth,expandedKeys:[...this.expandedKeys]}),this.requestRenderWithFocus(),!0):void 0}onHeaderClick(e){if(0===this.flattenedRows.length||!e.column.sortable)return!1;const t=this.grid?.query?.("sort:get-model",null);if(Array.isArray(t)&&t.length>0)return!1;const{field:r}=e.column;this.sortState&&this.sortState.field===r?1===this.sortState.direction?this.sortState={field:r,direction:-1}:this.sortState=null:this.sortState={field:r,direction:1};const n=this.grid;return void 0!==n._sortState&&(n._sortState=this.sortState?{...this.sortState}:null),this.broadcast("sort-change",{field:r,direction:this.sortState?.direction??0}),this.requestRender(),!0}afterRender(){const e=this.gridElement?.querySelector(".rows");if(!e)return;const t=this.animationStyle,r=!1!==t&&this.keysToAnimate.size>0,n="fade"===t?"tbw-tree-fade-in":"tbw-tree-slide-in";for(const i of e.querySelectorAll(".data-grid-row")){const e=i.querySelector(".cell[data-row]"),t=e?parseInt(e.getAttribute("data-row")??"-1",10):-1,s=this.flattenedRows[t];s?.hasChildren&&i.setAttribute("aria-expanded",String(s.isExpanded)),r&&s?.key&&this.keysToAnimate.has(s.key)&&(i.classList.add(n),i.addEventListener("animationend",()=>i.classList.remove(n),{once:!0}))}this.keysToAnimate.clear()}expand(e){this.expandedKeys.add(e),this.requestRender()}collapse(e){this.expandedKeys.delete(e),this.requestRender()}toggle(e){this.expandedKeys=p(this.expandedKeys,e);const t=this.rowKeyMap.get(e);t?this.broadcast("tree-expand",{key:e,row:t.data,expanded:this.expandedKeys.has(e),depth:t.depth,expandedKeys:[...this.expandedKeys]}):this.emitPluginEvent("tree-expand",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}expandAll(){this.expandedKeys=g(this.rows,this.config),this.emitPluginEvent("tree-expand",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}collapseAll(){this.expandedKeys=/* @__PURE__ */new Set,this.emitPluginEvent("tree-expand",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}isExpanded(e){return this.expandedKeys.has(e)}getExpandedKeys(){return[...this.expandedKeys]}getFlattenedRows(){return[...this.flattenedRows]}getRowByKey(e){return this.rowKeyMap.get(e)?.data}expandToKey(e){this.expandedKeys=w(this.rows,e,this.config,this.expandedKeys),this.requestRender()}}export{b as TreePlugin};
2
2
  //# sourceMappingURL=index.js.map