@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.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,277 @@ 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
+
306
590
  // ../grid-core/dist/create-grid-core.js
307
591
  var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
592
+ function isDataRow(entry) {
593
+ return entry.kind === "data";
594
+ }
595
+ function scanDataRows(rows, rowId) {
596
+ let count2 = 0;
597
+ let index = -1;
598
+ for (const entry of rows) {
599
+ if (!isDataRow(entry))
600
+ continue;
601
+ if (rowId !== null && entry.id === rowId)
602
+ index = count2;
603
+ count2 += 1;
604
+ }
605
+ return { count: count2, index };
606
+ }
607
+ function dataRowAt(rows, ordinal) {
608
+ if (ordinal < 0)
609
+ return void 0;
610
+ let seen = 0;
611
+ for (const entry of rows) {
612
+ if (!isDataRow(entry))
613
+ continue;
614
+ if (seen === ordinal)
615
+ return entry;
616
+ seen += 1;
617
+ }
618
+ return void 0;
619
+ }
308
620
  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));
621
+ const min2 = column.minWidthPx ?? 40;
622
+ const max2 = column.maxWidthPx ?? Infinity;
623
+ return Math.max(min2, Math.min(max2, width));
312
624
  }
313
625
  function applyAutosize(options, autosizeOptions) {
314
626
  const result = autosizeColumns({
@@ -357,8 +669,16 @@ function createGridCore(inputOptions) {
357
669
  let cachedVisibleRows = null;
358
670
  let cachedDerivedSort = null;
359
671
  let cachedDerivedFilters = null;
672
+ let cachedDerivedRowGroups = null;
673
+ let cachedDerivedOverrides = null;
674
+ let cachedDerivedDefaultExpanded = null;
675
+ let cachedDerivedAggregateFiltered = null;
360
676
  let sort = [];
361
677
  let filters = {};
678
+ let rowGroups = sanitizeRowGroups(options.columns.filter((column) => column.rowGroup === true).map((c) => c.id), options.columns);
679
+ let groupExpansionOverrides = /* @__PURE__ */ new Set();
680
+ let groupsDefaultExpanded = options.groupsDefaultExpanded ?? true;
681
+ let previousAggregates = /* @__PURE__ */ new Map();
362
682
  let selection = { ranges: [], anchor: null };
363
683
  let focus = { rowId: null, columnId: null };
364
684
  let editing = null;
@@ -463,9 +783,9 @@ function createGridCore(inputOptions) {
463
783
  emit();
464
784
  },
465
785
  selectAll() {
466
- const snapshot = getSnapshot();
467
- const firstRow = snapshot.visibleRows[0];
468
- const lastRow = snapshot.visibleRows[snapshot.visibleRows.length - 1];
786
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
787
+ const firstRow = dataRows[0];
788
+ const lastRow = dataRows[dataRows.length - 1];
469
789
  const firstColumn = options.columns[0];
470
790
  const lastColumn = options.columns[options.columns.length - 1];
471
791
  if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
@@ -553,17 +873,17 @@ function createGridCore(inputOptions) {
553
873
  emit();
554
874
  },
555
875
  setSelectAllVisible(checked) {
556
- const snapshot = getSnapshot();
876
+ const dataRows = getSnapshot().visibleRows.filter(isDataRow);
557
877
  const firstColumn = options.columns[0];
558
878
  const lastColumn = options.columns[options.columns.length - 1];
559
879
  if (!firstColumn || !lastColumn) {
560
880
  return;
561
881
  }
562
- const visibleIds = new Set(snapshot.visibleRows.map((r) => r.id));
882
+ const visibleIds = new Set(dataRows.map((r) => r.id));
563
883
  const nonRowRanges = selection.ranges.filter((r) => !isFullRowRange(r, r.startRowId, firstColumn.id, lastColumn.id) || !visibleIds.has(r.startRowId));
564
884
  let next;
565
885
  if (checked) {
566
- const newRanges = snapshot.visibleRows.map((row) => ({
886
+ const newRanges = dataRows.map((row) => ({
567
887
  startRowId: row.id,
568
888
  endRowId: row.id,
569
889
  startColumnId: firstColumn.id,
@@ -571,7 +891,7 @@ function createGridCore(inputOptions) {
571
891
  }));
572
892
  next = {
573
893
  ranges: [...nonRowRanges, ...newRanges],
574
- anchor: snapshot.visibleRows[0] ? { rowId: snapshot.visibleRows[0].id, columnId: firstColumn.id } : selection.anchor
894
+ anchor: dataRows[0] ? { rowId: dataRows[0].id, columnId: firstColumn.id } : selection.anchor
575
895
  };
576
896
  } else {
577
897
  next = { ranges: nonRowRanges, anchor: selection.anchor };
@@ -593,14 +913,13 @@ function createGridCore(inputOptions) {
593
913
  },
594
914
  moveFocus(direction, moveOptions = {}) {
595
915
  const snapshot = getSnapshot();
596
- const visibleRows = snapshot.visibleRows;
916
+ const { count: dataRowCount, index: currentRowIndex } = scanDataRows(snapshot.visibleRows, focus.rowId);
597
917
  const columnList = options.columns;
598
- if (visibleRows.length === 0 || columnList.length === 0) {
918
+ if (dataRowCount === 0 || columnList.length === 0) {
599
919
  focus = { rowId: null, columnId: null };
600
920
  emit();
601
921
  return;
602
922
  }
603
- const currentRowIndex = focus.rowId ? visibleRows.findIndex((r) => r.id === focus.rowId) : -1;
604
923
  const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
605
924
  const hasRowFocus = currentRowIndex !== -1;
606
925
  const hasColumnFocus = currentColumnIndex !== -1;
@@ -608,28 +927,28 @@ function createGridCore(inputOptions) {
608
927
  const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
609
928
  let nextRowIndex = baseRowIndex;
610
929
  let nextColumnIndex = baseColumnIndex;
611
- const pageStep = computePageStep(viewport, visibleRows);
930
+ const pageStep = computePageStep(viewport, dataRowCount);
612
931
  switch (direction) {
613
932
  case "up":
614
933
  if (moveOptions.jumpToEdge) {
615
934
  nextRowIndex = 0;
616
935
  } else if (!hasRowFocus) {
617
- nextRowIndex = visibleRows.length - 1;
936
+ nextRowIndex = dataRowCount - 1;
618
937
  } else if (moveOptions.byPage) {
619
- nextRowIndex = clamp(baseRowIndex - pageStep, 0, visibleRows.length - 1);
938
+ nextRowIndex = clamp(baseRowIndex - pageStep, 0, dataRowCount - 1);
620
939
  } else {
621
- nextRowIndex = clamp(baseRowIndex - 1, 0, visibleRows.length - 1);
940
+ nextRowIndex = clamp(baseRowIndex - 1, 0, dataRowCount - 1);
622
941
  }
623
942
  break;
624
943
  case "down":
625
944
  if (moveOptions.jumpToEdge) {
626
- nextRowIndex = visibleRows.length - 1;
945
+ nextRowIndex = dataRowCount - 1;
627
946
  } else if (!hasRowFocus) {
628
947
  nextRowIndex = 0;
629
948
  } else if (moveOptions.byPage) {
630
- nextRowIndex = clamp(baseRowIndex + pageStep, 0, visibleRows.length - 1);
949
+ nextRowIndex = clamp(baseRowIndex + pageStep, 0, dataRowCount - 1);
631
950
  } else {
632
- nextRowIndex = clamp(baseRowIndex + 1, 0, visibleRows.length - 1);
951
+ nextRowIndex = clamp(baseRowIndex + 1, 0, dataRowCount - 1);
633
952
  }
634
953
  break;
635
954
  case "left":
@@ -651,7 +970,7 @@ function createGridCore(inputOptions) {
651
970
  }
652
971
  break;
653
972
  }
654
- const nextRow = visibleRows[nextRowIndex];
973
+ const nextRow = dataRowAt(snapshot.visibleRows, nextRowIndex);
655
974
  const nextColumn = columnList[nextColumnIndex];
656
975
  if (!nextRow || !nextColumn) {
657
976
  return;
@@ -1022,6 +1341,37 @@ function createGridCore(inputOptions) {
1022
1341
  cachedVisibleRows = null;
1023
1342
  emit();
1024
1343
  },
1344
+ setRowGroups(columnIds) {
1345
+ const next = sanitizeRowGroups(columnIds, options.columns);
1346
+ if (stringListsEqual(rowGroups, next)) {
1347
+ return;
1348
+ }
1349
+ rowGroups = next;
1350
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1351
+ emit();
1352
+ },
1353
+ toggleGroup(groupId) {
1354
+ store.setGroupExpanded(groupId, !isGroupExpanded(groupId));
1355
+ },
1356
+ setGroupExpanded(groupId, expanded) {
1357
+ if (isGroupExpanded(groupId) === expanded) {
1358
+ return;
1359
+ }
1360
+ const next = new Set(groupExpansionOverrides);
1361
+ if (expanded === groupsDefaultExpanded) {
1362
+ next.delete(groupId);
1363
+ } else {
1364
+ next.add(groupId);
1365
+ }
1366
+ groupExpansionOverrides = next;
1367
+ emit();
1368
+ },
1369
+ expandAll() {
1370
+ setExpansionDefault(true);
1371
+ },
1372
+ collapseAll() {
1373
+ setExpansionDefault(false);
1374
+ },
1025
1375
  beginEdit(addr, opts) {
1026
1376
  editing = {
1027
1377
  rowId: addr.rowId,
@@ -1081,19 +1431,60 @@ function createGridCore(inputOptions) {
1081
1431
  }
1082
1432
  };
1083
1433
  return store;
1434
+ function isGroupExpanded(groupId) {
1435
+ return groupExpansionOverrides.has(groupId) ? !groupsDefaultExpanded : groupsDefaultExpanded;
1436
+ }
1437
+ function setExpansionDefault(expanded) {
1438
+ if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1439
+ return;
1440
+ }
1441
+ groupsDefaultExpanded = expanded;
1442
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1443
+ emit();
1444
+ }
1445
+ function preserveAggregateIdentity(rows) {
1446
+ const next = /* @__PURE__ */ new Map();
1447
+ let result = rows;
1448
+ for (let i = 0; i < rows.length; i += 1) {
1449
+ const entry = rows[i];
1450
+ if (entry.kind !== "group")
1451
+ continue;
1452
+ const previous = previousAggregates.get(entry.id);
1453
+ if (previous && recordsEqual(previous, entry.aggregates)) {
1454
+ if (result === rows)
1455
+ result = rows.slice();
1456
+ result[i] = { ...entry, aggregates: previous };
1457
+ next.set(entry.id, previous);
1458
+ } else {
1459
+ next.set(entry.id, entry.aggregates);
1460
+ }
1461
+ }
1462
+ previousAggregates = next;
1463
+ return result;
1464
+ }
1084
1465
  function getSnapshot() {
1085
1466
  if (cachedSnapshot) {
1086
1467
  return cachedSnapshot;
1087
1468
  }
1088
- const visibleRows = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters ? cachedVisibleRows : deriveVisibleRows({
1469
+ const aggregateFilteredRows = options.aggregateFilteredRows ?? false;
1470
+ const derivedIsFresh = cachedVisibleRows !== null && cachedDerivedSort === sort && cachedDerivedFilters === filters && cachedDerivedRowGroups === rowGroups && cachedDerivedOverrides === groupExpansionOverrides && cachedDerivedDefaultExpanded === groupsDefaultExpanded && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1471
+ const visibleRows = derivedIsFresh ? cachedVisibleRows : preserveAggregateIdentity(deriveVisibleRows({
1089
1472
  columns: options.columns,
1090
1473
  filters,
1091
1474
  rows: sourceRows,
1092
- sort
1093
- });
1475
+ sort,
1476
+ rowGroups,
1477
+ groupExpansionOverrides,
1478
+ groupsDefaultExpanded,
1479
+ aggregateFilteredRows
1480
+ }));
1094
1481
  cachedVisibleRows = visibleRows;
1095
1482
  cachedDerivedSort = sort;
1096
1483
  cachedDerivedFilters = filters;
1484
+ cachedDerivedRowGroups = rowGroups;
1485
+ cachedDerivedOverrides = groupExpansionOverrides;
1486
+ cachedDerivedDefaultExpanded = groupsDefaultExpanded;
1487
+ cachedDerivedAggregateFiltered = aggregateFilteredRows;
1097
1488
  cachedSnapshot = {
1098
1489
  viewport,
1099
1490
  sort: sort.map((entry) => ({ ...entry })),
@@ -1109,7 +1500,10 @@ function createGridCore(inputOptions) {
1109
1500
  start: 0,
1110
1501
  end: visibleRows.length
1111
1502
  },
1112
- editing: editing ? { ...editing } : null
1503
+ editing: editing ? { ...editing } : null,
1504
+ rowGroups: [...rowGroups],
1505
+ groupExpansionOverrides: new Set(groupExpansionOverrides),
1506
+ groupsDefaultExpanded
1113
1507
  };
1114
1508
  return cachedSnapshot;
1115
1509
  }
@@ -1120,8 +1514,8 @@ function createGridCore(inputOptions) {
1120
1514
  }
1121
1515
  }
1122
1516
  }
1123
- function clamp(value, min, max) {
1124
- return Math.max(min, Math.min(max, value));
1517
+ function clamp(value, min2, max2) {
1518
+ return Math.max(min2, Math.min(max2, value));
1125
1519
  }
1126
1520
  function sameColumnLayout(a, b) {
1127
1521
  if (a.length !== b.length) {
@@ -1132,12 +1526,12 @@ function sameColumnLayout(a, b) {
1132
1526
  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
1527
  });
1134
1528
  }
1135
- function computePageStep(viewport, visibleRows) {
1136
- if (viewport.height <= 0 || visibleRows.length === 0) {
1529
+ function computePageStep(viewport, dataRowCount) {
1530
+ if (viewport.height <= 0 || dataRowCount === 0) {
1137
1531
  return 1;
1138
1532
  }
1139
1533
  const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
1140
- return Math.min(estimatedRowsPerPage, visibleRows.length);
1534
+ return Math.min(estimatedRowsPerPage, dataRowCount);
1141
1535
  }
1142
1536
  function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
1143
1537
  return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
@@ -1167,6 +1561,28 @@ function sanitizeSortEntries(entries, columns) {
1167
1561
  return column !== void 0 && column.sortable !== false;
1168
1562
  });
1169
1563
  }
1564
+ function sanitizeRowGroups(columnIds, columns) {
1565
+ const known = new Set(columns.map((column) => column.id));
1566
+ const seen = /* @__PURE__ */ new Set();
1567
+ const next = [];
1568
+ for (const columnId of columnIds) {
1569
+ if (!known.has(columnId) || seen.has(columnId))
1570
+ continue;
1571
+ seen.add(columnId);
1572
+ next.push(columnId);
1573
+ }
1574
+ return next;
1575
+ }
1576
+ function stringListsEqual(a, b) {
1577
+ return a.length === b.length && a.every((value, i) => value === b[i]);
1578
+ }
1579
+ function recordsEqual(a, b) {
1580
+ const aKeys = Object.keys(a);
1581
+ if (aKeys.length !== Object.keys(b).length) {
1582
+ return false;
1583
+ }
1584
+ return aKeys.every((key) => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key]));
1585
+ }
1170
1586
  function sortsEqual(a, b) {
1171
1587
  if (a.length !== b.length) {
1172
1588
  return false;
@@ -1232,6 +1648,11 @@ function createGrid(options) {
1232
1648
  setSelectAllVisible: engine.setSelectAllVisible,
1233
1649
  setFocus: engine.setFocus,
1234
1650
  moveFocus: engine.moveFocus,
1651
+ setRowGroups: engine.setRowGroups,
1652
+ toggleGroup: engine.toggleGroup,
1653
+ setGroupExpanded: engine.setGroupExpanded,
1654
+ expandAll: engine.expandAll,
1655
+ collapseAll: engine.collapseAll,
1235
1656
  setViewport: engine.setViewport,
1236
1657
  autosizeColumns: engine.autosizeColumns,
1237
1658
  setColumnWidth: engine.setColumnWidth,