@stramien/core 0.13.0 → 0.14.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.d.ts CHANGED
@@ -414,7 +414,11 @@ interface CellValueChangedEvent<TData = Record<string, unknown>, TValue = unknow
414
414
  }
415
415
  interface ColumnStateItem {
416
416
  colId: string;
417
- width: number;
417
+ /**
418
+ * Left out with `fitColumns` for columns the grid sizes itself, so restoring the state
419
+ * keeps only the widths the user chose.
420
+ */
421
+ width?: number;
418
422
  pinned: PinnedDirection;
419
423
  hide: boolean;
420
424
  }
@@ -547,7 +551,11 @@ interface ColDef<TData = Record<string, unknown>, TValue = unknown> {
547
551
  width?: number;
548
552
  minWidth?: number;
549
553
  maxWidth?: number;
550
- /** Distributes remaining horizontal viewport width proportionally. Set `minWidth` when combined with `wrapText`. */
554
+ /**
555
+ * Takes this share of the width the other columns leave (`flex: 2` gets twice what
556
+ * `flex: 1` gets), following the grid's width. Set `minWidth` when combined with `wrapText`.
557
+ * Ignored with `GridOptions.fitColumns`, which sizes every column.
558
+ */
551
559
  flex?: number;
552
560
  resizable?: boolean;
553
561
  movable?: boolean;
@@ -653,7 +661,12 @@ interface RowSelectionConfig<TData = Record<string, unknown>> {
653
661
  enableClickSelection?: boolean;
654
662
  isRowSelectable?: (node: RowNode<TData>) => boolean;
655
663
  }
656
- type GridThemeMode = 'auto' | 'light' | 'dark';
664
+ /**
665
+ * `'auto'` follows the operating system, `'light'` and `'dark'` force one, `'inherit'`
666
+ * follows the `color-scheme` of the page (or a wrapper element), for apps with their own
667
+ * light/dark switch.
668
+ */
669
+ type GridThemeMode = 'auto' | 'light' | 'dark' | 'inherit';
657
670
  type GridDensity = 'default' | 'compact' | 'comfortable';
658
671
  interface GridReadyEvent<TData = Record<string, unknown>> {
659
672
  api: GridApi<TData>;
@@ -862,6 +875,14 @@ interface GridOptions<TData = Record<string, unknown>> {
862
875
  top?: string;
863
876
  };
864
877
  rowHeight?: number;
878
+ /**
879
+ * The columns always fill the grid's width exactly, in proportion to their `width`: on
880
+ * first display, when the grid resizes, and when columns are shown, hidden or pinned. A
881
+ * column the user resizes keeps that width (also in `getState`), and the others fit around
882
+ * it; `resetState()` lets it fit again. Columns never go below their `minWidth`: when they
883
+ * do not fit, the grid scrolls sideways.
884
+ */
885
+ fitColumns?: boolean;
865
886
  /**
866
887
  * Height in px of one body row, from its data, for rows whose height is known before they
867
888
  * render. Return `undefined` for `rowHeight`. With `autoHeight` columns it is the row's
@@ -1078,6 +1099,15 @@ interface HeaderGroupRowZones {
1078
1099
  declare class ColumnController<TData = Record<string, unknown>> {
1079
1100
  private columns;
1080
1101
  private manualWidths;
1102
+ /** `fitColumns`: the columns fill the width, except the ones in `userWidths`. */
1103
+ private fitColumns;
1104
+ /**
1105
+ * Columns the user (or `setColumnWidth`) resized: `fitColumns` keeps their width and fits
1106
+ * the others around them. Cleared by resetUserState().
1107
+ */
1108
+ private userWidths;
1109
+ /** Each column's width as defined, before user changes: the weights of `fitColumns`. */
1110
+ private baseWidths;
1081
1111
  private viewportWidth;
1082
1112
  private responsivePinning;
1083
1113
  private pinningCollapsed;
@@ -1163,6 +1193,13 @@ declare class ColumnController<TData = Record<string, unknown>> {
1163
1193
  widthsChanged: boolean;
1164
1194
  zonesChanged: boolean;
1165
1195
  };
1196
+ setFitColumns(fit: boolean): void;
1197
+ /**
1198
+ * Sizes the columns that follow the width: with `fitColumns` all of them except the ones
1199
+ * the user resized, in proportion to their defined widths; otherwise the `flex` columns,
1200
+ * in proportion to their `flex`. Both fill `availableWidth` exactly where the min and max
1201
+ * widths allow. Returns whether a width changed.
1202
+ */
1166
1203
  applyFlexWidths(availableWidth: number): boolean;
1167
1204
  setColumnWidth(colId: string, requestedWidth: number): number | null;
1168
1205
  setColumnPinned(colId: string, pinned: PinnedDirection): boolean;
package/dist/index.js CHANGED
@@ -22,9 +22,56 @@ function humanizeField(field) {
22
22
  if (!field) return "";
23
23
  return field.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_.-]+/g, " ").replace(/\b\w/g, (ch) => ch.toUpperCase()).trim();
24
24
  }
25
+ function distributeWidths(cols, total) {
26
+ const out = new Array(cols.length).fill(0);
27
+ let free = cols.map((_, i) => i);
28
+ let space = total;
29
+ for (; ; ) {
30
+ const weight = free.reduce((sum, i) => sum + Math.max(0, cols[i].weight), 0);
31
+ const share = (i) => weight > 0 ? Math.max(0, cols[i].weight) / weight * space : space / free.length;
32
+ const clamped = free.filter((i) => {
33
+ const ideal = share(i);
34
+ return ideal < cols[i].min || ideal > cols[i].max;
35
+ });
36
+ if (clamped.length === 0) break;
37
+ for (const i of clamped) {
38
+ out[i] = share(i) < cols[i].min ? cols[i].min : cols[i].max;
39
+ space -= out[i];
40
+ }
41
+ free = free.filter((i) => !clamped.includes(i));
42
+ if (free.length === 0) return out;
43
+ }
44
+ const exact = free.map((i) => {
45
+ const weight = free.reduce((sum, j) => sum + Math.max(0, cols[j].weight), 0);
46
+ return weight > 0 ? Math.max(0, cols[i].weight) / weight * space : space / free.length;
47
+ });
48
+ const floors = exact.map((x) => Math.floor(x + 1e-6));
49
+ let leftover = Math.round(space - floors.reduce((a, b) => a + b, 0));
50
+ const byRemainder = exact.map((x, k) => ({ k, rest: x - floors[k] })).sort((a, b) => b.rest - a.rest || a.k - b.k);
51
+ for (const { k } of byRemainder) {
52
+ if (leftover <= 0) break;
53
+ const i = free[k];
54
+ if (floors[k] + 1 > cols[i].max) continue;
55
+ floors[k]++;
56
+ leftover--;
57
+ }
58
+ free.forEach((i, k) => {
59
+ out[i] = floors[k];
60
+ });
61
+ return out;
62
+ }
25
63
  var ColumnController = class {
26
64
  columns = [];
27
65
  manualWidths = /* @__PURE__ */ new Set();
66
+ /** `fitColumns`: the columns fill the width, except the ones in `userWidths`. */
67
+ fitColumns = false;
68
+ /**
69
+ * Columns the user (or `setColumnWidth`) resized: `fitColumns` keeps their width and fits
70
+ * the others around them. Cleared by resetUserState().
71
+ */
72
+ userWidths = /* @__PURE__ */ new Set();
73
+ /** Each column's width as defined, before user changes: the weights of `fitColumns`. */
74
+ baseWidths = /* @__PURE__ */ new Map();
28
75
  viewportWidth = 0;
29
76
  responsivePinning = {
30
77
  enabled: true,
@@ -57,6 +104,7 @@ var ColumnController = class {
57
104
  /** Forget user column changes; the next setColumnDefs uses the definitions as written. */
58
105
  resetUserState() {
59
106
  this.userOverrides.clear();
107
+ this.userWidths.clear();
60
108
  this.userOrder = null;
61
109
  this.userRowGroup = null;
62
110
  }
@@ -290,6 +338,7 @@ var ColumnController = class {
290
338
  }
291
339
  this.declaredRowGroup = declared;
292
340
  this.columns = resolved;
341
+ this.baseWidths = new Map(resolved.map((c) => [c.colId, c.width]));
293
342
  this.manualWidths.clear();
294
343
  this.applyUserState();
295
344
  this.refreshRowGroups(false);
@@ -448,25 +497,37 @@ var ColumnController = class {
448
497
  const zonesChanged = this.updatePinningCollapse();
449
498
  return { widthsChanged, zonesChanged };
450
499
  }
500
+ setFitColumns(fit) {
501
+ this.fitColumns = fit;
502
+ }
503
+ /**
504
+ * Sizes the columns that follow the width: with `fitColumns` all of them except the ones
505
+ * the user resized, in proportion to their defined widths; otherwise the `flex` columns,
506
+ * in proportion to their `flex`. Both fill `availableWidth` exactly where the min and max
507
+ * widths allow. Returns whether a width changed.
508
+ */
451
509
  applyFlexWidths(availableWidth) {
510
+ if (availableWidth <= 0) return false;
452
511
  const visible = this.getVisibleColumns();
453
- const flexCols = visible.filter((c) => (c.flex ?? 0) > 0 && !this.manualWidths.has(c.colId));
454
- if (flexCols.length === 0 || availableWidth <= 0) {
455
- return false;
456
- }
457
- const fixedWidth = visible.filter((c) => !flexCols.includes(c)).reduce((sum, c) => sum + c.width, 0);
512
+ const sized = this.fitColumns ? visible.filter((c) => !this.isSystemColumn(c) && !this.userWidths.has(c.colId)) : visible.filter((c) => (c.flex ?? 0) > 0 && !this.manualWidths.has(c.colId));
513
+ if (sized.length === 0) return false;
514
+ const fixedWidth = visible.filter((c) => !sized.includes(c)).reduce((sum, c) => sum + c.width, 0);
458
515
  const remaining = Math.max(0, availableWidth - fixedWidth);
459
- const totalFlex = flexCols.reduce((sum, c) => sum + (c.flex ?? 0), 0);
460
- if (totalFlex <= 0) return false;
516
+ const widths = distributeWidths(
517
+ sized.map((c) => ({
518
+ weight: this.fitColumns ? this.baseWidths.get(c.colId) ?? c.width : c.flex ?? 0,
519
+ min: c.minWidth,
520
+ max: c.maxWidth
521
+ })),
522
+ remaining
523
+ );
461
524
  let changed = false;
462
- for (const col of flexCols) {
463
- const share = Math.floor(remaining * (col.flex ?? 1) / totalFlex);
464
- const clamped = Math.min(col.maxWidth, Math.max(col.minWidth, share));
465
- if (col.width !== clamped) {
466
- col.width = clamped;
525
+ sized.forEach((col, i) => {
526
+ if (col.width !== widths[i]) {
527
+ col.width = widths[i];
467
528
  changed = true;
468
529
  }
469
- }
530
+ });
470
531
  return changed;
471
532
  }
472
533
  setColumnWidth(colId, requestedWidth) {
@@ -475,7 +536,9 @@ var ColumnController = class {
475
536
  const clamped = Math.round(Math.min(col.maxWidth, Math.max(col.minWidth, requestedWidth)));
476
537
  col.width = clamped;
477
538
  this.manualWidths.add(colId);
539
+ this.userWidths.add(colId);
478
540
  this.setOverride(colId, { width: clamped });
541
+ if (this.fitColumns && this.viewportWidth > 0) this.applyFlexWidths(this.viewportWidth);
479
542
  return clamped;
480
543
  }
481
544
  setColumnPinned(colId, pinned) {
@@ -483,6 +546,7 @@ var ColumnController = class {
483
546
  if (!col || this.isSystemColumn(col) || col.pinned === pinned) return false;
484
547
  col.pinned = pinned;
485
548
  this.setOverride(colId, { pinned });
549
+ if (this.viewportWidth > 0) this.applyFlexWidths(this.viewportWidth);
486
550
  this.updatePinningCollapse();
487
551
  return true;
488
552
  }
@@ -536,15 +600,14 @@ var ColumnController = class {
536
600
  const targetSpace = Math.max(0, availableWidth - fixedWidth);
537
601
  const currentTotal = resizableCols.reduce((sum, c) => sum + c.width, 0);
538
602
  if (currentTotal <= 0) return false;
539
- let allocated = 0;
603
+ const widths = distributeWidths(
604
+ resizableCols.map((c) => ({ weight: c.width, min: c.minWidth, max: c.maxWidth })),
605
+ targetSpace
606
+ );
540
607
  resizableCols.forEach((col, index) => {
541
- const isLast = index === resizableCols.length - 1;
542
- const ideal = isLast ? targetSpace - allocated : Math.round(col.width / currentTotal * targetSpace);
543
- const clamped = Math.min(col.maxWidth, Math.max(col.minWidth, ideal));
544
- col.width = clamped;
545
- allocated += clamped;
608
+ col.width = widths[index];
546
609
  this.manualWidths.add(col.colId);
547
- this.setOverride(col.colId, { width: clamped });
610
+ this.setOverride(col.colId, { width: col.width });
548
611
  });
549
612
  this.updatePinningCollapse();
550
613
  return true;
@@ -552,7 +615,8 @@ var ColumnController = class {
552
615
  getColumnState() {
553
616
  return this.columns.filter((c) => !this.isSystemColumn(c)).map((c) => ({
554
617
  colId: c.colId,
555
- width: c.width,
618
+ // fitColumns sizes the others again; only a width the user chose is state
619
+ ...this.fitColumns && !this.userWidths.has(c.colId) ? {} : { width: c.width },
556
620
  pinned: c.pinned,
557
621
  hide: c.hide
558
622
  }));
@@ -576,6 +640,7 @@ var ColumnController = class {
576
640
  if (typeof item.width === "number" && Number.isFinite(item.width)) {
577
641
  col.width = Math.round(Math.min(col.maxWidth, Math.max(col.minWidth, item.width)));
578
642
  this.manualWidths.add(col.colId);
643
+ if (this.fitColumns) this.userWidths.add(col.colId);
579
644
  this.setOverride(col.colId, { width: col.width });
580
645
  }
581
646
  ordered.push(col);
@@ -3255,6 +3320,18 @@ var DomRenderer = class {
3255
3320
  getViewportWidth() {
3256
3321
  return Math.floor(this.surfaceEl.clientWidth);
3257
3322
  }
3323
+ /**
3324
+ * Width all columns can take without a horizontal scrollbar: the visible width of the
3325
+ * unpinned area plus the pinned zones' columns. Unlike `getViewportWidth`, this leaves out
3326
+ * the pinned zones' borders and a vertical scrollbar.
3327
+ */
3328
+ getColumnFitWidth() {
3329
+ const center = this.bodyCenterEl.clientWidth;
3330
+ if (center <= 0) return this.getViewportWidth();
3331
+ const zones = this.callbacks.getColumnsByZone();
3332
+ const pinned = [...zones.left, ...zones.right].reduce((sum, c) => sum + c.width, 0);
3333
+ return Math.floor(center + pinned);
3334
+ }
3258
3335
  renderAll() {
3259
3336
  this.syncGridRole();
3260
3337
  this.renderToolbar();
@@ -7520,6 +7597,7 @@ var StramienGridCore = class {
7520
7597
  this.api = this.createApi();
7521
7598
  this.selectionController.setConfig(normalizeSelectionConfig(options.rowSelection));
7522
7599
  this.columnController.setResponsivePinning(options.responsivePinning);
7600
+ this.columnController.setFitColumns(Boolean(options.fitColumns));
7523
7601
  this.columnController.setColumnDefs(
7524
7602
  options.columnDefs,
7525
7603
  options.defaultColDef,
@@ -7682,7 +7760,8 @@ var StramienGridCore = class {
7682
7760
  onSizeColumnsToFit: () => this.api.sizeColumnsToFit(),
7683
7761
  onGoToPage: (page) => this.api.goToPage(page),
7684
7762
  onSetPageSize: (size) => this.api.setPageSize(size),
7685
- onViewportResize: (width) => {
7763
+ onViewportResize: () => {
7764
+ const width = this.renderer.getColumnFitWidth();
7686
7765
  const { widthsChanged, zonesChanged } = this.columnController.setViewportWidth(width);
7687
7766
  if (zonesChanged) {
7688
7767
  this.renderer.renderAll();
@@ -7692,7 +7771,7 @@ var StramienGridCore = class {
7692
7771
  }
7693
7772
  });
7694
7773
  this.applyAppearance();
7695
- const initialWidth = this.renderer.getViewportWidth();
7774
+ const initialWidth = this.renderer.getColumnFitWidth();
7696
7775
  if (initialWidth > 0) {
7697
7776
  this.columnController.setViewportWidth(initialWidth);
7698
7777
  }
@@ -8778,7 +8857,7 @@ var StramienGridCore = class {
8778
8857
  Boolean(this.options.masterDetail),
8779
8858
  this.options.columnTypes
8780
8859
  );
8781
- const w = this.renderer.getViewportWidth();
8860
+ const w = this.renderer.getColumnFitWidth();
8782
8861
  if (w > 0) this.columnController.setViewportWidth(w);
8783
8862
  this.recomputeAndRender();
8784
8863
  },
@@ -8795,6 +8874,7 @@ var StramienGridCore = class {
8795
8874
  const prevTheme = this.theme;
8796
8875
  const prevDensity = this.density;
8797
8876
  const prevRowHeight = this.options.rowHeight;
8877
+ const prevFitColumns = Boolean(this.options.fitColumns);
8798
8878
  const prevLoading = this.loading;
8799
8879
  const prevRowModelType = this.rowModel.getMode();
8800
8880
  const prevDatasource = this.serverDatasource;
@@ -8835,6 +8915,11 @@ var StramienGridCore = class {
8835
8915
  structuralChange = true;
8836
8916
  }
8837
8917
  }
8918
+ if ("fitColumns" in partial && Boolean(partial.fitColumns) !== prevFitColumns) {
8919
+ this.columnController.setFitColumns(Boolean(partial.fitColumns));
8920
+ this.columnController.applyFlexWidths(this.renderer.getColumnFitWidth());
8921
+ structuralChange = true;
8922
+ }
8838
8923
  if (partial.undoRedoCellEditingLimit !== void 0) {
8839
8924
  this.undoRedoStack.setLimit(partial.undoRedoCellEditingLimit);
8840
8925
  }
@@ -8868,7 +8953,7 @@ var StramienGridCore = class {
8868
8953
  Boolean(this.options.masterDetail),
8869
8954
  this.options.columnTypes
8870
8955
  );
8871
- const w = this.renderer.getViewportWidth();
8956
+ const w = this.renderer.getColumnFitWidth();
8872
8957
  if (w > 0) this.columnController.setViewportWidth(w);
8873
8958
  structuralChange = true;
8874
8959
  }
@@ -9198,7 +9283,7 @@ var StramienGridCore = class {
9198
9283
  this.emitStateChanged();
9199
9284
  },
9200
9285
  sizeColumnsToFit: () => {
9201
- const width = this.renderer.getViewportWidth();
9286
+ const width = this.renderer.getColumnFitWidth();
9202
9287
  const wasCollapsed = this.columnController.isPinningCollapsed();
9203
9288
  if (this.columnController.sizeColumnsToFit(width)) {
9204
9289
  if (this.columnController.isPinningCollapsed() !== wasCollapsed) {
@@ -9336,7 +9421,7 @@ var StramienGridCore = class {
9336
9421
  Boolean(this.options.masterDetail),
9337
9422
  this.options.columnTypes
9338
9423
  );
9339
- const w = this.renderer.getViewportWidth();
9424
+ const w = this.renderer.getColumnFitWidth();
9340
9425
  if (w > 0) this.columnController.setViewportWidth(w);
9341
9426
  this.sortModel = [];
9342
9427
  this.filterModel = {};