@pretable/core 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -224,7 +224,86 @@ function evaluateFilter(cell, type, operator, value) {
224
224
  }
225
225
  }
226
226
 
227
- // ../grid-core/dist/derived-rows.js
227
+ // ../grid-core/dist/aggregators.js
228
+ function isAggregatableNumber(value) {
229
+ return typeof value === "number" && !Number.isNaN(value);
230
+ }
231
+ var sum = {
232
+ init: () => ({ sum: 0, count: 0 }),
233
+ accumulate(acc, value) {
234
+ if (!isAggregatableNumber(value))
235
+ return acc;
236
+ acc.sum += value;
237
+ acc.count += 1;
238
+ return acc;
239
+ },
240
+ merge: (a, b) => ({ sum: a.sum + b.sum, count: a.count + b.count }),
241
+ finalize: (acc) => acc.count === 0 ? null : acc.sum
242
+ };
243
+ var avg = {
244
+ init: () => ({ sum: 0, count: 0 }),
245
+ accumulate: sum.accumulate,
246
+ merge: sum.merge,
247
+ finalize: (acc) => acc.count === 0 ? null : acc.sum / acc.count
248
+ };
249
+ var min = {
250
+ init: () => ({ value: null }),
251
+ accumulate(acc, value) {
252
+ if (!isAggregatableNumber(value))
253
+ return acc;
254
+ if (acc.value === null || value < acc.value)
255
+ acc.value = value;
256
+ return acc;
257
+ },
258
+ merge: (a, b) => ({
259
+ value: a.value === null ? b.value : b.value === null ? a.value : Math.min(a.value, b.value)
260
+ }),
261
+ finalize: (acc) => acc.value
262
+ };
263
+ var max = {
264
+ init: () => ({ value: null }),
265
+ accumulate(acc, value) {
266
+ if (!isAggregatableNumber(value))
267
+ return acc;
268
+ if (acc.value === null || value > acc.value)
269
+ acc.value = value;
270
+ return acc;
271
+ },
272
+ merge: (a, b) => ({
273
+ value: a.value === null ? b.value : b.value === null ? a.value : Math.max(a.value, b.value)
274
+ }),
275
+ finalize: (acc) => acc.value
276
+ };
277
+ var count = {
278
+ init: () => ({ count: 0 }),
279
+ accumulate(acc) {
280
+ acc.count += 1;
281
+ return acc;
282
+ },
283
+ merge: (a, b) => ({ count: a.count + b.count }),
284
+ // `0` is arguably count's identity, unlike sum/min/max where "no numeric
285
+ // values seen" has no honest number. We still return `null`, for consistency
286
+ // with the other built-ins and because it is unreachable for a real group —
287
+ // a group only exists because at least one row landed in it.
288
+ finalize: (acc) => acc.count === 0 ? null : acc.count
289
+ };
290
+ var builtinAggregators = {
291
+ sum,
292
+ avg,
293
+ min,
294
+ max,
295
+ count
296
+ };
297
+ function resolveAggregator(spec) {
298
+ if (spec === void 0 || spec === null)
299
+ return null;
300
+ if (typeof spec === "string") {
301
+ return builtinAggregators[spec] ?? null;
302
+ }
303
+ return spec;
304
+ }
305
+
306
+ // ../grid-core/dist/row-utils.js
228
307
  function createSourceRows(options) {
229
308
  return options.rows.map((row, index) => ({
230
309
  id: options.getRowId?.(row, index) ?? String(index),
@@ -232,38 +311,6 @@ function createSourceRows(options) {
232
311
  sourceIndex: index
233
312
  }));
234
313
  }
235
- function deriveVisibleRows(input) {
236
- const resolvedFilters = resolveFilters(input.columns, input.filters);
237
- const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
238
- const sorted = sortRows(filtered, input.columns, input.sort);
239
- return sorted.map(({ id, row, sourceIndex }) => ({
240
- id,
241
- row,
242
- sourceIndex
243
- }));
244
- }
245
- function resolveFilters(columns, filters) {
246
- const columnMap = new Map(columns.map((c) => [c.id, c]));
247
- const resolved = [];
248
- for (const [columnId, filter] of Object.entries(filters)) {
249
- if (!filter || !isFilterActive(filter))
250
- continue;
251
- const column = columnMap.get(columnId);
252
- if (!column || column.filterable === false)
253
- continue;
254
- resolved.push({ column, filter });
255
- }
256
- return resolved;
257
- }
258
- function matchesFilters(row, resolvedFilters) {
259
- for (const { column, filter } of resolvedFilters) {
260
- const cell = readCellValue(row, column);
261
- if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
262
- return false;
263
- }
264
- }
265
- return true;
266
- }
267
314
  var collator = new Intl.Collator(void 0, {
268
315
  numeric: true,
269
316
  sensitivity: "base"
@@ -301,12 +348,277 @@ function readCellValue(row, column) {
301
348
  return column.value ? column.value(row) : row[column.id];
302
349
  }
303
350
 
351
+ // ../grid-core/dist/group-id.js
352
+ var GROUP_ID_PREFIX = "__group__:";
353
+ var LEVEL_SEPARATOR = "/";
354
+ var KEY_SEPARATOR = "=";
355
+ function escapeGroupKey(raw) {
356
+ return raw.replace(/%/g, "%25").replace(/\//g, "%2F").replace(/=/g, "%3D");
357
+ }
358
+ function stringifyGroupValue(value) {
359
+ if (value === null || value === void 0)
360
+ return "~";
361
+ switch (typeof value) {
362
+ case "string":
363
+ return `s:${value}`;
364
+ case "number":
365
+ return `n:${value}`;
366
+ case "boolean":
367
+ return `b:${value}`;
368
+ case "bigint":
369
+ return `i:${value}`;
370
+ default:
371
+ return `o:${safeToString(value)}`;
372
+ }
373
+ }
374
+ function safeToString(value) {
375
+ try {
376
+ return String(value);
377
+ } catch {
378
+ return "[unstringifiable]";
379
+ }
380
+ }
381
+ function makeGroupId(path) {
382
+ let id = GROUP_ID_PREFIX;
383
+ for (let i = 0; i < path.length; i += 1) {
384
+ if (i > 0)
385
+ id += LEVEL_SEPARATOR;
386
+ id += escapeGroupKey(path[i].columnId);
387
+ id += KEY_SEPARATOR;
388
+ id += escapeGroupKey(stringifyGroupValue(path[i].value));
389
+ }
390
+ return id;
391
+ }
392
+
393
+ // ../grid-core/dist/group-rows.js
394
+ function buildGroupedRows(args) {
395
+ const { rows, allRows, columns, rowGroups, sort } = args;
396
+ const sorted = sortRows(rows, columns, sort);
397
+ const levels = resolveLevels(columns, rowGroups);
398
+ if (levels.length === 0) {
399
+ return sorted.map(({ id, row, sourceIndex }) => ({
400
+ kind: "data",
401
+ id,
402
+ row,
403
+ sourceIndex,
404
+ depth: 0
405
+ }));
406
+ }
407
+ const roots = buildTree(sorted, levels);
408
+ const aggregateColumns = resolveAggregateColumns(columns);
409
+ if (aggregateColumns.length > 0) {
410
+ accumulate(allRows ? sortRows(allRows, columns, sort) : sorted, levels, roots, aggregateColumns);
411
+ }
412
+ const out = [];
413
+ flatten(roots, 0, levels, aggregateColumns, args, out);
414
+ return out;
415
+ }
416
+ function resolveLevels(columns, rowGroups) {
417
+ if (rowGroups.length === 0)
418
+ return [];
419
+ const byId = new Map(columns.map((column) => [column.id, column]));
420
+ const levels = [];
421
+ for (const columnId of rowGroups) {
422
+ const column = byId.get(columnId);
423
+ if (column)
424
+ levels.push(column);
425
+ }
426
+ return levels;
427
+ }
428
+ function resolveAggregateColumns(columns) {
429
+ const resolved = [];
430
+ for (const column of columns) {
431
+ const aggregator = resolveAggregator(column.aggregate);
432
+ if (aggregator)
433
+ resolved.push({ column, aggregator });
434
+ }
435
+ return resolved;
436
+ }
437
+ function buildTree(sorted, levels) {
438
+ const roots = /* @__PURE__ */ new Map();
439
+ const lastLevel = levels.length - 1;
440
+ const path = [];
441
+ for (const entry of sorted) {
442
+ let siblings = roots;
443
+ let node;
444
+ path.length = 0;
445
+ for (let depth = 0; depth <= lastLevel; depth += 1) {
446
+ const column = levels[depth];
447
+ const value = readCellValue(entry.row, column);
448
+ const key = stringifyGroupValue(value);
449
+ path.push({ columnId: column.id, value });
450
+ node = siblings.get(key);
451
+ if (!node) {
452
+ const isLeafLevel = depth === lastLevel;
453
+ node = {
454
+ id: makeGroupId(path),
455
+ depth,
456
+ columnId: column.id,
457
+ value,
458
+ childCount: 0,
459
+ children: isLeafLevel ? null : /* @__PURE__ */ new Map(),
460
+ rows: isLeafLevel ? [] : null,
461
+ accumulators: /* @__PURE__ */ new Map()
462
+ };
463
+ siblings.set(key, node);
464
+ }
465
+ node.childCount += 1;
466
+ if (node.children)
467
+ siblings = node.children;
468
+ }
469
+ node?.rows?.push(entry);
470
+ }
471
+ return roots;
472
+ }
473
+ function accumulate(source, levels, roots, aggregateColumns) {
474
+ for (const entry of source) {
475
+ const values = aggregateColumns.map(({ column }) => readCellValue(entry.row, column));
476
+ let siblings = roots;
477
+ for (let depth = 0; depth < levels.length && siblings; depth += 1) {
478
+ const key = stringifyGroupValue(readCellValue(entry.row, levels[depth]));
479
+ const node = siblings.get(key);
480
+ if (!node)
481
+ break;
482
+ for (let i = 0; i < aggregateColumns.length; i += 1) {
483
+ const { column, aggregator } = aggregateColumns[i];
484
+ const previous = node.accumulators.get(column.id);
485
+ const acc = previous === void 0 ? aggregator.init() : previous;
486
+ node.accumulators.set(column.id, aggregator.accumulate(acc, values[i], entry.row));
487
+ }
488
+ siblings = node.children;
489
+ }
490
+ }
491
+ }
492
+ function flatten(siblings, depth, levels, aggregateColumns, args, out) {
493
+ const nodes = [...siblings.values()];
494
+ sortSiblings(nodes, levels[depth], args.sort);
495
+ for (const node of nodes) {
496
+ out.push({
497
+ kind: "group",
498
+ id: node.id,
499
+ depth: node.depth,
500
+ columnId: node.columnId,
501
+ value: node.value,
502
+ childCount: node.childCount,
503
+ aggregates: finalizeAggregates(node, aggregateColumns)
504
+ });
505
+ if (!isExpanded(node.id, args))
506
+ continue;
507
+ if (node.children) {
508
+ flatten(node.children, depth + 1, levels, aggregateColumns, args, out);
509
+ continue;
510
+ }
511
+ for (const entry of node.rows ?? []) {
512
+ out.push({
513
+ kind: "data",
514
+ id: entry.id,
515
+ row: entry.row,
516
+ sourceIndex: entry.sourceIndex,
517
+ depth: depth + 1
518
+ });
519
+ }
520
+ }
521
+ }
522
+ function finalizeAggregates(node, aggregateColumns) {
523
+ const aggregates = {};
524
+ for (const { column, aggregator } of aggregateColumns) {
525
+ const acc = node.accumulators.get(column.id);
526
+ aggregates[column.id] = aggregator.finalize(acc === void 0 ? aggregator.init() : acc);
527
+ }
528
+ return aggregates;
529
+ }
530
+ function isExpanded(id, args) {
531
+ return args.groupExpansionOverrides.has(id) ? !args.defaultExpanded : args.defaultExpanded;
532
+ }
533
+ function sortSiblings(nodes, column, sort) {
534
+ if (nodes.length < 2)
535
+ return;
536
+ const entry = column ? sort.find((candidate) => candidate.columnId === column.id) : void 0;
537
+ const multiplier = entry?.direction === "desc" ? -1 : 1;
538
+ const allNumeric = nodes.every((node) => typeof node.value === "number");
539
+ nodes.sort((a, b) => {
540
+ const cmp = allNumeric ? a.value - b.value : collator.compare(String(a.value ?? ""), String(b.value ?? ""));
541
+ return cmp * multiplier;
542
+ });
543
+ }
544
+
545
+ // ../grid-core/dist/derived-rows.js
546
+ var NO_OVERRIDES = /* @__PURE__ */ new Set();
547
+ function deriveVisibleRows(input) {
548
+ const resolvedFilters = resolveFilters(input.columns, input.filters);
549
+ const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
550
+ return buildGroupedRows({
551
+ rows: filtered,
552
+ // Only worth carrying the pre-filter set when it can actually differ.
553
+ // Equal lengths mean the filter removed nothing, so `filtered` is a copy of
554
+ // `input.rows` in the same order and the two sort identically —
555
+ // `buildGroupedRows` sorts whichever it folds over, so skipping here does
556
+ // not change the fold order.
557
+ allRows: input.aggregateFilteredRows && filtered.length !== input.rows.length ? input.rows : void 0,
558
+ columns: input.columns,
559
+ rowGroups: input.rowGroups ?? [],
560
+ sort: input.sort,
561
+ groupExpansionOverrides: input.groupExpansionOverrides ?? NO_OVERRIDES,
562
+ defaultExpanded: input.groupsDefaultExpanded ?? true
563
+ });
564
+ }
565
+ function resolveFilters(columns, filters) {
566
+ const columnMap = new Map(columns.map((c) => [c.id, c]));
567
+ const resolved = [];
568
+ for (const [columnId, filter] of Object.entries(filters)) {
569
+ if (!filter || !isFilterActive(filter))
570
+ continue;
571
+ const column = columnMap.get(columnId);
572
+ if (!column || column.filterable === false)
573
+ continue;
574
+ resolved.push({ column, filter });
575
+ }
576
+ return resolved;
577
+ }
578
+ function matchesFilters(row, resolvedFilters) {
579
+ for (const { column, filter } of resolvedFilters) {
580
+ const cell = readCellValue(row, column);
581
+ if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
582
+ return false;
583
+ }
584
+ }
585
+ return true;
586
+ }
587
+
304
588
  // ../grid-core/dist/create-grid-core.js
305
589
  var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
590
+ function isDataRow(entry) {
591
+ return entry.kind === "data";
592
+ }
593
+ function scanDataRows(rows, rowId) {
594
+ let count2 = 0;
595
+ let index = -1;
596
+ for (const entry of rows) {
597
+ if (!isDataRow(entry))
598
+ continue;
599
+ if (rowId !== null && entry.id === rowId)
600
+ index = count2;
601
+ count2 += 1;
602
+ }
603
+ return { count: count2, index };
604
+ }
605
+ function dataRowAt(rows, ordinal) {
606
+ if (ordinal < 0)
607
+ return void 0;
608
+ let seen = 0;
609
+ for (const entry of rows) {
610
+ if (!isDataRow(entry))
611
+ continue;
612
+ if (seen === ordinal)
613
+ return entry;
614
+ seen += 1;
615
+ }
616
+ return void 0;
617
+ }
306
618
  function clampColumnWidth(width, column) {
307
- const min = column.minWidthPx ?? 40;
308
- const max = column.maxWidthPx ?? Infinity;
309
- return Math.max(min, Math.min(max, width));
619
+ const min2 = column.minWidthPx ?? 40;
620
+ const max2 = column.maxWidthPx ?? Infinity;
621
+ return Math.max(min2, Math.min(max2, width));
310
622
  }
311
623
  function applyAutosize(options, autosizeOptions) {
312
624
  const result = autosizeColumns({
@@ -355,8 +667,16 @@ function createGridCore(inputOptions) {
355
667
  let cachedVisibleRows = null;
356
668
  let cachedDerivedSort = null;
357
669
  let cachedDerivedFilters = null;
670
+ let cachedDerivedRowGroups = null;
671
+ let cachedDerivedOverrides = null;
672
+ let cachedDerivedDefaultExpanded = null;
673
+ let cachedDerivedAggregateFiltered = null;
358
674
  let sort = [];
359
675
  let filters = {};
676
+ let rowGroups = sanitizeRowGroups(options.columns.filter((column) => column.rowGroup === true).map((c) => c.id), options.columns);
677
+ let groupExpansionOverrides = /* @__PURE__ */ new Set();
678
+ let groupsDefaultExpanded = options.groupsDefaultExpanded ?? true;
679
+ let previousAggregates = /* @__PURE__ */ new Map();
360
680
  let selection = { ranges: [], anchor: null };
361
681
  let focus = { rowId: null, columnId: null };
362
682
  let editing = null;
@@ -461,9 +781,9 @@ function createGridCore(inputOptions) {
461
781
  emit();
462
782
  },
463
783
  selectAll() {
464
- const snapshot = getSnapshot();
465
- const firstRow = snapshot.visibleRows[0];
466
- const lastRow = snapshot.visibleRows[snapshot.visibleRows.length - 1];
784
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
785
+ const firstRow = dataRows[0];
786
+ const lastRow = dataRows[dataRows.length - 1];
467
787
  const firstColumn = options.columns[0];
468
788
  const lastColumn = options.columns[options.columns.length - 1];
469
789
  if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
@@ -551,17 +871,17 @@ function createGridCore(inputOptions) {
551
871
  emit();
552
872
  },
553
873
  setSelectAllVisible(checked) {
554
- const snapshot = getSnapshot();
874
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
555
875
  const firstColumn = options.columns[0];
556
876
  const lastColumn = options.columns[options.columns.length - 1];
557
877
  if (!firstColumn || !lastColumn) {
558
878
  return;
559
879
  }
560
- const visibleIds = new Set(snapshot.visibleRows.map((r) => r.id));
880
+ const visibleIds = new Set(dataRows.map((r) => r.id));
561
881
  const nonRowRanges = selection.ranges.filter((r) => !isFullRowRange(r, r.startRowId, firstColumn.id, lastColumn.id) || !visibleIds.has(r.startRowId));
562
882
  let next;
563
883
  if (checked) {
564
- const newRanges = snapshot.visibleRows.map((row) => ({
884
+ const newRanges = dataRows.map((row) => ({
565
885
  startRowId: row.id,
566
886
  endRowId: row.id,
567
887
  startColumnId: firstColumn.id,
@@ -569,7 +889,7 @@ function createGridCore(inputOptions) {
569
889
  }));
570
890
  next = {
571
891
  ranges: [...nonRowRanges, ...newRanges],
572
- anchor: snapshot.visibleRows[0] ? { rowId: snapshot.visibleRows[0].id, columnId: firstColumn.id } : selection.anchor
892
+ anchor: dataRows[0] ? { rowId: dataRows[0].id, columnId: firstColumn.id } : selection.anchor
573
893
  };
574
894
  } else {
575
895
  next = { ranges: nonRowRanges, anchor: selection.anchor };
@@ -591,14 +911,13 @@ function createGridCore(inputOptions) {
591
911
  },
592
912
  moveFocus(direction, moveOptions = {}) {
593
913
  const snapshot = getSnapshot();
594
- const visibleRows = snapshot.visibleRows;
914
+ const { count: dataRowCount, index: currentRowIndex } = scanDataRows(snapshot.visibleRows, focus.rowId);
595
915
  const columnList = options.columns;
596
- if (visibleRows.length === 0 || columnList.length === 0) {
916
+ if (dataRowCount === 0 || columnList.length === 0) {
597
917
  focus = { rowId: null, columnId: null };
598
918
  emit();
599
919
  return;
600
920
  }
601
- const currentRowIndex = focus.rowId ? visibleRows.findIndex((r) => r.id === focus.rowId) : -1;
602
921
  const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
603
922
  const hasRowFocus = currentRowIndex !== -1;
604
923
  const hasColumnFocus = currentColumnIndex !== -1;
@@ -606,28 +925,28 @@ function createGridCore(inputOptions) {
606
925
  const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
607
926
  let nextRowIndex = baseRowIndex;
608
927
  let nextColumnIndex = baseColumnIndex;
609
- const pageStep = computePageStep(viewport, visibleRows);
928
+ const pageStep = computePageStep(viewport, dataRowCount);
610
929
  switch (direction) {
611
930
  case "up":
612
931
  if (moveOptions.jumpToEdge) {
613
932
  nextRowIndex = 0;
614
933
  } else if (!hasRowFocus) {
615
- nextRowIndex = visibleRows.length - 1;
934
+ nextRowIndex = dataRowCount - 1;
616
935
  } else if (moveOptions.byPage) {
617
- nextRowIndex = clamp(baseRowIndex - pageStep, 0, visibleRows.length - 1);
936
+ nextRowIndex = clamp(baseRowIndex - pageStep, 0, dataRowCount - 1);
618
937
  } else {
619
- nextRowIndex = clamp(baseRowIndex - 1, 0, visibleRows.length - 1);
938
+ nextRowIndex = clamp(baseRowIndex - 1, 0, dataRowCount - 1);
620
939
  }
621
940
  break;
622
941
  case "down":
623
942
  if (moveOptions.jumpToEdge) {
624
- nextRowIndex = visibleRows.length - 1;
943
+ nextRowIndex = dataRowCount - 1;
625
944
  } else if (!hasRowFocus) {
626
945
  nextRowIndex = 0;
627
946
  } else if (moveOptions.byPage) {
628
- nextRowIndex = clamp(baseRowIndex + pageStep, 0, visibleRows.length - 1);
947
+ nextRowIndex = clamp(baseRowIndex + pageStep, 0, dataRowCount - 1);
629
948
  } else {
630
- nextRowIndex = clamp(baseRowIndex + 1, 0, visibleRows.length - 1);
949
+ nextRowIndex = clamp(baseRowIndex + 1, 0, dataRowCount - 1);
631
950
  }
632
951
  break;
633
952
  case "left":
@@ -649,7 +968,7 @@ function createGridCore(inputOptions) {
649
968
  }
650
969
  break;
651
970
  }
652
- const nextRow = visibleRows[nextRowIndex];
971
+ const nextRow = dataRowAt(snapshot.visibleRows, nextRowIndex);
653
972
  const nextColumn = columnList[nextColumnIndex];
654
973
  if (!nextRow || !nextColumn) {
655
974
  return;
@@ -1020,6 +1339,37 @@ function createGridCore(inputOptions) {
1020
1339
  cachedVisibleRows = null;
1021
1340
  emit();
1022
1341
  },
1342
+ setRowGroups(columnIds) {
1343
+ const next = sanitizeRowGroups(columnIds, options.columns);
1344
+ if (stringListsEqual(rowGroups, next)) {
1345
+ return;
1346
+ }
1347
+ rowGroups = next;
1348
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1349
+ emit();
1350
+ },
1351
+ toggleGroup(groupId) {
1352
+ store.setGroupExpanded(groupId, !isGroupExpanded(groupId));
1353
+ },
1354
+ setGroupExpanded(groupId, expanded) {
1355
+ if (isGroupExpanded(groupId) === expanded) {
1356
+ return;
1357
+ }
1358
+ const next = new Set(groupExpansionOverrides);
1359
+ if (expanded === groupsDefaultExpanded) {
1360
+ next.delete(groupId);
1361
+ } else {
1362
+ next.add(groupId);
1363
+ }
1364
+ groupExpansionOverrides = next;
1365
+ emit();
1366
+ },
1367
+ expandAll() {
1368
+ setExpansionDefault(true);
1369
+ },
1370
+ collapseAll() {
1371
+ setExpansionDefault(false);
1372
+ },
1023
1373
  beginEdit(addr, opts) {
1024
1374
  editing = {
1025
1375
  rowId: addr.rowId,
@@ -1079,19 +1429,60 @@ function createGridCore(inputOptions) {
1079
1429
  }
1080
1430
  };
1081
1431
  return store;
1432
+ function isGroupExpanded(groupId) {
1433
+ return groupExpansionOverrides.has(groupId) ? !groupsDefaultExpanded : groupsDefaultExpanded;
1434
+ }
1435
+ function setExpansionDefault(expanded) {
1436
+ if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1437
+ return;
1438
+ }
1439
+ groupsDefaultExpanded = expanded;
1440
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1441
+ emit();
1442
+ }
1443
+ function preserveAggregateIdentity(rows) {
1444
+ const next = /* @__PURE__ */ new Map();
1445
+ let result = rows;
1446
+ for (let i = 0; i < rows.length; i += 1) {
1447
+ const entry = rows[i];
1448
+ if (entry.kind !== "group")
1449
+ continue;
1450
+ const previous = previousAggregates.get(entry.id);
1451
+ if (previous && recordsEqual(previous, entry.aggregates)) {
1452
+ if (result === rows)
1453
+ result = rows.slice();
1454
+ result[i] = { ...entry, aggregates: previous };
1455
+ next.set(entry.id, previous);
1456
+ } else {
1457
+ next.set(entry.id, entry.aggregates);
1458
+ }
1459
+ }
1460
+ previousAggregates = next;
1461
+ return result;
1462
+ }
1082
1463
  function getSnapshot() {
1083
1464
  if (cachedSnapshot) {
1084
1465
  return cachedSnapshot;
1085
1466
  }
1086
- const visibleRows = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters ? cachedVisibleRows : deriveVisibleRows({
1467
+ const aggregateFilteredRows = options.aggregateFilteredRows ?? false;
1468
+ const derivedIsFresh = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters && cachedDerivedRowGroups === rowGroups && cachedDerivedOverrides === groupExpansionOverrides && cachedDerivedDefaultExpanded === groupsDefaultExpanded && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1469
+ const visibleRows = derivedIsFresh ? cachedVisibleRows : preserveAggregateIdentity(deriveVisibleRows({
1087
1470
  columns: options.columns,
1088
1471
  filters,
1089
1472
  rows: sourceRows,
1090
- sort
1091
- });
1473
+ sort,
1474
+ rowGroups,
1475
+ groupExpansionOverrides,
1476
+ groupsDefaultExpanded,
1477
+ aggregateFilteredRows
1478
+ }));
1092
1479
  cachedVisibleRows = visibleRows;
1093
1480
  cachedDerivedSort = sort;
1094
1481
  cachedDerivedFilters = filters;
1482
+ cachedDerivedRowGroups = rowGroups;
1483
+ cachedDerivedOverrides = groupExpansionOverrides;
1484
+ cachedDerivedDefaultExpanded = groupsDefaultExpanded;
1485
+ cachedDerivedAggregateFiltered = aggregateFilteredRows;
1095
1486
  cachedSnapshot = {
1096
1487
  viewport,
1097
1488
  sort: sort.map((entry) => ({ ...entry })),
@@ -1107,7 +1498,10 @@ function createGridCore(inputOptions) {
1107
1498
  start: 0,
1108
1499
  end: visibleRows.length
1109
1500
  },
1110
- editing: editing ? { ...editing } : null
1501
+ editing: editing ? { ...editing } : null,
1502
+ rowGroups: [...rowGroups],
1503
+ groupExpansionOverrides: new Set(groupExpansionOverrides),
1504
+ groupsDefaultExpanded
1111
1505
  };
1112
1506
  return cachedSnapshot;
1113
1507
  }
@@ -1118,8 +1512,8 @@ function createGridCore(inputOptions) {
1118
1512
  }
1119
1513
  }
1120
1514
  }
1121
- function clamp(value, min, max) {
1122
- return Math.max(min, Math.min(max, value));
1515
+ function clamp(value, min2, max2) {
1516
+ return Math.max(min2, Math.min(max2, value));
1123
1517
  }
1124
1518
  function sameColumnLayout(a, b) {
1125
1519
  if (a.length !== b.length) {
@@ -1130,12 +1524,12 @@ function sameColumnLayout(a, b) {
1130
1524
  return left.id === right.id && left.header === right.header && left.widthPx === right.widthPx && left.minWidthPx === right.minWidthPx && left.maxWidthPx === right.maxWidthPx && left.pinned === right.pinned && left.sortable === right.sortable && left.filterable === right.filterable && left.resizable === right.resizable && left.reorderable === right.reorderable && left.wrap === right.wrap;
1131
1525
  });
1132
1526
  }
1133
- function computePageStep(viewport, visibleRows) {
1134
- if (viewport.height <= 0 || visibleRows.length === 0) {
1527
+ function computePageStep(viewport, dataRowCount) {
1528
+ if (viewport.height <= 0 || dataRowCount === 0) {
1135
1529
  return 1;
1136
1530
  }
1137
1531
  const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
1138
- return Math.min(estimatedRowsPerPage, visibleRows.length);
1532
+ return Math.min(estimatedRowsPerPage, dataRowCount);
1139
1533
  }
1140
1534
  function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
1141
1535
  return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
@@ -1165,6 +1559,28 @@ function sanitizeSortEntries(entries, columns) {
1165
1559
  return column !== void 0 && column.sortable !== false;
1166
1560
  });
1167
1561
  }
1562
+ function sanitizeRowGroups(columnIds, columns) {
1563
+ const known = new Set(columns.map((column) => column.id));
1564
+ const seen = /* @__PURE__ */ new Set();
1565
+ const next = [];
1566
+ for (const columnId of columnIds) {
1567
+ if (!known.has(columnId) || seen.has(columnId))
1568
+ continue;
1569
+ seen.add(columnId);
1570
+ next.push(columnId);
1571
+ }
1572
+ return next;
1573
+ }
1574
+ function stringListsEqual(a, b) {
1575
+ return a.length === b.length && a.every((value, i) => value === b[i]);
1576
+ }
1577
+ function recordsEqual(a, b) {
1578
+ const aKeys = Object.keys(a);
1579
+ if (aKeys.length !== Object.keys(b).length) {
1580
+ return false;
1581
+ }
1582
+ return aKeys.every((key) => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key]));
1583
+ }
1168
1584
  function sortsEqual(a, b) {
1169
1585
  if (a.length !== b.length) {
1170
1586
  return false;
@@ -1230,6 +1646,11 @@ function createGrid(options) {
1230
1646
  setSelectAllVisible: engine.setSelectAllVisible,
1231
1647
  setFocus: engine.setFocus,
1232
1648
  moveFocus: engine.moveFocus,
1649
+ setRowGroups: engine.setRowGroups,
1650
+ toggleGroup: engine.toggleGroup,
1651
+ setGroupExpanded: engine.setGroupExpanded,
1652
+ expandAll: engine.expandAll,
1653
+ collapseAll: engine.collapseAll,
1233
1654
  setViewport: engine.setViewport,
1234
1655
  autosizeColumns: engine.autosizeColumns,
1235
1656
  setColumnWidth: engine.setColumnWidth,