@gridkitjs/core 0.14.1 → 0.16.0

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
@@ -116,6 +116,21 @@ function isEmpty(value) {
116
116
  function toTime(value) {
117
117
  return value instanceof Date ? value.getTime() : new Date(String(value)).getTime();
118
118
  }
119
+ function isFiniteNumber(value) {
120
+ return typeof value === "number" && Number.isFinite(value);
121
+ }
122
+ function isValidDate(value) {
123
+ return value instanceof Date && !Number.isNaN(value.getTime());
124
+ }
125
+ function compareUntyped(a, b) {
126
+ if (isFiniteNumber(a) && isFiniteNumber(b)) {
127
+ return a - b;
128
+ }
129
+ if (isValidDate(a) && isValidDate(b)) {
130
+ return a.getTime() - b.getTime();
131
+ }
132
+ return String(a).localeCompare(String(b));
133
+ }
119
134
  function compareValues(a, b, type) {
120
135
  if (isEmpty(a)) {
121
136
  return isEmpty(b) ? 0 : 1;
@@ -135,6 +150,8 @@ function compareValues(a, b, type) {
135
150
  case "dateTime":
136
151
  case "time":
137
152
  return toTime(a) - toTime(b);
153
+ case void 0:
154
+ return compareUntyped(a, b);
138
155
  default:
139
156
  return String(a).localeCompare(String(b));
140
157
  }
@@ -394,7 +411,8 @@ function resolveKeyboardDropTarget(orderedIds, columnId, direction) {
394
411
  }
395
412
 
396
413
  // src/util/paging.ts
397
- function unitsOf(rows) {
414
+ var NO_HEADERS = [];
415
+ function groupUnitsOf(rows) {
398
416
  const units = [];
399
417
  let start = 0;
400
418
  let openedByGroup = false;
@@ -406,7 +424,7 @@ function unitsOf(rows) {
406
424
  const isTopLevelHeader = "kind" in entry && entry.kind === "group" && entry.level === 0;
407
425
  const isBoundary = index !== start && (isTopLevelHeader || !openedByGroup);
408
426
  if (isBoundary) {
409
- units.push({ start, end: index });
427
+ units.push({ start, end: index, headers: NO_HEADERS });
410
428
  start = index;
411
429
  openedByGroup = isTopLevelHeader;
412
430
  } else if (index === start) {
@@ -414,13 +432,38 @@ function unitsOf(rows) {
414
432
  }
415
433
  }
416
434
  if (start < rows.length) {
417
- units.push({ start, end: rows.length });
435
+ units.push({ start, end: rows.length, headers: NO_HEADERS });
436
+ }
437
+ return units;
438
+ }
439
+ function rowUnitsOf(rows) {
440
+ const units = [];
441
+ let open = NO_HEADERS;
442
+ for (let index = 0; index < rows.length; index++) {
443
+ const entry = rows[index];
444
+ if (entry === void 0) {
445
+ continue;
446
+ }
447
+ if (!("kind" in entry)) {
448
+ units.push({ start: index, end: index + 1, headers: open });
449
+ } else if (entry.kind === "group") {
450
+ const ancestors = open.slice(0, entry.level);
451
+ open = entry.expanded ? [...ancestors, entry] : ancestors;
452
+ if (!entry.expanded) {
453
+ units.push({ start: index, end: index + 1, headers: ancestors });
454
+ }
455
+ } else {
456
+ const last = units.at(-1);
457
+ if (last !== void 0) {
458
+ units[units.length - 1] = { ...last, end: index + 1 };
459
+ }
460
+ }
418
461
  }
419
462
  return units;
420
463
  }
421
- function paginateRows(rows, pagination) {
464
+ function paginateRows(rows, pagination, options = {}) {
422
465
  const pageSize = Math.max(1, pagination.pageSize);
423
- const units = unitsOf(rows);
466
+ const units = options.unit === "row" ? rowUnitsOf(rows) : groupUnitsOf(rows);
424
467
  const pageCount = Math.ceil(units.length / pageSize);
425
468
  if (pageCount === 0) {
426
469
  return { rows: [], pageCount: 0, pageIndex: 0 };
@@ -431,7 +474,10 @@ function paginateRows(rows, pagination) {
431
474
  if (firstUnit === void 0 || lastUnit === void 0) {
432
475
  return { rows: [], pageCount, pageIndex };
433
476
  }
434
- const page = rows.slice(firstUnit.start, lastUnit.end);
477
+ const page = [
478
+ ...firstUnit.headers,
479
+ ...rows.slice(firstUnit.start, lastUnit.end)
480
+ ];
435
481
  const renumbered = page.map(
436
482
  (entry, index) => entry.rowIndex === index ? entry : { ...entry, rowIndex: index }
437
483
  );
@@ -1233,8 +1279,8 @@ function totalAggregates(rows, aggregates, dataColumns) {
1233
1279
  dataColumns
1234
1280
  );
1235
1281
  }
1236
- function paginate(displayRows, paginated, pagination) {
1237
- return paginated ? paginateRows(displayRows, pagination) : { rows: displayRows, pageCount: 1, pageIndex: 0 };
1282
+ function paginate(displayRows, paginated, pagination, unit) {
1283
+ return paginated ? paginateRows(displayRows, pagination, { unit }) : { rows: displayRows, pageCount: 1, pageIndex: 0 };
1238
1284
  }
1239
1285
  function narrowToDataRows(pageRows, rows, reshaped) {
1240
1286
  return reshaped ? pageRows.filter((entry) => !("kind" in entry)) : rows;
@@ -1295,7 +1341,12 @@ function createGridViewResolver() {
1295
1341
  rows: pageRows,
1296
1342
  pageCount,
1297
1343
  pageIndex
1298
- } = page(displayRows, paginated, state.pagination);
1344
+ } = page(
1345
+ displayRows,
1346
+ paginated,
1347
+ state.pagination,
1348
+ options.paginationUnit ?? "group"
1349
+ );
1299
1350
  return assemble(
1300
1351
  dataColumns,
1301
1352
  columns,
@@ -1545,6 +1596,21 @@ function indexRows(rows) {
1545
1596
  function distinct(ids) {
1546
1597
  return [...new Set(ids)];
1547
1598
  }
1599
+ function click(selection, orderedIds, anchor, id, intent, mode) {
1600
+ const live = anchor !== null && orderedIds.includes(anchor) ? anchor : null;
1601
+ const keepsAnchor = mode === false || intent === "range" && live !== null;
1602
+ return {
1603
+ selection: applySelectionIntent(
1604
+ selection,
1605
+ orderedIds,
1606
+ live,
1607
+ id,
1608
+ intent,
1609
+ mode
1610
+ ),
1611
+ anchor: keepsAnchor ? anchor : id
1612
+ };
1613
+ }
1548
1614
  function createSelectionActions(grid) {
1549
1615
  let rowAnchor = null;
1550
1616
  let columnAnchor = null;
@@ -1608,6 +1674,7 @@ function createSelectionActions(grid) {
1608
1674
  }
1609
1675
  return {
1610
1676
  setRowSelection(selection) {
1677
+ rowAnchor = null;
1611
1678
  if (!sameItems(grid.getState().rowSelection, selection)) {
1612
1679
  commitRows(selection);
1613
1680
  }
@@ -1628,7 +1695,7 @@ function createSelectionActions(grid) {
1628
1695
  }
1629
1696
  },
1630
1697
  selectRow(rowId, intent) {
1631
- const next = applySelectionIntent(
1698
+ const next = click(
1632
1699
  grid.getState().rowSelection,
1633
1700
  rowIds(),
1634
1701
  rowAnchor,
@@ -1636,10 +1703,8 @@ function createSelectionActions(grid) {
1636
1703
  intent,
1637
1704
  modes().rows
1638
1705
  );
1639
- if (intent !== "range") {
1640
- rowAnchor = rowId;
1641
- }
1642
- commitRows(next);
1706
+ rowAnchor = next.anchor;
1707
+ commitRows(next.selection);
1643
1708
  },
1644
1709
  selectAllRows() {
1645
1710
  commitRows(
@@ -1647,17 +1712,22 @@ function createSelectionActions(grid) {
1647
1712
  );
1648
1713
  },
1649
1714
  clearSelection() {
1650
- commitRows(clearSelection(grid.getState().rowSelection));
1651
- commitColumns(clearSelection(grid.getState().columnSelection));
1652
- commitCell(null);
1715
+ rowAnchor = null;
1716
+ columnAnchor = null;
1717
+ grid.transaction(() => {
1718
+ commitRows(clearSelection(grid.getState().rowSelection));
1719
+ commitColumns(clearSelection(grid.getState().columnSelection));
1720
+ commitCell(null);
1721
+ });
1653
1722
  },
1654
1723
  setColumnSelection(selection) {
1724
+ columnAnchor = null;
1655
1725
  if (!sameItems(grid.getState().columnSelection, selection)) {
1656
1726
  commitColumns(selection);
1657
1727
  }
1658
1728
  },
1659
1729
  selectColumn(columnId, intent) {
1660
- const next = applySelectionIntent(
1730
+ const next = click(
1661
1731
  grid.getState().columnSelection,
1662
1732
  columnIds(),
1663
1733
  columnAnchor,
@@ -1665,10 +1735,8 @@ function createSelectionActions(grid) {
1665
1735
  intent,
1666
1736
  modes().columns
1667
1737
  );
1668
- if (intent !== "range") {
1669
- columnAnchor = columnId;
1670
- }
1671
- commitColumns(next);
1738
+ columnAnchor = next.anchor;
1739
+ commitColumns(next.selection);
1672
1740
  },
1673
1741
  setCellSelection(selection) {
1674
1742
  if (!isSameCell(grid.getState().cellSelection, selection)) {
@@ -1742,11 +1810,16 @@ function definedColumns(columns, rows) {
1742
1810
  }
1743
1811
  return NO_COLUMNS;
1744
1812
  }
1745
- function pagingInputsOf(state) {
1813
+ var IDLE = { kind: "idle" };
1814
+ function samePagingInputs(a, b) {
1815
+ return a.filter === b.filter && a.columnSort === b.columnSort && a.groupBy === b.groupBy && a.paginationUnit === b.paginationUnit;
1816
+ }
1817
+ function pagingInputsOf(state, options) {
1746
1818
  return {
1747
1819
  filter: state.filter,
1748
1820
  columnSort: state.columnSort,
1749
- groupBy: state.groupBy
1821
+ groupBy: state.groupBy,
1822
+ paginationUnit: options.paginationUnit ?? "group"
1750
1823
  };
1751
1824
  }
1752
1825
  function createGridController(initial) {
@@ -1756,9 +1829,9 @@ function createGridController(initial) {
1756
1829
  const resolveColumns2 = memoizeLast(definedColumns);
1757
1830
  const listeners = /* @__PURE__ */ new Set();
1758
1831
  const reportedConflicts = /* @__PURE__ */ new Set();
1759
- let heldReports;
1832
+ let mode = IDLE;
1760
1833
  let state = resolveSlices();
1761
- let pagingInputs = pagingInputsOf(state);
1834
+ let pagingInputs = pagingInputsOf(state, options);
1762
1835
  reportConflicts();
1763
1836
  function controlledValue(key) {
1764
1837
  const controlled = options;
@@ -1798,16 +1871,36 @@ function createGridController(initial) {
1798
1871
  );
1799
1872
  }
1800
1873
  }
1801
- function refresh() {
1802
- let next = resolveSlices();
1803
- if (next.filter !== pagingInputs.filter || next.columnSort !== pagingInputs.columnSort || next.groupBy !== pagingInputs.groupBy) {
1804
- pagingInputs = pagingInputsOf(next);
1805
- if (!isControlled("pagination") && own.pagination.pageIndex !== 0) {
1806
- own.pagination = { ...own.pagination, pageIndex: 0 };
1807
- next = { ...next, pagination: own.pagination };
1808
- }
1809
- }
1874
+ function refresh(keepPage = false) {
1875
+ const next = followPagingInputs(resolveSlices(), keepPage);
1810
1876
  state = SLICE_KEYS.every((key) => next[key] === state[key]) ? state : next;
1877
+ storeShownPage();
1878
+ }
1879
+ function followPagingInputs(next, keepPage) {
1880
+ const inputs = pagingInputsOf(next, options);
1881
+ if (samePagingInputs(inputs, pagingInputs)) {
1882
+ return next;
1883
+ }
1884
+ pagingInputs = inputs;
1885
+ if (keepPage || isControlled("pagination") || own.pagination.pageIndex === 0) {
1886
+ return next;
1887
+ }
1888
+ own.pagination = { ...own.pagination, pageIndex: 0 };
1889
+ return { ...next, pagination: own.pagination };
1890
+ }
1891
+ function storeShownPage() {
1892
+ const stored = storedPagination(state.pagination);
1893
+ if (stored !== state.pagination) {
1894
+ own.pagination = stored;
1895
+ state = { ...state, pagination: stored };
1896
+ }
1897
+ }
1898
+ function storedPagination(pagination) {
1899
+ if (options.paginated !== true || isControlled("pagination")) {
1900
+ return pagination;
1901
+ }
1902
+ const { pageCount, pageIndex } = pageOf(pagination);
1903
+ return pageCount === 0 || pageIndex === pagination.pageIndex ? pagination : { ...pagination, pageIndex };
1811
1904
  }
1812
1905
  function notify() {
1813
1906
  for (const listener of [...listeners]) {
@@ -1821,14 +1914,38 @@ function createGridController(initial) {
1821
1914
  if (!isControlled(key)) {
1822
1915
  own[key] = next;
1823
1916
  }
1824
- if (heldReports !== void 0) {
1825
- heldReports.push(report);
1917
+ if (mode.kind === "transaction") {
1918
+ mode.held.push(report);
1826
1919
  return;
1827
1920
  }
1828
1921
  refresh();
1829
1922
  report();
1830
1923
  notify();
1831
1924
  }
1925
+ function transaction(run) {
1926
+ if (mode.kind === "transaction") {
1927
+ run();
1928
+ return;
1929
+ }
1930
+ const current = {
1931
+ kind: "transaction",
1932
+ held: [],
1933
+ pageAsked: false
1934
+ };
1935
+ mode = current;
1936
+ try {
1937
+ run();
1938
+ } finally {
1939
+ mode = IDLE;
1940
+ refresh(current.pageAsked);
1941
+ for (const report of current.held) {
1942
+ report();
1943
+ }
1944
+ if (current.held.length > 0) {
1945
+ notify();
1946
+ }
1947
+ }
1948
+ }
1832
1949
  function getView() {
1833
1950
  return resolveView({
1834
1951
  rows: options.dataSource ?? NO_ROWS,
@@ -1837,6 +1954,7 @@ function createGridController(initial) {
1837
1954
  state,
1838
1955
  options: {
1839
1956
  paginated: options.paginated,
1957
+ paginationUnit: options.paginationUnit,
1840
1958
  aggregates: options.aggregates,
1841
1959
  groupAggregateDisplay: options.groupAggregateDisplay,
1842
1960
  columnSizeDefaults: options.columnSizeDefaults,
@@ -1897,33 +2015,43 @@ function createGridController(initial) {
1897
2015
  options.onGroupExpansionChange?.({ groupId, expansion: next });
1898
2016
  });
1899
2017
  }
1900
- function commitPagination(next) {
2018
+ function commitPagination(asked) {
2019
+ let next = asked;
2020
+ if (mode.kind === "transaction") {
2021
+ mode.pageAsked = true;
2022
+ } else {
2023
+ next = storedPagination(asked);
2024
+ }
1901
2025
  if (samePagination(state.pagination, next)) {
1902
2026
  return;
1903
2027
  }
1904
2028
  commit("pagination", next, () => {
2029
+ const pagination = isControlled("pagination") ? next : own.pagination;
1905
2030
  options.onPaginationChange?.({
1906
- pagination: next,
1907
- pageCount: paginateRows(getView().displayRows, next).pageCount
2031
+ pagination,
2032
+ pageCount: pageOf(pagination).pageCount
1908
2033
  });
1909
2034
  });
1910
2035
  }
2036
+ function pageOf(pagination) {
2037
+ return paginateRows(getView().displayRows, pagination, {
2038
+ unit: options.paginationUnit
2039
+ });
2040
+ }
1911
2041
  function goToPage(pageIndex) {
1912
2042
  const current = state.pagination;
1913
- const clamped = paginateRows(getView().displayRows, {
1914
- ...current,
1915
- pageIndex
1916
- }).pageIndex;
2043
+ const clamped = pageOf({ ...current, pageIndex }).pageIndex;
1917
2044
  commitPagination({ ...current, pageIndex: clamped });
1918
2045
  }
1919
2046
  function shownPageIndex() {
1920
- return paginateRows(getView().displayRows, state.pagination).pageIndex;
2047
+ return pageOf(state.pagination).pageIndex;
1921
2048
  }
1922
2049
  const selection = createSelectionActions({
1923
2050
  getState: () => state,
1924
2051
  getOptions: () => options,
1925
2052
  getView,
1926
- commit
2053
+ commit,
2054
+ transaction
1927
2055
  });
1928
2056
  const controller = {
1929
2057
  ...selection,
@@ -1936,23 +2064,9 @@ function createGridController(initial) {
1936
2064
  getState: () => state,
1937
2065
  getView,
1938
2066
  restoreState(saved) {
1939
- const reports = [];
1940
- heldReports = reports;
1941
- try {
2067
+ transaction(() => {
1942
2068
  restoreSlices(saved);
1943
- } finally {
1944
- heldReports = void 0;
1945
- }
1946
- if (saved.pagination !== void 0) {
1947
- pagingInputs = pagingInputsOf(resolveSlices());
1948
- }
1949
- refresh();
1950
- for (const report of reports) {
1951
- report();
1952
- }
1953
- if (reports.length > 0) {
1954
- notify();
1955
- }
2069
+ });
1956
2070
  },
1957
2071
  subscribe(listener) {
1958
2072
  listeners.add(listener);
@@ -2106,7 +2220,7 @@ function createGridController(initial) {
2106
2220
  const first = { pageIndex: 0, pageSize };
2107
2221
  commitPagination({
2108
2222
  ...first,
2109
- pageIndex: paginateRows(getView().displayRows, first).pageIndex
2223
+ pageIndex: pageOf(first).pageIndex
2110
2224
  });
2111
2225
  }
2112
2226
  };
@@ -3207,7 +3321,8 @@ function createDefaultMessages(locale) {
3207
3321
  copySelection: "Copy selection",
3208
3322
  copied: (count, unit) => copied(n, count, unit),
3209
3323
  copyFailed: "Copy failed",
3210
- print: "Print"
3324
+ print: "Print",
3325
+ printFailed: "Print failed"
3211
3326
  };
3212
3327
  }
3213
3328
  var defaultMessages = createDefaultMessages();
@@ -3476,6 +3591,27 @@ function draftToFilter(draft, columnId, current) {
3476
3591
  const kept = current.filter((entry) => !touchesColumn(entry, columnId));
3477
3592
  return [...kept.slice(0, first), ...replacement, ...kept.slice(first)];
3478
3593
  }
3594
+ function sameValues(a, b) {
3595
+ return a.includeBlanks === b.includeBlanks && a.checked.size === b.checked.size && [...a.checked.keys()].every((key) => b.checked.has(key));
3596
+ }
3597
+ function sameOperand2(a, b, type) {
3598
+ if (isScalarList(a) || isScalarList(b)) {
3599
+ return isScalarList(a) && isScalarList(b) && a.length === b.length && a.every((scalar, index) => sameOperand2(scalar, b[index], type));
3600
+ }
3601
+ return filterValueKey(a, type) === filterValueKey(b, type);
3602
+ }
3603
+ function sameConditions(a, b) {
3604
+ return a.conditions.length === b.conditions.length && (a.conditions.length < 2 || a.combinator === b.combinator) && a.conditions.every((condition, index) => {
3605
+ const other = b.conditions[index];
3606
+ return other !== void 0 && condition.operator === other.operator && sameOperand2(condition.value, other.value, a.columnType);
3607
+ });
3608
+ }
3609
+ function filterDraftChanged(opened, draft) {
3610
+ if (draft.mode === "values") {
3611
+ return !sameValues(opened, draft);
3612
+ }
3613
+ return opened.mode !== "conditions" || !sameConditions(opened, draft);
3614
+ }
3479
3615
 
3480
3616
  // src/commands/builtInCommands.ts
3481
3617
  function isOn(context, areas) {
@@ -3577,9 +3713,10 @@ function exportCommand({
3577
3713
  selection ? { ...defaults, scope: "selection" } : defaults,
3578
3714
  resolveExportWriters(exportWriters)
3579
3715
  );
3580
- if (file !== null) {
3581
- context.host.saveFile(file);
3716
+ if (file === null) {
3717
+ return;
3582
3718
  }
3719
+ return context.host.saveFile(file) !== false;
3583
3720
  }
3584
3721
  };
3585
3722
  }
@@ -3883,8 +4020,7 @@ function runCommand(command, context) {
3883
4020
  if (!isCommandVisible(command, context) || !isCommandEnabled(command, context)) {
3884
4021
  return false;
3885
4022
  }
3886
- command.execute(context);
3887
- return true;
4023
+ return command.execute(context) !== false;
3888
4024
  }
3889
4025
 
3890
4026
  // src/menu/resolveMenu.ts
@@ -4150,7 +4286,14 @@ function exportMenu() {
4150
4286
  };
4151
4287
  }
4152
4288
  function resolveToolbar(entries, options) {
4153
- const { commands, contextFor } = options;
4289
+ const { commands, contextFor, itemAttributes } = options;
4290
+ function withAttributes(item) {
4291
+ if (item.type === "separator" || item.type === "spacer" || itemAttributes === void 0 || !Object.hasOwn(itemAttributes, item.key)) {
4292
+ return item;
4293
+ }
4294
+ const attributes = itemAttributes[item.key];
4295
+ return attributes === void 0 ? item : { ...item, attributes };
4296
+ }
4154
4297
  function resolveEntry(entry, index) {
4155
4298
  if (entry === "search") {
4156
4299
  return { type: "search", key: "search" };
@@ -4182,7 +4325,7 @@ function resolveToolbar(entries, options) {
4182
4325
  return { ...item, type: "button", key: item.id, pressed: checked };
4183
4326
  }
4184
4327
  return collapseSeparators(
4185
- entries.map(resolveEntry).filter((entry) => entry !== null),
4328
+ entries.map(resolveEntry).filter((entry) => entry !== null).map(withAttributes),
4186
4329
  isSpacer
4187
4330
  );
4188
4331
  }
@@ -4670,6 +4813,7 @@ export {
4670
4813
  exportFile,
4671
4814
  exportPrintDocument,
4672
4815
  filterChecklist,
4816
+ filterDraftChanged,
4673
4817
  filterEditorReducer,
4674
4818
  filterOperandArity,
4675
4819
  filterOperatorFamily,