@pretable/core 0.3.0 → 0.3.2

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
@@ -398,12 +398,30 @@ function makeGroupId(path) {
398
398
  }
399
399
 
400
400
  // ../grid-core/dist/group-rows.js
401
- function buildGroupedRows(args) {
401
+ function buildGroupModel(args) {
402
402
  const { rows, allRows, columns, rowGroups, sort } = args;
403
403
  const sorted = sortRows(rows, columns, sort);
404
404
  const levels = resolveLevels(columns, rowGroups);
405
405
  if (levels.length === 0) {
406
- return sorted.map(({ id, row, sourceIndex }) => ({
406
+ return {
407
+ sorted,
408
+ levels,
409
+ roots: /* @__PURE__ */ new Map(),
410
+ aggregateColumns: [],
411
+ sort,
412
+ orderedRoots: null
413
+ };
414
+ }
415
+ const roots = buildTree(sorted, levels);
416
+ const aggregateColumns = resolveAggregateColumns(columns);
417
+ if (aggregateColumns.length > 0) {
418
+ accumulate(allRows ? sortRows(allRows, columns, sort) : sorted, levels, roots, aggregateColumns);
419
+ }
420
+ return { sorted, levels, roots, aggregateColumns, sort, orderedRoots: null };
421
+ }
422
+ function flattenGroupModel(model, groupExpansionOverrides, defaultExpanded) {
423
+ if (model.levels.length === 0) {
424
+ return model.sorted.map(({ id, row, sourceIndex }) => ({
407
425
  kind: "data",
408
426
  id,
409
427
  row,
@@ -411,13 +429,11 @@ function buildGroupedRows(args) {
411
429
  depth: 0
412
430
  }));
413
431
  }
414
- const roots = buildTree(sorted, levels);
415
- const aggregateColumns = resolveAggregateColumns(columns);
416
- if (aggregateColumns.length > 0) {
417
- accumulate(allRows ? sortRows(allRows, columns, sort) : sorted, levels, roots, aggregateColumns);
432
+ if (!model.orderedRoots) {
433
+ model.orderedRoots = orderSiblings(model.roots, model.levels[0], model.sort);
418
434
  }
419
435
  const out = [];
420
- flatten(roots, 0, levels, aggregateColumns, args, out);
436
+ flatten(model.orderedRoots, 0, model, groupExpansionOverrides, defaultExpanded, out);
421
437
  return out;
422
438
  }
423
439
  function resolveLevels(columns, rowGroups) {
@@ -464,8 +480,10 @@ function buildTree(sorted, levels) {
464
480
  value,
465
481
  childCount: 0,
466
482
  children: isLeafLevel ? null : /* @__PURE__ */ new Map(),
483
+ orderedChildren: null,
467
484
  rows: isLeafLevel ? [] : null,
468
- accumulators: /* @__PURE__ */ new Map()
485
+ accumulators: /* @__PURE__ */ new Map(),
486
+ finalizedAggregates: null
469
487
  };
470
488
  siblings.set(key, node);
471
489
  }
@@ -496,9 +514,7 @@ function accumulate(source, levels, roots, aggregateColumns) {
496
514
  }
497
515
  }
498
516
  }
499
- function flatten(siblings, depth, levels, aggregateColumns, args, out) {
500
- const nodes = [...siblings.values()];
501
- sortSiblings(nodes, levels[depth], args.sort);
517
+ function flatten(nodes, depth, model, groupExpansionOverrides, defaultExpanded, out) {
502
518
  for (const node of nodes) {
503
519
  out.push({
504
520
  kind: "group",
@@ -507,12 +523,16 @@ function flatten(siblings, depth, levels, aggregateColumns, args, out) {
507
523
  columnId: node.columnId,
508
524
  value: node.value,
509
525
  childCount: node.childCount,
510
- aggregates: finalizeAggregates(node, aggregateColumns)
526
+ aggregates: finalizeAggregates(node, model.aggregateColumns)
511
527
  });
512
- if (!isExpanded(node.id, args))
528
+ const expanded = groupExpansionOverrides.has(node.id) ? !defaultExpanded : defaultExpanded;
529
+ if (!expanded)
513
530
  continue;
514
531
  if (node.children) {
515
- flatten(node.children, depth + 1, levels, aggregateColumns, args, out);
532
+ if (!node.orderedChildren) {
533
+ node.orderedChildren = orderSiblings(node.children, model.levels[depth + 1], model.sort);
534
+ }
535
+ flatten(node.orderedChildren, depth + 1, model, groupExpansionOverrides, defaultExpanded, out);
516
536
  continue;
517
537
  }
518
538
  for (const entry of node.rows ?? []) {
@@ -527,15 +547,20 @@ function flatten(siblings, depth, levels, aggregateColumns, args, out) {
527
547
  }
528
548
  }
529
549
  function finalizeAggregates(node, aggregateColumns) {
550
+ if (node.finalizedAggregates)
551
+ return node.finalizedAggregates;
530
552
  const aggregates = {};
531
553
  for (const { column, aggregator } of aggregateColumns) {
532
554
  const acc = node.accumulators.get(column.id);
533
555
  aggregates[column.id] = aggregator.finalize(acc === void 0 ? aggregator.init() : acc);
534
556
  }
557
+ node.finalizedAggregates = aggregates;
535
558
  return aggregates;
536
559
  }
537
- function isExpanded(id, args) {
538
- return args.groupExpansionOverrides.has(id) ? !args.defaultExpanded : args.defaultExpanded;
560
+ function orderSiblings(siblings, column, sort) {
561
+ const nodes = [...siblings.values()];
562
+ sortSiblings(nodes, column, sort);
563
+ return nodes;
539
564
  }
540
565
  function sortSiblings(nodes, column, sort) {
541
566
  if (nodes.length < 2)
@@ -551,27 +576,31 @@ function sortSiblings(nodes, column, sort) {
551
576
 
552
577
  // ../grid-core/dist/derived-rows.js
553
578
  var NO_OVERRIDES = /* @__PURE__ */ new Set();
554
- function deriveVisibleRows(input) {
579
+ function buildDerivedRowModel(input) {
555
580
  const resolvedFilters = resolveFilters(input.columns, input.filters);
556
581
  const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
557
582
  return {
558
- rows: buildGroupedRows({
583
+ groups: buildGroupModel({
559
584
  rows: filtered,
560
585
  // Only worth carrying the pre-filter set when it can actually differ.
561
586
  // Equal lengths mean the filter removed nothing, so `filtered` is a copy
562
587
  // of `input.rows` in the same order and the two sort identically —
563
- // `buildGroupedRows` sorts whichever it folds over, so skipping here does
588
+ // `buildGroupModel` sorts whichever it folds over, so skipping here does
564
589
  // not change the fold order.
565
590
  allRows: input.aggregateFilteredRows && filtered.length !== input.rows.length ? input.rows : void 0,
566
591
  columns: input.columns,
567
592
  rowGroups: input.rowGroups ?? [],
568
- sort: input.sort,
569
- groupExpansionOverrides: input.groupExpansionOverrides ?? NO_OVERRIDES,
570
- defaultExpanded: input.groupsDefaultExpanded ?? true
593
+ sort: input.sort
571
594
  }),
572
595
  filteredCount: filtered.length
573
596
  };
574
597
  }
598
+ function flattenDerivedRowModel(model, groupExpansionOverrides, groupsDefaultExpanded) {
599
+ return {
600
+ rows: flattenGroupModel(model.groups, groupExpansionOverrides ?? NO_OVERRIDES, groupsDefaultExpanded ?? true),
601
+ filteredCount: model.filteredCount
602
+ };
603
+ }
575
604
  function resolveFilters(columns, filters) {
576
605
  const columnMap = new Map(columns.map((c) => [c.id, c]));
577
606
  const resolved = [];
@@ -726,6 +755,7 @@ function createGridCore(inputOptions) {
726
755
  let cachedDerivedOverrides = null;
727
756
  let cachedDerivedDefaultExpanded = null;
728
757
  let cachedDerivedAggregateFiltered = null;
758
+ let cachedRowModel = null;
729
759
  let cachedEffectiveColumns = null;
730
760
  let cachedEffectiveColumnsSource = null;
731
761
  let cachedEffectiveColumnsRowGroups = null;
@@ -1367,6 +1397,7 @@ function createGridCore(inputOptions) {
1367
1397
  }
1368
1398
  if (groupingSemanticsChanged) {
1369
1399
  cachedDerivation = null;
1400
+ cachedRowModel = null;
1370
1401
  reconcileFocusAfterVisibleModelChange(before);
1371
1402
  }
1372
1403
  const selectionChanged = reconcileSelectionAfterColumnModelChange(beforeColumns);
@@ -1420,6 +1451,7 @@ function createGridCore(inputOptions) {
1420
1451
  }
1421
1452
  }
1422
1453
  cachedDerivation = null;
1454
+ cachedRowModel = null;
1423
1455
  reconcileFocusAfterVisibleModelChange(before);
1424
1456
  emit();
1425
1457
  },
@@ -1468,6 +1500,7 @@ function createGridCore(inputOptions) {
1468
1500
  };
1469
1501
  }
1470
1502
  cachedDerivation = null;
1503
+ cachedRowModel = null;
1471
1504
  reconcileFocusAfterVisibleModelChange(before);
1472
1505
  emit();
1473
1506
  },
@@ -1789,17 +1822,19 @@ function createGridCore(inputOptions) {
1789
1822
  const derivedFilters = filterAuthority === "external" ? NO_FILTERS : filters;
1790
1823
  const derivedSort = sortAuthority === "external" ? NO_SORT : sort;
1791
1824
  const derivedIsFresh = cachedDerivation !== null && cachedDerivedSort === derivedSort && cachedDerivedFilters === derivedFilters && cachedDerivedRowGroups === rowGroups && cachedDerivedOverrides === groupExpansionOverrides && cachedDerivedDefaultExpanded === groupsDefaultExpanded && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1825
+ const rowModelIsFresh = cachedRowModel !== null && cachedDerivedSort === derivedSort && cachedDerivedFilters === derivedFilters && cachedDerivedRowGroups === rowGroups && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1792
1826
  if (!derivedIsFresh) {
1793
- const derived = deriveVisibleRows({
1794
- columns: options.columns,
1795
- filters: derivedFilters,
1796
- rows: sourceRows,
1797
- sort: derivedSort,
1798
- rowGroups,
1799
- groupExpansionOverrides,
1800
- groupsDefaultExpanded,
1801
- aggregateFilteredRows
1802
- });
1827
+ if (!rowModelIsFresh) {
1828
+ cachedRowModel = buildDerivedRowModel({
1829
+ columns: options.columns,
1830
+ filters: derivedFilters,
1831
+ rows: sourceRows,
1832
+ sort: derivedSort,
1833
+ rowGroups,
1834
+ aggregateFilteredRows
1835
+ });
1836
+ }
1837
+ const derived = flattenDerivedRowModel(cachedRowModel, groupExpansionOverrides, groupsDefaultExpanded);
1803
1838
  cachedDerivation = {
1804
1839
  rows: preserveAggregateIdentity(derived.rows),
1805
1840
  filteredCount: derived.filteredCount
package/dist/index.mjs CHANGED
@@ -396,12 +396,30 @@ function makeGroupId(path) {
396
396
  }
397
397
 
398
398
  // ../grid-core/dist/group-rows.js
399
- function buildGroupedRows(args) {
399
+ function buildGroupModel(args) {
400
400
  const { rows, allRows, columns, rowGroups, sort } = args;
401
401
  const sorted = sortRows(rows, columns, sort);
402
402
  const levels = resolveLevels(columns, rowGroups);
403
403
  if (levels.length === 0) {
404
- return sorted.map(({ id, row, sourceIndex }) => ({
404
+ return {
405
+ sorted,
406
+ levels,
407
+ roots: /* @__PURE__ */ new Map(),
408
+ aggregateColumns: [],
409
+ sort,
410
+ orderedRoots: null
411
+ };
412
+ }
413
+ const roots = buildTree(sorted, levels);
414
+ const aggregateColumns = resolveAggregateColumns(columns);
415
+ if (aggregateColumns.length > 0) {
416
+ accumulate(allRows ? sortRows(allRows, columns, sort) : sorted, levels, roots, aggregateColumns);
417
+ }
418
+ return { sorted, levels, roots, aggregateColumns, sort, orderedRoots: null };
419
+ }
420
+ function flattenGroupModel(model, groupExpansionOverrides, defaultExpanded) {
421
+ if (model.levels.length === 0) {
422
+ return model.sorted.map(({ id, row, sourceIndex }) => ({
405
423
  kind: "data",
406
424
  id,
407
425
  row,
@@ -409,13 +427,11 @@ function buildGroupedRows(args) {
409
427
  depth: 0
410
428
  }));
411
429
  }
412
- const roots = buildTree(sorted, levels);
413
- const aggregateColumns = resolveAggregateColumns(columns);
414
- if (aggregateColumns.length > 0) {
415
- accumulate(allRows ? sortRows(allRows, columns, sort) : sorted, levels, roots, aggregateColumns);
430
+ if (!model.orderedRoots) {
431
+ model.orderedRoots = orderSiblings(model.roots, model.levels[0], model.sort);
416
432
  }
417
433
  const out = [];
418
- flatten(roots, 0, levels, aggregateColumns, args, out);
434
+ flatten(model.orderedRoots, 0, model, groupExpansionOverrides, defaultExpanded, out);
419
435
  return out;
420
436
  }
421
437
  function resolveLevels(columns, rowGroups) {
@@ -462,8 +478,10 @@ function buildTree(sorted, levels) {
462
478
  value,
463
479
  childCount: 0,
464
480
  children: isLeafLevel ? null : /* @__PURE__ */ new Map(),
481
+ orderedChildren: null,
465
482
  rows: isLeafLevel ? [] : null,
466
- accumulators: /* @__PURE__ */ new Map()
483
+ accumulators: /* @__PURE__ */ new Map(),
484
+ finalizedAggregates: null
467
485
  };
468
486
  siblings.set(key, node);
469
487
  }
@@ -494,9 +512,7 @@ function accumulate(source, levels, roots, aggregateColumns) {
494
512
  }
495
513
  }
496
514
  }
497
- function flatten(siblings, depth, levels, aggregateColumns, args, out) {
498
- const nodes = [...siblings.values()];
499
- sortSiblings(nodes, levels[depth], args.sort);
515
+ function flatten(nodes, depth, model, groupExpansionOverrides, defaultExpanded, out) {
500
516
  for (const node of nodes) {
501
517
  out.push({
502
518
  kind: "group",
@@ -505,12 +521,16 @@ function flatten(siblings, depth, levels, aggregateColumns, args, out) {
505
521
  columnId: node.columnId,
506
522
  value: node.value,
507
523
  childCount: node.childCount,
508
- aggregates: finalizeAggregates(node, aggregateColumns)
524
+ aggregates: finalizeAggregates(node, model.aggregateColumns)
509
525
  });
510
- if (!isExpanded(node.id, args))
526
+ const expanded = groupExpansionOverrides.has(node.id) ? !defaultExpanded : defaultExpanded;
527
+ if (!expanded)
511
528
  continue;
512
529
  if (node.children) {
513
- flatten(node.children, depth + 1, levels, aggregateColumns, args, out);
530
+ if (!node.orderedChildren) {
531
+ node.orderedChildren = orderSiblings(node.children, model.levels[depth + 1], model.sort);
532
+ }
533
+ flatten(node.orderedChildren, depth + 1, model, groupExpansionOverrides, defaultExpanded, out);
514
534
  continue;
515
535
  }
516
536
  for (const entry of node.rows ?? []) {
@@ -525,15 +545,20 @@ function flatten(siblings, depth, levels, aggregateColumns, args, out) {
525
545
  }
526
546
  }
527
547
  function finalizeAggregates(node, aggregateColumns) {
548
+ if (node.finalizedAggregates)
549
+ return node.finalizedAggregates;
528
550
  const aggregates = {};
529
551
  for (const { column, aggregator } of aggregateColumns) {
530
552
  const acc = node.accumulators.get(column.id);
531
553
  aggregates[column.id] = aggregator.finalize(acc === void 0 ? aggregator.init() : acc);
532
554
  }
555
+ node.finalizedAggregates = aggregates;
533
556
  return aggregates;
534
557
  }
535
- function isExpanded(id, args) {
536
- return args.groupExpansionOverrides.has(id) ? !args.defaultExpanded : args.defaultExpanded;
558
+ function orderSiblings(siblings, column, sort) {
559
+ const nodes = [...siblings.values()];
560
+ sortSiblings(nodes, column, sort);
561
+ return nodes;
537
562
  }
538
563
  function sortSiblings(nodes, column, sort) {
539
564
  if (nodes.length < 2)
@@ -549,27 +574,31 @@ function sortSiblings(nodes, column, sort) {
549
574
 
550
575
  // ../grid-core/dist/derived-rows.js
551
576
  var NO_OVERRIDES = /* @__PURE__ */ new Set();
552
- function deriveVisibleRows(input) {
577
+ function buildDerivedRowModel(input) {
553
578
  const resolvedFilters = resolveFilters(input.columns, input.filters);
554
579
  const filtered = input.rows.filter((entry) => matchesFilters(entry.row, resolvedFilters));
555
580
  return {
556
- rows: buildGroupedRows({
581
+ groups: buildGroupModel({
557
582
  rows: filtered,
558
583
  // Only worth carrying the pre-filter set when it can actually differ.
559
584
  // Equal lengths mean the filter removed nothing, so `filtered` is a copy
560
585
  // of `input.rows` in the same order and the two sort identically —
561
- // `buildGroupedRows` sorts whichever it folds over, so skipping here does
586
+ // `buildGroupModel` sorts whichever it folds over, so skipping here does
562
587
  // not change the fold order.
563
588
  allRows: input.aggregateFilteredRows && filtered.length !== input.rows.length ? input.rows : void 0,
564
589
  columns: input.columns,
565
590
  rowGroups: input.rowGroups ?? [],
566
- sort: input.sort,
567
- groupExpansionOverrides: input.groupExpansionOverrides ?? NO_OVERRIDES,
568
- defaultExpanded: input.groupsDefaultExpanded ?? true
591
+ sort: input.sort
569
592
  }),
570
593
  filteredCount: filtered.length
571
594
  };
572
595
  }
596
+ function flattenDerivedRowModel(model, groupExpansionOverrides, groupsDefaultExpanded) {
597
+ return {
598
+ rows: flattenGroupModel(model.groups, groupExpansionOverrides ?? NO_OVERRIDES, groupsDefaultExpanded ?? true),
599
+ filteredCount: model.filteredCount
600
+ };
601
+ }
573
602
  function resolveFilters(columns, filters) {
574
603
  const columnMap = new Map(columns.map((c) => [c.id, c]));
575
604
  const resolved = [];
@@ -724,6 +753,7 @@ function createGridCore(inputOptions) {
724
753
  let cachedDerivedOverrides = null;
725
754
  let cachedDerivedDefaultExpanded = null;
726
755
  let cachedDerivedAggregateFiltered = null;
756
+ let cachedRowModel = null;
727
757
  let cachedEffectiveColumns = null;
728
758
  let cachedEffectiveColumnsSource = null;
729
759
  let cachedEffectiveColumnsRowGroups = null;
@@ -1365,6 +1395,7 @@ function createGridCore(inputOptions) {
1365
1395
  }
1366
1396
  if (groupingSemanticsChanged) {
1367
1397
  cachedDerivation = null;
1398
+ cachedRowModel = null;
1368
1399
  reconcileFocusAfterVisibleModelChange(before);
1369
1400
  }
1370
1401
  const selectionChanged = reconcileSelectionAfterColumnModelChange(beforeColumns);
@@ -1418,6 +1449,7 @@ function createGridCore(inputOptions) {
1418
1449
  }
1419
1450
  }
1420
1451
  cachedDerivation = null;
1452
+ cachedRowModel = null;
1421
1453
  reconcileFocusAfterVisibleModelChange(before);
1422
1454
  emit();
1423
1455
  },
@@ -1466,6 +1498,7 @@ function createGridCore(inputOptions) {
1466
1498
  };
1467
1499
  }
1468
1500
  cachedDerivation = null;
1501
+ cachedRowModel = null;
1469
1502
  reconcileFocusAfterVisibleModelChange(before);
1470
1503
  emit();
1471
1504
  },
@@ -1787,17 +1820,19 @@ function createGridCore(inputOptions) {
1787
1820
  const derivedFilters = filterAuthority === "external" ? NO_FILTERS : filters;
1788
1821
  const derivedSort = sortAuthority === "external" ? NO_SORT : sort;
1789
1822
  const derivedIsFresh = cachedDerivation !== null && cachedDerivedSort === derivedSort && cachedDerivedFilters === derivedFilters && cachedDerivedRowGroups === rowGroups && cachedDerivedOverrides === groupExpansionOverrides && cachedDerivedDefaultExpanded === groupsDefaultExpanded && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1823
+ const rowModelIsFresh = cachedRowModel !== null && cachedDerivedSort === derivedSort && cachedDerivedFilters === derivedFilters && cachedDerivedRowGroups === rowGroups && cachedDerivedAggregateFiltered === aggregateFilteredRows;
1790
1824
  if (!derivedIsFresh) {
1791
- const derived = deriveVisibleRows({
1792
- columns: options.columns,
1793
- filters: derivedFilters,
1794
- rows: sourceRows,
1795
- sort: derivedSort,
1796
- rowGroups,
1797
- groupExpansionOverrides,
1798
- groupsDefaultExpanded,
1799
- aggregateFilteredRows
1800
- });
1825
+ if (!rowModelIsFresh) {
1826
+ cachedRowModel = buildDerivedRowModel({
1827
+ columns: options.columns,
1828
+ filters: derivedFilters,
1829
+ rows: sourceRows,
1830
+ sort: derivedSort,
1831
+ rowGroups,
1832
+ aggregateFilteredRows
1833
+ });
1834
+ }
1835
+ const derived = flattenDerivedRowModel(cachedRowModel, groupExpansionOverrides, groupsDefaultExpanded);
1801
1836
  cachedDerivation = {
1802
1837
  rows: preserveAggregateIdentity(derived.rows),
1803
1838
  filteredCount: derived.filteredCount
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretable/core",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Framework-agnostic grid state primitives for Pretable.",
5
5
  "license": "MIT",
6
6
  "repository": {