@smartnet360/svelte-grid 0.1.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.
@@ -0,0 +1,299 @@
1
+ /**
2
+ * Grid State Factory
3
+ *
4
+ * Creates reactive state for the grid using Svelte 5 runes.
5
+ * This is the central state management for all grid operations.
6
+ */
7
+ import type { ColumnDefinition, GridOptions, TablePlugin, RowData, SortConfig, SortDirection, FilterConfig, FilterOperator, GroupBy, GroupConfig, GroupInfo, DisplayRow } from '../types.js';
8
+ declare const GRID_CONTEXT_KEY: unique symbol;
9
+ export { GRID_CONTEXT_KEY };
10
+ /**
11
+ * Grid state class using Svelte 5 runes
12
+ * Manages all reactive state for a grid instance
13
+ */
14
+ export declare class GridStateManager<T = RowData> {
15
+ /** Raw data array */
16
+ rawData: T[];
17
+ /** Column definitions */
18
+ columns: ColumnDefinition<T>[];
19
+ /** Scroll position */
20
+ scrollTop: number;
21
+ scrollLeft: number;
22
+ /** Container dimensions */
23
+ containerHeight: number;
24
+ containerWidth: number;
25
+ /** Row height for virtual scrolling */
26
+ rowHeight: number;
27
+ /** Grid options */
28
+ options: Partial<GridOptions<T>>;
29
+ /** Current sort configuration (supports multi-sort) */
30
+ sortConfigs: SortConfig[];
31
+ /** Current filter configurations */
32
+ filterConfigs: FilterConfig[];
33
+ /** Header filter values (for UI inputs) */
34
+ headerFilterValues: Record<string, string>;
35
+ /** Group configuration */
36
+ groupByConfig: GroupConfig<T>[];
37
+ /** Set of open group keys (using string path like "field:value" or "field:value/field2:value2" for nested) */
38
+ openGroups: Set<string>;
39
+ /** Height of group header rows */
40
+ groupHeaderHeight: number;
41
+ /** Column widths (overrides column.width when user resizes) */
42
+ columnWidths: Record<string, number>;
43
+ /** Registered plugins */
44
+ private plugins;
45
+ /** Data pipeline handlers sorted by priority */
46
+ private pipelineHandlers;
47
+ /** Data after passing through filtering, sorting, and plugin pipeline */
48
+ processedData: T[];
49
+ /** Whether grouping is enabled */
50
+ isGrouped: boolean;
51
+ /** Build groups from processed data */
52
+ groupedData: GroupInfo<T>[];
53
+ /** Display rows - either flat data or grouped data with headers */
54
+ displayRows: DisplayRow<T>[];
55
+ /** Flatten groups into display rows (group headers + visible rows) */
56
+ private flattenGroups;
57
+ /** Build group hierarchy recursively */
58
+ private buildGroups;
59
+ /** Count total rows in a group including nested groups */
60
+ private countNestedRows;
61
+ /** Calculate aggregates for a group's rows */
62
+ private calculateAggregates;
63
+ /** Check if a group is open */
64
+ private isGroupOpen;
65
+ /** Apply filter configs to data */
66
+ private applyFilters;
67
+ /** Apply header filter values to data */
68
+ private applyHeaderFilters;
69
+ /** Check if a value matches a filter condition */
70
+ private matchesFilter;
71
+ /** Apply sort configs to data */
72
+ private applySorting;
73
+ /** Total number of rows after processing (includes group headers when grouped) */
74
+ totalRows: number;
75
+ /** Total data rows (without group headers) */
76
+ totalDataRows: number;
77
+ /** Visible columns (filtered for visibility) */
78
+ visibleColumns: ColumnDefinition<T>[];
79
+ /** Columns frozen to the left */
80
+ frozenLeftColumns: ColumnDefinition<T>[];
81
+ /** Columns frozen to the right */
82
+ frozenRightColumns: ColumnDefinition<T>[];
83
+ /** Scrollable (non-frozen) columns */
84
+ scrollableColumns: ColumnDefinition<T>[];
85
+ /** Number of rows frozen at the top (only applies when not grouped) */
86
+ frozenRowCount: number;
87
+ /** Frozen rows (always rendered at top) - only when not grouped */
88
+ frozenRows: T[];
89
+ /** Non-frozen display rows for virtual scrolling */
90
+ scrollableDisplayRows: DisplayRow<T>[];
91
+ /** Total scrollable rows (excluding frozen) */
92
+ scrollableRowCount: number;
93
+ /** Get effective row height for a display row */
94
+ getDisplayRowHeight(displayRow: DisplayRow<T>): number;
95
+ /** Total content height for scrollable area */
96
+ totalHeight: number;
97
+ /** Number of visible rows that fit in container (accounting for frozen row height) */
98
+ visibleRowCount: number;
99
+ /** Start index for visible scrollable rows (accounts for variable heights when grouped) */
100
+ startIndex: number;
101
+ /** End index for visible scrollable rows */
102
+ endIndex: number;
103
+ /** Visible display rows slice */
104
+ visibleDisplayRows: DisplayRow<T>[];
105
+ /** Legacy: visible data rows (for non-grouped compatibility) */
106
+ visibleRows: T[];
107
+ /** Offset for positioning visible rows (accounts for variable heights when grouped) */
108
+ offsetY: number;
109
+ constructor(options: GridOptions<T>);
110
+ /**
111
+ * Set grid options and update state
112
+ */
113
+ setOptions(options: GridOptions<T>): void;
114
+ /**
115
+ * Set new data
116
+ */
117
+ setData(data: T[]): void;
118
+ /**
119
+ * Update a single row by index
120
+ */
121
+ updateRow(index: number, data: Partial<T>): void;
122
+ /**
123
+ * Add a row at the end or at specific index
124
+ */
125
+ addRow(data: T, index?: number): void;
126
+ /**
127
+ * Remove a row by index
128
+ */
129
+ removeRow(index: number): void;
130
+ /**
131
+ * Get row by key value
132
+ */
133
+ getRowByKey(keyField: keyof T, keyValue: unknown): T | undefined;
134
+ /**
135
+ * Set column definitions
136
+ */
137
+ setColumns(columns: ColumnDefinition<T>[]): void;
138
+ /**
139
+ * Update a single column definition
140
+ */
141
+ updateColumn(field: string, updates: Partial<ColumnDefinition<T>>): void;
142
+ /**
143
+ * Show/hide a column
144
+ */
145
+ setColumnVisibility(field: string, visible: boolean): void;
146
+ /**
147
+ * Get column by field name
148
+ */
149
+ getColumn(field: string): ColumnDefinition<T> | undefined;
150
+ /**
151
+ * Set sort for a field (replaces existing sort)
152
+ */
153
+ setSort(field: string, direction: SortDirection): void;
154
+ /**
155
+ * Toggle sort for a field (cycles through: none -> asc -> desc -> none)
156
+ */
157
+ toggleSort(field: string, multiSort?: boolean): void;
158
+ /**
159
+ * Clear all sorting
160
+ */
161
+ clearSort(): void;
162
+ /**
163
+ * Get current sort direction for a field
164
+ */
165
+ getSortDirection(field: string): SortDirection;
166
+ /**
167
+ * Get sort index for multi-sort (0-based, -1 if not sorted)
168
+ */
169
+ getSortIndex(field: string): number;
170
+ /**
171
+ * Set a programmatic filter
172
+ */
173
+ setFilter(field: string, operator: FilterOperator, value: unknown): void;
174
+ /**
175
+ * Remove a filter for a field
176
+ */
177
+ removeFilter(field: string): void;
178
+ /**
179
+ * Clear all filters
180
+ */
181
+ clearFilters(): void;
182
+ /**
183
+ * Set header filter value (for UI input)
184
+ */
185
+ setHeaderFilter(field: string, value: string): void;
186
+ /**
187
+ * Get header filter value
188
+ */
189
+ getHeaderFilter(field: string): string;
190
+ /**
191
+ * Set column width (from user resize)
192
+ */
193
+ setColumnWidth(field: string, width: number): void;
194
+ /**
195
+ * Get effective column width (user-set or default)
196
+ */
197
+ getColumnWidth(field: string): number | undefined;
198
+ /**
199
+ * Reset column width to default
200
+ */
201
+ resetColumnWidth(field: string): void;
202
+ /**
203
+ * Reset all column widths
204
+ */
205
+ resetAllColumnWidths(): void;
206
+ /**
207
+ * Auto-fit column width to content (double-click resize)
208
+ */
209
+ autoFitColumn(field: string): void;
210
+ /**
211
+ * Auto-fit all columns to their content
212
+ */
213
+ autoFitAllColumns(): void;
214
+ /**
215
+ * Get the sticky left position for a frozen left column
216
+ */
217
+ getFrozenLeftPosition(field: string): number;
218
+ /**
219
+ * Get the sticky right position for a frozen right column
220
+ */
221
+ getFrozenRightPosition(field: string): number;
222
+ /**
223
+ * Get total width of frozen left columns
224
+ */
225
+ get frozenLeftWidth(): number;
226
+ /**
227
+ * Get total width of frozen right columns
228
+ */
229
+ get frozenRightWidth(): number;
230
+ /**
231
+ * Set the number of frozen rows
232
+ */
233
+ setFrozenRowCount(count: number): void;
234
+ /**
235
+ * Set group configuration
236
+ * @param groupBy - Field name, array of fields, function, or GroupConfig
237
+ */
238
+ setGroupBy(groupBy: GroupBy<T> | undefined): void;
239
+ /**
240
+ * Toggle a group open/closed
241
+ */
242
+ toggleGroup(groupPath: string): void;
243
+ /**
244
+ * Expand all groups
245
+ */
246
+ expandAllGroups(): void;
247
+ /**
248
+ * Collapse all groups
249
+ */
250
+ collapseAllGroups(): void;
251
+ /**
252
+ * Get the path for a group (used for open/closed tracking)
253
+ */
254
+ private getGroupPath;
255
+ /**
256
+ * Get groups at a specific level
257
+ */
258
+ getGroupsAtLevel(level: number): GroupInfo<T>[];
259
+ /**
260
+ * Set group header height
261
+ */
262
+ setGroupHeaderHeight(height: number): void;
263
+ /**
264
+ * Update scroll position
265
+ */
266
+ setScrollPosition(top: number, left?: number): void;
267
+ /**
268
+ * Update container dimensions
269
+ */
270
+ setContainerDimensions(width: number, height: number): void;
271
+ /**
272
+ * Scroll to a specific row index
273
+ */
274
+ scrollToRow(index: number): void;
275
+ /**
276
+ * Register a plugin
277
+ */
278
+ registerPlugin(plugin: TablePlugin<T>): void;
279
+ /**
280
+ * Unregister a plugin
281
+ */
282
+ unregisterPlugin(name: string): void;
283
+ /**
284
+ * Get plugin API
285
+ */
286
+ getPluginApi(name: string): Record<string, (...args: unknown[]) => unknown> | undefined;
287
+ /**
288
+ * Get all plugin header cell enhancements for a column
289
+ */
290
+ getHeaderCellEnhancements(column: ColumnDefinition<T>): Record<string, unknown>;
291
+ /**
292
+ * Destroy the state manager and cleanup plugins
293
+ */
294
+ destroy(): void;
295
+ }
296
+ /**
297
+ * Factory function to create a new grid state manager
298
+ */
299
+ export declare function createGridState<T>(options: GridOptions<T>): GridStateManager<T>;