@gridkitjs/core 0.0.0-dev-20261109130950 → 0.0.0-dev-20261109132710

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.js CHANGED
@@ -275,7 +275,7 @@ function buildLevel(rows, levels, parentPath, expansion, byId, out) {
275
275
  for (const group of groups) {
276
276
  const path = [...parentPath, group.value];
277
277
  const groupId = groupRowId(path);
278
- const expanded = !expansion.includes(groupId);
278
+ const expanded = isGroupExpanded(expansion, groupId);
279
279
  const header = {
280
280
  kind: "group",
281
281
  groupId,
@@ -311,16 +311,30 @@ function groupRows(rows, groupBy, expansion, columns) {
311
311
  buildLevel(rows, active, [], expansion, byId, out);
312
312
  return renumberRows(out);
313
313
  }
314
+ var ALL_EXPANDED = {
315
+ mode: "allExpanded",
316
+ except: []
317
+ };
318
+ var ALL_COLLAPSED = {
319
+ mode: "allCollapsed",
320
+ except: []
321
+ };
322
+ function isGroupExpanded(expansion, groupId) {
323
+ const excepted = expansion.except.includes(groupId);
324
+ return expansion.mode === "allExpanded" ? !excepted : excepted;
325
+ }
314
326
  function toggleGroupExpansion(expansion, groupId) {
315
- return expansion.includes(groupId) ? expansion.filter((id) => id !== groupId) : [...expansion, groupId];
327
+ const except = expansion.except.includes(groupId) ? expansion.except.filter((id) => id !== groupId) : [...expansion.except, groupId];
328
+ return { mode: expansion.mode, except };
316
329
  }
317
330
  function expandAllGroups(expansion) {
318
- return expansion.length === 0 ? expansion : [];
331
+ return isWithoutExceptions(expansion, "allExpanded") ? expansion : ALL_EXPANDED;
319
332
  }
320
- function collapseAllGroups(rows) {
321
- return rows.filter(
322
- (entry) => "kind" in entry && entry.kind === "group"
323
- ).map((entry) => entry.groupId);
333
+ function collapseAllGroups(expansion) {
334
+ return isWithoutExceptions(expansion, "allCollapsed") ? expansion : ALL_COLLAPSED;
335
+ }
336
+ function isWithoutExceptions(expansion, mode) {
337
+ return expansion.mode === mode && expansion.except.length === 0;
324
338
  }
325
339
  function moveGroupByBefore(groupBy, columnId, beforeColumnId) {
326
340
  if (columnId === beforeColumnId) {
@@ -923,6 +937,23 @@ function reviveFilter(filter, columns) {
923
937
  const types = new Map(columns.map((entry) => [entry.id, entry.column.type]));
924
938
  return reviveEntries(filter, (columnId) => types.get(columnId));
925
939
  }
940
+ function isStringList(value) {
941
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
942
+ }
943
+ function isGroupExpansion(value) {
944
+ return typeof value === "object" && value !== null && "mode" in value && (value.mode === "allExpanded" || value.mode === "allCollapsed") && "except" in value && isStringList(value.except);
945
+ }
946
+ function reviveGroupExpansion(saved) {
947
+ if (isStringList(saved)) {
948
+ return { mode: "allExpanded", except: saved };
949
+ }
950
+ if (isGroupExpansion(saved)) {
951
+ return saved;
952
+ }
953
+ throw new TypeError(
954
+ "GridKit: a saved `groupExpansion` is neither `{ mode, except }` nor a list of group ids."
955
+ );
956
+ }
926
957
 
927
958
  // src/util/sizing.ts
928
959
  var DEFAULT_COLUMN_SIZES = {
@@ -1434,6 +1465,9 @@ function changedKeys(previous, next) {
1434
1465
  function sameItems(previous, next) {
1435
1466
  return previous.length === next.length && previous.every((item, index) => item === next[index]);
1436
1467
  }
1468
+ function sameGroupExpansion(previous, next) {
1469
+ return previous.mode === next.mode && sameItems(previous.except, next.except);
1470
+ }
1437
1471
  function samePagination(previous, next) {
1438
1472
  return previous.pageIndex === next.pageIndex && previous.pageSize === next.pageSize;
1439
1473
  }
@@ -1831,7 +1865,7 @@ function startingState(options) {
1831
1865
  columnSort: options.defaultColumnSort ?? [],
1832
1866
  filter: options.defaultFilter ?? [],
1833
1867
  groupBy: options.defaultGroupBy ?? [],
1834
- groupExpansion: options.defaultGroupExpansion ?? [],
1868
+ groupExpansion: options.defaultGroupExpansion ?? ALL_EXPANDED,
1835
1869
  pagination: options.defaultPagination ?? {
1836
1870
  pageIndex: 0,
1837
1871
  pageSize: DEFAULT_PAGE_SIZE
@@ -2236,7 +2270,7 @@ function createGridController(initial) {
2236
2270
  );
2237
2271
  },
2238
2272
  setGroupExpansion(expansion) {
2239
- if (!sameItems(state.groupExpansion, expansion)) {
2273
+ if (!sameGroupExpansion(state.groupExpansion, expansion)) {
2240
2274
  commitGroupExpansion(expansion, null);
2241
2275
  }
2242
2276
  },
@@ -2250,7 +2284,7 @@ function createGridController(initial) {
2250
2284
  commitGroupExpansion(expandAllGroups(state.groupExpansion), null);
2251
2285
  },
2252
2286
  collapseAllGroups() {
2253
- commitGroupExpansion(collapseAllGroups(getView().displayRows), null);
2287
+ commitGroupExpansion(collapseAllGroups(state.groupExpansion), null);
2254
2288
  },
2255
2289
  setPagination: commitPagination,
2256
2290
  goToPage,
@@ -2288,7 +2322,7 @@ function createGridController(initial) {
2288
2322
  controller.setGroupBy(saved.groupBy);
2289
2323
  }
2290
2324
  if (saved.groupExpansion !== void 0) {
2291
- controller.setGroupExpansion(saved.groupExpansion);
2325
+ controller.setGroupExpansion(reviveGroupExpansion(saved.groupExpansion));
2292
2326
  }
2293
2327
  if (saved.pagination !== void 0) {
2294
2328
  controller.setPagination(saved.pagination);
@@ -2632,7 +2666,12 @@ function buildExportTable(source, options = {}) {
2632
2666
  messages: source.messages
2633
2667
  };
2634
2668
  const rows = scopedRows(source, options.scope ?? "all");
2635
- const grouped = groupRows(rows, state.groupBy, [], view.dataColumns);
2669
+ const grouped = groupRows(
2670
+ rows,
2671
+ state.groupBy,
2672
+ ALL_EXPANDED,
2673
+ view.dataColumns
2674
+ );
2636
2675
  const displayRows = options.includeGroups ?? true ? withGroupAggregates(
2637
2676
  grouped,
2638
2677
  rows,
@@ -3492,38 +3531,53 @@ function everyOption(draft) {
3492
3531
  draft.options.map((option) => [keyOf2(draft, option.value), option.value])
3493
3532
  );
3494
3533
  }
3534
+ function baseOf(draft) {
3535
+ const { options, blanks, search, openedShape, columnType } = draft;
3536
+ return { options, blanks, search, openedShape, columnType };
3537
+ }
3538
+ function openedValues(base) {
3539
+ const shape = base.openedShape;
3540
+ if (shape.kind !== "values") {
3541
+ return {
3542
+ kind: "values",
3543
+ ...base,
3544
+ checked: everyOption(base),
3545
+ includeBlanks: base.blanks > 0
3546
+ };
3547
+ }
3548
+ return {
3549
+ kind: "values",
3550
+ ...base,
3551
+ checked: new Map(
3552
+ shape.values.filter((value) => !isEmptyCell(value)).map((value) => [keyOf2(base, value), value])
3553
+ ),
3554
+ includeBlanks: shape.includeBlanks
3555
+ };
3556
+ }
3557
+ function openedConditions(base) {
3558
+ const shape = base.openedShape;
3559
+ return shape.kind === "conditions" ? {
3560
+ kind: "conditions",
3561
+ ...base,
3562
+ conditions: shape.conditions,
3563
+ combinator: shape.combinator
3564
+ } : { kind: "conditions", ...base, conditions: [], combinator: "and" };
3565
+ }
3495
3566
  function initialFilterEditorDraft(shape, values) {
3496
- const draft = {
3497
- mode: "values",
3567
+ const base = {
3498
3568
  options: values.options,
3499
3569
  blanks: values.blanks,
3500
3570
  search: "",
3501
- checked: everyOption(values),
3502
- includeBlanks: values.blanks > 0,
3503
- conditions: [],
3504
- combinator: "and",
3571
+ openedShape: shape,
3505
3572
  columnType: values.columnType
3506
3573
  };
3507
- switch (shape.kind) {
3508
- case "values":
3509
- return {
3510
- ...draft,
3511
- checked: new Map(
3512
- shape.values.filter((value) => !isEmptyCell(value)).map((value) => [keyOf2(draft, value), value])
3513
- ),
3514
- includeBlanks: shape.includeBlanks
3515
- };
3516
- case "conditions":
3517
- return {
3518
- ...draft,
3519
- mode: "conditions",
3520
- conditions: shape.conditions,
3521
- combinator: shape.combinator
3522
- };
3523
- case "none":
3524
- case "custom":
3525
- return draft;
3526
- }
3574
+ return shape.kind === "conditions" ? openedConditions(base) : openedValues(base);
3575
+ }
3576
+ function toValuesDraft(draft) {
3577
+ return draft.kind === "values" ? draft : openedValues(baseOf(draft));
3578
+ }
3579
+ function toConditionsDraft(draft) {
3580
+ return draft.kind === "conditions" ? draft : openedConditions(baseOf(draft));
3527
3581
  }
3528
3582
  function visibleFilterOptions(draft) {
3529
3583
  return draft.search === "" ? draft.options : draft.options.filter(
@@ -3545,6 +3599,16 @@ function selectAllState(draft) {
3545
3599
  }
3546
3600
  return states.some(Boolean) ? "mixed" : "unchecked";
3547
3601
  }
3602
+ function toggleValue(draft, value) {
3603
+ const key = keyOf2(draft, value);
3604
+ const checked = new Map(draft.checked);
3605
+ if (checked.has(key)) {
3606
+ checked.delete(key);
3607
+ } else {
3608
+ checked.set(key, value);
3609
+ }
3610
+ return { ...draft, checked };
3611
+ }
3548
3612
  function toggleAll(draft) {
3549
3613
  const check = selectAllState(draft) !== "checked";
3550
3614
  const checked = new Map(draft.checked);
@@ -3558,44 +3622,43 @@ function toggleAll(draft) {
3558
3622
  }
3559
3623
  return {
3560
3624
  ...draft,
3561
- mode: "values",
3562
3625
  checked,
3563
3626
  includeBlanks: selectAllCoversBlanks(draft) ? check : draft.includeBlanks
3564
3627
  };
3565
3628
  }
3629
+ function cleared(draft) {
3630
+ switch (draft.kind) {
3631
+ case "values":
3632
+ return {
3633
+ ...draft,
3634
+ search: "",
3635
+ checked: everyOption(draft),
3636
+ includeBlanks: draft.blanks > 0
3637
+ };
3638
+ case "conditions":
3639
+ return { ...draft, search: "", conditions: [] };
3640
+ }
3641
+ }
3566
3642
  function filterEditorReducer(draft, action) {
3567
3643
  switch (action.type) {
3568
3644
  case "search":
3569
3645
  return { ...draft, search: action.search };
3570
- case "toggleValue": {
3571
- const key = keyOf2(draft, action.value);
3572
- const checked = new Map(draft.checked);
3573
- if (checked.has(key)) {
3574
- checked.delete(key);
3575
- } else {
3576
- checked.set(key, action.value);
3577
- }
3578
- return { ...draft, mode: "values", checked };
3579
- }
3646
+ case "toggleValue":
3647
+ return toggleValue(toValuesDraft(draft), action.value);
3580
3648
  case "toggleAll":
3581
- return toggleAll(draft);
3649
+ return toggleAll(toValuesDraft(draft));
3582
3650
  case "setIncludeBlanks":
3583
- return { ...draft, mode: "values", includeBlanks: action.includeBlanks };
3584
- case "setConditions":
3651
+ return { ...toValuesDraft(draft), includeBlanks: action.includeBlanks };
3652
+ case "setConditions": {
3653
+ const conditions = toConditionsDraft(draft);
3585
3654
  return {
3586
- ...draft,
3587
- mode: "conditions",
3655
+ ...conditions,
3588
3656
  conditions: action.conditions,
3589
- combinator: action.combinator ?? draft.combinator
3657
+ combinator: action.combinator ?? conditions.combinator
3590
3658
  };
3659
+ }
3591
3660
  case "clear":
3592
- return {
3593
- ...draft,
3594
- search: "",
3595
- checked: everyOption(draft),
3596
- includeBlanks: draft.blanks > 0,
3597
- conditions: []
3598
- };
3661
+ return cleared(draft);
3599
3662
  }
3600
3663
  }
3601
3664
  function checksEverything(draft) {
@@ -3603,14 +3666,7 @@ function checksEverything(draft) {
3603
3666
  (option) => draft.checked.has(keyOf2(draft, option.value))
3604
3667
  ) && (draft.includeBlanks || draft.blanks === 0);
3605
3668
  }
3606
- function draftEntries(draft, columnId) {
3607
- if (draft.mode === "conditions") {
3608
- const conditions = draft.conditions.map((condition) => ({
3609
- ...condition,
3610
- columnId
3611
- }));
3612
- return conditions.length > 1 ? [{ kind: "group", combinator: draft.combinator, entries: conditions }] : conditions;
3613
- }
3669
+ function valuesEntries(draft, columnId) {
3614
3670
  if (checksEverything(draft)) {
3615
3671
  return [];
3616
3672
  }
@@ -3631,6 +3687,21 @@ function draftEntries(draft, columnId) {
3631
3687
  }
3632
3688
  ] : [picked];
3633
3689
  }
3690
+ function conditionsEntries(draft, columnId) {
3691
+ const conditions = draft.conditions.map((condition) => ({
3692
+ ...condition,
3693
+ columnId
3694
+ }));
3695
+ return conditions.length > 1 ? [{ kind: "group", combinator: draft.combinator, entries: conditions }] : conditions;
3696
+ }
3697
+ function draftEntries(draft, columnId) {
3698
+ switch (draft.kind) {
3699
+ case "values":
3700
+ return valuesEntries(draft, columnId);
3701
+ case "conditions":
3702
+ return conditionsEntries(draft, columnId);
3703
+ }
3704
+ }
3634
3705
  function draftToFilter(draft, columnId, current) {
3635
3706
  const replacement = draftEntries(draft, columnId);
3636
3707
  const first = current.findIndex((entry) => touchesColumn(entry, columnId));
@@ -3656,10 +3727,12 @@ function sameConditions(a, b) {
3656
3727
  });
3657
3728
  }
3658
3729
  function filterDraftChanged(opened, draft) {
3659
- if (draft.mode === "values") {
3660
- return !sameValues(opened, draft);
3730
+ switch (draft.kind) {
3731
+ case "values":
3732
+ return !sameValues(toValuesDraft(opened), draft);
3733
+ case "conditions":
3734
+ return opened.kind !== "conditions" || !sameConditions(opened, draft);
3661
3735
  }
3662
- return opened.mode !== "conditions" || !sameConditions(opened, draft);
3663
3736
  }
3664
3737
 
3665
3738
  // src/commands/builtInCommands.ts
@@ -4819,6 +4892,8 @@ var excelFilter = {
4819
4892
  filterMenu: { ui: "excel", showCounts: true }
4820
4893
  };
4821
4894
  export {
4895
+ ALL_COLLAPSED,
4896
+ ALL_EXPANDED,
4822
4897
  DEFAULT_COLUMN_SIZES,
4823
4898
  DEFAULT_FILTER_VALUE_LIMIT,
4824
4899
  HEADER_ROW,
@@ -4885,6 +4960,7 @@ export {
4885
4960
  intentOf,
4886
4961
  isColumnFiltered,
4887
4962
  isColumnVisible,
4963
+ isGroupExpanded,
4888
4964
  isPinnedToolbarItem,
4889
4965
  isSameCell,
4890
4966
  jsonWriter,
@@ -4935,10 +5011,12 @@ export {
4935
5011
  sortPriorityFor,
4936
5012
  sortRows,
4937
5013
  splitToolbarItems,
5014
+ toConditionsDraft,
4938
5015
  toCsv,
4939
5016
  toHtmlTable,
4940
5017
  toJson,
4941
5018
  toTsv,
5019
+ toValuesDraft,
4942
5020
  toggleCellSelection,
4943
5021
  toggleColumnSort,
4944
5022
  toggleGroupExpansion,