@toolbox-web/grid 2.3.0 → 2.4.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 (102) hide show
  1. package/all.d.ts +1 -0
  2. package/all.js +2 -2
  3. package/all.js.map +1 -1
  4. package/index.js +1 -1
  5. package/index.js.map +1 -1
  6. package/lib/core/grid.d.ts +2 -1
  7. package/lib/core/internal/diagnostics.d.ts +5 -1
  8. package/lib/core/internal/dom-builder.d.ts +0 -25
  9. package/lib/core/internal/drag-drop-registry.d.ts +66 -0
  10. package/lib/core/internal/render-scheduler.d.ts +9 -8
  11. package/lib/core/plugin/base-plugin.d.ts +23 -0
  12. package/lib/core/plugin/plugin-manager.d.ts +9 -0
  13. package/lib/core/types.d.ts +67 -46
  14. package/lib/features/registry.js.map +1 -1
  15. package/lib/features/reorder-rows.d.ts +3 -3
  16. package/lib/features/reorder-rows.js +1 -1
  17. package/lib/features/reorder-rows.js.map +1 -1
  18. package/lib/features/row-drag-drop.d.ts +9 -0
  19. package/lib/features/row-drag-drop.js +2 -0
  20. package/lib/features/row-drag-drop.js.map +1 -0
  21. package/lib/features/server-side.js.map +1 -1
  22. package/lib/plugins/clipboard/index.js +1 -1
  23. package/lib/plugins/clipboard/index.js.map +1 -1
  24. package/lib/plugins/column-virtualization/index.js +1 -1
  25. package/lib/plugins/column-virtualization/index.js.map +1 -1
  26. package/lib/plugins/context-menu/index.js +1 -1
  27. package/lib/plugins/context-menu/index.js.map +1 -1
  28. package/lib/plugins/editing/index.js +1 -1
  29. package/lib/plugins/editing/index.js.map +1 -1
  30. package/lib/plugins/export/ExportPlugin.d.ts +89 -0
  31. package/lib/plugins/export/index.d.ts +3 -2
  32. package/lib/plugins/export/index.js +1 -1
  33. package/lib/plugins/export/index.js.map +1 -1
  34. package/lib/plugins/export/types.d.ts +30 -0
  35. package/lib/plugins/filtering/index.js +1 -1
  36. package/lib/plugins/filtering/index.js.map +1 -1
  37. package/lib/plugins/grouping-columns/index.js +1 -1
  38. package/lib/plugins/grouping-columns/index.js.map +1 -1
  39. package/lib/plugins/grouping-rows/index.js +2 -2
  40. package/lib/plugins/grouping-rows/index.js.map +1 -1
  41. package/lib/plugins/master-detail/index.js +1 -1
  42. package/lib/plugins/master-detail/index.js.map +1 -1
  43. package/lib/plugins/multi-sort/index.js +1 -1
  44. package/lib/plugins/multi-sort/index.js.map +1 -1
  45. package/lib/plugins/pinned-columns/index.js +1 -1
  46. package/lib/plugins/pinned-columns/index.js.map +1 -1
  47. package/lib/plugins/pinned-rows/index.js +1 -1
  48. package/lib/plugins/pinned-rows/index.js.map +1 -1
  49. package/lib/plugins/pivot/index.js +1 -1
  50. package/lib/plugins/pivot/index.js.map +1 -1
  51. package/lib/plugins/print/index.js +1 -1
  52. package/lib/plugins/print/index.js.map +1 -1
  53. package/lib/plugins/reorder-columns/index.js +1 -1
  54. package/lib/plugins/reorder-columns/index.js.map +1 -1
  55. package/lib/plugins/reorder-rows/RowReorderPlugin.d.ts +14 -156
  56. package/lib/plugins/reorder-rows/index.d.ts +10 -4
  57. package/lib/plugins/reorder-rows/index.js +1 -1
  58. package/lib/plugins/reorder-rows/index.js.map +1 -1
  59. package/lib/plugins/reorder-rows/types.d.ts +9 -86
  60. package/lib/plugins/responsive/index.js +1 -1
  61. package/lib/plugins/responsive/index.js.map +1 -1
  62. package/lib/plugins/row-drag-drop/RowDragDropPlugin.d.ts +106 -0
  63. package/lib/plugins/row-drag-drop/index.d.ts +9 -0
  64. package/lib/plugins/row-drag-drop/index.js +2 -0
  65. package/lib/plugins/row-drag-drop/index.js.map +1 -0
  66. package/lib/plugins/row-drag-drop/types.d.ts +255 -0
  67. package/lib/plugins/selection/index.js +1 -1
  68. package/lib/plugins/selection/index.js.map +1 -1
  69. package/lib/plugins/server-side/ServerSidePlugin.d.ts +13 -0
  70. package/lib/plugins/server-side/datasource-types.d.ts +54 -7
  71. package/lib/plugins/server-side/datasource.d.ts +10 -2
  72. package/lib/plugins/server-side/index.d.ts +1 -1
  73. package/lib/plugins/server-side/index.js +1 -1
  74. package/lib/plugins/server-side/index.js.map +1 -1
  75. package/lib/plugins/server-side/types.d.ts +1 -1
  76. package/lib/plugins/shared/drag-drop-protocol.d.ts +98 -0
  77. package/lib/plugins/tooltip/index.js +1 -1
  78. package/lib/plugins/tooltip/index.js.map +1 -1
  79. package/lib/plugins/tree/TreePlugin.d.ts +19 -6
  80. package/lib/plugins/tree/index.js +1 -1
  81. package/lib/plugins/tree/index.js.map +1 -1
  82. package/lib/plugins/undo-redo/index.js +1 -1
  83. package/lib/plugins/undo-redo/index.js.map +1 -1
  84. package/lib/plugins/visibility/index.js +1 -1
  85. package/lib/plugins/visibility/index.js.map +1 -1
  86. package/package.json +1 -1
  87. package/umd/grid.all.umd.js +1 -1
  88. package/umd/grid.all.umd.js.map +1 -1
  89. package/umd/grid.umd.js +1 -1
  90. package/umd/grid.umd.js.map +1 -1
  91. package/umd/plugins/export.umd.js +1 -1
  92. package/umd/plugins/export.umd.js.map +1 -1
  93. package/umd/plugins/reorder-rows.umd.js +1 -1
  94. package/umd/plugins/reorder-rows.umd.js.map +1 -1
  95. package/umd/plugins/row-drag-drop.umd.js +2 -0
  96. package/umd/plugins/row-drag-drop.umd.js.map +1 -0
  97. package/umd/plugins/selection.umd.js +1 -1
  98. package/umd/plugins/selection.umd.js.map +1 -1
  99. package/umd/plugins/server-side.umd.js +1 -1
  100. package/umd/plugins/server-side.umd.js.map +1 -1
  101. package/umd/plugins/tree.umd.js +1 -1
  102. package/umd/plugins/tree.umd.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"server-side.umd.js","sources":["../../../../../libs/grid/src/lib/plugins/server-side/datasource.ts","../../../../../libs/grid/src/lib/plugins/server-side/ServerSidePlugin.ts"],"sourcesContent":["import type { GetRowsParams, GetRowsResult, ServerSideDataSource } from './datasource-types';\n\nexport function getBlockNumber(nodeIndex: number, blockSize: number): number {\n return Math.floor(nodeIndex / blockSize);\n}\n\nexport function getBlockRange(blockNumber: number, blockSize: number): { start: number; end: number } {\n return {\n start: blockNumber * blockSize,\n end: (blockNumber + 1) * blockSize,\n };\n}\n\nexport function getRequiredBlocks(startNode: number, endNode: number, blockSize: number): number[] {\n const startBlock = getBlockNumber(startNode, blockSize);\n const endBlock = getBlockNumber(endNode - 1, blockSize);\n\n const blocks: number[] = [];\n for (let i = startBlock; i <= endBlock; i++) {\n blocks.push(i);\n }\n return blocks;\n}\n\nexport async function loadBlock(\n dataSource: ServerSideDataSource,\n blockNumber: number,\n blockSize: number,\n params: Partial<GetRowsParams>,\n): Promise<GetRowsResult> {\n const range = getBlockRange(blockNumber, blockSize);\n\n return dataSource.getRows({\n startNode: range.start,\n endNode: range.end,\n sortModel: params.sortModel,\n filterModel: params.filterModel,\n });\n}\n\nexport function getRowFromCache(\n nodeIndex: number,\n blockSize: number,\n loadedBlocks: Map<number, unknown[]>,\n): unknown | undefined {\n const blockNumber = getBlockNumber(nodeIndex, blockSize);\n const block = loadedBlocks.get(blockNumber);\n if (!block) return undefined;\n\n const indexInBlock = nodeIndex % blockSize;\n return block[indexInBlock];\n}\n\nexport function isBlockLoaded(blockNumber: number, loadedBlocks: Map<number, unknown[]>): boolean {\n return loadedBlocks.has(blockNumber);\n}\n\nexport function isBlockLoading(blockNumber: number, loadingBlocks: Set<number>): boolean {\n return loadingBlocks.has(blockNumber);\n}\n","/**\n * Server-Side Data Plugin (Class-based)\n *\n * Central data orchestrator for the grid. Owns fetch + cache + row-model management.\n * Structural plugins (Tree, GroupingRows) claim data via events; core grid uses it\n * as flat rows when unclaimed.\n */\n\nimport {\n DATASOURCE_CHILD_FETCH_ERROR,\n DATASOURCE_FETCH_ERROR,\n DATASOURCE_NO_CHILD_HANDLER,\n DATASOURCE_THROTTLED,\n debugDiagnostic,\n errorDiagnostic,\n warnDiagnostic,\n} from '../../core/internal/diagnostics';\nimport { builtInSort } from '../../core/internal/sorting';\nimport { BaseGridPlugin, ScrollEvent, type PluginManifest, type PluginQuery } from '../../core/plugin/base-plugin';\nimport type { ColumnConfig, GridHost } from '../../core/types';\nimport { getBlockNumber, getRequiredBlocks, getRowFromCache, loadBlock } from './datasource';\nimport type {\n DataSourceChildrenDetail,\n DataSourceDataDetail,\n DataSourceErrorDetail,\n DataSourceLoadingDetail,\n FetchChildrenQuery,\n GetRowsParams,\n GetRowsResult,\n ServerSideDataSource,\n ViewportMappingQuery,\n ViewportMappingResponse,\n} from './datasource-types';\nimport type { ServerSideConfig } from './types';\n\n/** Scroll debounce delay in ms */\nconst SCROLL_DEBOUNCE_MS = 100;\n\n/**\n * Server-Side Data Plugin for tbw-grid\n *\n * Central data orchestrator for the grid. Manages fetch, cache, and row-model for\n * server-side data loading. Structural plugins (Tree, GroupingRows) can claim data\n * via events; the core grid uses it as flat rows when unclaimed.\n *\n * ## Installation\n *\n * ```ts\n * import { ServerSidePlugin } from '@toolbox-web/grid/plugins/server-side';\n * ```\n *\n * ## DataSource Interface\n *\n * ```ts\n * interface ServerSideDataSource {\n * getRows(params: GetRowsParams): Promise<GetRowsResult>;\n * getChildRows?(params: GetChildRowsParams): Promise<GetChildRowsResult>;\n * }\n * ```\n *\n * @example Basic Server-Side Loading\n * ```ts\n * import '@toolbox-web/grid';\n * import '@toolbox-web/grid/features/server-side';\n *\n * grid.gridConfig = {\n * columns: [...],\n * features: {\n * serverSide: {\n * pageSize: 50,\n * dataSource: {\n * async getRows(params) {\n * const response = await fetch(\n * `/api/data?start=${params.startNode}&end=${params.endNode}`\n * );\n * const data = await response.json();\n * return { rows: data.rows, totalNodeCount: data.total };\n * },\n * },\n * },\n * },\n * };\n * ```\n *\n * @see {@link ServerSideConfig} for configuration options\n * @see {@link ServerSideDataSource} for data source interface\n *\n * @internal Extends BaseGridPlugin\n */\nexport class ServerSidePlugin extends BaseGridPlugin<ServerSideConfig> {\n /**\n * Plugin manifest declaring capabilities, hooks, events, and queries.\n * @internal\n */\n static override readonly manifest: PluginManifest = {\n modifiesRowStructure: true,\n hookPriority: {\n processRows: -10, // Run before structural plugins (Tree, GroupingRows)\n },\n incompatibleWith: [\n {\n name: 'pivot',\n reason:\n 'PivotPlugin requires the full dataset to compute aggregations. ' +\n 'ServerSidePlugin lazy-loads rows in blocks, so pivot aggregation cannot be performed client-side.',\n },\n ],\n events: [\n { type: 'datasource:data', description: 'Root data page/block loaded' },\n { type: 'datasource:children', description: 'Child data loaded for a parent context' },\n { type: 'datasource:loading', description: 'Loading state changed' },\n { type: 'datasource:error', description: 'Fetch operation failed' },\n ],\n queries: [\n { type: 'datasource:fetch-children', description: 'Request child rows for a parent context' },\n { type: 'datasource:is-active', description: 'Check if ServerSide plugin has an active data source' },\n ],\n };\n\n /** @internal */\n readonly name = 'serverSide';\n\n /** @internal */\n protected override get defaultConfig(): Partial<ServerSideConfig> {\n return {\n pageSize: 100,\n cacheBlockSize: 100,\n maxConcurrentRequests: 2,\n };\n }\n\n // #region Internal State\n private dataSource: ServerSideDataSource | null = null;\n private totalNodeCount = 0;\n private infiniteScrollMode = false;\n private loadedBlocks = new Map<number, unknown[]>();\n private loadingBlocks = new Set<number>();\n private lastRequestId = 0;\n private scrollDebounceTimer?: ReturnType<typeof setTimeout>;\n /** Persistent node array with stable placeholder references to avoid unnecessary DOM rebuilds. */\n private managedNodes: unknown[] = [];\n // #endregion\n\n // #region Lifecycle\n\n /** @internal */\n override attach(grid: import('../../core/plugin/base-plugin').GridElement): void {\n super.attach(grid);\n\n // Invalidate cache and refetch on sort/filter changes — gated by sortMode/filterMode.\n // 'local' (opt-in): keep cache; sort/filter the loaded rows in place via a re-render.\n // See getEnrichmentParams() — local-mode state is also omitted from block fetch params\n // so scroll-triggered loadRequiredBlocks() doesn't leak local state to the backend.\n this.on('sort-change', () => {\n if (this.config.sortMode === 'local') {\n this.requestRender();\n } else {\n this.onModelChange();\n }\n });\n this.on('filter-change', () => {\n if (this.config.filterMode === 'local') {\n this.requestRender();\n } else {\n this.onModelChange();\n }\n });\n\n // Auto-initialize from config when dataSource is provided declaratively\n if (this.config.dataSource) {\n this.setDataSource(this.config.dataSource);\n }\n }\n\n /** @internal */\n override detach(): void {\n this.dataSource = null;\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.managedNodes = [];\n this.lastRequestId = 0;\n if (this.scrollDebounceTimer) {\n clearTimeout(this.scrollDebounceTimer);\n this.scrollDebounceTimer = undefined;\n }\n }\n // #endregion\n\n // #region Private Methods\n\n /**\n * Build enrichment params by querying sort/filter models from loaded plugins.\n *\n * When `sortMode === 'local'` the `sortModel` is omitted so scroll-triggered\n * block fetches don't ask the backend for an ordering it isn't applying.\n * Same for `filterMode === 'local'` and `filterModel`.\n */\n private getEnrichmentParams(): Partial<GetRowsParams> {\n const sortLocal = this.config.sortMode === 'local';\n const filterLocal = this.config.filterMode === 'local';\n const sortResults = sortLocal\n ? undefined\n : (this.grid?.query?.('sort:get-model', null) as\n | Array<{ field: string; direction: 'asc' | 'desc' }>[]\n | undefined);\n const filterResults = filterLocal\n ? undefined\n : (this.grid?.query?.('filter:get-model', null) as Record<string, unknown>[] | undefined);\n\n // Fallback to core single-column sort state when no plugin answers the\n // 'sort:get-model' query (e.g. MultiSortPlugin not loaded). Translate the\n // numeric direction (1/-1) to the public 'asc'/'desc' string form.\n let sortModel = sortResults?.[0];\n const host = this.grid as unknown as GridHost | undefined;\n if (!sortLocal && !sortModel && host?._sortState) {\n const { field, direction } = host._sortState;\n sortModel = [{ field, direction: direction === 1 ? 'asc' : 'desc' }];\n }\n\n return {\n sortModel,\n filterModel: filterResults?.[0],\n };\n }\n\n /**\n * Translate visible viewport indices to node-space indices via structural plugins.\n * Falls back to 1:1 mapping (flat data) when no structural plugin responds.\n */\n private getViewportMapping(viewportStart: number, viewportEnd: number): ViewportMappingResponse {\n const query: ViewportMappingQuery = { viewportStart, viewportEnd };\n const results = this.grid?.query?.('datasource:viewport-mapping', query) as ViewportMappingResponse[] | undefined;\n\n // Structural plugin responded — use its mapping\n if (results?.[0]) return results[0];\n\n // No structural plugin → 1:1 mapping (flat data)\n return {\n startNode: viewportStart,\n endNode: viewportEnd,\n totalLoadedNodes: this.totalNodeCount,\n };\n }\n\n /**\n * Handle sort or filter model changes.\n * Purge cache and refetch current viewport with new enrichment params.\n */\n private onModelChange(): void {\n if (!this.dataSource) return;\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.managedNodes = [];\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n this.requestRender();\n // Eagerly fetch the current viewport with the new enrichment params.\n // Without this, the table stays blank until the user scrolls (no other\n // path triggers a fetch — processRows just rebuilds from cached blocks).\n this.loadRequiredBlocks();\n }\n\n /**\n * Apply server response metadata: resolve totalNodeCount and infinite scroll mode.\n * When `lastNode` is provided, it takes priority and finalizes the total.\n * When `totalNodeCount` is -1, switch to infinite scroll (growing array).\n * When a block returns fewer rows than blockSize, detect end-of-data.\n */\n private applyServerResult(result: GetRowsResult, blockNum: number, blockSize: number): void {\n if (result.lastNode !== undefined) {\n // Server declared the exact end\n this.totalNodeCount = result.lastNode + 1;\n this.infiniteScrollMode = false;\n } else if (result.totalNodeCount === -1) {\n this.infiniteScrollMode = true;\n } else {\n this.totalNodeCount = result.totalNodeCount;\n this.infiniteScrollMode = false;\n }\n\n // Auto-detect end of data: short block means server has no more rows\n if (this.infiniteScrollMode && result.rows.length < blockSize) {\n this.totalNodeCount = blockNum * blockSize + result.rows.length;\n this.infiniteScrollMode = false;\n }\n }\n\n /**\n * Estimate the row count for infinite scroll mode.\n * Returns loaded rows + one extra block of placeholders to trigger next fetch.\n */\n private getInfiniteScrollEstimate(blockSize: number): number {\n let maxLoadedEnd = 0;\n for (const [block, rows] of this.loadedBlocks) {\n const end = block * blockSize + rows.length;\n if (end > maxLoadedEnd) maxLoadedEnd = end;\n }\n return maxLoadedEnd + blockSize;\n }\n\n /**\n * Check current viewport and load any missing blocks.\n */\n private loadRequiredBlocks(): void {\n if (!this.dataSource) return;\n\n const gridRef = this.grid as unknown as GridHost;\n const blockSize = this.config.cacheBlockSize ?? 100;\n\n // Translate viewport to node space via structural plugins\n const viewport = this.getViewportMapping(gridRef._virtualization.start, gridRef._virtualization.end);\n\n // Expand the viewport by loadThreshold in both directions to prefetch\n // blocks the user is about to scroll into. The end is clamped to\n // totalNodeCount when known so we don't request blocks past the end of\n // data — but `totalNodeCount = 0` means \"not yet loaded\" (e.g. just after\n // purgeCache or before the first fetch resolves), in which case we leave\n // it unclamped so the initial block can be fetched.\n const threshold = Math.max(0, this.config.loadThreshold ?? 0);\n const expandedStart = Math.max(0, viewport.startNode - threshold);\n const knownTotal = this.totalNodeCount > 0 ? this.totalNodeCount : Infinity;\n const expandedEnd = Math.min(knownTotal, viewport.endNode + threshold);\n if (expandedEnd <= expandedStart) return;\n\n // Determine which blocks are needed for current viewport (in node space)\n const requiredBlocks = getRequiredBlocks(expandedStart, expandedEnd, blockSize);\n const enrichment = this.getEnrichmentParams();\n const gridId = this.grid?.getAttribute?.('id') ?? undefined;\n\n // Load missing blocks\n for (const blockNum of requiredBlocks) {\n if (this.loadedBlocks.has(blockNum) || this.loadingBlocks.has(blockNum)) {\n continue;\n }\n\n // Check concurrent request limit\n if (this.loadingBlocks.size >= (this.config.maxConcurrentRequests ?? 2)) {\n debugDiagnostic(DATASOURCE_THROTTLED, 'Concurrent request limit reached, deferring block load', gridId);\n break;\n }\n\n this.loadingBlocks.add(blockNum);\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: true });\n\n loadBlock(this.dataSource, blockNum, blockSize, enrichment)\n .then((result) => {\n this.loadedBlocks.set(blockNum, result.rows);\n // Capture pre-update length so we can detect whether processRows must\n // re-run to grow the managedNodes array (e.g. after onModelChange reset).\n const previousManagedLength = this.managedNodes.length;\n this.applyServerResult(result, blockNum, blockSize);\n this.loadingBlocks.delete(blockNum);\n\n // Update managed nodes in place for this block (avoids full processRows rebuild)\n const start = blockNum * blockSize;\n for (let i = 0; i < result.rows.length; i++) {\n if (start + i < this.managedNodes.length) {\n this.managedNodes[start + i] = result.rows[i];\n }\n }\n\n // Broadcast data event with claimed flag\n const detail: DataSourceDataDetail = {\n rows: result.rows,\n totalNodeCount: result.totalNodeCount,\n startNode: start,\n endNode: start + result.rows.length,\n claimed: false,\n };\n this.broadcast('datasource:data', detail);\n\n if (this.loadingBlocks.size === 0) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n }\n\n // If managedNodes still hasn't been sized for the (possibly newly known)\n // totalNodeCount — typically because onModelChange() reset it to 0 right\n // before this fetch — we MUST run processRows to grow the array. The\n // in-place writes above are no-ops in that case (length is 0).\n // Without this, sort/filter changes leave the grid permanently blank\n // until something else triggers a ROWS phase.\n const needsRowModelRebuild =\n previousManagedLength === 0 ||\n this.managedNodes.length < (Number.isFinite(this.totalNodeCount) ? this.totalNodeCount : 0);\n\n if (needsRowModelRebuild) {\n this.requestRender();\n } else {\n // Re-render visible rows without force geometry recalculation.\n // requestVirtualRefresh() skips spacer height writes that cause oscillation\n // with the scheduler's afterRender microtask. Node count hasn't changed —\n // only cached data replaced placeholders — so geometry is stable.\n this.requestVirtualRefresh();\n }\n\n // Re-check with fresh viewport: user may have scrolled further\n this.loadRequiredBlocks();\n })\n .catch((error: unknown) => {\n this.loadingBlocks.delete(blockNum);\n const err = error instanceof Error ? error : new Error(String(error));\n errorDiagnostic(DATASOURCE_FETCH_ERROR, `getRows() failed: ${err.message}`, gridId);\n this.broadcast<DataSourceErrorDetail>('datasource:error', { error: err });\n\n if (this.loadingBlocks.size === 0) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n }\n });\n }\n }\n // #endregion\n\n // #region Hooks\n\n /** @internal */\n override processRows(rows: readonly unknown[]): unknown[] {\n if (!this.dataSource) return [...rows];\n\n const blockSize = this.config.cacheBlockSize ?? 100;\n\n // Guard against invalid totalNodeCount (e.g. undefined from a malformed datasource response).\n // In infinite scroll mode, estimate the total from loaded data + one extra block.\n const nodeCount = this.infiniteScrollMode\n ? this.getInfiniteScrollEstimate(blockSize)\n : Number.isFinite(this.totalNodeCount) && this.totalNodeCount >= 0\n ? this.totalNodeCount\n : 0;\n\n // Grow array with stable placeholder objects (created once, reused across renders)\n while (this.managedNodes.length < nodeCount) {\n const i = this.managedNodes.length;\n this.managedNodes.push({ __loading: true, __index: i });\n }\n // Shrink if total decreased\n this.managedNodes.length = nodeCount;\n\n // Replace placeholders with cached data (stable refs for unchanged entries)\n for (let i = 0; i < nodeCount; i++) {\n const cached = getRowFromCache(i, blockSize, this.loadedBlocks);\n if (cached) {\n this.managedNodes[i] = cached;\n }\n }\n\n // Local-mode sort: when sortMode === 'local' the plugin owns ordering of\n // the loaded rows itself (core sort ran on the input but we discarded it\n // by returning managedNodes). Apply the current core sort state on top of\n // managedNodes so the user sees in-place sorting without a refetch.\n // Note: any plugin sort that runs after us (priority > -10, e.g. multi-sort)\n // will further re-sort our output, which is the intended chain.\n const host = this.grid as unknown as GridHost | undefined;\n if (this.config.sortMode === 'local' && host?._sortState) {\n const columns = (host._columns ?? []) as ColumnConfig[];\n return builtInSort(this.managedNodes, host._sortState, columns);\n }\n\n return this.managedNodes;\n }\n\n /** @internal */\n override onScroll(event: ScrollEvent): void {\n if (!this.dataSource) return;\n\n // Immediate check for blocks\n this.loadRequiredBlocks();\n\n // Debounce: when scrolling stops, do a final check with fresh viewport\n if (this.scrollDebounceTimer) {\n clearTimeout(this.scrollDebounceTimer);\n }\n this.scrollDebounceTimer = setTimeout(() => {\n this.loadRequiredBlocks();\n }, SCROLL_DEBOUNCE_MS);\n }\n\n /** @internal */\n override handleQuery(query: PluginQuery): unknown {\n switch (query.type) {\n case 'datasource:is-active':\n return this.dataSource != null;\n\n case 'datasource:fetch-children': {\n const { context } = query.context as FetchChildrenQuery;\n this.fetchChildren(context);\n return undefined;\n }\n }\n return undefined;\n }\n // #endregion\n\n // #region Child Data Fetching\n\n /**\n * Fetch child rows via the datasource and broadcast the result.\n */\n private fetchChildren(context: { source: string; [key: string]: unknown }): void {\n if (!this.dataSource) return;\n\n const gridId = this.grid?.getAttribute?.('id') ?? undefined;\n\n if (!this.dataSource.getChildRows) {\n warnDiagnostic(\n DATASOURCE_NO_CHILD_HANDLER,\n `Plugin \"${context.source}\" requested child rows but getChildRows() is not implemented on the dataSource`,\n gridId,\n );\n return;\n }\n\n const enrichment = this.getEnrichmentParams();\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: true, context });\n\n this.dataSource\n .getChildRows({ context, sortModel: enrichment.sortModel, filterModel: enrichment.filterModel })\n .then((result) => {\n const detail: DataSourceChildrenDetail = {\n rows: result.rows,\n context,\n claimed: false,\n };\n this.broadcast('datasource:children', detail);\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false, context });\n })\n .catch((error: unknown) => {\n const err = error instanceof Error ? error : new Error(String(error));\n errorDiagnostic(DATASOURCE_CHILD_FETCH_ERROR, `getChildRows() failed: ${err.message}`, gridId);\n this.broadcast<DataSourceErrorDetail>('datasource:error', { error: err, context });\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false, context });\n });\n }\n // #endregion\n\n // #region Public API\n\n /**\n * Set the data source for server-side loading.\n * @param dataSource - Data source implementing the getRows method (and optionally getChildRows)\n */\n setDataSource(dataSource: ServerSideDataSource): void {\n this.dataSource = dataSource;\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.managedNodes = [];\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n\n // Load first block with enrichment params\n const blockSize = this.config.cacheBlockSize ?? 100;\n const enrichment = this.getEnrichmentParams();\n const gridId = this.grid?.getAttribute?.('id') ?? undefined;\n\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: true });\n\n loadBlock(dataSource, 0, blockSize, enrichment)\n .then((result) => {\n this.loadedBlocks.set(0, result.rows);\n this.applyServerResult(result, 0, blockSize);\n\n const detail: DataSourceDataDetail = {\n rows: result.rows,\n totalNodeCount: result.totalNodeCount,\n startNode: 0,\n endNode: result.rows.length,\n claimed: false,\n };\n this.broadcast('datasource:data', detail);\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n this.requestRender();\n\n // When loadThreshold is configured, re-check the viewport so the\n // prefetch can pull in additional blocks immediately rather than\n // waiting for the user's first scroll. Gated on the threshold to\n // preserve the historical \"first fetch loads block 0 only\" behavior.\n if ((this.config.loadThreshold ?? 0) > 0) {\n this.loadRequiredBlocks();\n }\n })\n .catch((error: unknown) => {\n const err = error instanceof Error ? error : new Error(String(error));\n errorDiagnostic(DATASOURCE_FETCH_ERROR, `getRows() failed: ${err.message}`, gridId);\n this.broadcast<DataSourceErrorDetail>('datasource:error', { error: err });\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n });\n }\n\n /**\n * Refresh all data from the server.\n * Purges cache and refetches from block 0.\n */\n refresh(): void {\n if (!this.dataSource) return;\n const ds = this.dataSource;\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.managedNodes = [];\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n // Re-trigger load via setDataSource which handles enrichment and broadcasting\n this.setDataSource(ds);\n }\n\n /**\n * Clear all cached data without refreshing.\n */\n purgeCache(): void {\n this.loadedBlocks.clear();\n this.managedNodes = [];\n }\n\n /**\n * Get the total node count from the server.\n */\n getTotalNodeCount(): number {\n return this.totalNodeCount;\n }\n\n /**\n * @deprecated Use {@link getTotalNodeCount} instead. Will be removed in a future version.\n */\n getTotalRowCount(): number {\n return this.totalNodeCount;\n }\n\n /**\n * Check if a specific node is loaded in the cache.\n * @param nodeIndex - Node index to check\n */\n isNodeLoaded(nodeIndex: number): boolean {\n const blockSize = this.config.cacheBlockSize ?? 100;\n const blockNum = getBlockNumber(nodeIndex, blockSize);\n return this.loadedBlocks.has(blockNum);\n }\n\n /**\n * @deprecated Use {@link isNodeLoaded} instead. Will be removed in a future version.\n */\n isRowLoaded(rowIndex: number): boolean {\n return this.isNodeLoaded(rowIndex);\n }\n\n /**\n * Get the number of loaded cache blocks.\n */\n getLoadedBlockCount(): number {\n return this.loadedBlocks.size;\n }\n // #endregion\n}\n"],"names":["getBlockNumber","nodeIndex","blockSize","Math","floor","async","loadBlock","dataSource","blockNumber","params","range","start","end","getBlockRange","getRows","startNode","endNode","sortModel","filterModel","getRowFromCache","loadedBlocks","block","get","ServerSidePlugin","BaseGridPlugin","static","modifiesRowStructure","hookPriority","processRows","incompatibleWith","name","reason","events","type","description","queries","defaultConfig","pageSize","cacheBlockSize","maxConcurrentRequests","totalNodeCount","infiniteScrollMode","Map","loadingBlocks","Set","lastRequestId","scrollDebounceTimer","managedNodes","attach","grid","super","this","on","config","sortMode","requestRender","onModelChange","filterMode","setDataSource","detach","clear","clearTimeout","getEnrichmentParams","sortLocal","filterLocal","sortResults","query","filterResults","host","_sortState","field","direction","getViewportMapping","viewportStart","viewportEnd","results","totalLoadedNodes","loadRequiredBlocks","applyServerResult","result","blockNum","lastNode","rows","length","getInfiniteScrollEstimate","maxLoadedEnd","gridRef","viewport","_virtualization","threshold","max","loadThreshold","expandedStart","knownTotal","Infinity","expandedEnd","min","requiredBlocks","startBlock","endBlock","blocks","i","push","getRequiredBlocks","enrichment","gridId","getAttribute","has","size","debugDiagnostic","DATASOURCE_THROTTLED","add","broadcast","loading","then","set","previousManagedLength","delete","detail","claimed","Number","isFinite","requestVirtualRefresh","catch","error","err","Error","String","errorDiagnostic","DATASOURCE_FETCH_ERROR","message","nodeCount","__loading","__index","cached","columns","_columns","builtInSort","onScroll","event","setTimeout","handleQuery","context","fetchChildren","getChildRows","warnDiagnostic","DATASOURCE_NO_CHILD_HANDLER","source","DATASOURCE_CHILD_FETCH_ERROR","refresh","ds","purgeCache","getTotalNodeCount","getTotalRowCount","isNodeLoaded","isRowLoaded","rowIndex","getLoadedBlockCount"],"mappings":"8fAEO,SAASA,EAAeC,EAAmBC,GAChD,OAAOC,KAAKC,MAAMH,EAAYC,EAChC,CAoBAG,eAAsBC,EACpBC,EACAC,EACAN,EACAO,GAEA,M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AKA,WAAA2E,CAAYC,GACV,OAAOzG,KAAKuG,aAAaE,EAC3B,CAKA,mBAAAC,GACE,OAAO1G,KAAK/B,aAAawF,IAC3B"}
1
+ {"version":3,"file":"server-side.umd.js","sources":["../../../../../libs/grid/src/lib/plugins/server-side/datasource.ts","../../../../../libs/grid/src/lib/plugins/server-side/ServerSidePlugin.ts"],"sourcesContent":["import type { GetRowsParams, GetRowsResult, ServerSideDataSource, Subscribable } from './datasource-types';\n\nexport function getBlockNumber(nodeIndex: number, blockSize: number): number {\n return Math.floor(nodeIndex / blockSize);\n}\n\nexport function getBlockRange(blockNumber: number, blockSize: number): { start: number; end: number } {\n return {\n start: blockNumber * blockSize,\n end: (blockNumber + 1) * blockSize,\n };\n}\n\nexport function getRequiredBlocks(startNode: number, endNode: number, blockSize: number): number[] {\n const startBlock = getBlockNumber(startNode, blockSize);\n const endBlock = getBlockNumber(endNode - 1, blockSize);\n\n const blocks: number[] = [];\n for (let i = startBlock; i <= endBlock; i++) {\n blocks.push(i);\n }\n return blocks;\n}\n\nfunction isSubscribable<T>(value: unknown): value is Subscribable<T> {\n return (\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { subscribe?: unknown }).subscribe === 'function' &&\n // Promises don't have `.subscribe`, but defensively rule them out.\n typeof (value as { then?: unknown }).then !== 'function'\n );\n}\n\nfunction makeAbortError(): DOMException {\n return new DOMException('Aborted', 'AbortError');\n}\n\n/**\n * Bridge a `Promise | Subscribable` getRows return value into a single\n * `Promise<GetRowsResult>`. For Subscribables, abort triggers `unsubscribe()`\n * so the underlying request (e.g. Angular `HttpClient` XHR) is cancelled.\n * Promise sources should pass `signal` to `fetch` themselves — we still reject\n * on abort either way so the plugin's abort path is consistent.\n */\nexport function toResultPromise<T>(\n source: Promise<T> | Subscribable<T>,\n signal: AbortSignal,\n): Promise<T> {\n if (signal.aborted) {\n return Promise.reject(makeAbortError());\n }\n if (!isSubscribable<T>(source)) {\n // Promise path: reject on abort so the plugin's catch sees a consistent\n // signal even if the underlying fetch ignored params.signal.\n return new Promise<T>((resolve, reject) => {\n const onAbort = () => reject(makeAbortError());\n signal.addEventListener('abort', onAbort, { once: true });\n Promise.resolve(source).then(\n (v) => {\n signal.removeEventListener('abort', onAbort);\n resolve(v);\n },\n (e) => {\n signal.removeEventListener('abort', onAbort);\n reject(e);\n },\n );\n });\n }\n // Subscribable path: subscribe once, settle on next/error/complete, and\n // unsubscribe on abort — that's what cancels HttpClient's XHR.\n return new Promise<T>((resolve, reject) => {\n let settled = false;\n const subscription = source.subscribe({\n next: (value) => {\n if (settled) return;\n settled = true;\n resolve(value);\n subscription.unsubscribe();\n },\n error: (err) => {\n if (settled) return;\n settled = true;\n reject(err);\n },\n complete: () => {\n if (settled) return;\n settled = true;\n reject(new Error('getRows observable completed without emitting a value'));\n },\n });\n if (settled) return; // synchronous emit\n const onAbort = () => {\n if (settled) return;\n settled = true;\n subscription.unsubscribe();\n reject(makeAbortError());\n };\n signal.addEventListener('abort', onAbort, { once: true });\n });\n}\n\nexport function loadBlock(\n dataSource: ServerSideDataSource,\n blockNumber: number,\n blockSize: number,\n params: Partial<GetRowsParams>,\n signal: AbortSignal,\n): Promise<GetRowsResult> {\n const range = getBlockRange(blockNumber, blockSize);\n\n const result = dataSource.getRows({\n startNode: range.start,\n endNode: range.end,\n sortModel: params.sortModel,\n filterModel: params.filterModel,\n signal,\n });\n return toResultPromise(result, signal);\n}\n\nexport function getRowFromCache(\n nodeIndex: number,\n blockSize: number,\n loadedBlocks: Map<number, unknown[]>,\n): unknown | undefined {\n const blockNumber = getBlockNumber(nodeIndex, blockSize);\n const block = loadedBlocks.get(blockNumber);\n if (!block) return undefined;\n\n const indexInBlock = nodeIndex % blockSize;\n return block[indexInBlock];\n}\n\nexport function isBlockLoaded(blockNumber: number, loadedBlocks: Map<number, unknown[]>): boolean {\n return loadedBlocks.has(blockNumber);\n}\n\nexport function isBlockLoading(blockNumber: number, loadingBlocks: Set<number>): boolean {\n return loadingBlocks.has(blockNumber);\n}\n","/**\n * Server-Side Data Plugin (Class-based)\n *\n * Central data orchestrator for the grid. Owns fetch + cache + row-model management.\n * Structural plugins (Tree, GroupingRows) claim data via events; core grid uses it\n * as flat rows when unclaimed.\n */\n\nimport {\n DATASOURCE_CHILD_FETCH_ERROR,\n DATASOURCE_FETCH_ERROR,\n DATASOURCE_NO_CHILD_HANDLER,\n DATASOURCE_THROTTLED,\n debugDiagnostic,\n errorDiagnostic,\n warnDiagnostic,\n} from '../../core/internal/diagnostics';\nimport { builtInSort } from '../../core/internal/sorting';\nimport { BaseGridPlugin, ScrollEvent, type PluginManifest, type PluginQuery } from '../../core/plugin/base-plugin';\nimport type { ColumnConfig, GridHost } from '../../core/types';\nimport { getBlockNumber, getRequiredBlocks, getRowFromCache, loadBlock } from './datasource';\nimport type {\n DataSourceChildrenDetail,\n DataSourceDataDetail,\n DataSourceErrorDetail,\n DataSourceLoadingDetail,\n FetchChildrenQuery,\n GetRowsParams,\n GetRowsResult,\n ServerSideDataSource,\n ViewportMappingQuery,\n ViewportMappingResponse,\n} from './datasource-types';\nimport type { ServerSideConfig } from './types';\n\n/** Scroll debounce delay in ms */\nconst SCROLL_DEBOUNCE_MS = 100;\n\n/**\n * Server-Side Data Plugin for tbw-grid\n *\n * Central data orchestrator for the grid. Manages fetch, cache, and row-model for\n * server-side data loading. Structural plugins (Tree, GroupingRows) can claim data\n * via events; the core grid uses it as flat rows when unclaimed.\n *\n * ## Installation\n *\n * ```ts\n * import { ServerSidePlugin } from '@toolbox-web/grid/plugins/server-side';\n * ```\n *\n * ## DataSource Interface\n *\n * ```ts\n * interface ServerSideDataSource {\n * getRows(params: GetRowsParams): Promise<GetRowsResult>;\n * getChildRows?(params: GetChildRowsParams): Promise<GetChildRowsResult>;\n * }\n * ```\n *\n * @example Basic Server-Side Loading\n * ```ts\n * import '@toolbox-web/grid';\n * import '@toolbox-web/grid/features/server-side';\n *\n * grid.gridConfig = {\n * columns: [...],\n * features: {\n * serverSide: {\n * pageSize: 50,\n * dataSource: {\n * async getRows(params) {\n * const response = await fetch(\n * `/api/data?start=${params.startNode}&end=${params.endNode}`\n * );\n * const data = await response.json();\n * return { rows: data.rows, totalNodeCount: data.total };\n * },\n * },\n * },\n * },\n * };\n * ```\n *\n * @see {@link ServerSideConfig} for configuration options\n * @see {@link ServerSideDataSource} for data source interface\n *\n * @internal Extends BaseGridPlugin\n */\nexport class ServerSidePlugin extends BaseGridPlugin<ServerSideConfig> {\n /**\n * Plugin manifest declaring capabilities, hooks, events, and queries.\n * @internal\n */\n static override readonly manifest: PluginManifest = {\n modifiesRowStructure: true,\n hookPriority: {\n processRows: -10, // Run before structural plugins (Tree, GroupingRows)\n },\n incompatibleWith: [\n {\n name: 'pivot',\n reason:\n 'PivotPlugin requires the full dataset to compute aggregations. ' +\n 'ServerSidePlugin lazy-loads rows in blocks, so pivot aggregation cannot be performed client-side.',\n },\n ],\n events: [\n { type: 'datasource:data', description: 'Root data page/block loaded' },\n { type: 'datasource:children', description: 'Child data loaded for a parent context' },\n { type: 'datasource:loading', description: 'Loading state changed' },\n { type: 'datasource:error', description: 'Fetch operation failed' },\n ],\n queries: [\n { type: 'datasource:fetch-children', description: 'Request child rows for a parent context' },\n { type: 'datasource:is-active', description: 'Check if ServerSide plugin has an active data source' },\n ],\n };\n\n /** @internal */\n readonly name = 'serverSide';\n\n /** @internal */\n protected override get defaultConfig(): Partial<ServerSideConfig> {\n return {\n pageSize: 100,\n cacheBlockSize: 100,\n maxConcurrentRequests: 2,\n };\n }\n\n // #region Internal State\n private dataSource: ServerSideDataSource | null = null;\n private totalNodeCount = 0;\n private infiniteScrollMode = false;\n private loadedBlocks = new Map<number, unknown[]>();\n private loadingBlocks = new Set<number>();\n /**\n * Per-block AbortControllers for in-flight requests. Aborted when a block\n * is superseded (sort/filter change, refresh, purgeCache, detach) so data\n * sources that honor `params.signal` (e.g. `fetch`, RxJS via `fromObservable`)\n * can cancel the underlying network call.\n */\n private blockControllers = new Map<number, AbortController>();\n private lastRequestId = 0;\n private scrollDebounceTimer?: ReturnType<typeof setTimeout>;\n /** Persistent node array with stable placeholder references to avoid unnecessary DOM rebuilds. */\n private managedNodes: unknown[] = [];\n // #endregion\n\n // #region Lifecycle\n\n /** @internal */\n override attach(grid: import('../../core/plugin/base-plugin').GridElement): void {\n super.attach(grid);\n\n // Invalidate cache and refetch on sort/filter changes — gated by sortMode/filterMode.\n // 'local' (opt-in): keep cache; sort/filter the loaded rows in place via a re-render.\n // See getEnrichmentParams() — local-mode state is also omitted from block fetch params\n // so scroll-triggered loadRequiredBlocks() doesn't leak local state to the backend.\n this.on('sort-change', () => {\n if (this.config.sortMode === 'local') {\n this.requestRender();\n } else {\n this.onModelChange();\n }\n });\n this.on('filter-change', () => {\n if (this.config.filterMode === 'local') {\n this.requestRender();\n } else {\n this.onModelChange();\n }\n });\n\n // Auto-initialize from config when dataSource is provided declaratively\n if (this.config.dataSource) {\n this.setDataSource(this.config.dataSource);\n }\n }\n\n /** @internal */\n override detach(): void {\n this.dataSource = null;\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.abortAllBlocks();\n this.managedNodes = [];\n this.lastRequestId = 0;\n if (this.scrollDebounceTimer) {\n clearTimeout(this.scrollDebounceTimer);\n this.scrollDebounceTimer = undefined;\n }\n }\n // #endregion\n\n // #region Private Methods\n\n /**\n * Abort all in-flight block requests. Called when the plugin tears down,\n * the data source is replaced, the cache is purged, or the sort/filter\n * model changes (the previously loaded block coordinates no longer apply).\n */\n private abortAllBlocks(): void {\n for (const controller of this.blockControllers.values()) {\n controller.abort();\n }\n this.blockControllers.clear();\n }\n\n /**\n * Build enrichment params by querying sort/filter models from loaded plugins.\n *\n * When `sortMode === 'local'` the `sortModel` is omitted so scroll-triggered\n * block fetches don't ask the backend for an ordering it isn't applying.\n * Same for `filterMode === 'local'` and `filterModel`.\n */\n private getEnrichmentParams(): Partial<GetRowsParams> {\n const sortLocal = this.config.sortMode === 'local';\n const filterLocal = this.config.filterMode === 'local';\n const sortResults = sortLocal\n ? undefined\n : (this.grid?.query?.('sort:get-model', null) as\n | Array<{ field: string; direction: 'asc' | 'desc' }>[]\n | undefined);\n const filterResults = filterLocal\n ? undefined\n : (this.grid?.query?.('filter:get-model', null) as Record<string, unknown>[] | undefined);\n\n // Fallback to core single-column sort state when no plugin answers the\n // 'sort:get-model' query (e.g. MultiSortPlugin not loaded). Translate the\n // numeric direction (1/-1) to the public 'asc'/'desc' string form.\n let sortModel = sortResults?.[0];\n const host = this.grid as unknown as GridHost | undefined;\n if (!sortLocal && !sortModel && host?._sortState) {\n const { field, direction } = host._sortState;\n sortModel = [{ field, direction: direction === 1 ? 'asc' : 'desc' }];\n }\n\n return {\n sortModel,\n filterModel: filterResults?.[0],\n };\n }\n\n /**\n * Translate visible viewport indices to node-space indices via structural plugins.\n * Falls back to 1:1 mapping (flat data) when no structural plugin responds.\n */\n private getViewportMapping(viewportStart: number, viewportEnd: number): ViewportMappingResponse {\n const query: ViewportMappingQuery = { viewportStart, viewportEnd };\n const results = this.grid?.query?.('datasource:viewport-mapping', query) as ViewportMappingResponse[] | undefined;\n\n // Structural plugin responded — use its mapping\n if (results?.[0]) return results[0];\n\n // No structural plugin → 1:1 mapping (flat data)\n return {\n startNode: viewportStart,\n endNode: viewportEnd,\n totalLoadedNodes: this.totalNodeCount,\n };\n }\n\n /**\n * Handle sort or filter model changes.\n * Purge cache and refetch current viewport with new enrichment params.\n */\n private onModelChange(): void {\n if (!this.dataSource) return;\n this.abortAllBlocks();\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.managedNodes = [];\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n this.requestRender();\n // Eagerly fetch the current viewport with the new enrichment params.\n // Without this, the table stays blank until the user scrolls (no other\n // path triggers a fetch — processRows just rebuilds from cached blocks).\n this.loadRequiredBlocks();\n }\n\n /**\n * Apply server response metadata: resolve totalNodeCount and infinite scroll mode.\n * When `lastNode` is provided, it takes priority and finalizes the total.\n * When `totalNodeCount` is -1, switch to infinite scroll (growing array).\n * When a block returns fewer rows than blockSize, detect end-of-data.\n */\n private applyServerResult(result: GetRowsResult, blockNum: number, blockSize: number): void {\n if (result.lastNode !== undefined) {\n // Server declared the exact end\n this.totalNodeCount = result.lastNode + 1;\n this.infiniteScrollMode = false;\n } else if (result.totalNodeCount === -1) {\n this.infiniteScrollMode = true;\n } else {\n this.totalNodeCount = result.totalNodeCount;\n this.infiniteScrollMode = false;\n }\n\n // Auto-detect end of data: short block means server has no more rows\n if (this.infiniteScrollMode && result.rows.length < blockSize) {\n this.totalNodeCount = blockNum * blockSize + result.rows.length;\n this.infiniteScrollMode = false;\n }\n }\n\n /**\n * Estimate the row count for infinite scroll mode.\n * Returns loaded rows + one extra block of placeholders to trigger next fetch.\n */\n private getInfiniteScrollEstimate(blockSize: number): number {\n let maxLoadedEnd = 0;\n for (const [block, rows] of this.loadedBlocks) {\n const end = block * blockSize + rows.length;\n if (end > maxLoadedEnd) maxLoadedEnd = end;\n }\n return maxLoadedEnd + blockSize;\n }\n\n /**\n * Check current viewport and load any missing blocks.\n */\n private loadRequiredBlocks(): void {\n if (!this.dataSource) return;\n\n const gridRef = this.grid as unknown as GridHost;\n const blockSize = this.config.cacheBlockSize ?? 100;\n\n // Translate viewport to node space via structural plugins\n const viewport = this.getViewportMapping(gridRef._virtualization.start, gridRef._virtualization.end);\n\n // Expand the viewport by loadThreshold in both directions to prefetch\n // blocks the user is about to scroll into. The end is clamped to\n // totalNodeCount when known so we don't request blocks past the end of\n // data — but `totalNodeCount = 0` means \"not yet loaded\" (e.g. just after\n // purgeCache or before the first fetch resolves), in which case we leave\n // it unclamped so the initial block can be fetched.\n const threshold = Math.max(0, this.config.loadThreshold ?? 0);\n const expandedStart = Math.max(0, viewport.startNode - threshold);\n const knownTotal = this.totalNodeCount > 0 ? this.totalNodeCount : Infinity;\n const expandedEnd = Math.min(knownTotal, viewport.endNode + threshold);\n if (expandedEnd <= expandedStart) return;\n\n // Determine which blocks are needed for current viewport (in node space)\n const requiredBlocks = getRequiredBlocks(expandedStart, expandedEnd, blockSize);\n const enrichment = this.getEnrichmentParams();\n const gridId = this.grid?.getAttribute?.('id') ?? undefined;\n\n // Load missing blocks\n for (const blockNum of requiredBlocks) {\n if (this.loadedBlocks.has(blockNum) || this.loadingBlocks.has(blockNum)) {\n continue;\n }\n\n // Check concurrent request limit\n if (this.loadingBlocks.size >= (this.config.maxConcurrentRequests ?? 2)) {\n debugDiagnostic(DATASOURCE_THROTTLED, 'Concurrent request limit reached, deferring block load', gridId);\n break;\n }\n\n this.loadingBlocks.add(blockNum);\n const controller = new AbortController();\n this.blockControllers.set(blockNum, controller);\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: true });\n\n loadBlock(this.dataSource, blockNum, blockSize, enrichment, controller.signal)\n .then((result) => {\n this.blockControllers.delete(blockNum);\n // Drop results from a request that was superseded after the await\n // resolved (data source ignored the abort signal). Without this guard\n // a stale block would land in the cache after a sort/filter change.\n if (controller.signal.aborted) {\n this.loadingBlocks.delete(blockNum);\n if (this.loadingBlocks.size === 0) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n }\n return;\n }\n this.loadedBlocks.set(blockNum, result.rows);\n // Capture pre-update length so we can detect whether processRows must\n // re-run to grow the managedNodes array (e.g. after onModelChange reset).\n const previousManagedLength = this.managedNodes.length;\n this.applyServerResult(result, blockNum, blockSize);\n this.loadingBlocks.delete(blockNum);\n\n // Update managed nodes in place for this block (avoids full processRows rebuild)\n const start = blockNum * blockSize;\n for (let i = 0; i < result.rows.length; i++) {\n if (start + i < this.managedNodes.length) {\n this.managedNodes[start + i] = result.rows[i];\n }\n }\n\n // Broadcast data event with claimed flag\n const detail: DataSourceDataDetail = {\n rows: result.rows,\n totalNodeCount: result.totalNodeCount,\n startNode: start,\n endNode: start + result.rows.length,\n claimed: false,\n };\n this.broadcast('datasource:data', detail);\n\n if (this.loadingBlocks.size === 0) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n }\n\n // If managedNodes still hasn't been sized for the (possibly newly known)\n // totalNodeCount — typically because onModelChange() reset it to 0 right\n // before this fetch — we MUST run processRows to grow the array. The\n // in-place writes above are no-ops in that case (length is 0).\n // Without this, sort/filter changes leave the grid permanently blank\n // until something else triggers a ROWS phase.\n const needsRowModelRebuild =\n previousManagedLength === 0 ||\n this.managedNodes.length < (Number.isFinite(this.totalNodeCount) ? this.totalNodeCount : 0);\n\n if (needsRowModelRebuild) {\n this.requestRender();\n } else {\n // Re-render visible rows without force geometry recalculation.\n // requestVirtualRefresh() skips spacer height writes that cause oscillation\n // with the scheduler's afterRender microtask. Node count hasn't changed —\n // only cached data replaced placeholders — so geometry is stable.\n this.requestVirtualRefresh();\n }\n\n // Re-check with fresh viewport: user may have scrolled further\n this.loadRequiredBlocks();\n })\n .catch((error: unknown) => {\n this.blockControllers.delete(blockNum);\n this.loadingBlocks.delete(blockNum);\n // A superseded request may surface as either an AbortError or as a\n // user-thrown error after the data source detected the abort —\n // suppress diagnostics in both cases. Real failures still surface.\n if (controller.signal.aborted) {\n if (this.loadingBlocks.size === 0) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n }\n return;\n }\n const err = error instanceof Error ? error : new Error(String(error));\n errorDiagnostic(DATASOURCE_FETCH_ERROR, `getRows() failed: ${err.message}`, gridId);\n this.broadcast<DataSourceErrorDetail>('datasource:error', { error: err });\n\n if (this.loadingBlocks.size === 0) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n }\n });\n }\n }\n // #endregion\n\n // #region Hooks\n\n /** @internal */\n override processRows(rows: readonly unknown[]): unknown[] {\n if (!this.dataSource) return [...rows];\n\n const blockSize = this.config.cacheBlockSize ?? 100;\n\n // Guard against invalid totalNodeCount (e.g. undefined from a malformed datasource response).\n // In infinite scroll mode, estimate the total from loaded data + one extra block.\n const nodeCount = this.infiniteScrollMode\n ? this.getInfiniteScrollEstimate(blockSize)\n : Number.isFinite(this.totalNodeCount) && this.totalNodeCount >= 0\n ? this.totalNodeCount\n : 0;\n\n // Grow array with stable placeholder objects (created once, reused across renders)\n while (this.managedNodes.length < nodeCount) {\n const i = this.managedNodes.length;\n this.managedNodes.push({ __loading: true, __index: i });\n }\n // Shrink if total decreased\n this.managedNodes.length = nodeCount;\n\n // Replace placeholders with cached data (stable refs for unchanged entries)\n for (let i = 0; i < nodeCount; i++) {\n const cached = getRowFromCache(i, blockSize, this.loadedBlocks);\n if (cached) {\n this.managedNodes[i] = cached;\n }\n }\n\n // Local-mode sort: when sortMode === 'local' the plugin owns ordering of\n // the loaded rows itself (core sort ran on the input but we discarded it\n // by returning managedNodes). Apply the current core sort state on top of\n // managedNodes so the user sees in-place sorting without a refetch.\n // Note: any plugin sort that runs after us (priority > -10, e.g. multi-sort)\n // will further re-sort our output, which is the intended chain.\n const host = this.grid as unknown as GridHost | undefined;\n if (this.config.sortMode === 'local' && host?._sortState) {\n const columns = (host._columns ?? []) as ColumnConfig[];\n // Honor user's gridConfig.sortHandler when provided — same resolution\n // as core's reapplyCoreSort/applySort. processRows is synchronous, so\n // async handlers cannot be awaited here: keep the current managedNodes\n // order for this pass and swallow any rejection so it doesn't surface\n // as an unhandled promise rejection. The next core sort cycle (or the\n // next block load) will re-invoke this path with up-to-date state.\n const handler = host.effectiveConfig?.sortHandler ?? builtInSort;\n const result = handler(this.managedNodes, host._sortState, columns);\n if (result && typeof (result as Promise<unknown[]>).then === 'function') {\n void (result as Promise<unknown[]>).catch(() => undefined);\n return this.managedNodes;\n }\n return result as unknown[];\n }\n\n return this.managedNodes;\n }\n\n /** @internal */\n override onScroll(event: ScrollEvent): void {\n if (!this.dataSource) return;\n\n // Immediate check for blocks\n this.loadRequiredBlocks();\n\n // Debounce: when scrolling stops, do a final check with fresh viewport\n if (this.scrollDebounceTimer) {\n clearTimeout(this.scrollDebounceTimer);\n }\n this.scrollDebounceTimer = setTimeout(() => {\n this.loadRequiredBlocks();\n }, SCROLL_DEBOUNCE_MS);\n }\n\n /** @internal */\n override handleQuery(query: PluginQuery): unknown {\n switch (query.type) {\n case 'datasource:is-active':\n return this.dataSource != null;\n\n case 'datasource:fetch-children': {\n const { context } = query.context as FetchChildrenQuery;\n this.fetchChildren(context);\n return undefined;\n }\n }\n return undefined;\n }\n // #endregion\n\n // #region Child Data Fetching\n\n /**\n * Fetch child rows via the datasource and broadcast the result.\n */\n private fetchChildren(context: { source: string; [key: string]: unknown }): void {\n if (!this.dataSource) return;\n\n const gridId = this.grid?.getAttribute?.('id') ?? undefined;\n\n if (!this.dataSource.getChildRows) {\n warnDiagnostic(\n DATASOURCE_NO_CHILD_HANDLER,\n `Plugin \"${context.source}\" requested child rows but getChildRows() is not implemented on the dataSource`,\n gridId,\n );\n return;\n }\n\n const enrichment = this.getEnrichmentParams();\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: true, context });\n\n this.dataSource\n .getChildRows({ context, sortModel: enrichment.sortModel, filterModel: enrichment.filterModel })\n .then((result) => {\n const detail: DataSourceChildrenDetail = {\n rows: result.rows,\n context,\n claimed: false,\n };\n this.broadcast('datasource:children', detail);\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false, context });\n })\n .catch((error: unknown) => {\n const err = error instanceof Error ? error : new Error(String(error));\n errorDiagnostic(DATASOURCE_CHILD_FETCH_ERROR, `getChildRows() failed: ${err.message}`, gridId);\n this.broadcast<DataSourceErrorDetail>('datasource:error', { error: err, context });\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false, context });\n });\n }\n // #endregion\n\n // #region Public API\n\n /**\n * Set the data source for server-side loading.\n * @param dataSource - Data source implementing the getRows method (and optionally getChildRows)\n */\n setDataSource(dataSource: ServerSideDataSource): void {\n this.abortAllBlocks();\n this.dataSource = dataSource;\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.managedNodes = [];\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n\n // Load first block with enrichment params\n const blockSize = this.config.cacheBlockSize ?? 100;\n const enrichment = this.getEnrichmentParams();\n const gridId = this.grid?.getAttribute?.('id') ?? undefined;\n const controller = new AbortController();\n this.blockControllers.set(0, controller);\n this.loadingBlocks.add(0);\n\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: true });\n\n loadBlock(dataSource, 0, blockSize, enrichment, controller.signal)\n .then((result) => {\n this.blockControllers.delete(0);\n this.loadingBlocks.delete(0);\n if (controller.signal.aborted) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n return;\n }\n this.loadedBlocks.set(0, result.rows);\n this.applyServerResult(result, 0, blockSize);\n\n const detail: DataSourceDataDetail = {\n rows: result.rows,\n totalNodeCount: result.totalNodeCount,\n startNode: 0,\n endNode: result.rows.length,\n claimed: false,\n };\n this.broadcast('datasource:data', detail);\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n this.requestRender();\n\n // When loadThreshold is configured, re-check the viewport so the\n // prefetch can pull in additional blocks immediately rather than\n // waiting for the user's first scroll. Gated on the threshold to\n // preserve the historical \"first fetch loads block 0 only\" behavior.\n if ((this.config.loadThreshold ?? 0) > 0) {\n this.loadRequiredBlocks();\n }\n })\n .catch((error: unknown) => {\n this.blockControllers.delete(0);\n this.loadingBlocks.delete(0);\n if (controller.signal.aborted) {\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n return;\n }\n const err = error instanceof Error ? error : new Error(String(error));\n errorDiagnostic(DATASOURCE_FETCH_ERROR, `getRows() failed: ${err.message}`, gridId);\n this.broadcast<DataSourceErrorDetail>('datasource:error', { error: err });\n this.broadcast<DataSourceLoadingDetail>('datasource:loading', { loading: false });\n });\n }\n\n /**\n * Refresh all data from the server.\n * Purges cache and refetches from block 0.\n */\n refresh(): void {\n if (!this.dataSource) return;\n const ds = this.dataSource;\n this.abortAllBlocks();\n this.loadedBlocks.clear();\n this.loadingBlocks.clear();\n this.managedNodes = [];\n this.totalNodeCount = 0;\n this.infiniteScrollMode = false;\n // Re-trigger load via setDataSource which handles enrichment and broadcasting\n this.setDataSource(ds);\n }\n\n /**\n * Clear all cached data without refreshing.\n */\n purgeCache(): void {\n this.abortAllBlocks();\n this.loadedBlocks.clear();\n this.managedNodes = [];\n }\n\n /**\n * Get the total node count from the server.\n */\n getTotalNodeCount(): number {\n return this.totalNodeCount;\n }\n\n /**\n * @deprecated Use {@link getTotalNodeCount} instead. Will be removed in a future version.\n */\n getTotalRowCount(): number {\n return this.totalNodeCount;\n }\n\n /**\n * Check if a specific node is loaded in the cache.\n * @param nodeIndex - Node index to check\n */\n isNodeLoaded(nodeIndex: number): boolean {\n const blockSize = this.config.cacheBlockSize ?? 100;\n const blockNum = getBlockNumber(nodeIndex, blockSize);\n return this.loadedBlocks.has(blockNum);\n }\n\n /**\n * @deprecated Use {@link isNodeLoaded} instead. Will be removed in a future version.\n */\n isRowLoaded(rowIndex: number): boolean {\n return this.isNodeLoaded(rowIndex);\n }\n\n /**\n * Get the number of loaded cache blocks.\n */\n getLoadedBlockCount(): number {\n return this.loadedBlocks.size;\n }\n // #endregion\n}\n"],"names":["getBlockNumber","nodeIndex","blockSize","Math","floor","makeAbortError","DOMException","loadBlock","dataSource","blockNumber","params","signal","range","start","end","getBlockRange","source","aborted","Promise","reject","value","subscribe","then","resolve","onAbort","addEventListener","once","v","removeEventListener","e","settled","subscription","next","unsubscribe","error","err","complete","Error","toResultPromise","getRows","startNode","endNode","sortModel","filterModel","getRowFromCache","loadedBlocks","block","get","ServerSidePlugin","BaseGridPlugin","static","modifiesRowStructure","hookPriority","processRows","incompatibleWith","name","reason","events","type","description","queries","defaultConfig","pageSize","cacheBlockSize","maxConcurrentRequests","totalNodeCount","infiniteScrollMode","Map","loadingBlocks","Set","blockControllers","lastRequestId","scrollDebounceTimer","managedNodes","attach","grid","super","this","on","config","sortMode","requestRender","onModelChange","filterMode","setDataSource","detach","clear","abortAllBlocks","clearTimeout","controller","values","abort","getEnrichmentParams","sortLocal","filterLocal","sortResults","query","filterResults","host","_sortState","field","direction","getViewportMapping","viewportStart","viewportEnd","results","totalLoadedNodes","loadRequiredBlocks","applyServerResult","result","blockNum","lastNode","rows","length","getInfiniteScrollEstimate","maxLoadedEnd","gridRef","viewport","_virtualization","threshold","max","loadThreshold","expandedStart","knownTotal","Infinity","expandedEnd","min","requiredBlocks","startBlock","endBlock","blocks","i","push","getRequiredBlocks","enrichment","gridId","getAttribute","has","size","debugDiagnostic","DATASOURCE_THROTTLED","add","AbortController","set","broadcast","loading","delete","previousManagedLength","detail","claimed","Number","isFinite","requestVirtualRefresh","catch","String","errorDiagnostic","DATASOURCE_FETCH_ERROR","message","nodeCount","__loading","__index","cached","columns","_columns","effectiveConfig","sortHandler","builtInSort","onScroll","event","setTimeout","handleQuery","context","fetchChildren","getChildRows","warnDiagnostic","DATASOURCE_NO_CHILD_HANDLER","DATASOURCE_CHILD_FETCH_ERROR","refresh","ds","purgeCache","getTotalNodeCount","getTotalRowCount","isNodeLoaded","isRowLoaded","rowIndex","getLoadedBlockCount"],"mappings":"8fAEO,SAASA,EAAeC,EAAmBC,GAChD,OAAOC,KAAKC,MAAMH,EAAYC,EAChC,CA8BA,SAASG,IACP,OAAO,IAAIC,aAAa,UAAW,aACrC,CAmEO,SAASC,EACdC,EACAC,EACAP,EACAQ,EACAC,GAEA,MAAMC,EAxGD,SAAuBH,EAAqBP,GACjD,MAAO,CACLW,MAAOJ,EAAcP,EACrBY,KAAML,EAAc,GAAKP,EAE7B,CAmGgBa,CAAcN,EAAaP,GASzC,OA1EK,SACLc,EACAL,GAEA,OAAIA,EAAOM,QACFC,QAAQC,OAAOd,KAxBL,iBAFMe,EA4BFJ,IAzBX,OAAVI,GACwD,mBAAhDA,EAAkCC,WAEI,mBAAtCD,EAA6BE,KAyB9B,IAAIJ,QAAW,CAACK,EAASJ,KAC9B,MAAMK,EAAU,IAAML,EAAOd,KAC7BM,EAAOc,iBAAiB,QAASD,EAAS,CAAEE,MAAM,IAClDR,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,QAClBT,KAAKT,cAAckB,QACnBT,KAAKJ,aAAe,GACpBI,KAAKZ,eAAiB,EACtBY,KAAKX,oBAAqB,EAE1BW,KAAKO,cAAc8F,EACrB,CAKA,UAAAC,GACEtG,KAAKU,iBACLV,KAAKhC,aAAayC,QAClBT,KAAKJ,aAAe,EACtB,CAKA,iBAAA2G,GACE,OAAOvG,KAAKZ,cACd,CAKA,gBAAAoH,GACE,OAAOxG,KAAKZ,cACd,CAMA,YAAAqH,CAAarL,GACX,MACM6G,EAAW9G,EAAeC,EADd4E,KAAKE,OAAOhB,gBAAkB,KAEhD,OAAOc,KAAKhC,aAAa4F,IAAI3B,EAC/B,CAKA,WAAAyE,CAAYC,GACV,OAAO3G,KAAKyG,aAAaE,EAC3B,CAKA,mBAAAC,GACE,OAAO5G,KAAKhC,aAAa6F,IAC3B"}
@@ -1,2 +1,2 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("../../core/constants"),require("../../core/plugin/base-plugin")):"function"==typeof define&&define.amd?define(["exports","../../core/constants","../../core/plugin/base-plugin"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TbwGridPlugin_tree={},e.TbwGrid,e.TbwGrid)}(this,function(e,t,n){"use strict";function r(e,t,n){return void 0!==e.id?String(e.id):n?`${n}-${t}`:String(t)}function i(e,t){const n=new Set(e);return n.has(t)?n.delete(t):n.add(t),n}function s(e,t,n=null,i=0){const a=t.childrenField??"children",o=new Set;for(let d=0;d<e.length;d++){const l=e[d],h=r(l,d,n),c=l[a];if(Array.isArray(c)&&c.length>0){o.add(h);const e=s(c,t,h,i+1);for(const t of e)o.add(t)}}return o}function a(e,t,n,i=null,s=0){const o=n.childrenField??"children";for(let d=0;d<e.length;d++){const l=e[d],h=r(l,d,i);if(h===t)return[h];const c=l[o];if(Array.isArray(c)&&c.length>0){const e=a(c,t,n,h,s+1);if(e)return[h,...e]}}return null}function o(e,t,n,r){const i=a(e,t,n);if(!i)return r;const s=new Set(r);for(let a=0;a<i.length-1;a++)s.add(i[a]);return s}function d(e,t){const n=Math.min(t,e.length-1);if(n<0)return 0;let r=n;for(;r>0&&e[r].depth>0;)r--;let i=0;for(let s=0;s<=r;s++)0===e[s].depth&&i++;return i-1}function l(e,t="children"){if(!Array.isArray(e)||0===e.length)return!1;for(const n of e){if(!n)continue;const e=n[t];if(Array.isArray(e)&&e.length>0)return!0;if(null!=e&&!Array.isArray(e)&&e)return!0}return!1}class h extends n.BaseGridPlugin{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=new Set;initialExpansionDone=!1;flattenedRows=[];rowKeyMap=new Map;previousVisibleKeys=new Set;keysToAnimate=new Set;sortState=null;loadingKeys=new Set;originalTreeColumnRenderer;wrappedTreeColumnField;detach(){this.expandedKeys.clear(),this.initialExpansionDone=!1,this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),this.keysToAnimate.clear(),this.sortState=null,this.loadingKeys.clear(),this.originalTreeColumnRenderer=void 0,this.wrappedTreeColumnField=void 0}handleQuery(e){if("canMoveRow"===e.type){const t=e.context,n=this.config.childrenField??"children",r=t?.[n];if(Array.isArray(r)&&r.length>0)return!1}if("datasource:viewport-mapping"===e.type){const{viewportStart:t,viewportEnd:n}=e.context;if(0===this.flattenedRows.length)return;return{startNode:d(this.flattenedRows,t),endNode:d(this.flattenedRows,n)+1,totalLoadedNodes:function(e){let t=0;for(const n of e)0===n.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 n=t.context.parentNode;if(n){n[this.config.childrenField??"children"]=t.rows;const e=n.__stableKey??String(n.id??"?");this.loadingKeys.delete(e),this.requestRender()}})}get animationStyle(){return!!this.isAnimationEnabled&&(this.config.animation??"slide")}detect(e){if(!this.config.autoDetect)return!1;const t=e,n=this.config.childrenField??function(e){if(!Array.isArray(e)||0===e.length)return null;const t=["children","items","nodes","subRows","nested"];for(const n of e)if(n&&"object"==typeof n)for(const e of t){const t=n[e];if(Array.isArray(t)&&t.length>0)return e}return null}(t)??"children";return l(t,n)}processRows(e){const t=this.config.childrenField??"children",n=e;if(0===n.length||!l(n,t))return this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),[...e];let r=this.withStableKeys(n);const i=this.resolveEffectiveSortState();i&&(r=this.sortTree(r,i.field,i.direction)),this.config.defaultExpanded&&!this.initialExpansionDone&&(this.expandedKeys=s(r,this.config),this.initialExpansionDone=!0),this.flattenedRows=this.flattenTree(r,this.expandedKeys),this.rowKeyMap.clear(),this.keysToAnimate.clear();const a=new Set;for(const s of this.flattenedRows)this.rowKeyMap.set(s.key,s),a.add(s.key),!this.previousVisibleKeys.has(s.key)&&s.depth>0&&this.keysToAnimate.add(s.key);return this.previousVisibleKeys=a,this.flattenedRows.map(e=>({...e.data,__treeKey:e.key,__treeDepth:e.depth,__treeHasChildren:e.hasChildren,__treeExpanded:e.isExpanded}))}withStableKeys(e,t=null){const n=this.config.childrenField??"children";return e.map((e,r)=>{const i=e.__stableKey,s=void 0!==e.id?String(e.id):i??(t?`${t}-${r}`:String(r)),a=e[n],o=Array.isArray(a)&&a.length>0;return{...e,__stableKey:s,...o?{[n]:this.withStableKeys(a,s)}:{}}})}flattenTree(e,t,n=0){const r=this.config.childrenField??"children",i=[];for(const s of e){const e=s.__stableKey??String(s.id??"?"),a=s[r],o=Array.isArray(a)&&a.length>0,d=null!=a&&!Array.isArray(a)&&!!a,l=o||d,h=t.has(e);i.push({key:e,data:s,depth:n,hasChildren:l,isExpanded:h,parentKey:n>0&&e.substring(0,e.lastIndexOf("-"))||null}),o&&h&&i.push(...this.flattenTree(a,t,n+1))}return i}requestLazyChildren(e){if(this.loadingKeys.has(e.key))return;const t=this.config.childrenField??"children",n=e.data[t];if(Array.isArray(n)&&n.length>0)return;const r=this.grid?.query?.("datasource:is-active",null);r&&(this.loadingKeys.add(e.key),this.grid.query("datasource:fetch-children",{context:{source:"tree",parentNode:e.data,nodePath:[e.key]}}))}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,n){const r=this.config.childrenField??"children";return[...e].sort((e,r)=>{const i=e[t],s=r[t];return null==i&&null==s?0:null==i?-1:null==s?1:i>s?n:i<s?-n:0}).map(e=>{const i=e[r];return Array.isArray(i)&&i.length>0?{...e,[r]:this.sortTree(i,t,n)}:e})}processColumns(e){if(0===this.flattenedRows.length)return[...e];const n=[...e];if(0===n.length)return n;const{treeColumn:r}=this.config;let i=0;if(r){const e=n.findIndex(e=>e.field===r);e>=0&&(i=e)}const s=n[i],a=s.field;this.wrappedTreeColumnField!==a&&(this.originalTreeColumnRenderer=s.viewRenderer,this.wrappedTreeColumnField=a);const o=this.originalTreeColumnRenderer,d=()=>this.config,l=this.setIcon.bind(this);return n[i]={...s,viewRenderer:e=>{const{row:n,value:r}=e,{showExpandIcons:i=!0,indentWidth:s}=d(),a=n,h=a.__treeDepth??0,c=document.createElement("span");if(c.className="tree-cell-wrapper",c.style.setProperty("--tbw-tree-depth",String(h)),void 0!==s&&c.style.setProperty("--tbw-tree-indent-width",`${s}px`),i)if(a.__treeHasChildren){const e=document.createElement("span");e.className=`${t.GridClasses.TREE_TOGGLE}${a.__treeExpanded?` ${t.GridClasses.EXPANDED}`:""}`,l(e,a.__treeExpanded?"collapse":"expand"),e.setAttribute("data-tree-key",String(a.__treeKey??"")),c.appendChild(e)}else{const e=document.createElement("span");e.className="tree-spacer",c.appendChild(e)}const p=document.createElement("span");if(p.className="tree-content",o){const t=o(e);t instanceof Node?p.appendChild(t):"string"==typeof t&&(p.innerHTML=t)}else p.textContent=null!=r?String(r):"";return c.appendChild(p),c}},n}onCellClick(e){const n=e.originalEvent?.target;if(!n?.classList.contains(t.GridClasses.TREE_TOGGLE))return!1;const r=n.getAttribute("data-tree-key");if(!r)return!1;const s=this.rowKeyMap.get(r);return!!s&&(this.expandedKeys=i(this.expandedKeys,r),this.expandedKeys.has(r)&&this.requestLazyChildren(s),this.broadcast("tree-expand",{key:r,row:s.data,expanded:this.expandedKeys.has(r),depth:s.depth,expandedKeys:[...this.expandedKeys]}),this.requestRender(),!0)}onKeyDown(e){if(" "!==e.key)return;const t=this.grid._focusRow,n=this.flattenedRows[t];return n?.hasChildren?(e.preventDefault(),this.expandedKeys=i(this.expandedKeys,n.key),this.expandedKeys.has(n.key)&&this.requestLazyChildren(n),this.broadcast("tree-expand",{key:n.key,row:n.data,expanded:this.expandedKeys.has(n.key),depth:n.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:n}=e.column;this.sortState&&this.sortState.field===n?1===this.sortState.direction?this.sortState={field:n,direction:-1}:this.sortState=null:this.sortState={field:n,direction:1};const r=this.grid;return void 0!==r._sortState&&(r._sortState=this.sortState?{...this.sortState}:null),this.broadcast("sort-change",{field:n,direction:this.sortState?.direction??0}),this.requestRender(),!0}afterRender(){const e=this.gridElement?.querySelector(".rows");if(!e)return;const t=this.animationStyle,n=!1!==t&&this.keysToAnimate.size>0,r="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)),n&&s?.key&&this.keysToAnimate.has(s.key)&&(i.classList.add(r),i.addEventListener("animationend",()=>i.classList.remove(r),{once:!0}))}this.keysToAnimate.clear()}expand(e){this.expandedKeys.add(e);const t=this.rowKeyMap.get(e);t&&this.requestLazyChildren(t),this.requestRender()}collapse(e){this.expandedKeys.delete(e),this.requestRender()}toggle(e){this.expandedKeys=i(this.expandedKeys,e);const t=this.rowKeyMap.get(e);t?(this.expandedKeys.has(e)&&this.requestLazyChildren(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=s(this.rows,this.config),this.emitPluginEvent("tree-expand",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}collapseAll(){this.expandedKeys=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=o(this.rows,e,this.config,this.expandedKeys),this.requestRender()}}e.TreePlugin=h,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("../../core/constants"),require("../../core/internal/sorting"),require("../../core/plugin/base-plugin")):"function"==typeof define&&define.amd?define(["exports","../../core/constants","../../core/internal/sorting","../../core/plugin/base-plugin"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TbwGridPlugin_tree={},e.TbwGrid,e.TbwGrid,e.TbwGrid)}(this,function(e,t,n,i){"use strict";function r(e,t,n){return void 0!==e.id?String(e.id):n?`${n}-${t}`:String(t)}function s(e,t){const n=new Set(e);return n.has(t)?n.delete(t):n.add(t),n}function a(e,t,n=null,i=0){const s=t.childrenField??"children",o=new Set;for(let d=0;d<e.length;d++){const l=e[d],h=r(l,d,n),c=l[s];if(Array.isArray(c)&&c.length>0){o.add(h);const e=a(c,t,h,i+1);for(const t of e)o.add(t)}}return o}function o(e,t,n,i=null,s=0){const a=n.childrenField??"children";for(let d=0;d<e.length;d++){const l=e[d],h=r(l,d,i);if(h===t)return[h];const c=l[a];if(Array.isArray(c)&&c.length>0){const e=o(c,t,n,h,s+1);if(e)return[h,...e]}}return null}function d(e,t,n,i){const r=o(e,t,n);if(!r)return i;const s=new Set(i);for(let a=0;a<r.length-1;a++)s.add(r[a]);return s}function l(e,t){const n=Math.min(t,e.length-1);if(n<0)return 0;let i=n;for(;i>0&&e[i].depth>0;)i--;let r=0;for(let s=0;s<=i;s++)0===e[s].depth&&r++;return r-1}function h(e,t="children"){if(!Array.isArray(e)||0===e.length)return!1;for(const n of e){if(!n)continue;const e=n[t];if(Array.isArray(e)&&e.length>0)return!0;if(null!=e&&!Array.isArray(e)&&e)return!0}return!1}class c extends i.BaseGridPlugin{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=new Set;initialExpansionDone=!1;flattenedRows=[];rowKeyMap=new Map;previousVisibleKeys=new Set;keysToAnimate=new Set;sortState=null;loadingKeys=new Set;#e=new WeakMap;#t=new WeakMap;originalTreeColumnRenderer;wrappedTreeColumnField;detach(){this.expandedKeys.clear(),this.initialExpansionDone=!1,this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),this.keysToAnimate.clear(),this.sortState=null,this.loadingKeys.clear(),this.originalTreeColumnRenderer=void 0,this.wrappedTreeColumnField=void 0}handleQuery(e){if("canMoveRow"===e.type){const t=e.context,n=this.config.childrenField??"children",i=t?.[n];if(Array.isArray(i)&&i.length>0)return!1}if("datasource:viewport-mapping"===e.type){const{viewportStart:t,viewportEnd:n}=e.context;if(0===this.flattenedRows.length)return;return{startNode:l(this.flattenedRows,t),endNode:l(this.flattenedRows,n)+1,totalLoadedNodes:function(e){let t=0;for(const n of e)0===n.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 n=t.context.parentNode;if(n){n[this.config.childrenField??"children"]=t.rows;const e=this.#e.get(n)??String(n.id??"?");this.loadingKeys.delete(e),this.requestRender()}})}get animationStyle(){return!!this.isAnimationEnabled&&(this.config.animation??"slide")}detect(e){if(!this.config.autoDetect)return!1;const t=e,n=this.config.childrenField??function(e){if(!Array.isArray(e)||0===e.length)return null;const t=["children","items","nodes","subRows","nested"];for(const n of e)if(n&&"object"==typeof n)for(const e of t){const t=n[e];if(Array.isArray(t)&&t.length>0)return e}return null}(t)??"children";return h(t,n)}processRows(e){const t=this.config.childrenField??"children",n=e;if(0===n.length||!h(n,t))return this.flattenedRows=[],this.rowKeyMap.clear(),this.previousVisibleKeys.clear(),[...e];const i=this.resolveEffectiveSortState();this.config.defaultExpanded&&!this.initialExpansionDone&&(this.expandedKeys=a(n,this.config),this.initialExpansionDone=!0),this.flattenedRows=this.#n(n,this.expandedKeys,i,null,0),this.#t=new WeakMap,this.rowKeyMap.clear(),this.keysToAnimate.clear();const r=new Set;for(const s of this.flattenedRows)this.rowKeyMap.set(s.key,s),this.#t.set(s.data,s),r.add(s.key),!this.previousVisibleKeys.has(s.key)&&s.depth>0&&this.keysToAnimate.add(s.key);return this.previousVisibleKeys=r,this.flattenedRows.map(e=>e.data)}#i(e,t,n){if(void 0!==e.id){const t=String(e.id);return this.#e.set(e,t),t}const i=this.#e.get(e);if(void 0!==i)return i;const r=n?`${n}-${t}`:String(t);return this.#e.set(e,r),r}#n(e,t,n,i,r){const s=this.config.childrenField??"children";for(let d=0;d<e.length;d++)this.#i(e[d],d,i);const a=n?this.#r(e,n.field,n.direction):e,o=[];for(let d=0;d<a.length;d++){const e=a[d],l=this.#i(e,d,i),h=e[s],c=Array.isArray(h)&&h.length>0,p=null!=h&&!Array.isArray(h)&&!!h,u=c||p,f=t.has(l);o.push({key:l,data:e,depth:r,hasChildren:u,isExpanded:f,parentKey:i}),c&&f&&o.push(...this.#n(h,t,n,l,r+1))}return o}#r(e,t,i){const r=this.grid,s=r?._columns??[],a={field:t,direction:i},o=[...e],d=(r?.effectiveConfig?.sortHandler??n.builtInSort)(o,a,s);return d&&"function"==typeof d.then?(d.catch(()=>{}),n.builtInSort(o,a,s)):d}requestLazyChildren(e){if(this.loadingKeys.has(e.key))return;const t=this.config.childrenField??"children",n=e.data[t];if(Array.isArray(n)&&n.length>0)return;const i=this.grid?.query?.("datasource:is-active",null);i&&(this.loadingKeys.add(e.key),this.grid.query("datasource:fetch-children",{context:{source:"tree",parentNode:e.data,nodePath:[e.key]}}))}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}processColumns(e){if(0===this.flattenedRows.length)return[...e];const n=[...e];if(0===n.length)return n;const{treeColumn:i}=this.config;let r=0;if(i){const e=n.findIndex(e=>e.field===i);e>=0&&(r=e)}const s=n[r],a=s.field;this.wrappedTreeColumnField!==a&&(this.originalTreeColumnRenderer=s.viewRenderer,this.wrappedTreeColumnField=a);const o=this.originalTreeColumnRenderer,d=()=>this.config,l=this.setIcon.bind(this);return n[r]={...s,viewRenderer:e=>{const{row:n,value:i}=e,{showExpandIcons:r=!0,indentWidth:s}=d(),a=this.#t.get(n),h=a?.depth??0,c=document.createElement("span");if(c.className="tree-cell-wrapper",c.style.setProperty("--tbw-tree-depth",String(h)),void 0!==s&&c.style.setProperty("--tbw-tree-indent-width",`${s}px`),r)if(a&&a.hasChildren){const e=document.createElement("span");e.className=`${t.GridClasses.TREE_TOGGLE}${a.isExpanded?` ${t.GridClasses.EXPANDED}`:""}`,l(e,a.isExpanded?"collapse":"expand"),e.setAttribute("data-tree-key",a.key),c.appendChild(e)}else{const e=document.createElement("span");e.className="tree-spacer",c.appendChild(e)}const p=document.createElement("span");if(p.className="tree-content",o){const t=o(e);t instanceof Node?p.appendChild(t):"string"==typeof t&&(p.innerHTML=t)}else p.textContent=null!=i?String(i):"";return c.appendChild(p),c}},n}onCellClick(e){const n=e.originalEvent?.target;if(!n?.classList.contains(t.GridClasses.TREE_TOGGLE))return!1;const i=n.getAttribute("data-tree-key");if(!i)return!1;const r=this.rowKeyMap.get(i);return!!r&&(this.expandedKeys=s(this.expandedKeys,i),this.expandedKeys.has(i)&&this.requestLazyChildren(r),this.broadcast("tree-expand",{key:i,row:r.data,expanded:this.expandedKeys.has(i),depth:r.depth,expandedKeys:[...this.expandedKeys]}),this.requestRender(),!0)}onKeyDown(e){if(" "!==e.key)return;const t=this.grid._focusRow,n=this.flattenedRows[t];return n?.hasChildren?(e.preventDefault(),this.expandedKeys=s(this.expandedKeys,n.key),this.expandedKeys.has(n.key)&&this.requestLazyChildren(n),this.broadcast("tree-expand",{key:n.key,row:n.data,expanded:this.expandedKeys.has(n.key),depth:n.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:n}=e.column;this.sortState&&this.sortState.field===n?1===this.sortState.direction?this.sortState={field:n,direction:-1}:this.sortState=null:this.sortState={field:n,direction:1};const i=this.grid;return void 0!==i._sortState&&(i._sortState=this.sortState?{...this.sortState}:null),this.broadcast("sort-change",{field:n,direction:this.sortState?.direction??0}),this.requestRender(),!0}afterRender(){const e=this.gridElement?.querySelector(".rows");if(!e)return;const t=this.animationStyle,n=!1!==t&&this.keysToAnimate.size>0,i="fade"===t?"tbw-tree-fade-in":"tbw-tree-slide-in";for(const r of e.querySelectorAll(".data-grid-row")){const e=r.querySelector(".cell[data-row]"),t=e?parseInt(e.getAttribute("data-row")??"-1",10):-1,s=this.flattenedRows[t];s?.hasChildren&&r.setAttribute("aria-expanded",String(s.isExpanded)),n&&s?.key&&this.keysToAnimate.has(s.key)&&(r.classList.add(i),r.addEventListener("animationend",()=>r.classList.remove(i),{once:!0}))}this.keysToAnimate.clear()}expand(e){this.expandedKeys.add(e);const t=this.rowKeyMap.get(e);t&&this.requestLazyChildren(t),this.requestRender()}collapse(e){this.expandedKeys.delete(e),this.requestRender()}toggle(e){this.expandedKeys=s(this.expandedKeys,e);const t=this.rowKeyMap.get(e);t?(this.expandedKeys.has(e)&&this.requestLazyChildren(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=a(this.rows,this.config),this.emitPluginEvent("tree-expand",{expandedKeys:[...this.expandedKeys]}),this.requestRender()}collapseAll(){this.expandedKeys=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]}getRowMeta(e){return this.#t.get(e)}getRowByKey(e){return this.rowKeyMap.get(e)?.data}expandToKey(e){this.expandedKeys=d(this.rows,e,this.config,this.expandedKeys),this.requestRender()}}e.TreePlugin=c,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
2
2
  //# sourceMappingURL=tree.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tree.umd.js","sources":["../../../../../libs/grid/src/lib/plugins/tree/tree-data.ts","../../../../../libs/grid/src/lib/plugins/tree/tree-datasource.ts","../../../../../libs/grid/src/lib/plugins/tree/tree-detect.ts","../../../../../libs/grid/src/lib/plugins/tree/TreePlugin.ts"],"sourcesContent":["/**\n * Core Tree Data Logic\n *\n * Pure functions for tree flattening, expansion, and traversal.\n */\n\nimport type { FlattenedTreeRow, TreeConfig, TreeRow } from './types';\n\n/**\n * Generates a unique key for a row.\n * Uses row.id if available, otherwise generates from path.\n */\nexport function generateRowKey(row: TreeRow, index: number, parentKey: string | null): string {\n if (row.id !== undefined) return String(row.id);\n return parentKey ? `${parentKey}-${index}` : String(index);\n}\n\n/**\n * Flattens a hierarchical tree into a flat array of rows with metadata.\n * Only includes children of expanded nodes.\n */\nexport function flattenTree(\n rows: readonly TreeRow[],\n config: TreeConfig,\n expandedKeys: Set<string>,\n parentKey: string | null = null,\n depth = 0,\n): FlattenedTreeRow[] {\n const childrenField = config.childrenField ?? 'children';\n const result: FlattenedTreeRow[] = [];\n\n for (let i = 0; i < rows.length; i++) {\n const row = rows[i];\n const key = generateRowKey(row, i, parentKey);\n const children = row[childrenField];\n const hasChildren = Array.isArray(children) && children.length > 0;\n const isExpanded = expandedKeys.has(key);\n\n result.push({\n key,\n data: row,\n depth,\n hasChildren,\n isExpanded,\n parentKey,\n });\n\n // Recursively add children if expanded\n if (hasChildren && isExpanded) {\n const childRows = flattenTree(children as TreeRow[], config, expandedKeys, key, depth + 1);\n result.push(...childRows);\n }\n }\n\n return result;\n}\n\n/**\n * Toggles the expansion state of a row.\n * Returns a new Set with the toggled state.\n */\nexport function toggleExpand(expandedKeys: Set<string>, key: string): Set<string> {\n const newExpanded = new Set(expandedKeys);\n if (newExpanded.has(key)) {\n newExpanded.delete(key);\n } else {\n newExpanded.add(key);\n }\n return newExpanded;\n}\n\n/**\n * Expands all nodes in the tree.\n * Returns a Set of all parent row keys.\n */\nexport function expandAll(\n rows: readonly TreeRow[],\n config: TreeConfig,\n parentKey: string | null = null,\n depth = 0,\n): Set<string> {\n const childrenField = config.childrenField ?? 'children';\n const keys = new Set<string>();\n\n for (let i = 0; i < rows.length; i++) {\n const row = rows[i];\n const key = generateRowKey(row, i, parentKey);\n const children = row[childrenField];\n\n if (Array.isArray(children) && children.length > 0) {\n keys.add(key);\n const childKeys = expandAll(children as TreeRow[], config, key, depth + 1);\n for (const k of childKeys) keys.add(k);\n }\n }\n\n return keys;\n}\n\n/**\n * Collapses all nodes.\n * Returns an empty Set.\n */\nexport function collapseAll(): Set<string> {\n return new Set();\n}\n\n/**\n * Gets all descendants of a node from the flattened row list.\n * Useful for operations that need to affect an entire subtree.\n */\nexport function getDescendants(flattenedRows: FlattenedTreeRow[], parentKey: string): FlattenedTreeRow[] {\n const descendants: FlattenedTreeRow[] = [];\n let collecting = false;\n let parentDepth = -1;\n\n for (const row of flattenedRows) {\n if (row.key === parentKey) {\n collecting = true;\n parentDepth = row.depth;\n continue;\n }\n\n if (collecting) {\n if (row.depth > parentDepth) {\n descendants.push(row);\n } else {\n break; // No longer a descendant\n }\n }\n }\n\n return descendants;\n}\n\n/**\n * Finds the path from root to a specific row key.\n * Returns an array of keys from root to the target (inclusive).\n */\nexport function getPathToKey(\n rows: readonly TreeRow[],\n targetKey: string,\n config: TreeConfig,\n parentKey: string | null = null,\n depth = 0,\n): string[] | null {\n const childrenField = config.childrenField ?? 'children';\n\n for (let i = 0; i < rows.length; i++) {\n const row = rows[i];\n const key = generateRowKey(row, i, parentKey);\n\n if (key === targetKey) {\n return [key];\n }\n\n const children = row[childrenField];\n if (Array.isArray(children) && children.length > 0) {\n const childPath = getPathToKey(children as TreeRow[], targetKey, config, key, depth + 1);\n if (childPath) {\n return [key, ...childPath];\n }\n }\n }\n\n return null;\n}\n\n/**\n * Expands all ancestors of a specific row to make it visible.\n * Returns a new Set with the required keys added.\n */\nexport function expandToKey(\n rows: readonly TreeRow[],\n targetKey: string,\n config: TreeConfig,\n existingExpanded: Set<string>,\n): Set<string> {\n const path = getPathToKey(rows, targetKey, config);\n if (!path) return existingExpanded;\n\n const newExpanded = new Set(existingExpanded);\n // Add all keys except the last one (the target itself)\n for (let i = 0; i < path.length - 1; i++) {\n newExpanded.add(path[i]);\n }\n return newExpanded;\n}\n","/**\n * Tree Viewport Mapping Utilities\n *\n * Pure functions for translating between flat viewport row indices and\n * top-level tree node indices. Used by the Tree plugin's\n * `datasource:viewport-mapping` query handler.\n */\n\nimport type { FlattenedTreeRow } from './types';\n\n/**\n * Given a flat row index in the viewport, find the index of the\n * top-level node (depth 0) that \"owns\" that row.\n *\n * Walk backwards from the row index until a depth-0 row is found,\n * then return its position among all depth-0 rows.\n */\nexport function getTopLevelNodeIndex(flattenedRows: FlattenedTreeRow[], flatRowIndex: number): number {\n // Clamp to valid range\n const idx = Math.min(flatRowIndex, flattenedRows.length - 1);\n if (idx < 0) return 0;\n\n // Walk backwards to find the owning top-level node\n let current = idx;\n while (current > 0 && flattenedRows[current].depth > 0) {\n current--;\n }\n\n // Count how many depth-0 nodes precede this one (inclusive)\n let topLevelIndex = 0;\n for (let i = 0; i <= current; i++) {\n if (flattenedRows[i].depth === 0) {\n topLevelIndex++;\n }\n }\n // Convert to 0-based\n return topLevelIndex - 1;\n}\n\n/**\n * Count the number of top-level (depth 0) nodes in the flattened rows.\n */\nexport function countTopLevelNodes(flattenedRows: FlattenedTreeRow[]): number {\n let count = 0;\n for (const row of flattenedRows) {\n if (row.depth === 0) count++;\n }\n return count;\n}\n","/**\n * Tree Structure Auto-Detection\n *\n * Utilities for detecting hierarchical tree data structures.\n */\n\nimport type { TreeRow } from './types';\n\n/**\n * Detects if the data has a tree structure by checking for children arrays.\n */\nexport function detectTreeStructure(rows: readonly TreeRow[], childrenField = 'children'): boolean {\n if (!Array.isArray(rows) || rows.length === 0) return false;\n\n // Check if any row has children (embedded array or lazy indicator)\n for (const row of rows) {\n if (!row) continue;\n const children = row[childrenField];\n // Embedded children: non-empty array\n if (Array.isArray(children) && children.length > 0) return true;\n // Lazy children: truthy non-array value (e.g. `true`, number > 0)\n if (children != null && !Array.isArray(children) && !!children) return true;\n }\n\n return false;\n}\n\n/**\n * Attempts to infer the children field name from common patterns.\n * Returns the first field that contains an array with items.\n */\nexport function inferChildrenField(rows: readonly TreeRow[]): string | null {\n if (!Array.isArray(rows) || rows.length === 0) return null;\n\n const commonArrayFields = ['children', 'items', 'nodes', 'subRows', 'nested'];\n\n for (const row of rows) {\n if (!row || typeof row !== 'object') continue;\n\n for (const field of commonArrayFields) {\n const value = row[field];\n if (Array.isArray(value) && value.length > 0) {\n return field;\n }\n }\n }\n\n return null;\n}\n\n/**\n * Calculates the maximum depth of the tree.\n * Useful for layout calculations and virtualization.\n */\nexport function getMaxDepth(rows: readonly TreeRow[], childrenField = 'children', currentDepth = 0): number {\n if (!Array.isArray(rows) || rows.length === 0) return currentDepth;\n\n let maxDepth = currentDepth;\n\n for (const row of rows) {\n if (!row) continue;\n const children = row[childrenField];\n if (Array.isArray(children) && children.length > 0) {\n const childDepth = getMaxDepth(children as TreeRow[], childrenField, currentDepth + 1);\n if (childDepth > maxDepth) {\n maxDepth = childDepth;\n }\n }\n }\n\n return maxDepth;\n}\n\n/**\n * Counts total nodes in the tree (including all descendants).\n */\nexport function countNodes(rows: readonly TreeRow[], childrenField = 'children'): number {\n if (!Array.isArray(rows)) return 0;\n\n let count = 0;\n for (const row of rows) {\n if (!row) continue;\n count++;\n const children = row[childrenField];\n if (Array.isArray(children)) {\n count += countNodes(children as TreeRow[], childrenField);\n }\n }\n\n return count;\n}\n","/**\n * Tree Data Plugin\n *\n * Enables hierarchical tree data with expand/collapse, sorting, and auto-detection.\n */\n\nimport { GridClasses } from '../../core/constants';\nimport type { GridElement } from '../../core/plugin/base-plugin';\nimport {\n BaseGridPlugin,\n CellClickEvent,\n HeaderClickEvent,\n type PluginManifest,\n type PluginQuery,\n} from '../../core/plugin/base-plugin';\nimport type { ColumnConfig, ColumnViewRenderer, GridHost } from '../../core/types';\nimport type {\n DataSourceChildrenDetail,\n DataSourceDataDetail,\n FetchChildrenQuery,\n ViewportMappingQuery,\n ViewportMappingResponse,\n} from '../server-side/datasource-types';\nimport { collapseAll, expandAll, expandToKey, toggleExpand } from './tree-data';\nimport { countTopLevelNodes, getTopLevelNodeIndex } from './tree-datasource';\nimport { detectTreeStructure, inferChildrenField } from './tree-detect';\nimport styles from './tree.css?inline';\nimport type { ExpandCollapseAnimation, FlattenedTreeRow, TreeConfig, TreeExpandDetail, TreeRow } from './types';\n\n/**\n * Tree Data Plugin for tbw-grid\n *\n * Transforms your flat grid into a hierarchical tree view with expandable parent-child\n * relationships. Ideal for file explorers, organizational charts, nested categories,\n * or any data with a natural hierarchy.\n *\n * ## Installation\n *\n * ```ts\n * import { TreePlugin } from '@toolbox-web/grid/plugins/tree';\n * ```\n *\n * ## CSS Custom Properties\n *\n * | Property | Default | Description |\n * |----------|---------|-------------|\n * | `--tbw-tree-toggle-size` | `1.25em` | Toggle icon width |\n * | `--tbw-tree-indent-width` | `var(--tbw-tree-toggle-size)` | Indentation per level |\n * | `--tbw-tree-accent` | `var(--tbw-color-accent)` | Toggle icon hover color |\n * | `--tbw-animation-duration` | `200ms` | Expand/collapse animation duration |\n * | `--tbw-animation-easing` | `ease-out` | Animation curve |\n *\n * @example Basic Tree with Nested Children\n * ```ts\n * import { queryGrid } from '@toolbox-web/grid';\n * import { TreePlugin } from '@toolbox-web/grid/plugins/tree';\n *\n * const grid = queryGrid('tbw-grid');\n * grid.gridConfig = {\n * columns: [\n * { field: 'name', header: 'Name' },\n * { field: 'type', header: 'Type' },\n * { field: 'size', header: 'Size' },\n * ],\n * plugins: [new TreePlugin({ childrenField: 'children', indentWidth: 24 })],\n * };\n * grid.rows = [\n * {\n * id: 1,\n * name: 'Documents',\n * type: 'folder',\n * children: [\n * { id: 2, name: 'Report.docx', type: 'file', size: '24 KB' },\n * ],\n * },\n * ];\n * ```\n *\n * @example Expanded by Default with Custom Animation\n * ```ts\n * new TreePlugin({\n * defaultExpanded: true,\n * animation: 'fade', // 'slide' | 'fade' | false\n * indentWidth: 32,\n * })\n * ```\n *\n * @see {@link TreeConfig} for all configuration options\n * @see {@link FlattenedTreeRow} for the flattened row structure\n *\n * @internal Extends BaseGridPlugin\n */\nexport class TreePlugin extends BaseGridPlugin<TreeConfig> {\n static override readonly manifest: PluginManifest = {\n modifiesRowStructure: true,\n hookPriority: {\n processRows: 10, // Run after ServerSide (-10) so we receive managedNodes[]\n },\n incompatibleWith: [\n {\n name: 'groupingRows',\n reason:\n 'Both plugins transform the entire row model. TreePlugin flattens nested hierarchies while ' +\n 'GroupingRowsPlugin groups flat rows with synthetic headers. Use one approach per grid.',\n },\n {\n name: 'pivot',\n reason:\n 'PivotPlugin replaces the entire row and column structure with aggregated pivot data. ' +\n 'Tree hierarchy cannot coexist with pivot aggregation.',\n },\n ],\n events: [\n {\n type: 'tree-expand',\n description:\n 'Emitted when tree expansion state changes (toggle, expand all, collapse all). Broadcast to both DOM consumers and plugin bus.',\n },\n ],\n queries: [\n {\n type: 'canMoveRow',\n description: 'Returns false for rows with children (parent nodes cannot be reordered)',\n },\n {\n type: 'datasource:viewport-mapping',\n description: 'Translates flat viewport row indices to top-level node indices for ServerSide pagination.',\n },\n ],\n };\n\n /**\n * Optional dependency on MultiSort for coordinated sorting.\n * When MultiSort is loaded, Tree defers header click sorting to it and queries the\n * sort model in processRows. When MultiSort is absent, Tree uses its own sort state.\n */\n static override readonly dependencies = [\n { name: 'multiSort', required: false, reason: 'Queries sort model for coordinated tree sorting' },\n { name: 'serverSide', required: false, reason: 'Consumes datasource events for lazy-loaded tree data' },\n ];\n\n /** @internal */\n readonly name = 'tree';\n /** @internal */\n override readonly styles = styles;\n\n /** @internal */\n protected override get defaultConfig(): Partial<TreeConfig> {\n return {\n childrenField: 'children',\n autoDetect: true,\n defaultExpanded: false,\n indentWidth: 20,\n showExpandIcons: true,\n animation: 'slide',\n };\n }\n\n // #region State\n\n private expandedKeys = new Set<string>();\n private initialExpansionDone = false;\n private flattenedRows: FlattenedTreeRow[] = [];\n private rowKeyMap = new Map<string, FlattenedTreeRow>();\n private previousVisibleKeys = new Set<string>();\n private keysToAnimate = new Set<string>();\n private sortState: { field: string; direction: 1 | -1 } | null = null;\n /** Keys of nodes that are currently loading lazy children via ServerSide. */\n private loadingKeys = new Set<string>();\n\n /** Cached original (unwrapped) renderer to prevent re-wrapping on repeated processColumns calls. */\n private originalTreeColumnRenderer: ColumnViewRenderer | undefined;\n /** Field name of the column currently wrapped with tree decorations. */\n private wrappedTreeColumnField: string | undefined;\n\n /** @internal */\n override detach(): void {\n this.expandedKeys.clear();\n this.initialExpansionDone = false;\n this.flattenedRows = [];\n this.rowKeyMap.clear();\n this.previousVisibleKeys.clear();\n this.keysToAnimate.clear();\n this.sortState = null;\n this.loadingKeys.clear();\n this.originalTreeColumnRenderer = undefined;\n this.wrappedTreeColumnField = undefined;\n }\n\n /**\n * Handle plugin queries.\n * @internal\n */\n override handleQuery(query: PluginQuery): unknown {\n if (query.type === 'canMoveRow') {\n // Tree rows with children cannot be reordered\n const row = query.context as { [key: string]: unknown } | null | undefined;\n const childrenField = this.config.childrenField ?? 'children';\n const children = row?.[childrenField];\n if (Array.isArray(children) && children.length > 0) {\n return false;\n }\n }\n\n if (query.type === 'datasource:viewport-mapping') {\n // Translate visible flat row indices → top-level node indices for ServerSide pagination\n const { viewportStart, viewportEnd } = query.context as ViewportMappingQuery;\n if (this.flattenedRows.length === 0) return undefined;\n\n const startNode = getTopLevelNodeIndex(this.flattenedRows, viewportStart);\n const endNode = getTopLevelNodeIndex(this.flattenedRows, viewportEnd) + 1; // exclusive\n const totalLoadedNodes = countTopLevelNodes(this.flattenedRows);\n\n return { startNode, endNode, totalLoadedNodes } satisfies ViewportMappingResponse;\n }\n\n return undefined;\n }\n\n // #endregion\n\n // #region Lifecycle\n\n /** @internal */\n override attach(grid: GridElement): void {\n super.attach(grid);\n\n // Listen for datasource:data from ServerSidePlugin — claim data for tree processing\n this.on('datasource:data', (detail: unknown) => {\n const d = detail as DataSourceDataDetail;\n if (!d.claimed) {\n d.claimed = true;\n }\n // Data flows through processRows pipeline — Tree receives it via the rows parameter\n // since ServerSide's processRows (hookPriority -10) runs first and returns managedNodes[]\n });\n\n // Listen for datasource:children — consume child rows from ServerSide\n this.on('datasource:children', (detail: unknown) => {\n const d = detail as DataSourceChildrenDetail;\n if (d.context?.source !== 'tree') return;\n d.claimed = true;\n\n // Merge children into the parent node\n const parentRow = d.context.parentNode as TreeRow | undefined;\n if (parentRow) {\n const childrenField = this.config.childrenField ?? 'children';\n (parentRow as Record<string, unknown>)[childrenField] = d.rows;\n const key = (parentRow.__stableKey as string | undefined) ?? String(parentRow.id ?? '?');\n this.loadingKeys.delete(key);\n this.requestRender();\n }\n });\n }\n\n // #endregion\n\n // #region Animation\n\n /**\n * Get expand/collapse animation style from plugin config.\n * Uses base class isAnimationEnabled to respect grid-level settings.\n */\n private get animationStyle(): ExpandCollapseAnimation {\n if (!this.isAnimationEnabled) return false;\n return this.config.animation ?? 'slide';\n }\n\n // #endregion\n\n // #region Auto-Detection\n\n detect(rows: readonly unknown[]): boolean {\n if (!this.config.autoDetect) return false;\n const treeRows = rows as readonly TreeRow[];\n const field = this.config.childrenField ?? inferChildrenField(treeRows) ?? 'children';\n return detectTreeStructure(treeRows, field);\n }\n\n // #endregion\n\n // #region Data Processing\n\n /** @internal */\n override processRows(rows: readonly unknown[]): TreeRow[] {\n const childrenField = this.config.childrenField ?? 'children';\n\n const treeRows = rows as readonly TreeRow[];\n\n if (treeRows.length === 0 || !detectTreeStructure(treeRows, childrenField)) {\n this.flattenedRows = [];\n this.rowKeyMap.clear();\n this.previousVisibleKeys.clear();\n return [...rows] as TreeRow[];\n }\n\n // Assign stable keys, then optionally sort.\n // When MultiSort is active, use its model instead of local sort state so\n // Tree and MultiSort don't fight over sort ownership.\n let data = this.withStableKeys(treeRows);\n const effectiveSortState = this.resolveEffectiveSortState();\n if (effectiveSortState) {\n data = this.sortTree(data, effectiveSortState.field, effectiveSortState.direction);\n }\n\n // Initialize expansion if needed\n if (this.config.defaultExpanded && !this.initialExpansionDone) {\n this.expandedKeys = expandAll(data, this.config);\n this.initialExpansionDone = true;\n }\n\n // Flatten and track animations\n this.flattenedRows = this.flattenTree(data, this.expandedKeys);\n this.rowKeyMap.clear();\n this.keysToAnimate.clear();\n const currentKeys = new Set<string>();\n\n for (const row of this.flattenedRows) {\n this.rowKeyMap.set(row.key, row);\n currentKeys.add(row.key);\n if (!this.previousVisibleKeys.has(row.key) && row.depth > 0) {\n this.keysToAnimate.add(row.key);\n }\n }\n this.previousVisibleKeys = currentKeys;\n\n return this.flattenedRows.map((r) => ({\n ...r.data,\n __treeKey: r.key,\n __treeDepth: r.depth,\n __treeHasChildren: r.hasChildren,\n __treeExpanded: r.isExpanded,\n }));\n }\n\n /** Assign stable keys to rows (preserves key across sort operations) */\n private withStableKeys(rows: readonly TreeRow[], parentKey: string | null = null): TreeRow[] {\n const childrenField = this.config.childrenField ?? 'children';\n return rows.map((row, i) => {\n const stableKey = row.__stableKey as string | undefined;\n const key = row.id !== undefined ? String(row.id) : (stableKey ?? (parentKey ? `${parentKey}-${i}` : String(i)));\n const children = row[childrenField];\n const hasChildren = Array.isArray(children) && children.length > 0;\n return {\n ...row,\n __stableKey: key,\n ...(hasChildren ? { [childrenField]: this.withStableKeys(children as TreeRow[], key) } : {}),\n };\n });\n }\n\n /** Flatten tree using stable keys */\n private flattenTree(rows: readonly TreeRow[], expanded: Set<string>, depth = 0): FlattenedTreeRow[] {\n const childrenField = this.config.childrenField ?? 'children';\n const result: FlattenedTreeRow[] = [];\n\n for (const row of rows) {\n const stableKey = row.__stableKey as string | undefined;\n const key = stableKey ?? String(row.id ?? '?');\n const children = row[childrenField];\n const embeddedChildren = Array.isArray(children) && children.length > 0;\n // Lazy children: truthy non-array value (e.g. `children: true`) signals\n // that children exist on the server but haven't been fetched yet.\n const lazyChildren = children != null && !Array.isArray(children) && !!children;\n const hasChildren = embeddedChildren || lazyChildren;\n const isExpanded = expanded.has(key);\n\n result.push({\n key,\n data: row,\n depth,\n hasChildren,\n isExpanded,\n parentKey: depth > 0 ? key.substring(0, key.lastIndexOf('-')) || null : null,\n });\n\n if (embeddedChildren && isExpanded) {\n result.push(...this.flattenTree(children as TreeRow[], expanded, depth + 1));\n }\n }\n return result;\n }\n\n /**\n * Request lazy children for a node via ServerSide's `datasource:fetch-children` query.\n * Called when expanding a node whose children are not yet embedded (lazy indicator only).\n * No-op if ServerSide is not active, children are already loading, or children are embedded.\n */\n private requestLazyChildren(flatRow: FlattenedTreeRow): void {\n if (this.loadingKeys.has(flatRow.key)) return;\n\n const childrenField = this.config.childrenField ?? 'children';\n const children = flatRow.data[childrenField];\n // Only fetch if children is a lazy indicator (truthy but not a non-empty array)\n if (Array.isArray(children) && children.length > 0) return;\n\n const isServerSideActive = this.grid?.query?.('datasource:is-active', null);\n if (!isServerSideActive) return;\n\n this.loadingKeys.add(flatRow.key);\n this.grid.query('datasource:fetch-children', {\n context: { source: 'tree', parentNode: flatRow.data, nodePath: [flatRow.key] },\n } satisfies FetchChildrenQuery);\n }\n\n /**\n * Resolve the effective sort state: prefer MultiSort's model when available,\n * fall back to local tree sort state.\n * This follows the same pattern as GroupingRowsPlugin.resolveGroupSortDirections.\n */\n private resolveEffectiveSortState(): { field: string; direction: 1 | -1 } | null {\n // When MultiSort is loaded, prefer its model for consistency\n const multiSortResults = this.grid?.query?.('sort:get-model', null);\n if (Array.isArray(multiSortResults) && multiSortResults.length > 0) {\n const sortModel = multiSortResults[0] as Array<{ field: string; direction: 'asc' | 'desc' }>;\n if (Array.isArray(sortModel) && sortModel.length > 0) {\n // Use the primary sort column from MultiSort\n return {\n field: sortModel[0].field,\n direction: sortModel[0].direction === 'desc' ? -1 : 1,\n };\n }\n }\n // Fallback: local sort state (when MultiSort is not loaded)\n return this.sortState;\n }\n\n /** Sort tree recursively, keeping children with parents */\n private sortTree(rows: readonly TreeRow[], field: string, dir: 1 | -1): TreeRow[] {\n const childrenField = this.config.childrenField ?? 'children';\n const sorted = [...rows].sort((a, b) => {\n const aVal = a[field],\n bVal = b[field];\n if (aVal == null && bVal == null) return 0;\n if (aVal == null) return -1;\n if (bVal == null) return 1;\n return aVal > bVal ? dir : aVal < bVal ? -dir : 0;\n });\n return sorted.map((row) => {\n const children = row[childrenField];\n return Array.isArray(children) && children.length > 0\n ? { ...row, [childrenField]: this.sortTree(children as TreeRow[], field, dir) }\n : row;\n });\n }\n\n /** @internal */\n override processColumns(columns: readonly ColumnConfig[]): ColumnConfig[] {\n if (this.flattenedRows.length === 0) return [...columns];\n\n const cols = [...columns] as ColumnConfig[];\n if (cols.length === 0) return cols;\n\n // Determine which column gets the tree toggle and indentation.\n // If treeColumn is configured, find it by field name; otherwise use the first column.\n const { treeColumn } = this.config;\n let targetIndex = 0;\n if (treeColumn) {\n const idx = cols.findIndex((c) => c.field === treeColumn);\n if (idx >= 0) targetIndex = idx;\n }\n const targetCol = cols[targetIndex];\n const targetField = targetCol.field;\n\n // Capture the original (unwrapped) renderer only once per target column.\n // On subsequent processColumns calls, reuse the cached original so we\n // don't nest tree-cell-wrappers.\n if (this.wrappedTreeColumnField !== targetField) {\n this.originalTreeColumnRenderer = targetCol.viewRenderer;\n this.wrappedTreeColumnField = targetField;\n }\n const originalRenderer = this.originalTreeColumnRenderer;\n const getConfig = () => this.config;\n const setIconFn = this.setIcon.bind(this);\n\n const wrappedRenderer: ColumnViewRenderer = (ctx) => {\n const { row, value } = ctx;\n const { showExpandIcons = true, indentWidth } = getConfig();\n const treeRow = row as TreeRow;\n const depth = treeRow.__treeDepth ?? 0;\n\n const container = document.createElement('span');\n container.className = 'tree-cell-wrapper';\n container.style.setProperty('--tbw-tree-depth', String(depth));\n // Allow config-based indentWidth to override CSS default\n if (indentWidth !== undefined) {\n container.style.setProperty('--tbw-tree-indent-width', `${indentWidth}px`);\n }\n\n // Add expand/collapse icon or spacer\n if (showExpandIcons) {\n if (treeRow.__treeHasChildren) {\n const icon = document.createElement('span');\n icon.className = `${GridClasses.TREE_TOGGLE}${treeRow.__treeExpanded ? ` ${GridClasses.EXPANDED}` : ''}`;\n setIconFn(icon, treeRow.__treeExpanded ? 'collapse' : 'expand');\n icon.setAttribute('data-tree-key', String(treeRow.__treeKey ?? ''));\n container.appendChild(icon);\n } else {\n const spacer = document.createElement('span');\n spacer.className = 'tree-spacer';\n container.appendChild(spacer);\n }\n }\n\n // Add the original content\n const content = document.createElement('span');\n content.className = 'tree-content';\n if (originalRenderer) {\n const result = originalRenderer(ctx);\n if (result instanceof Node) {\n content.appendChild(result);\n } else if (typeof result === 'string') {\n content.innerHTML = result;\n }\n } else {\n content.textContent = value != null ? String(value) : '';\n }\n container.appendChild(content);\n\n return container;\n };\n\n cols[targetIndex] = { ...targetCol, viewRenderer: wrappedRenderer };\n return cols;\n }\n\n // #endregion\n\n // #region Event Handlers\n\n /** @internal */\n override onCellClick(event: CellClickEvent): boolean {\n const target = event.originalEvent?.target as HTMLElement;\n if (!target?.classList.contains(GridClasses.TREE_TOGGLE)) return false;\n\n const key = target.getAttribute('data-tree-key');\n if (!key) return false;\n\n const flatRow = this.rowKeyMap.get(key);\n if (!flatRow) return false;\n\n this.expandedKeys = toggleExpand(this.expandedKeys, key);\n\n // Request lazy children when expanding a node without embedded children\n if (this.expandedKeys.has(key)) {\n this.requestLazyChildren(flatRow);\n }\n\n this.broadcast<TreeExpandDetail>('tree-expand', {\n key,\n row: flatRow.data,\n expanded: this.expandedKeys.has(key),\n depth: flatRow.depth,\n expandedKeys: [...this.expandedKeys],\n });\n this.requestRender();\n return true;\n }\n\n /** @internal */\n override onKeyDown(event: KeyboardEvent): boolean | void {\n // SPACE toggles expansion when on a row with children\n if (event.key !== ' ') return;\n\n const focusRow = this.grid._focusRow;\n const flatRow = this.flattenedRows[focusRow];\n if (!flatRow?.hasChildren) return;\n\n event.preventDefault();\n this.expandedKeys = toggleExpand(this.expandedKeys, flatRow.key);\n\n // Request lazy children when expanding a node without embedded children\n if (this.expandedKeys.has(flatRow.key)) {\n this.requestLazyChildren(flatRow);\n }\n\n this.broadcast<TreeExpandDetail>('tree-expand', {\n key: flatRow.key,\n row: flatRow.data,\n expanded: this.expandedKeys.has(flatRow.key),\n depth: flatRow.depth,\n expandedKeys: [...this.expandedKeys],\n });\n this.requestRenderWithFocus();\n return true;\n }\n\n /** @internal */\n override onHeaderClick(event: HeaderClickEvent): boolean {\n if (this.flattenedRows.length === 0 || !event.column.sortable) return false;\n\n // When MultiSort is active, let it handle header clicks entirely.\n // Tree will pick up the sort model in processRows via resolveEffectiveSortState().\n const multiSortResults = this.grid?.query?.('sort:get-model', null);\n if (Array.isArray(multiSortResults) && multiSortResults.length > 0) {\n // MultiSort is loaded — don't consume the event, let MultiSort handle it\n return false;\n }\n\n // Fallback: manage own sort state when MultiSort is not loaded\n const { field } = event.column;\n if (!this.sortState || this.sortState.field !== field) {\n this.sortState = { field, direction: 1 };\n } else if (this.sortState.direction === 1) {\n this.sortState = { field, direction: -1 };\n } else {\n this.sortState = null;\n }\n\n // Sync grid sort indicator\n const gridEl = this.grid as unknown as GridHost;\n if (gridEl._sortState !== undefined) {\n gridEl._sortState = this.sortState ? { ...this.sortState } : null;\n }\n\n this.broadcast('sort-change', { field, direction: this.sortState?.direction ?? 0 });\n this.requestRender();\n return true;\n }\n\n /** @internal */\n override afterRender(): void {\n const body = this.gridElement?.querySelector('.rows');\n if (!body) return;\n\n const style = this.animationStyle;\n const shouldAnimate = style !== false && this.keysToAnimate.size > 0;\n const animClass = style === 'fade' ? 'tbw-tree-fade-in' : 'tbw-tree-slide-in';\n\n for (const rowEl of body.querySelectorAll('.data-grid-row')) {\n const cell = rowEl.querySelector('.cell[data-row]');\n const idx = cell ? parseInt(cell.getAttribute('data-row') ?? '-1', 10) : -1;\n const treeRow = this.flattenedRows[idx];\n\n // Set aria-expanded on parent rows for screen readers\n if (treeRow?.hasChildren) {\n rowEl.setAttribute('aria-expanded', String(treeRow.isExpanded));\n }\n\n if (shouldAnimate && treeRow?.key && this.keysToAnimate.has(treeRow.key)) {\n rowEl.classList.add(animClass);\n rowEl.addEventListener('animationend', () => rowEl.classList.remove(animClass), { once: true });\n }\n }\n this.keysToAnimate.clear();\n }\n\n // #endregion\n\n // #region Public API\n\n /**\n * Expand a specific tree node, revealing its children.\n *\n * If the node is already expanded, this is a no-op.\n * Does **not** emit a `tree-expand` event (use {@link toggle} for event emission).\n *\n * @param key - The unique key of the node to expand (from {@link FlattenedTreeRow.key})\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.expand('documents'); // Expand a root node\n * tree.expand('documents||reports'); // Expand a nested node\n * ```\n */\n expand(key: string): void {\n this.expandedKeys.add(key);\n const flatRow = this.rowKeyMap.get(key);\n if (flatRow) {\n this.requestLazyChildren(flatRow);\n }\n this.requestRender();\n }\n\n /**\n * Collapse a specific tree node, hiding its children.\n *\n * If the node is already collapsed, this is a no-op.\n * Does **not** emit a `tree-expand` event (use {@link toggle} for event emission).\n *\n * @param key - The unique key of the node to collapse (from {@link FlattenedTreeRow.key})\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.collapse('documents');\n * ```\n */\n collapse(key: string): void {\n this.expandedKeys.delete(key);\n this.requestRender();\n }\n\n /**\n * Toggle the expanded state of a tree node.\n *\n * If the node is expanded it will be collapsed, and vice versa.\n * Emits a `tree-expand` event (broadcast to both DOM consumers and plugin bus).\n *\n * @param key - The unique key of the node to toggle (from {@link FlattenedTreeRow.key})\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.toggle('documents'); // Expand if collapsed, collapse if expanded\n * ```\n */\n toggle(key: string): void {\n this.expandedKeys = toggleExpand(this.expandedKeys, key);\n const flatRow = this.rowKeyMap.get(key);\n if (flatRow) {\n // Request lazy children when expanding a node without embedded children\n if (this.expandedKeys.has(key)) {\n this.requestLazyChildren(flatRow);\n }\n this.broadcast<TreeExpandDetail>('tree-expand', {\n key,\n row: flatRow.data,\n expanded: this.expandedKeys.has(key),\n depth: flatRow.depth,\n expandedKeys: [...this.expandedKeys],\n });\n } else {\n this.emitPluginEvent('tree-expand', { expandedKeys: [...this.expandedKeys] });\n }\n this.requestRender();\n }\n\n /**\n * Expand all tree nodes recursively.\n *\n * Every node with children will be expanded, revealing the full tree hierarchy.\n * Emits a `tree-expand` plugin event.\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.expandAll();\n * ```\n */\n expandAll(): void {\n this.expandedKeys = expandAll(this.rows as TreeRow[], this.config);\n this.emitPluginEvent('tree-expand', { expandedKeys: [...this.expandedKeys] });\n this.requestRender();\n }\n\n /**\n * Collapse all tree nodes.\n *\n * Every node will be collapsed, showing only root-level rows.\n * Emits a `tree-expand` plugin event.\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.collapseAll();\n * ```\n */\n collapseAll(): void {\n this.expandedKeys = collapseAll();\n this.emitPluginEvent('tree-expand', { expandedKeys: [...this.expandedKeys] });\n this.requestRender();\n }\n\n /**\n * Check whether a specific tree node is currently expanded.\n *\n * @param key - The unique key of the node to check\n * @returns `true` if the node is expanded, `false` otherwise\n */\n isExpanded(key: string): boolean {\n return this.expandedKeys.has(key);\n }\n\n /**\n * Get the keys of all currently expanded nodes.\n *\n * Returns a snapshot copy — mutating the returned array does not affect the tree state.\n *\n * @returns Array of expanded node keys\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * const keys = tree.getExpandedKeys();\n * localStorage.setItem('treeState', JSON.stringify(keys));\n * ```\n */\n getExpandedKeys(): string[] {\n return [...this.expandedKeys];\n }\n\n /**\n * Get the flattened row model used for rendering.\n *\n * Returns a snapshot copy of the internal flattened tree rows, including\n * hierarchy metadata (depth, hasChildren, isExpanded, parentKey).\n *\n * @returns Array of {@link FlattenedTreeRow} objects\n */\n getFlattenedRows(): FlattenedTreeRow[] {\n return [...this.flattenedRows];\n }\n\n /**\n * Look up an original row data object by its tree key.\n *\n * @param key - The unique key of the node\n * @returns The original row data, or `undefined` if not found\n */\n getRowByKey(key: string): TreeRow | undefined {\n return this.rowKeyMap.get(key)?.data;\n }\n\n /**\n * Expand all ancestor nodes of the target key, revealing it in the tree.\n *\n * Useful for \"scroll to node\" or search-and-reveal scenarios where a deeply\n * nested node needs to be made visible.\n *\n * @param key - The unique key of the node to reveal\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * // Reveal a deeply nested node by expanding all its parents\n * tree.expandToKey('root||child||grandchild');\n * ```\n */\n expandToKey(key: string): void {\n this.expandedKeys = expandToKey(this.rows as TreeRow[], key, this.config, this.expandedKeys);\n this.requestRender();\n }\n\n // #endregion\n}\n"],"names":["generateRowKey","row","index","parentKey","id","String","toggleExpand","expandedKeys","key","newExpanded","Set","has","delete","add","expandAll","rows","config","depth","childrenField","keys","i","length","children","Array","isArray","childKeys","k","getPathToKey","targetKey","childPath","expandToKey","existingExpanded","path","getTopLevelNodeIndex","flattenedRows","flatRowIndex","idx","Math","min","current","topLevelIndex","detectTreeStructure","TreePlugin","BaseGridPlugin","static","modifiesRowStructure","hookPriority","processRows","incompatibleWith","name","reason","events","type","description","queries","required","styles","defaultConfig","autoDetect","defaultExpanded","indentWidth","showExpandIcons","animation","initialExpansionDone","rowKeyMap","Map","previousVisibleKeys","keysToAnimate","sortState","loadingKeys","originalTreeColumnRenderer","wrappedTreeColumnField","detach","this","clear","handleQuery","query","context","viewportStart","viewportEnd","startNode","endNode","totalLoadedNodes","count","countTopLevelNodes","attach","grid","super","on","detail","d","claimed","source","parentRow","parentNode","__stableKey","requestRender","animationStyle","isAnimationEnabled","detect","treeRows","field","commonArrayFields","value","inferChildrenField","data","withStableKeys","effectiveSortState","resolveEffectiveSortState","sortTree","direction","flattenTree","currentKeys","set","map","r","__treeKey","__treeDepth","__treeHasChildren","hasChildren","__treeExpanded","isExpanded","stableKey","expanded","result","embeddedChildren","lazyChildren","push","substring","lastIndexOf","requestLazyChildren","flatRow","isServerSideActive","nodePath","multiSortResults","sortModel","dir","sort","a","b","aVal","bVal","processColumns","columns","cols","treeColumn","targetIndex","findIndex","c","targetCol","targetField","viewRenderer","originalRenderer","getConfig","setIconFn","setIcon","bind","ctx","treeRow","container","document","createElement","className","style","setProperty","icon","GridClasses","TREE_TOGGLE","EXPANDED","setAttribute","appendChild","spacer","content","Node","innerHTML","textContent","onCellClick","event","target","originalEvent","classList","contains","getAttribute","get","broadcast","onKeyDown","focusRow","_focusRow","preventDefault","requestRenderWithFocus","onHeaderClick","column","sortable","gridEl","_sortState","afterRender","body","gridElement","querySelector","shouldAnimate","size","animClass","rowEl","querySelectorAll","cell","parseInt","addEventListener","remove","once","expand","collapse","toggle","emitPluginEvent","collapseAll","getExpandedKeys","getFlattenedRows","getRowByKey"],"mappings":"iZAYO,SAASA,EAAeC,EAAcC,EAAeC,GAC1D,YAAe,IAAXF,EAAIG,GAAyBC,OAAOJ,EAAIG,IACrCD,EAAY,GAAGA,KAAaD,IAAUG,OAAOH,EACtD,C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AAO3M,GACLiE,KAAKlE,aAAeD,EAAamE,KAAKlE,aAAcC,GACpD,MAAM2H,EAAU1D,KAAKT,UAAUuH,IAAI/K,GAC/B2H,GAEE1D,KAAKlE,aAAaI,IAAIH,IACxBiE,KAAKyD,oBAAoBC,GAE3B1D,KAAK+G,UAA4B,cAAe,CAC9ChL,MACAP,IAAKkI,EAAQ1B,KACbkB,SAAUlD,KAAKlE,aAAaI,IAAIH,GAChCS,MAAOkH,EAAQlH,MACfV,aAAc,IAAIkE,KAAKlE,iBAGzBkE,KAAK2I,gBAAgB,cAAe,CAAE7M,aAAc,IAAIkE,KAAKlE,gBAE/DkE,KAAKuB,eACP,CAcA,SAAAlF,GACE2D,KAAKlE,aAAeO,EAAU2D,KAAK1D,KAAmB0D,KAAKzD,QAC3DyD,KAAK2I,gBAAgB,cAAe,CAAE7M,aAAc,IAAIkE,KAAKlE,gBAC7DkE,KAAKuB,eACP,CAcA,WAAAqH,GACE5I,KAAKlE,iBHhpBIG,IGipBT+D,KAAK2I,gBAAgB,cAAe,CAAE7M,aAAc,IAAIkE,KAAKlE,gBAC7DkE,KAAKuB,eACP,CAQA,UAAAyB,CAAWjH,GACT,OAAOiE,KAAKlE,aAAaI,IAAIH,EAC/B,CAgBA,eAAA8M,GACE,MAAO,IAAI7I,KAAKlE,aAClB,CAUA,gBAAAgN,GACE,MAAO,IAAI9I,KAAKvC,cAClB,CAQA,WAAAsL,CAAYhN,GACV,OAAOiE,KAAKT,UAAUuH,IAAI/K,IAAMiG,IAClC,CAiBA,WAAA3E,CAAYtB,GACViE,KAAKlE,aAAeuB,EAAY2C,KAAK1D,KAAmBP,EAAKiE,KAAKzD,OAAQyD,KAAKlE,cAC/EkE,KAAKuB,eACP"}
1
+ {"version":3,"file":"tree.umd.js","sources":["../../../../../libs/grid/src/lib/plugins/tree/tree-data.ts","../../../../../libs/grid/src/lib/plugins/tree/tree-datasource.ts","../../../../../libs/grid/src/lib/plugins/tree/tree-detect.ts","../../../../../libs/grid/src/lib/plugins/tree/TreePlugin.ts"],"sourcesContent":["/**\n * Core Tree Data Logic\n *\n * Pure functions for tree flattening, expansion, and traversal.\n */\n\nimport type { FlattenedTreeRow, TreeConfig, TreeRow } from './types';\n\n/**\n * Generates a unique key for a row.\n * Uses row.id if available, otherwise generates from path.\n */\nexport function generateRowKey(row: TreeRow, index: number, parentKey: string | null): string {\n if (row.id !== undefined) return String(row.id);\n return parentKey ? `${parentKey}-${index}` : String(index);\n}\n\n/**\n * Flattens a hierarchical tree into a flat array of rows with metadata.\n * Only includes children of expanded nodes.\n */\nexport function flattenTree(\n rows: readonly TreeRow[],\n config: TreeConfig,\n expandedKeys: Set<string>,\n parentKey: string | null = null,\n depth = 0,\n): FlattenedTreeRow[] {\n const childrenField = config.childrenField ?? 'children';\n const result: FlattenedTreeRow[] = [];\n\n for (let i = 0; i < rows.length; i++) {\n const row = rows[i];\n const key = generateRowKey(row, i, parentKey);\n const children = row[childrenField];\n const hasChildren = Array.isArray(children) && children.length > 0;\n const isExpanded = expandedKeys.has(key);\n\n result.push({\n key,\n data: row,\n depth,\n hasChildren,\n isExpanded,\n parentKey,\n });\n\n // Recursively add children if expanded\n if (hasChildren && isExpanded) {\n const childRows = flattenTree(children as TreeRow[], config, expandedKeys, key, depth + 1);\n result.push(...childRows);\n }\n }\n\n return result;\n}\n\n/**\n * Toggles the expansion state of a row.\n * Returns a new Set with the toggled state.\n */\nexport function toggleExpand(expandedKeys: Set<string>, key: string): Set<string> {\n const newExpanded = new Set(expandedKeys);\n if (newExpanded.has(key)) {\n newExpanded.delete(key);\n } else {\n newExpanded.add(key);\n }\n return newExpanded;\n}\n\n/**\n * Expands all nodes in the tree.\n * Returns a Set of all parent row keys.\n */\nexport function expandAll(\n rows: readonly TreeRow[],\n config: TreeConfig,\n parentKey: string | null = null,\n depth = 0,\n): Set<string> {\n const childrenField = config.childrenField ?? 'children';\n const keys = new Set<string>();\n\n for (let i = 0; i < rows.length; i++) {\n const row = rows[i];\n const key = generateRowKey(row, i, parentKey);\n const children = row[childrenField];\n\n if (Array.isArray(children) && children.length > 0) {\n keys.add(key);\n const childKeys = expandAll(children as TreeRow[], config, key, depth + 1);\n for (const k of childKeys) keys.add(k);\n }\n }\n\n return keys;\n}\n\n/**\n * Collapses all nodes.\n * Returns an empty Set.\n */\nexport function collapseAll(): Set<string> {\n return new Set();\n}\n\n/**\n * Gets all descendants of a node from the flattened row list.\n * Useful for operations that need to affect an entire subtree.\n */\nexport function getDescendants(flattenedRows: FlattenedTreeRow[], parentKey: string): FlattenedTreeRow[] {\n const descendants: FlattenedTreeRow[] = [];\n let collecting = false;\n let parentDepth = -1;\n\n for (const row of flattenedRows) {\n if (row.key === parentKey) {\n collecting = true;\n parentDepth = row.depth;\n continue;\n }\n\n if (collecting) {\n if (row.depth > parentDepth) {\n descendants.push(row);\n } else {\n break; // No longer a descendant\n }\n }\n }\n\n return descendants;\n}\n\n/**\n * Finds the path from root to a specific row key.\n * Returns an array of keys from root to the target (inclusive).\n */\nexport function getPathToKey(\n rows: readonly TreeRow[],\n targetKey: string,\n config: TreeConfig,\n parentKey: string | null = null,\n depth = 0,\n): string[] | null {\n const childrenField = config.childrenField ?? 'children';\n\n for (let i = 0; i < rows.length; i++) {\n const row = rows[i];\n const key = generateRowKey(row, i, parentKey);\n\n if (key === targetKey) {\n return [key];\n }\n\n const children = row[childrenField];\n if (Array.isArray(children) && children.length > 0) {\n const childPath = getPathToKey(children as TreeRow[], targetKey, config, key, depth + 1);\n if (childPath) {\n return [key, ...childPath];\n }\n }\n }\n\n return null;\n}\n\n/**\n * Expands all ancestors of a specific row to make it visible.\n * Returns a new Set with the required keys added.\n */\nexport function expandToKey(\n rows: readonly TreeRow[],\n targetKey: string,\n config: TreeConfig,\n existingExpanded: Set<string>,\n): Set<string> {\n const path = getPathToKey(rows, targetKey, config);\n if (!path) return existingExpanded;\n\n const newExpanded = new Set(existingExpanded);\n // Add all keys except the last one (the target itself)\n for (let i = 0; i < path.length - 1; i++) {\n newExpanded.add(path[i]);\n }\n return newExpanded;\n}\n","/**\n * Tree Viewport Mapping Utilities\n *\n * Pure functions for translating between flat viewport row indices and\n * top-level tree node indices. Used by the Tree plugin's\n * `datasource:viewport-mapping` query handler.\n */\n\nimport type { FlattenedTreeRow } from './types';\n\n/**\n * Given a flat row index in the viewport, find the index of the\n * top-level node (depth 0) that \"owns\" that row.\n *\n * Walk backwards from the row index until a depth-0 row is found,\n * then return its position among all depth-0 rows.\n */\nexport function getTopLevelNodeIndex(flattenedRows: FlattenedTreeRow[], flatRowIndex: number): number {\n // Clamp to valid range\n const idx = Math.min(flatRowIndex, flattenedRows.length - 1);\n if (idx < 0) return 0;\n\n // Walk backwards to find the owning top-level node\n let current = idx;\n while (current > 0 && flattenedRows[current].depth > 0) {\n current--;\n }\n\n // Count how many depth-0 nodes precede this one (inclusive)\n let topLevelIndex = 0;\n for (let i = 0; i <= current; i++) {\n if (flattenedRows[i].depth === 0) {\n topLevelIndex++;\n }\n }\n // Convert to 0-based\n return topLevelIndex - 1;\n}\n\n/**\n * Count the number of top-level (depth 0) nodes in the flattened rows.\n */\nexport function countTopLevelNodes(flattenedRows: FlattenedTreeRow[]): number {\n let count = 0;\n for (const row of flattenedRows) {\n if (row.depth === 0) count++;\n }\n return count;\n}\n","/**\n * Tree Structure Auto-Detection\n *\n * Utilities for detecting hierarchical tree data structures.\n */\n\nimport type { TreeRow } from './types';\n\n/**\n * Detects if the data has a tree structure by checking for children arrays.\n */\nexport function detectTreeStructure(rows: readonly TreeRow[], childrenField = 'children'): boolean {\n if (!Array.isArray(rows) || rows.length === 0) return false;\n\n // Check if any row has children (embedded array or lazy indicator)\n for (const row of rows) {\n if (!row) continue;\n const children = row[childrenField];\n // Embedded children: non-empty array\n if (Array.isArray(children) && children.length > 0) return true;\n // Lazy children: truthy non-array value (e.g. `true`, number > 0)\n if (children != null && !Array.isArray(children) && !!children) return true;\n }\n\n return false;\n}\n\n/**\n * Attempts to infer the children field name from common patterns.\n * Returns the first field that contains an array with items.\n */\nexport function inferChildrenField(rows: readonly TreeRow[]): string | null {\n if (!Array.isArray(rows) || rows.length === 0) return null;\n\n const commonArrayFields = ['children', 'items', 'nodes', 'subRows', 'nested'];\n\n for (const row of rows) {\n if (!row || typeof row !== 'object') continue;\n\n for (const field of commonArrayFields) {\n const value = row[field];\n if (Array.isArray(value) && value.length > 0) {\n return field;\n }\n }\n }\n\n return null;\n}\n\n/**\n * Calculates the maximum depth of the tree.\n * Useful for layout calculations and virtualization.\n */\nexport function getMaxDepth(rows: readonly TreeRow[], childrenField = 'children', currentDepth = 0): number {\n if (!Array.isArray(rows) || rows.length === 0) return currentDepth;\n\n let maxDepth = currentDepth;\n\n for (const row of rows) {\n if (!row) continue;\n const children = row[childrenField];\n if (Array.isArray(children) && children.length > 0) {\n const childDepth = getMaxDepth(children as TreeRow[], childrenField, currentDepth + 1);\n if (childDepth > maxDepth) {\n maxDepth = childDepth;\n }\n }\n }\n\n return maxDepth;\n}\n\n/**\n * Counts total nodes in the tree (including all descendants).\n */\nexport function countNodes(rows: readonly TreeRow[], childrenField = 'children'): number {\n if (!Array.isArray(rows)) return 0;\n\n let count = 0;\n for (const row of rows) {\n if (!row) continue;\n count++;\n const children = row[childrenField];\n if (Array.isArray(children)) {\n count += countNodes(children as TreeRow[], childrenField);\n }\n }\n\n return count;\n}\n","/**\n * Tree Data Plugin\n *\n * Enables hierarchical tree data with expand/collapse, sorting, and auto-detection.\n */\n\nimport { GridClasses } from '../../core/constants';\nimport { builtInSort } from '../../core/internal/sorting';\nimport type { GridElement } from '../../core/plugin/base-plugin';\nimport {\n BaseGridPlugin,\n CellClickEvent,\n HeaderClickEvent,\n type PluginManifest,\n type PluginQuery,\n} from '../../core/plugin/base-plugin';\nimport type { ColumnConfig, ColumnViewRenderer, GridHost, SortHandler } from '../../core/types';\nimport type {\n DataSourceChildrenDetail,\n DataSourceDataDetail,\n FetchChildrenQuery,\n ViewportMappingQuery,\n ViewportMappingResponse,\n} from '../server-side/datasource-types';\nimport { collapseAll, expandAll, expandToKey, toggleExpand } from './tree-data';\nimport { countTopLevelNodes, getTopLevelNodeIndex } from './tree-datasource';\nimport { detectTreeStructure, inferChildrenField } from './tree-detect';\nimport styles from './tree.css?inline';\nimport type { ExpandCollapseAnimation, FlattenedTreeRow, TreeConfig, TreeExpandDetail, TreeRow } from './types';\n\n/**\n * Tree Data Plugin for tbw-grid\n *\n * Transforms your flat grid into a hierarchical tree view with expandable parent-child\n * relationships. Ideal for file explorers, organizational charts, nested categories,\n * or any data with a natural hierarchy.\n *\n * ## Installation\n *\n * ```ts\n * import { TreePlugin } from '@toolbox-web/grid/plugins/tree';\n * ```\n *\n * ## CSS Custom Properties\n *\n * | Property | Default | Description |\n * |----------|---------|-------------|\n * | `--tbw-tree-toggle-size` | `1.25em` | Toggle icon width |\n * | `--tbw-tree-indent-width` | `var(--tbw-tree-toggle-size)` | Indentation per level |\n * | `--tbw-tree-accent` | `var(--tbw-color-accent)` | Toggle icon hover color |\n * | `--tbw-animation-duration` | `200ms` | Expand/collapse animation duration |\n * | `--tbw-animation-easing` | `ease-out` | Animation curve |\n *\n * @example Basic Tree with Nested Children\n * ```ts\n * import { queryGrid } from '@toolbox-web/grid';\n * import { TreePlugin } from '@toolbox-web/grid/plugins/tree';\n *\n * const grid = queryGrid('tbw-grid');\n * grid.gridConfig = {\n * columns: [\n * { field: 'name', header: 'Name' },\n * { field: 'type', header: 'Type' },\n * { field: 'size', header: 'Size' },\n * ],\n * plugins: [new TreePlugin({ childrenField: 'children', indentWidth: 24 })],\n * };\n * grid.rows = [\n * {\n * id: 1,\n * name: 'Documents',\n * type: 'folder',\n * children: [\n * { id: 2, name: 'Report.docx', type: 'file', size: '24 KB' },\n * ],\n * },\n * ];\n * ```\n *\n * @example Expanded by Default with Custom Animation\n * ```ts\n * new TreePlugin({\n * defaultExpanded: true,\n * animation: 'fade', // 'slide' | 'fade' | false\n * indentWidth: 32,\n * })\n * ```\n *\n * @see {@link TreeConfig} for all configuration options\n * @see {@link FlattenedTreeRow} for the flattened row structure\n *\n * @internal Extends BaseGridPlugin\n */\nexport class TreePlugin extends BaseGridPlugin<TreeConfig> {\n static override readonly manifest: PluginManifest = {\n modifiesRowStructure: true,\n hookPriority: {\n processRows: 10, // Run after ServerSide (-10) so we receive managedNodes[]\n },\n incompatibleWith: [\n {\n name: 'groupingRows',\n reason:\n 'Both plugins transform the entire row model. TreePlugin flattens nested hierarchies while ' +\n 'GroupingRowsPlugin groups flat rows with synthetic headers. Use one approach per grid.',\n },\n {\n name: 'pivot',\n reason:\n 'PivotPlugin replaces the entire row and column structure with aggregated pivot data. ' +\n 'Tree hierarchy cannot coexist with pivot aggregation.',\n },\n ],\n events: [\n {\n type: 'tree-expand',\n description:\n 'Emitted when tree expansion state changes (toggle, expand all, collapse all). Broadcast to both DOM consumers and plugin bus.',\n },\n ],\n queries: [\n {\n type: 'canMoveRow',\n description: 'Returns false for rows with children (parent nodes cannot be reordered)',\n },\n {\n type: 'datasource:viewport-mapping',\n description: 'Translates flat viewport row indices to top-level node indices for ServerSide pagination.',\n },\n ],\n };\n\n /**\n * Optional dependency on MultiSort for coordinated sorting.\n * When MultiSort is loaded, Tree defers header click sorting to it and queries the\n * sort model in processRows. When MultiSort is absent, Tree uses its own sort state.\n */\n static override readonly dependencies = [\n { name: 'multiSort', required: false, reason: 'Queries sort model for coordinated tree sorting' },\n { name: 'serverSide', required: false, reason: 'Consumes datasource events for lazy-loaded tree data' },\n ];\n\n /** @internal */\n readonly name = 'tree';\n /** @internal */\n override readonly styles = styles;\n\n /** @internal */\n protected override get defaultConfig(): Partial<TreeConfig> {\n return {\n childrenField: 'children',\n autoDetect: true,\n defaultExpanded: false,\n indentWidth: 20,\n showExpandIcons: true,\n animation: 'slide',\n };\n }\n\n // #region State\n\n private expandedKeys = new Set<string>();\n private initialExpansionDone = false;\n private flattenedRows: FlattenedTreeRow[] = [];\n private rowKeyMap = new Map<string, FlattenedTreeRow>();\n private previousVisibleKeys = new Set<string>();\n private keysToAnimate = new Set<string>();\n private sortState: { field: string; direction: 1 | -1 } | null = null;\n /** Keys of nodes that are currently loading lazy children via ServerSide. */\n private loadingKeys = new Set<string>();\n\n /**\n * Stable key cache keyed by row identity.\n * Persists across sort operations (object identity is preserved by sort);\n * replaces the previous `__stableKey` field-mutation approach so that\n * `_rows[i]` remains the user's original row reference and `updateRow(s)`\n * mutations survive the next `processRows` rebuild.\n *\n * INVARIANT: never mutate row objects to attach tree metadata — keep all\n * tree-specific state in this map and `#rowMeta` (see plugin-author rule\n * in `.github/knowledge/grid-plugins.md`).\n */\n #rowKeys = new WeakMap<object, string>();\n\n /**\n * Per-row tree metadata (depth, hasChildren, isExpanded, key) keyed by\n * row identity. Looked up by the column renderer via {@link getRowMeta}.\n * Reassigned to a fresh `WeakMap` at the start of each `processRows` call so\n * collapsed/hidden rows don't return stale metadata from a prior pass.\n */\n #rowMeta = new WeakMap<object, FlattenedTreeRow>();\n\n /** Cached original (unwrapped) renderer to prevent re-wrapping on repeated processColumns calls. */\n private originalTreeColumnRenderer: ColumnViewRenderer | undefined;\n /** Field name of the column currently wrapped with tree decorations. */\n private wrappedTreeColumnField: string | undefined;\n\n /** @internal */\n override detach(): void {\n this.expandedKeys.clear();\n this.initialExpansionDone = false;\n this.flattenedRows = [];\n this.rowKeyMap.clear();\n this.previousVisibleKeys.clear();\n this.keysToAnimate.clear();\n this.sortState = null;\n this.loadingKeys.clear();\n this.originalTreeColumnRenderer = undefined;\n this.wrappedTreeColumnField = undefined;\n // WeakMaps GC themselves once row references are dropped — nothing to clear.\n }\n\n /**\n * Handle plugin queries.\n * @internal\n */\n override handleQuery(query: PluginQuery): unknown {\n if (query.type === 'canMoveRow') {\n // Tree rows with children cannot be reordered\n const row = query.context as { [key: string]: unknown } | null | undefined;\n const childrenField = this.config.childrenField ?? 'children';\n const children = row?.[childrenField];\n if (Array.isArray(children) && children.length > 0) {\n return false;\n }\n }\n\n if (query.type === 'datasource:viewport-mapping') {\n // Translate visible flat row indices → top-level node indices for ServerSide pagination\n const { viewportStart, viewportEnd } = query.context as ViewportMappingQuery;\n if (this.flattenedRows.length === 0) return undefined;\n\n const startNode = getTopLevelNodeIndex(this.flattenedRows, viewportStart);\n const endNode = getTopLevelNodeIndex(this.flattenedRows, viewportEnd) + 1; // exclusive\n const totalLoadedNodes = countTopLevelNodes(this.flattenedRows);\n\n return { startNode, endNode, totalLoadedNodes } satisfies ViewportMappingResponse;\n }\n\n return undefined;\n }\n\n // #endregion\n\n // #region Lifecycle\n\n /** @internal */\n override attach(grid: GridElement): void {\n super.attach(grid);\n\n // Listen for datasource:data from ServerSidePlugin — claim data for tree processing\n this.on('datasource:data', (detail: unknown) => {\n const d = detail as DataSourceDataDetail;\n if (!d.claimed) {\n d.claimed = true;\n }\n // Data flows through processRows pipeline — Tree receives it via the rows parameter\n // since ServerSide's processRows (hookPriority -10) runs first and returns managedNodes[]\n });\n\n // Listen for datasource:children — consume child rows from ServerSide\n this.on('datasource:children', (detail: unknown) => {\n const d = detail as DataSourceChildrenDetail;\n if (d.context?.source !== 'tree') return;\n d.claimed = true;\n\n // Merge children into the parent node\n const parentRow = d.context.parentNode as TreeRow | undefined;\n if (parentRow) {\n const childrenField = this.config.childrenField ?? 'children';\n (parentRow as Record<string, unknown>)[childrenField] = d.rows;\n // Look up the stable key by row identity (was stored on row as __stableKey\n // historically; now lives in a parallel WeakMap to keep row identity intact).\n const key = this.#rowKeys.get(parentRow as object) ?? String(parentRow.id ?? '?');\n this.loadingKeys.delete(key);\n this.requestRender();\n }\n });\n }\n\n // #endregion\n\n // #region Animation\n\n /**\n * Get expand/collapse animation style from plugin config.\n * Uses base class isAnimationEnabled to respect grid-level settings.\n */\n private get animationStyle(): ExpandCollapseAnimation {\n if (!this.isAnimationEnabled) return false;\n return this.config.animation ?? 'slide';\n }\n\n // #endregion\n\n // #region Auto-Detection\n\n detect(rows: readonly unknown[]): boolean {\n if (!this.config.autoDetect) return false;\n const treeRows = rows as readonly TreeRow[];\n const field = this.config.childrenField ?? inferChildrenField(treeRows) ?? 'children';\n return detectTreeStructure(treeRows, field);\n }\n\n // #endregion\n\n // #region Data Processing\n\n /** @internal */\n override processRows(rows: readonly unknown[]): TreeRow[] {\n const childrenField = this.config.childrenField ?? 'children';\n\n const treeRows = rows as readonly TreeRow[];\n\n if (treeRows.length === 0 || !detectTreeStructure(treeRows, childrenField)) {\n this.flattenedRows = [];\n this.rowKeyMap.clear();\n this.previousVisibleKeys.clear();\n // _rows[i] must remain the user's row reference. Return a shallow array\n // copy (so callers can't mutate the input array via the returned ref)\n // but DO NOT spread/clone the row objects themselves.\n return [...rows] as TreeRow[];\n }\n\n // Initialize expansion if needed.\n // When MultiSort is active, use its model instead of local sort state so\n // Tree and MultiSort don't fight over sort ownership.\n const effectiveSortState = this.resolveEffectiveSortState();\n\n if (this.config.defaultExpanded && !this.initialExpansionDone) {\n this.expandedKeys = expandAll(treeRows, this.config);\n this.initialExpansionDone = true;\n }\n\n // Single pass: sort + flatten in one walk, never cloning row objects.\n // `data` on each FlattenedTreeRow stays === the user's source row.\n this.flattenedRows = this.#flattenWithSort(treeRows, this.expandedKeys, effectiveSortState, null, 0);\n\n // Reset per-row metadata so rows that are no longer in the flattened\n // output (e.g. children of a now-collapsed parent) don't keep returning\n // stale entries via getRowMeta(). WeakMap reassignment is cheap and the\n // old map becomes GC-eligible immediately.\n this.#rowMeta = new WeakMap();\n this.rowKeyMap.clear();\n this.keysToAnimate.clear();\n const currentKeys = new Set<string>();\n\n for (const row of this.flattenedRows) {\n this.rowKeyMap.set(row.key, row);\n this.#rowMeta.set(row.data as object, row);\n currentKeys.add(row.key);\n if (!this.previousVisibleKeys.has(row.key) && row.depth > 0) {\n this.keysToAnimate.add(row.key);\n }\n }\n this.previousVisibleKeys = currentKeys;\n\n // Return source row references directly. Tree metadata (depth/key/etc.)\n // is read by the renderer via `getRowMeta(row)` instead of being spread\n // onto cloned row objects \\u2014 this keeps `_rows[i]` === user's row so that\n // `grid.updateRow(s)` mutations survive the next ROWS-phase rebuild.\n return this.flattenedRows.map((r) => r.data);\n }\n\n /**\n * Resolve the stable key for a row, caching by identity in {@link #rowKeys}.\n * Prefers `row.id` (already stable across sort), then any previously cached\n * key for this row reference, then falls back to a path-based key.\n */\n #keyFor(row: TreeRow, index: number, parentKey: string | null): string {\n if (row.id !== undefined) {\n const key = String(row.id);\n this.#rowKeys.set(row as object, key);\n return key;\n }\n const cached = this.#rowKeys.get(row as object);\n if (cached !== undefined) return cached;\n const key = parentKey ? `${parentKey}-${index}` : String(index);\n this.#rowKeys.set(row as object, key);\n return key;\n }\n\n /**\n * Recursive single-pass sort + flatten.\n * - Per-level sort uses `[...rows].sort(...)` which produces a new array of\n * the SAME row references in a new order \\u2014 never spreads the row objects.\n * - Children arrays are NOT mutated on the source rows; the sort produces a\n * transient ordering used only for traversal.\n */\n #flattenWithSort(\n rows: readonly TreeRow[],\n expanded: Set<string>,\n sort: { field: string; direction: 1 | -1 } | null,\n parentKey: string | null,\n depth: number,\n ): FlattenedTreeRow[] {\n const childrenField = this.config.childrenField ?? 'children';\n // Assign stable keys using the ORIGINAL (unsorted) index so that\n // path-based keys match those produced by `expandAll` (which walks the\n // tree in source order). `#keyFor` caches by row identity, so the\n // subsequent lookup in the post-sort loop returns the same key.\n for (let i = 0; i < rows.length; i++) {\n this.#keyFor(rows[i], i, parentKey);\n }\n const ordered = sort ? this.#sortLevel(rows, sort.field, sort.direction) : rows;\n const result: FlattenedTreeRow[] = [];\n\n for (let i = 0; i < ordered.length; i++) {\n const row = ordered[i];\n const key = this.#keyFor(row, i, parentKey);\n const children = row[childrenField];\n const embeddedChildren = Array.isArray(children) && children.length > 0;\n // Lazy children: truthy non-array value (e.g. `children: true`) signals\n // that children exist on the server but haven't been fetched yet.\n const lazyChildren = children != null && !Array.isArray(children) && !!children;\n const hasChildren = embeddedChildren || lazyChildren;\n const isExpanded = expanded.has(key);\n\n result.push({\n key,\n data: row,\n depth,\n hasChildren,\n isExpanded,\n parentKey,\n });\n\n if (embeddedChildren && isExpanded) {\n result.push(...this.#flattenWithSort(children as TreeRow[], expanded, sort, key, depth + 1));\n }\n }\n return result;\n }\n\n /**\n * Sort rows at a single level, returning a new array of the SAME row references\n * in sorted order. Never clones row objects, and never mutates the input array\n * (children arrays are user-owned for tree rows — we always pass a shallow copy\n * to the handler so a custom in-place sortHandler can't corrupt user data).\n *\n * Delegates to the same handler chain core uses: `gridConfig.sortHandler` when\n * provided, otherwise `builtInSort` (which honors per-column `sortComparator`\n * and `valueAccessor`). Async handlers cannot be awaited inside the synchronous\n * tree flatten — fall back to `builtInSort` when one is detected, and swallow\n * any rejection so it doesn't surface as an unhandled promise rejection.\n */\n #sortLevel(rows: readonly TreeRow[], field: string, dir: 1 | -1): TreeRow[] {\n const host = this.grid as unknown as GridHost<TreeRow> | undefined;\n const columns = (host?._columns ?? []) as ColumnConfig<TreeRow>[];\n const handler: SortHandler<TreeRow> = host?.effectiveConfig?.sortHandler ?? builtInSort;\n const sortState = { field, direction: dir };\n // Always pass a shallow copy: `rows` may be a user-owned `row.children`\n // array, and a non-defensive sortHandler could otherwise mutate it.\n const input = [...rows] as TreeRow[];\n const result = handler(input, sortState, columns);\n if (result && typeof (result as Promise<unknown[]>).then === 'function') {\n void (result as Promise<unknown[]>).catch(() => undefined);\n return builtInSort(input, sortState, columns);\n }\n return result as TreeRow[];\n }\n\n /**\n * Request lazy children for a node via ServerSide's `datasource:fetch-children` query.\n * Called when expanding a node whose children are not yet embedded (lazy indicator only).\n * No-op if ServerSide is not active, children are already loading, or children are embedded.\n */\n private requestLazyChildren(flatRow: FlattenedTreeRow): void {\n if (this.loadingKeys.has(flatRow.key)) return;\n\n const childrenField = this.config.childrenField ?? 'children';\n const children = flatRow.data[childrenField];\n // Only fetch if children is a lazy indicator (truthy but not a non-empty array)\n if (Array.isArray(children) && children.length > 0) return;\n\n const isServerSideActive = this.grid?.query?.('datasource:is-active', null);\n if (!isServerSideActive) return;\n\n this.loadingKeys.add(flatRow.key);\n this.grid.query('datasource:fetch-children', {\n context: { source: 'tree', parentNode: flatRow.data, nodePath: [flatRow.key] },\n } satisfies FetchChildrenQuery);\n }\n\n /**\n * Resolve the effective sort state: prefer MultiSort's model when available,\n * fall back to local tree sort state.\n * This follows the same pattern as GroupingRowsPlugin.resolveGroupSortDirections.\n */\n private resolveEffectiveSortState(): { field: string; direction: 1 | -1 } | null {\n // When MultiSort is loaded, prefer its model for consistency\n const multiSortResults = this.grid?.query?.('sort:get-model', null);\n if (Array.isArray(multiSortResults) && multiSortResults.length > 0) {\n const sortModel = multiSortResults[0] as Array<{ field: string; direction: 'asc' | 'desc' }>;\n if (Array.isArray(sortModel) && sortModel.length > 0) {\n // Use the primary sort column from MultiSort\n return {\n field: sortModel[0].field,\n direction: sortModel[0].direction === 'desc' ? -1 : 1,\n };\n }\n }\n // Fallback: local sort state (when MultiSort is not loaded)\n return this.sortState;\n }\n\n /** @internal */\n override processColumns(columns: readonly ColumnConfig[]): ColumnConfig[] {\n if (this.flattenedRows.length === 0) return [...columns];\n\n const cols = [...columns] as ColumnConfig[];\n if (cols.length === 0) return cols;\n\n // Determine which column gets the tree toggle and indentation.\n // If treeColumn is configured, find it by field name; otherwise use the first column.\n const { treeColumn } = this.config;\n let targetIndex = 0;\n if (treeColumn) {\n const idx = cols.findIndex((c) => c.field === treeColumn);\n if (idx >= 0) targetIndex = idx;\n }\n const targetCol = cols[targetIndex];\n const targetField = targetCol.field;\n\n // Capture the original (unwrapped) renderer only once per target column.\n // On subsequent processColumns calls, reuse the cached original so we\n // don't nest tree-cell-wrappers.\n if (this.wrappedTreeColumnField !== targetField) {\n this.originalTreeColumnRenderer = targetCol.viewRenderer;\n this.wrappedTreeColumnField = targetField;\n }\n const originalRenderer = this.originalTreeColumnRenderer;\n const getConfig = () => this.config;\n const setIconFn = this.setIcon.bind(this);\n\n const wrappedRenderer: ColumnViewRenderer = (ctx) => {\n const { row, value } = ctx;\n const { showExpandIcons = true, indentWidth } = getConfig();\n const meta = this.#rowMeta.get(row as object);\n const depth = meta?.depth ?? 0;\n\n const container = document.createElement('span');\n container.className = 'tree-cell-wrapper';\n container.style.setProperty('--tbw-tree-depth', String(depth));\n // Allow config-based indentWidth to override CSS default\n if (indentWidth !== undefined) {\n container.style.setProperty('--tbw-tree-indent-width', `${indentWidth}px`);\n }\n\n // Add expand/collapse icon or spacer\n if (showExpandIcons) {\n if (meta && meta.hasChildren) {\n const icon = document.createElement('span');\n icon.className = `${GridClasses.TREE_TOGGLE}${meta.isExpanded ? ` ${GridClasses.EXPANDED}` : ''}`;\n setIconFn(icon, meta.isExpanded ? 'collapse' : 'expand');\n icon.setAttribute('data-tree-key', meta.key);\n container.appendChild(icon);\n } else {\n const spacer = document.createElement('span');\n spacer.className = 'tree-spacer';\n container.appendChild(spacer);\n }\n }\n\n // Add the original content\n const content = document.createElement('span');\n content.className = 'tree-content';\n if (originalRenderer) {\n const result = originalRenderer(ctx);\n if (result instanceof Node) {\n content.appendChild(result);\n } else if (typeof result === 'string') {\n content.innerHTML = result;\n }\n } else {\n content.textContent = value != null ? String(value) : '';\n }\n container.appendChild(content);\n\n return container;\n };\n\n cols[targetIndex] = { ...targetCol, viewRenderer: wrappedRenderer };\n return cols;\n }\n\n // #endregion\n\n // #region Event Handlers\n\n /** @internal */\n override onCellClick(event: CellClickEvent): boolean {\n const target = event.originalEvent?.target as HTMLElement;\n if (!target?.classList.contains(GridClasses.TREE_TOGGLE)) return false;\n\n const key = target.getAttribute('data-tree-key');\n if (!key) return false;\n\n const flatRow = this.rowKeyMap.get(key);\n if (!flatRow) return false;\n\n this.expandedKeys = toggleExpand(this.expandedKeys, key);\n\n // Request lazy children when expanding a node without embedded children\n if (this.expandedKeys.has(key)) {\n this.requestLazyChildren(flatRow);\n }\n\n this.broadcast<TreeExpandDetail>('tree-expand', {\n key,\n row: flatRow.data,\n expanded: this.expandedKeys.has(key),\n depth: flatRow.depth,\n expandedKeys: [...this.expandedKeys],\n });\n this.requestRender();\n return true;\n }\n\n /** @internal */\n override onKeyDown(event: KeyboardEvent): boolean | void {\n // SPACE toggles expansion when on a row with children\n if (event.key !== ' ') return;\n\n const focusRow = this.grid._focusRow;\n const flatRow = this.flattenedRows[focusRow];\n if (!flatRow?.hasChildren) return;\n\n event.preventDefault();\n this.expandedKeys = toggleExpand(this.expandedKeys, flatRow.key);\n\n // Request lazy children when expanding a node without embedded children\n if (this.expandedKeys.has(flatRow.key)) {\n this.requestLazyChildren(flatRow);\n }\n\n this.broadcast<TreeExpandDetail>('tree-expand', {\n key: flatRow.key,\n row: flatRow.data,\n expanded: this.expandedKeys.has(flatRow.key),\n depth: flatRow.depth,\n expandedKeys: [...this.expandedKeys],\n });\n this.requestRenderWithFocus();\n return true;\n }\n\n /** @internal */\n override onHeaderClick(event: HeaderClickEvent): boolean {\n if (this.flattenedRows.length === 0 || !event.column.sortable) return false;\n\n // When MultiSort is active, let it handle header clicks entirely.\n // Tree will pick up the sort model in processRows via resolveEffectiveSortState().\n const multiSortResults = this.grid?.query?.('sort:get-model', null);\n if (Array.isArray(multiSortResults) && multiSortResults.length > 0) {\n // MultiSort is loaded — don't consume the event, let MultiSort handle it\n return false;\n }\n\n // Fallback: manage own sort state when MultiSort is not loaded\n const { field } = event.column;\n if (!this.sortState || this.sortState.field !== field) {\n this.sortState = { field, direction: 1 };\n } else if (this.sortState.direction === 1) {\n this.sortState = { field, direction: -1 };\n } else {\n this.sortState = null;\n }\n\n // Sync grid sort indicator\n const gridEl = this.grid as unknown as GridHost;\n if (gridEl._sortState !== undefined) {\n gridEl._sortState = this.sortState ? { ...this.sortState } : null;\n }\n\n this.broadcast('sort-change', { field, direction: this.sortState?.direction ?? 0 });\n this.requestRender();\n return true;\n }\n\n /** @internal */\n override afterRender(): void {\n const body = this.gridElement?.querySelector('.rows');\n if (!body) return;\n\n const style = this.animationStyle;\n const shouldAnimate = style !== false && this.keysToAnimate.size > 0;\n const animClass = style === 'fade' ? 'tbw-tree-fade-in' : 'tbw-tree-slide-in';\n\n for (const rowEl of body.querySelectorAll('.data-grid-row')) {\n const cell = rowEl.querySelector('.cell[data-row]');\n const idx = cell ? parseInt(cell.getAttribute('data-row') ?? '-1', 10) : -1;\n const treeRow = this.flattenedRows[idx];\n\n // Set aria-expanded on parent rows for screen readers\n if (treeRow?.hasChildren) {\n rowEl.setAttribute('aria-expanded', String(treeRow.isExpanded));\n }\n\n if (shouldAnimate && treeRow?.key && this.keysToAnimate.has(treeRow.key)) {\n rowEl.classList.add(animClass);\n rowEl.addEventListener('animationend', () => rowEl.classList.remove(animClass), { once: true });\n }\n }\n this.keysToAnimate.clear();\n }\n\n // #endregion\n\n // #region Public API\n\n /**\n * Expand a specific tree node, revealing its children.\n *\n * If the node is already expanded, this is a no-op.\n * Does **not** emit a `tree-expand` event (use {@link toggle} for event emission).\n *\n * @param key - The unique key of the node to expand (from {@link FlattenedTreeRow.key})\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.expand('documents'); // Expand a root node\n * tree.expand('documents||reports'); // Expand a nested node\n * ```\n */\n expand(key: string): void {\n this.expandedKeys.add(key);\n const flatRow = this.rowKeyMap.get(key);\n if (flatRow) {\n this.requestLazyChildren(flatRow);\n }\n this.requestRender();\n }\n\n /**\n * Collapse a specific tree node, hiding its children.\n *\n * If the node is already collapsed, this is a no-op.\n * Does **not** emit a `tree-expand` event (use {@link toggle} for event emission).\n *\n * @param key - The unique key of the node to collapse (from {@link FlattenedTreeRow.key})\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.collapse('documents');\n * ```\n */\n collapse(key: string): void {\n this.expandedKeys.delete(key);\n this.requestRender();\n }\n\n /**\n * Toggle the expanded state of a tree node.\n *\n * If the node is expanded it will be collapsed, and vice versa.\n * Emits a `tree-expand` event (broadcast to both DOM consumers and plugin bus).\n *\n * @param key - The unique key of the node to toggle (from {@link FlattenedTreeRow.key})\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.toggle('documents'); // Expand if collapsed, collapse if expanded\n * ```\n */\n toggle(key: string): void {\n this.expandedKeys = toggleExpand(this.expandedKeys, key);\n const flatRow = this.rowKeyMap.get(key);\n if (flatRow) {\n // Request lazy children when expanding a node without embedded children\n if (this.expandedKeys.has(key)) {\n this.requestLazyChildren(flatRow);\n }\n this.broadcast<TreeExpandDetail>('tree-expand', {\n key,\n row: flatRow.data,\n expanded: this.expandedKeys.has(key),\n depth: flatRow.depth,\n expandedKeys: [...this.expandedKeys],\n });\n } else {\n this.emitPluginEvent('tree-expand', { expandedKeys: [...this.expandedKeys] });\n }\n this.requestRender();\n }\n\n /**\n * Expand all tree nodes recursively.\n *\n * Every node with children will be expanded, revealing the full tree hierarchy.\n * Emits a `tree-expand` plugin event.\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.expandAll();\n * ```\n */\n expandAll(): void {\n this.expandedKeys = expandAll(this.rows as TreeRow[], this.config);\n this.emitPluginEvent('tree-expand', { expandedKeys: [...this.expandedKeys] });\n this.requestRender();\n }\n\n /**\n * Collapse all tree nodes.\n *\n * Every node will be collapsed, showing only root-level rows.\n * Emits a `tree-expand` plugin event.\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * tree.collapseAll();\n * ```\n */\n collapseAll(): void {\n this.expandedKeys = collapseAll();\n this.emitPluginEvent('tree-expand', { expandedKeys: [...this.expandedKeys] });\n this.requestRender();\n }\n\n /**\n * Check whether a specific tree node is currently expanded.\n *\n * @param key - The unique key of the node to check\n * @returns `true` if the node is expanded, `false` otherwise\n */\n isExpanded(key: string): boolean {\n return this.expandedKeys.has(key);\n }\n\n /**\n * Get the keys of all currently expanded nodes.\n *\n * Returns a snapshot copy — mutating the returned array does not affect the tree state.\n *\n * @returns Array of expanded node keys\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * const keys = tree.getExpandedKeys();\n * localStorage.setItem('treeState', JSON.stringify(keys));\n * ```\n */\n getExpandedKeys(): string[] {\n return [...this.expandedKeys];\n }\n\n /**\n * Get the flattened row model used for rendering.\n *\n * Returns a snapshot copy of the internal flattened tree rows, including\n * hierarchy metadata (depth, hasChildren, isExpanded, parentKey).\n *\n * @returns Array of {@link FlattenedTreeRow} objects\n */\n getFlattenedRows(): FlattenedTreeRow[] {\n return [...this.flattenedRows];\n }\n\n /**\n * Get tree metadata (depth, key, hasChildren, isExpanded, parentKey) for a\n * specific row reference. Returns `undefined` if the row is not part of the\n * currently-flattened tree (e.g. collapsed under a parent or never processed).\n *\n * Tree metadata lives in a parallel WeakMap keyed by row identity \\u2014 it is\n * NOT stored on the row object itself. This preserves the invariant that\n * `_rows[i]` IS the user's source row reference, so `grid.updateRow(s)`\n * mutations survive the next ROWS-phase rebuild.\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * const meta = tree.getRowMeta(grid.rows[0]);\n * console.log(meta?.depth, meta?.hasChildren);\n * ```\n */\n getRowMeta(row: TreeRow): FlattenedTreeRow | undefined {\n return this.#rowMeta.get(row as object);\n }\n\n /**\n * Look up an original row data object by its tree key.\n *\n * @param key - The unique key of the node\n * @returns The original row data, or `undefined` if not found\n */\n getRowByKey(key: string): TreeRow | undefined {\n return this.rowKeyMap.get(key)?.data;\n }\n\n /**\n * Expand all ancestor nodes of the target key, revealing it in the tree.\n *\n * Useful for \"scroll to node\" or search-and-reveal scenarios where a deeply\n * nested node needs to be made visible.\n *\n * @param key - The unique key of the node to reveal\n *\n * @example\n * ```ts\n * const tree = grid.getPluginByName('tree');\n * // Reveal a deeply nested node by expanding all its parents\n * tree.expandToKey('root||child||grandchild');\n * ```\n */\n expandToKey(key: string): void {\n this.expandedKeys = expandToKey(this.rows as TreeRow[], key, this.config, this.expandedKeys);\n this.requestRender();\n }\n\n // #endregion\n}\n"],"names":["generateRowKey","row","index","parentKey","id","String","toggleExpand","expandedKeys","key","newExpanded","Set","has","delete","add","expandAll","rows","config","depth","childrenField","keys","i","length","children","Array","isArray","childKeys","k","getPathToKey","targetKey","childPath","expandToKey","existingExpanded","path","getTopLevelNodeIndex","flattenedRows","flatRowIndex","idx","Math","min","current","topLevelIndex","detectTreeStructure","TreePlugin","BaseGridPlugin","static","modifiesRowStructure","hookPriority","processRows","incompatibleWith","name","reason","events","type","description","queries","required","styles","defaultConfig","autoDetect","defaultExpanded","indentWidth","showExpandIcons","animation","initialExpansionDone","rowKeyMap","Map","previousVisibleKeys","keysToAnimate","sortState","loadingKeys","rowKeys","WeakMap","rowMeta","originalTreeColumnRenderer","wrappedTreeColumnField","detach","this","clear","handleQuery","query","context","viewportStart","viewportEnd","startNode","endNode","totalLoadedNodes","count","countTopLevelNodes","attach","grid","super","on","detail","d","claimed","source","parentRow","parentNode","get","requestRender","animationStyle","isAnimationEnabled","detect","treeRows","field","commonArrayFields","value","inferChildrenField","effectiveSortState","resolveEffectiveSortState","flattenWithSort","currentKeys","set","data","map","r","keyFor","cached","expanded","sort","ordered","sortLevel","direction","result","embeddedChildren","lazyChildren","hasChildren","isExpanded","push","dir","host","columns","_columns","input","effectiveConfig","sortHandler","builtInSort","then","catch","requestLazyChildren","flatRow","isServerSideActive","nodePath","multiSortResults","sortModel","processColumns","cols","treeColumn","targetIndex","findIndex","c","targetCol","targetField","viewRenderer","originalRenderer","getConfig","setIconFn","setIcon","bind","ctx","meta","container","document","createElement","className","style","setProperty","icon","GridClasses","TREE_TOGGLE","EXPANDED","setAttribute","appendChild","spacer","content","Node","innerHTML","textContent","onCellClick","event","target","originalEvent","classList","contains","getAttribute","broadcast","onKeyDown","focusRow","_focusRow","preventDefault","requestRenderWithFocus","onHeaderClick","column","sortable","gridEl","_sortState","afterRender","body","gridElement","querySelector","shouldAnimate","size","animClass","rowEl","querySelectorAll","cell","parseInt","treeRow","addEventListener","remove","once","expand","collapse","toggle","emitPluginEvent","collapseAll","getExpandedKeys","getFlattenedRows","getRowMeta","getRowByKey"],"mappings":"keAYO,SAASA,EAAeC,EAAcC,EAAeC,GAC1D,YAAe,IAAXF,EAAIG,GAAyBC,OAAO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