@pretable/core 0.0.3 → 0.0.6

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.cjs CHANGED
@@ -226,7 +226,86 @@ function evaluateFilter(cell, type, operator, value) {
226
226
  }
227
227
  }
228
228
 
229
- // ../grid-core/dist/derived-rows.js
229
+ // ../grid-core/dist/aggregators.js
230
+ function isAggregatableNumber(value) {
231
+ return typeof value === "number" && !Number.isNaN(value);
232
+ }
233
+ var sum = {
234
+ init: () => ({ sum: 0, count: 0 }),
235
+ accumulate(acc, value) {
236
+ if (!isAggregatableNumber(value))
237
+ return acc;
238
+ acc.sum += value;
239
+ acc.count += 1;
240
+ return acc;
241
+ },
242
+ merge: (a, b) => ({ sum: a.sum + b.sum, count: a.count + b.count }),
243
+ finalize: (acc) => acc.count === 0 ? null : acc.sum
244
+ };
245
+ var avg = {
246
+ init: () => ({ sum: 0, count: 0 }),
247
+ accumulate: sum.accumulate,
248
+ merge: sum.merge,
249
+ finalize: (acc) => acc.count === 0 ? null : acc.sum / acc.count
250
+ };
251
+ var min = {
252
+ init: () => ({ value: null }),
253
+ accumulate(acc, value) {
254
+ if (!isAggregatableNumber(value))
255
+ return acc;
256
+ if (acc.value === null || value < acc.value)
257
+ acc.value = value;
258
+ return acc;
259
+ },
260
+ merge: (a, b) => ({
261
+ value: a.value === null ? b.value : b.value === null ? a.value : Math.min(a.value, b.value)
262
+ }),
263
+ finalize: (acc) => acc.value
264
+ };
265
+ var max = {
266
+ init: () => ({ value: null }),
267
+ accumulate(acc, value) {
268
+ if (!isAggregatableNumber(value))
269
+ return acc;
270
+ if (acc.value === null || value > acc.value)
271
+ acc.value = value;
272
+ return acc;
273
+ },
274
+ merge: (a, b) => ({
275
+ value: a.value === null ? b.value : b.value === null ? a.value : Math.max(a.value, b.value)
276
+ }),
277
+ finalize: (acc) => acc.value
278
+ };
279
+ var count = {
280
+ init: () => ({ count: 0 }),
281
+ accumulate(acc) {
282
+ acc.count += 1;
283
+ return acc;
284
+ },
285
+ merge: (a, b) => ({ count: a.count + b.count }),
286
+ // `0` is arguably count's identity, unlike sum/min/max where "no numeric
287
+ // values seen" has no honest number. We still return `null`, for consistency
288
+ // with the other built-ins and because it is unreachable for a real group —
289
+ // a group only exists because at least one row landed in it.
290
+ finalize: (acc) => acc.count === 0 ? null : acc.count
291
+ };
292
+ var builtinAggregators = {
293
+ sum,
294
+ avg,
295
+ min,
296
+ max,
297
+ count
298
+ };
299
+ function resolveAggregator(spec) {
300
+ if (spec === void 0 || spec === null)
301
+ return null;
302
+ if (typeof spec === "string") {
303
+ return builtinAggregators[spec] ?? null;
304
+ }
305
+ return spec;
306
+ }
307
+
308
+ // ../grid-core/dist/row-utils.js
230
309
  function createSourceRows(options) {
231
310
  return options.rows.map((row, index) => ({
232
311
  id: options.getRowId?.(row, index) ?? String(index),
@@ -234,38 +313,6 @@ function createSourceRows(options) {
234
313
  sourceIndex: index
235
314
  }));
236
315
  }
237
- function deriveVisibleRows(input) {
238
- const resolvedFilters = resolveFilters(input.columns, input.filters);
239
- const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
240
- const sorted = sortRows(filtered, input.columns, input.sort);
241
- return sorted.map(({ id, row, sourceIndex }) => ({
242
- id,
243
- row,
244
- sourceIndex
245
- }));
246
- }
247
- function resolveFilters(columns, filters) {
248
- const columnMap = new Map(columns.map((c) => [c.id, c]));
249
- const resolved = [];
250
- for (const [columnId, filter] of Object.entries(filters)) {
251
- if (!filter || !isFilterActive(filter))
252
- continue;
253
- const column = columnMap.get(columnId);
254
- if (!column || column.filterable === false)
255
- continue;
256
- resolved.push({ column, filter });
257
- }
258
- return resolved;
259
- }
260
- function matchesFilters(row, resolvedFilters) {
261
- for (const { column, filter } of resolvedFilters) {
262
- const cell = readCellValue(row, column);
263
- if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
264
- return false;
265
- }
266
- }
267
- return true;
268
- }
269
316
  var collator = new Intl.Collator(void 0, {
270
317
  numeric: true,
271
318
  sensitivity: "base"
@@ -303,12 +350,300 @@ function readCellValue(row, column) {
303
350
  return column.value ? column.value(row) : row[column.id];
304
351
  }
305
352
 
353
+ // ../grid-core/dist/group-id.js
354
+ var GROUP_ID_PREFIX = "__group__:";
355
+ var LEVEL_SEPARATOR = "/";
356
+ var KEY_SEPARATOR = "=";
357
+ function escapeGroupKey(raw) {
358
+ return raw.replace(/%/g, "%25").replace(/\//g, "%2F").replace(/=/g, "%3D");
359
+ }
360
+ function stringifyGroupValue(value) {
361
+ if (value === null || value === void 0)
362
+ return "~";
363
+ switch (typeof value) {
364
+ case "string":
365
+ return `s:${value}`;
366
+ case "number":
367
+ return `n:${value}`;
368
+ case "boolean":
369
+ return `b:${value}`;
370
+ case "bigint":
371
+ return `i:${value}`;
372
+ default:
373
+ return `o:${safeToString(value)}`;
374
+ }
375
+ }
376
+ function safeToString(value) {
377
+ try {
378
+ return String(value);
379
+ } catch {
380
+ return "[unstringifiable]";
381
+ }
382
+ }
383
+ function makeGroupId(path) {
384
+ let id = GROUP_ID_PREFIX;
385
+ for (let i = 0; i < path.length; i += 1) {
386
+ if (i > 0)
387
+ id += LEVEL_SEPARATOR;
388
+ id += escapeGroupKey(path[i].columnId);
389
+ id += KEY_SEPARATOR;
390
+ id += escapeGroupKey(stringifyGroupValue(path[i].value));
391
+ }
392
+ return id;
393
+ }
394
+
395
+ // ../grid-core/dist/group-rows.js
396
+ function buildGroupedRows(args) {
397
+ const { rows, allRows, columns, rowGroups, sort } = args;
398
+ const sorted = sortRows(rows, columns, sort);
399
+ const levels = resolveLevels(columns, rowGroups);
400
+ if (levels.length === 0) {
401
+ return sorted.map(({ id, row, sourceIndex }) => ({
402
+ kind: "data",
403
+ id,
404
+ row,
405
+ sourceIndex,
406
+ depth: 0
407
+ }));
408
+ }
409
+ const roots = buildTree(sorted, levels);
410
+ const aggregateColumns = resolveAggregateColumns(columns);
411
+ if (aggregateColumns.length > 0) {
412
+ accumulate(allRows ? sortRows(allRows, columns, sort) : sorted, levels, roots, aggregateColumns);
413
+ }
414
+ const out = [];
415
+ flatten(roots, 0, levels, aggregateColumns, args, out);
416
+ return out;
417
+ }
418
+ function resolveLevels(columns, rowGroups) {
419
+ if (rowGroups.length === 0)
420
+ return [];
421
+ const byId = new Map(columns.map((column) => [column.id, column]));
422
+ const levels = [];
423
+ for (const columnId of rowGroups) {
424
+ const column = byId.get(columnId);
425
+ if (column)
426
+ levels.push(column);
427
+ }
428
+ return levels;
429
+ }
430
+ function resolveAggregateColumns(columns) {
431
+ const resolved = [];
432
+ for (const column of columns) {
433
+ const aggregator = resolveAggregator(column.aggregate);
434
+ if (aggregator)
435
+ resolved.push({ column, aggregator });
436
+ }
437
+ return resolved;
438
+ }
439
+ function buildTree(sorted, levels) {
440
+ const roots = /* @__PURE__ */ new Map();
441
+ const lastLevel = levels.length - 1;
442
+ const path = [];
443
+ for (const entry of sorted) {
444
+ let siblings = roots;
445
+ let node;
446
+ path.length = 0;
447
+ for (let depth = 0; depth <= lastLevel; depth += 1) {
448
+ const column = levels[depth];
449
+ const value = readCellValue(entry.row, column);
450
+ const key = stringifyGroupValue(value);
451
+ path.push({ columnId: column.id, value });
452
+ node = siblings.get(key);
453
+ if (!node) {
454
+ const isLeafLevel = depth === lastLevel;
455
+ node = {
456
+ id: makeGroupId(path),
457
+ depth,
458
+ columnId: column.id,
459
+ value,
460
+ childCount: 0,
461
+ children: isLeafLevel ? null : /* @__PURE__ */ new Map(),
462
+ rows: isLeafLevel ? [] : null,
463
+ accumulators: /* @__PURE__ */ new Map()
464
+ };
465
+ siblings.set(key, node);
466
+ }
467
+ node.childCount += 1;
468
+ if (node.children)
469
+ siblings = node.children;
470
+ }
471
+ node?.rows?.push(entry);
472
+ }
473
+ return roots;
474
+ }
475
+ function accumulate(source, levels, roots, aggregateColumns) {
476
+ for (const entry of source) {
477
+ const values = aggregateColumns.map(({ column }) => readCellValue(entry.row, column));
478
+ let siblings = roots;
479
+ for (let depth = 0; depth < levels.length && siblings; depth += 1) {
480
+ const key = stringifyGroupValue(readCellValue(entry.row, levels[depth]));
481
+ const node = siblings.get(key);
482
+ if (!node)
483
+ break;
484
+ for (let i = 0; i < aggregateColumns.length; i += 1) {
485
+ const { column, aggregator } = aggregateColumns[i];
486
+ const previous = node.accumulators.get(column.id);
487
+ const acc = previous === void 0 ? aggregator.init() : previous;
488
+ node.accumulators.set(column.id, aggregator.accumulate(acc, values[i], entry.row));
489
+ }
490
+ siblings = node.children;
491
+ }
492
+ }
493
+ }
494
+ function flatten(siblings, depth, levels, aggregateColumns, args, out) {
495
+ const nodes = [...siblings.values()];
496
+ sortSiblings(nodes, levels[depth], args.sort);
497
+ for (const node of nodes) {
498
+ out.push({
499
+ kind: "group",
500
+ id: node.id,
501
+ depth: node.depth,
502
+ columnId: node.columnId,
503
+ value: node.value,
504
+ childCount: node.childCount,
505
+ aggregates: finalizeAggregates(node, aggregateColumns)
506
+ });
507
+ if (!isExpanded(node.id, args))
508
+ continue;
509
+ if (node.children) {
510
+ flatten(node.children, depth + 1, levels, aggregateColumns, args, out);
511
+ continue;
512
+ }
513
+ for (const entry of node.rows ?? []) {
514
+ out.push({
515
+ kind: "data",
516
+ id: entry.id,
517
+ row: entry.row,
518
+ sourceIndex: entry.sourceIndex,
519
+ depth: depth + 1
520
+ });
521
+ }
522
+ }
523
+ }
524
+ function finalizeAggregates(node, aggregateColumns) {
525
+ const aggregates = {};
526
+ for (const { column, aggregator } of aggregateColumns) {
527
+ const acc = node.accumulators.get(column.id);
528
+ aggregates[column.id] = aggregator.finalize(acc === void 0 ? aggregator.init() : acc);
529
+ }
530
+ return aggregates;
531
+ }
532
+ function isExpanded(id, args) {
533
+ return args.groupExpansionOverrides.has(id) ? !args.defaultExpanded : args.defaultExpanded;
534
+ }
535
+ function sortSiblings(nodes, column, sort) {
536
+ if (nodes.length < 2)
537
+ return;
538
+ const entry = column ? sort.find((candidate) => candidate.columnId === column.id) : void 0;
539
+ const multiplier = entry?.direction === "desc" ? -1 : 1;
540
+ const allNumeric = nodes.every((node) => typeof node.value === "number");
541
+ nodes.sort((a, b) => {
542
+ const cmp = allNumeric ? a.value - b.value : collator.compare(String(a.value ?? ""), String(b.value ?? ""));
543
+ return cmp * multiplier;
544
+ });
545
+ }
546
+
547
+ // ../grid-core/dist/derived-rows.js
548
+ var NO_OVERRIDES = /* @__PURE__ */ new Set();
549
+ function deriveVisibleRows(input) {
550
+ const resolvedFilters = resolveFilters(input.columns, input.filters);
551
+ const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
552
+ return buildGroupedRows({
553
+ rows: filtered,
554
+ // Only worth carrying the pre-filter set when it can actually differ.
555
+ // Equal lengths mean the filter removed nothing, so `filtered` is a copy of
556
+ // `input.rows` in the same order and the two sort identically —
557
+ // `buildGroupedRows` sorts whichever it folds over, so skipping here does
558
+ // not change the fold order.
559
+ allRows: input.aggregateFilteredRows && filtered.length !== input.rows.length ? input.rows : void 0,
560
+ columns: input.columns,
561
+ rowGroups: input.rowGroups ?? [],
562
+ sort: input.sort,
563
+ groupExpansionOverrides: input.groupExpansionOverrides ?? NO_OVERRIDES,
564
+ defaultExpanded: input.groupsDefaultExpanded ?? true
565
+ });
566
+ }
567
+ function resolveFilters(columns, filters) {
568
+ const columnMap = new Map(columns.map((c) => [c.id, c]));
569
+ const resolved = [];
570
+ for (const [columnId, filter] of Object.entries(filters)) {
571
+ if (!filter || !isFilterActive(filter))
572
+ continue;
573
+ const column = columnMap.get(columnId);
574
+ if (!column || column.filterable === false)
575
+ continue;
576
+ resolved.push({ column, filter });
577
+ }
578
+ return resolved;
579
+ }
580
+ function matchesFilters(row, resolvedFilters) {
581
+ for (const { column, filter } of resolvedFilters) {
582
+ const cell = readCellValue(row, column);
583
+ if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
584
+ return false;
585
+ }
586
+ }
587
+ return true;
588
+ }
589
+
590
+ // ../grid-core/dist/group-expansion.js
591
+ var DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT = 1e4;
592
+ function resolveGroupExpansionOverrideLimit(configured) {
593
+ if (configured === void 0)
594
+ return DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT;
595
+ if (configured === Number.POSITIVE_INFINITY)
596
+ return configured;
597
+ if (!Number.isFinite(configured)) {
598
+ return DEFAULT_GROUP_EXPANSION_OVERRIDE_LIMIT;
599
+ }
600
+ return Math.max(1, Math.floor(configured));
601
+ }
602
+ function addGroupExpansionOverride(current, groupId, limit) {
603
+ const next = new Set(current);
604
+ next.delete(groupId);
605
+ next.add(groupId);
606
+ while (next.size > limit) {
607
+ const oldest = next.values().next().value;
608
+ next.delete(oldest);
609
+ }
610
+ return next;
611
+ }
612
+
306
613
  // ../grid-core/dist/create-grid-core.js
307
614
  var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
615
+ function isDataRow(entry) {
616
+ return entry.kind === "data";
617
+ }
618
+ function scanDataRows(rows, rowId) {
619
+ let count2 = 0;
620
+ let index = -1;
621
+ for (const entry of rows) {
622
+ if (!isDataRow(entry))
623
+ continue;
624
+ if (rowId !== null && entry.id === rowId)
625
+ index = count2;
626
+ count2 += 1;
627
+ }
628
+ return { count: count2, index };
629
+ }
630
+ function dataRowAt(rows, ordinal) {
631
+ if (ordinal < 0)
632
+ return void 0;
633
+ let seen = 0;
634
+ for (const entry of rows) {
635
+ if (!isDataRow(entry))
636
+ continue;
637
+ if (seen === ordinal)
638
+ return entry;
639
+ seen += 1;
640
+ }
641
+ return void 0;
642
+ }
308
643
  function clampColumnWidth(width, column) {
309
- const min = column.minWidthPx ?? 40;
310
- const max = column.maxWidthPx ?? Infinity;
311
- return Math.max(min, Math.min(max, width));
644
+ const min2 = column.minWidthPx ?? 40;
645
+ const max2 = column.maxWidthPx ?? Infinity;
646
+ return Math.max(min2, Math.min(max2, width));
312
647
  }
313
648
  function applyAutosize(options, autosizeOptions) {
314
649
  const result = autosizeColumns({
@@ -357,8 +692,17 @@ function createGridCore(inputOptions) {
357
692
  let cachedVisibleRows = null;
358
693
  let cachedDerivedSort = null;
359
694
  let cachedDerivedFilters = null;
695
+ let cachedDerivedRowGroups = null;
696
+ let cachedDerivedOverrides = null;
697
+ let cachedDerivedDefaultExpanded = null;
698
+ let cachedDerivedAggregateFiltered = null;
360
699
  let sort = [];
361
700
  let filters = {};
701
+ let rowGroups = sanitizeRowGroups(options.columns.filter((column) => column.rowGroup === true).map((c) => c.id), options.columns);
702
+ let groupExpansionOverrides = /* @__PURE__ */ new Set();
703
+ const groupExpansionOverrideLimit = resolveGroupExpansionOverrideLimit(options.groupExpansionOverrideLimit);
704
+ let groupsDefaultExpanded = options.groupsDefaultExpanded ?? true;
705
+ let previousAggregates = /* @__PURE__ */ new Map();
362
706
  let selection = { ranges: [], anchor: null };
363
707
  let focus = { rowId: null, columnId: null };
364
708
  let editing = null;
@@ -463,9 +807,9 @@ function createGridCore(inputOptions) {
463
807
  emit();
464
808
  },
465
809
  selectAll() {
466
- const snapshot = getSnapshot();
467
- const firstRow = snapshot.visibleRows[0];
468
- const lastRow = snapshot.visibleRows[snapshot.visibleRows.length - 1];
810
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
811
+ const firstRow = dataRows[0];
812
+ const lastRow = dataRows[dataRows.length - 1];
469
813
  const firstColumn = options.columns[0];
470
814
  const lastColumn = options.columns[options.columns.length - 1];
471
815
  if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
@@ -553,17 +897,17 @@ function createGridCore(inputOptions) {
553
897
  emit();
554
898
  },
555
899
  setSelectAllVisible(checked) {
556
- const snapshot = getSnapshot();
900
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
557
901
  const firstColumn = options.columns[0];
558
902
  const lastColumn = options.columns[options.columns.length - 1];
559
903
  if (!firstColumn || !lastColumn) {
560
904
  return;
561
905
  }
562
- const visibleIds = new Set(snapshot.visibleRows.map((r) => r.id));
906
+ const visibleIds = new Set(dataRows.map((r) => r.id));
563
907
  const nonRowRanges = selection.ranges.filter((r) => !isFullRowRange(r, r.startRowId, firstColumn.id, lastColumn.id) || !visibleIds.has(r.startRowId));
564
908
  let next;
565
909
  if (checked) {
566
- const newRanges = snapshot.visibleRows.map((row) => ({
910
+ const newRanges = dataRows.map((row) => ({
567
911
  startRowId: row.id,
568
912
  endRowId: row.id,
569
913
  startColumnId: firstColumn.id,
@@ -571,7 +915,7 @@ function createGridCore(inputOptions) {
571
915
  }));
572
916
  next = {
573
917
  ranges: [...nonRowRanges, ...newRanges],
574
- anchor: snapshot.visibleRows[0] ? { rowId: snapshot.visibleRows[0].id, columnId: firstColumn.id } : selection.anchor
918
+ anchor: dataRows[0] ? { rowId: dataRows[0].id, columnId: firstColumn.id } : selection.anchor
575
919
  };
576
920
  } else {
577
921
  next = { ranges: nonRowRanges, anchor: selection.anchor };
@@ -593,14 +937,13 @@ function createGridCore(inputOptions) {
593
937
  },
594
938
  moveFocus(direction, moveOptions = {}) {
595
939
  const snapshot = getSnapshot();
596
- const visibleRows = snapshot.visibleRows;
940
+ const { count: dataRowCount, index: currentRowIndex } = scanDataRows(snapshot.visibleRows, focus.rowId);
597
941
  const columnList = options.columns;
598
- if (visibleRows.length === 0 || columnList.length === 0) {
942
+ if (dataRowCount === 0 || columnList.length === 0) {
599
943
  focus = { rowId: null, columnId: null };
600
944
  emit();
601
945
  return;
602
946
  }
603
- const currentRowIndex = focus.rowId ? visibleRows.findIndex((r) => r.id === focus.rowId) : -1;
604
947
  const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
605
948
  const hasRowFocus = currentRowIndex !== -1;
606
949
  const hasColumnFocus = currentColumnIndex !== -1;
@@ -608,28 +951,28 @@ function createGridCore(inputOptions) {
608
951
  const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
609
952
  let nextRowIndex = baseRowIndex;
610
953
  let nextColumnIndex = baseColumnIndex;
611
- const pageStep = computePageStep(viewport, visibleRows);
954
+ const pageStep = computePageStep(viewport, dataRowCount);
612
955
  switch (direction) {
613
956
  case "up":
614
957
  if (moveOptions.jumpToEdge) {
615
958
  nextRowIndex = 0;
616
959
  } else if (!hasRowFocus) {
617
- nextRowIndex = visibleRows.length - 1;
960
+ nextRowIndex = dataRowCount - 1;
618
961
  } else if (moveOptions.byPage) {
619
- nextRowIndex = clamp(baseRowIndex - pageStep, 0, visibleRows.length - 1);
962
+ nextRowIndex = clamp(baseRowIndex - pageStep, 0, dataRowCount - 1);
620
963
  } else {
621
- nextRowIndex = clamp(baseRowIndex - 1, 0, visibleRows.length - 1);
964
+ nextRowIndex = clamp(baseRowIndex - 1, 0, dataRowCount - 1);
622
965
  }
623
966
  break;
624
967
  case "down":
625
968
  if (moveOptions.jumpToEdge) {
626
- nextRowIndex = visibleRows.length - 1;
969
+ nextRowIndex = dataRowCount - 1;
627
970
  } else if (!hasRowFocus) {
628
971
  nextRowIndex = 0;
629
972
  } else if (moveOptions.byPage) {
630
- nextRowIndex = clamp(baseRowIndex + pageStep, 0, visibleRows.length - 1);
973
+ nextRowIndex = clamp(baseRowIndex + pageStep, 0, dataRowCount - 1);
631
974
  } else {
632
- nextRowIndex = clamp(baseRowIndex + 1, 0, visibleRows.length - 1);
975
+ nextRowIndex = clamp(baseRowIndex + 1, 0, dataRowCount - 1);
633
976
  }
634
977
  break;
635
978
  case "left":
@@ -651,7 +994,7 @@ function createGridCore(inputOptions) {
651
994
  }
652
995
  break;
653
996
  }
654
- const nextRow = visibleRows[nextRowIndex];
997
+ const nextRow = dataRowAt(snapshot.visibleRows, nextRowIndex);
655
998
  const nextColumn = columnList[nextColumnIndex];
656
999
  if (!nextRow || !nextColumn) {
657
1000
  return;
@@ -1022,6 +1365,37 @@ function createGridCore(inputOptions) {
1022
1365
  cachedVisibleRows = null;
1023
1366
  emit();
1024
1367
  },
1368
+ setRowGroups(columnIds) {
1369
+ const next = sanitizeRowGroups(columnIds, options.columns);
1370
+ if (stringListsEqual(rowGroups, next)) {
1371
+ return;
1372
+ }
1373
+ rowGroups = next;
1374
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1375
+ emit();
1376
+ },
1377
+ toggleGroup(groupId) {
1378
+ store.setGroupExpanded(groupId, !isGroupExpanded(groupId));
1379
+ },
1380
+ setGroupExpanded(groupId, expanded) {
1381
+ if (isGroupExpanded(groupId) === expanded) {
1382
+ return;
1383
+ }
1384
+ if (expanded === groupsDefaultExpanded) {
1385
+ const next = new Set(groupExpansionOverrides);
1386
+ next.delete(groupId);
1387
+ groupExpansionOverrides = next;
1388
+ } else {
1389
+ groupExpansionOverrides = addGroupExpansionOverride(groupExpansionOverrides, groupId, groupExpansionOverrideLimit);
1390
+ }
1391
+ emit();
1392
+ },
1393
+ expandAll() {
1394
+ setExpansionDefault(true);
1395
+ },
1396
+ collapseAll() {
1397
+ setExpansionDefault(false);
1398
+ },
1025
1399
  beginEdit(addr, opts) {
1026
1400
  editing = {
1027
1401
  rowId: addr.rowId,
@@ -1081,19 +1455,60 @@ function createGridCore(inputOptions) {
1081
1455
  }
1082
1456
  };
1083
1457
  return store;
1458
+ function isGroupExpanded(groupId) {
1459
+ return groupExpansionOverrides.has(groupId) ? !groupsDefaultExpanded : groupsDefaultExpanded;
1460
+ }
1461
+ function setExpansionDefault(expanded) {
1462
+ if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1463
+ return;
1464
+ }
1465
+ groupsDefaultExpanded = expanded;
1466
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1467
+ emit();
1468
+ }
1469
+ function preserveAggregateIdentity(rows) {
1470
+ const next = /* @__PURE__ */ new Map();
1471
+ let result = rows;
1472
+ for (let i = 0; i < rows.length; i += 1) {
1473
+ const entry = rows[i];
1474
+ if (entry.kind !== "group")
1475
+ continue;
1476
+ const previous = previousAggregates.get(entry.id);
1477
+ if (previous && recordsEqual(previous, entry.aggregates)) {
1478
+ if (result === rows)
1479
+ result = rows.slice();
1480
+ result[i] = { ...entry, aggregates: previous };
1481
+ next.set(entry.id, previous);
1482
+ } else {
1483
+ next.set(entry.id, entry.aggregates);
1484
+ }
1485
+ }
1486
+ previousAggregates = next;
1487
+ return result;
1488
+ }
1084
1489
  function getSnapshot() {
1085
1490
  if (cachedSnapshot) {
1086
1491
  return cachedSnapshot;
1087
1492
  }
1088
- const visibleRows = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters ? cachedVisibleRows : deriveVisibleRows({
1493
+ const aggregateFilteredRows = options.aggregateFilteredRows ?? false;
1494
+ const derivedIsFresh = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters && cachedDerivedRowGroups === rowGroups && cachedDerivedOverrides === groupExpansionOverrides && cachedDerivedDefaultExpanded === groupsDefaultExpanded && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1495
+ const visibleRows = derivedIsFresh ? cachedVisibleRows : preserveAggregateIdentity(deriveVisibleRows({
1089
1496
  columns: options.columns,
1090
1497
  filters,
1091
1498
  rows: sourceRows,
1092
- sort
1093
- });
1499
+ sort,
1500
+ rowGroups,
1501
+ groupExpansionOverrides,
1502
+ groupsDefaultExpanded,
1503
+ aggregateFilteredRows
1504
+ }));
1094
1505
  cachedVisibleRows = visibleRows;
1095
1506
  cachedDerivedSort = sort;
1096
1507
  cachedDerivedFilters = filters;
1508
+ cachedDerivedRowGroups = rowGroups;
1509
+ cachedDerivedOverrides = groupExpansionOverrides;
1510
+ cachedDerivedDefaultExpanded = groupsDefaultExpanded;
1511
+ cachedDerivedAggregateFiltered = aggregateFilteredRows;
1097
1512
  cachedSnapshot = {
1098
1513
  viewport,
1099
1514
  sort: sort.map((entry) => ({ ...entry })),
@@ -1109,7 +1524,10 @@ function createGridCore(inputOptions) {
1109
1524
  start: 0,
1110
1525
  end: visibleRows.length
1111
1526
  },
1112
- editing: editing ? { ...editing } : null
1527
+ editing: editing ? { ...editing } : null,
1528
+ rowGroups: [...rowGroups],
1529
+ groupExpansionOverrides: new Set(groupExpansionOverrides),
1530
+ groupsDefaultExpanded
1113
1531
  };
1114
1532
  return cachedSnapshot;
1115
1533
  }
@@ -1120,8 +1538,8 @@ function createGridCore(inputOptions) {
1120
1538
  }
1121
1539
  }
1122
1540
  }
1123
- function clamp(value, min, max) {
1124
- return Math.max(min, Math.min(max, value));
1541
+ function clamp(value, min2, max2) {
1542
+ return Math.max(min2, Math.min(max2, value));
1125
1543
  }
1126
1544
  function sameColumnLayout(a, b) {
1127
1545
  if (a.length !== b.length) {
@@ -1132,12 +1550,12 @@ function sameColumnLayout(a, b) {
1132
1550
  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;
1133
1551
  });
1134
1552
  }
1135
- function computePageStep(viewport, visibleRows) {
1136
- if (viewport.height <= 0 || visibleRows.length === 0) {
1553
+ function computePageStep(viewport, dataRowCount) {
1554
+ if (viewport.height <= 0 || dataRowCount === 0) {
1137
1555
  return 1;
1138
1556
  }
1139
1557
  const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
1140
- return Math.min(estimatedRowsPerPage, visibleRows.length);
1558
+ return Math.min(estimatedRowsPerPage, dataRowCount);
1141
1559
  }
1142
1560
  function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
1143
1561
  return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
@@ -1167,6 +1585,28 @@ function sanitizeSortEntries(entries, columns) {
1167
1585
  return column !== void 0 && column.sortable !== false;
1168
1586
  });
1169
1587
  }
1588
+ function sanitizeRowGroups(columnIds, columns) {
1589
+ const known = new Set(columns.map((column) => column.id));
1590
+ const seen = /* @__PURE__ */ new Set();
1591
+ const next = [];
1592
+ for (const columnId of columnIds) {
1593
+ if (!known.has(columnId) || seen.has(columnId))
1594
+ continue;
1595
+ seen.add(columnId);
1596
+ next.push(columnId);
1597
+ }
1598
+ return next;
1599
+ }
1600
+ function stringListsEqual(a, b) {
1601
+ return a.length === b.length && a.every((value, i) => value === b[i]);
1602
+ }
1603
+ function recordsEqual(a, b) {
1604
+ const aKeys = Object.keys(a);
1605
+ if (aKeys.length !== Object.keys(b).length) {
1606
+ return false;
1607
+ }
1608
+ return aKeys.every((key) => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key]));
1609
+ }
1170
1610
  function sortsEqual(a, b) {
1171
1611
  if (a.length !== b.length) {
1172
1612
  return false;
@@ -1232,6 +1672,11 @@ function createGrid(options) {
1232
1672
  setSelectAllVisible: engine.setSelectAllVisible,
1233
1673
  setFocus: engine.setFocus,
1234
1674
  moveFocus: engine.moveFocus,
1675
+ setRowGroups: engine.setRowGroups,
1676
+ toggleGroup: engine.toggleGroup,
1677
+ setGroupExpanded: engine.setGroupExpanded,
1678
+ expandAll: engine.expandAll,
1679
+ collapseAll: engine.collapseAll,
1235
1680
  setViewport: engine.setViewport,
1236
1681
  autosizeColumns: engine.autosizeColumns,
1237
1682
  setColumnWidth: engine.setColumnWidth,