@one-grid-core/angular 0.5.2 → 0.5.3

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.
@@ -2,7 +2,7 @@ import { v4 } from 'uuid';
2
2
  import * as i0 from '@angular/core';
3
3
  import { Injectable, inject, NgZone, Pipe, Input, Component, EventEmitter, HostListener, ViewChildren, Output, ViewChild, ChangeDetectionStrategy, Directive, NgModule } from '@angular/core';
4
4
  import { BehaviorSubject, Subject, Subscription } from 'rxjs';
5
- import _ from 'lodash';
5
+ import lodash from 'lodash';
6
6
  import { filter, map, debounceTime, take } from 'rxjs/operators';
7
7
  import { NgClass, NgStyle, NgTemplateOutlet, DecimalPipe } from '@angular/common';
8
8
  import * as i2 from 'lucide-angular';
@@ -148,10 +148,20 @@ const ONE_GROUP_VALUE_FIELD = '__oneGroupValue';
148
148
  const ONE_GROUP_CHILDREN_FIELD = '__oneGroupChildren';
149
149
  /** What a group of rows with no value in the grouped field is labelled. */
150
150
  const ONE_GROUP_BLANK_LABEL = '(Blank)';
151
- /** The `rowGroup` fields of a column set, in definition order. */
151
+ /**
152
+ * The `rowGroup` fields of a column set, in definition order.
153
+ *
154
+ * Grouping keys on a row's own field, so a column that asks to group without
155
+ * one has nothing to group by — it is dropped, but not silently: that is a
156
+ * definition that reads as if it works.
157
+ */
152
158
  function rowGroupFields(columns) {
153
- return (columns ?? [])
154
- .filter(column => column.rowGroup && column.field)
159
+ const grouping = (columns ?? []).filter(column => column.rowGroup);
160
+ for (const column of grouping.filter(column => !column.field)) {
161
+ console.warn(`[one-grid] the column "${column.headerName ?? column.id ?? '(unnamed)'}" declares rowGroup but has no field — grouping reads the row's own value, so a valueGetter is not enough. The column was left ungrouped.`);
162
+ }
163
+ return grouping
164
+ .filter(column => column.field)
155
165
  .map(column => column.field);
156
166
  }
157
167
  /** The `aggFunc` columns of a column set. */
@@ -714,6 +724,16 @@ class OgColumnService {
714
724
  this.setGroupColumnDefs(groupColumnDefs);
715
725
  this.setColumnDefs(columnDefs);
716
726
  }
727
+ /**
728
+ * Every registered column, hidden ones included, in display order.
729
+ *
730
+ * `getColumnState` answers what is on screen; this answers what the grid
731
+ * knows about — which is what anything reading a column's *configuration*
732
+ * (an `aggFunc`, say) needs, since hiding a column does not unconfigure it.
733
+ */
734
+ getAllColumnDefs() {
735
+ return this.allColumnDefs;
736
+ }
717
737
  getColumnState() {
718
738
  const columns = this.columnDefs$.getValue();
719
739
  const groups = this.groupColumnDefs$.getValue();
@@ -728,7 +748,7 @@ class OgColumnService {
728
748
  const column = {
729
749
  ...this.defaultColumnDef,
730
750
  ...col,
731
- id: col?.field ? col.field : _.uniqueId('col_'),
751
+ id: col?.field ? col.field : lodash.uniqueId('col_'),
732
752
  };
733
753
  return this.resolveHeaderPresentation(column);
734
754
  });
@@ -823,7 +843,7 @@ class OgColumnService {
823
843
  * place relative to its neighbours.
824
844
  */
825
845
  reorderColumnDefs(fromIndex, toIndex) {
826
- const columns = _.clone(this.allColumnDefs);
846
+ const columns = lodash.clone(this.allColumnDefs);
827
847
  const visible = columns.filter(column => !column.hide);
828
848
  const moved = visible[fromIndex];
829
849
  if (!moved)
@@ -1425,12 +1445,26 @@ class OneGridApi {
1425
1445
  this._quickFilterValue = null;
1426
1446
  this._activeFilters = [];
1427
1447
  this._visibleRowNodes = [];
1448
+ /**
1449
+ * The rows as the host last handed them over — before grouping folded
1450
+ * them. Grouping is a transform on the way in, so this is what a change of
1451
+ * grouping re-folds, and it is why regrouping keeps the host's row order
1452
+ * instead of inheriting the order the previous grouping happened to leave.
1453
+ */
1454
+ this._sourceRows = [];
1428
1455
  this._pinnedRowNodes = { top: [], bottom: [] };
1429
1456
  this._originalRowNodes = new Map();
1430
1457
  this._nodeCleanupMap = new Map(); // Cleanup registry (prevents memory leaks)
1431
1458
  this._destroyed = false;
1432
1459
  // Advanced Features
1433
1460
  this.treeData = false;
1461
+ /**
1462
+ * Whether the *host* declared tree data, as opposed to grouping having
1463
+ * turned tree mode on. `treeData` above answers "is this a tree right now",
1464
+ * which grouping flips; this answers "whose tree is it", which decides
1465
+ * whether grouping may touch it at all.
1466
+ */
1467
+ this._hostTreeData = false;
1434
1468
  this.treeDataChildrenField = null;
1435
1469
  this.groupDefaultExpanded = 0;
1436
1470
  this.masterDetail = false;
@@ -1472,6 +1506,7 @@ class OneGridApi {
1472
1506
  this.detailCellRendererParams = detailCellRendererParams;
1473
1507
  this.detailRendererComponent = detailRendererComponent;
1474
1508
  this.treeData = treeData;
1509
+ this._hostTreeData = treeData;
1475
1510
  this.selectionService.setSelectionMode(rowSelection);
1476
1511
  // Synthetic group rows carry their own stable id — the path of group
1477
1512
  // values — which no host getRowId knows how to derive. The wrapper reads
@@ -1555,6 +1590,9 @@ class OneGridApi {
1555
1590
  }
1556
1591
  return next;
1557
1592
  });
1593
+ // Grouped roots are synthetic, so the data is on the leaves; otherwise the
1594
+ // roots are the data. Read from the nodes rather than from `_sourceRows`
1595
+ // so a reorder the user dragged into place survives the transaction.
1558
1596
  const currentRows = grouping
1559
1597
  ? this._rowNodes.filter(node => node.leaf).map(node => node.data)
1560
1598
  : this._rowNodes.filter(node => !node.parent).map(node => node.data);
@@ -1576,6 +1614,7 @@ class OneGridApi {
1576
1614
  this.selectionService.deselectAll();
1577
1615
  this.expansionService.clear();
1578
1616
  }
1617
+ this._sourceRows = rowData ?? [];
1579
1618
  // Row grouping is a transform on the way in: the host keeps handing the
1580
1619
  // grid flat rows, and the tree pipeline below sees synthetic group rows
1581
1620
  // with children — the same shape tree data arrives in.
@@ -1627,6 +1666,14 @@ class OneGridApi {
1627
1666
  if (node) {
1628
1667
  node.data = data;
1629
1668
  node.setRowIndex(this._currentIndex);
1669
+ // Where a row sits is rebuilt, not remembered. A reused node used to
1670
+ // keep the level and parent it had when it was first built, so a row
1671
+ // that changed place in the hierarchy — a leaf moved to another
1672
+ // parent, or lifted to the root when the grouping changed — kept
1673
+ // pointing at its old parent, and the visibility walk (which climbs
1674
+ // parents) hid it behind a node that was no longer above it.
1675
+ node.level = level;
1676
+ node.parent = parent;
1630
1677
  }
1631
1678
  else {
1632
1679
  node = new OneRowNode(rowId, data, this._currentIndex);
@@ -2256,14 +2303,56 @@ class OneGridApi {
2256
2303
  return this._rowGroupFields;
2257
2304
  }
2258
2305
  /**
2259
- * Turn row grouping on. Called by `OneGridComponent` when column
2260
- * definitions declare `rowGroup`; the grid then behaves as a tree over
2261
- * synthetic group rows, and `setRowData` / `updateRowData` keep taking the
2262
- * flat rows they always took.
2306
+ * Group the rows by these fields, in nesting order — or ungroup by passing
2307
+ * an empty array.
2308
+ *
2309
+ * `OneGridComponent` calls this once for the column definitions that
2310
+ * declare `rowGroup`; hosts call it to regroup afterwards. Either way the
2311
+ * grid re-plumbs itself around the new grouping — tree mode, the auto group
2312
+ * column, the visibility of the grouped-by columns — and folds the rows it
2313
+ * already holds again, so nothing has to be rebound and no grid has to be
2314
+ * torn down and re-created to group by a different column.
2315
+ *
2316
+ * `aggregations` defaults to the `aggFunc` columns currently registered, so
2317
+ * `setRowGrouping(['country'])` keeps the totals the column definitions
2318
+ * already describe.
2263
2319
  */
2264
- setRowGrouping(groupFields, aggregations = []) {
2265
- this._rowGroupFields = groupFields ?? [];
2266
- this._aggColumns = aggregations ?? [];
2320
+ setRowGrouping(groupFields, aggregations) {
2321
+ if (this._destroyed)
2322
+ return;
2323
+ const previous = this._rowGroupFields;
2324
+ // Empty fields buy nothing and a repeated one would nest a level under
2325
+ // itself, where every child group holds the whole parent.
2326
+ const next = (groupFields ?? [])
2327
+ .filter(field => !!field)
2328
+ .filter((field, index, fields) => fields.indexOf(field) === index);
2329
+ // Grouping *is* the tree. A host that brought its own tree data, or a
2330
+ // master/detail grid, already has a hierarchy, and folding a second one
2331
+ // over it would throw the first away.
2332
+ if (next.length && (this._hostTreeData || this.masterDetail)) {
2333
+ console.warn('[one-grid] setRowGrouping does not apply to tree data or master/detail grids — they carry their own hierarchy. Nothing was changed.');
2334
+ return;
2335
+ }
2336
+ const unchanged = next.length === previous.length && next.every((field, index) => field === previous[index]);
2337
+ if (unchanged && !aggregations)
2338
+ return;
2339
+ this._rowGroupFields = next;
2340
+ this._aggColumns = aggregations ?? aggColumns(this.columnService.getAllColumnDefs());
2341
+ // The row model's half of the switch: grouped rows carry their children
2342
+ // under the grouping helper's own field, and the tree pipeline walks them
2343
+ // from there. Ungrouping puts both back — which is safe because the guard
2344
+ // above means tree mode can only have come from grouping.
2345
+ this.treeData = next.length > 0;
2346
+ this.treeDataChildrenField = next.length ? ONE_GROUP_CHILDREN_FIELD : null;
2347
+ // Everything grouping implies outside the row model — the auto group
2348
+ // column, which of the host's columns are hidden, the grid type the body
2349
+ // renders — belongs to the component that owns the template. It re-plumbs
2350
+ // on this event, synchronously, so the rebuild below already sees the
2351
+ // columns the new grouping needs.
2352
+ this.eventService.dispatchEvent('rowGroupingChanged', { groupFields: next, previousGroupFields: previous });
2353
+ // Nothing to fold before the first load: `setRowData` will do it.
2354
+ if (this._rowNodes.length)
2355
+ this.rebuildRows(this._sourceRows, false);
2267
2356
  }
2268
2357
  /**
2269
2358
  * Start recording committed cell edits for undo/redo. Called by
@@ -6106,6 +6195,20 @@ class OneGridComponent {
6106
6195
  this.bodyScrollLeft = 0;
6107
6196
  this._oneRowNumberColumnDef = null;
6108
6197
  this.subscriptions = new Subscription();
6198
+ /**
6199
+ * Row grouping, as the grid sees it.
6200
+ *
6201
+ * The three `_host*` fields hold what was bound before grouping touched
6202
+ * anything, because grouping is reversible: turn it off and the columns,
6203
+ * the group column and the grid type all have to go back to what the host
6204
+ * asked for. `_appliedGroupFields` is the grouping the registered columns
6205
+ * currently reflect — the row model's copy lives in `api.rowGroupFields`.
6206
+ */
6207
+ this._hostColumnDefs = [];
6208
+ this._hostAutoGroupColumnDef = null;
6209
+ this._hostGridType = 'master';
6210
+ this._hostTreeData = false;
6211
+ this._appliedGroupFields = [];
6109
6212
  /**
6110
6213
  * Totals for `aria-rowcount` / `aria-colcount`.
6111
6214
  *
@@ -6164,29 +6267,25 @@ class OneGridComponent {
6164
6267
  * tree over synthetic group rows. Configured before `api.init` because
6165
6268
  * everything downstream — the treeData flag the API captures, the group
6166
6269
  * column the tree block below prepends — has to see the tree shape.
6270
+ *
6271
+ * What the host declared is kept as it was given: regrouping at runtime
6272
+ * re-derives the registered columns from it, so a column hidden because
6273
+ * it was grouped comes back when it stops being, and one the host hid
6274
+ * itself stays hidden.
6167
6275
  */
6276
+ this._hostColumnDefs = this.columnDefs ?? [];
6277
+ this._hostAutoGroupColumnDef = this.autoGroupColumnDef;
6278
+ this._hostGridType = this.gridType;
6279
+ this._hostTreeData = this.treeData;
6168
6280
  const rowGroupColumnFields = this.treeData || this.masterDetail
6169
6281
  ? []
6170
6282
  : rowGroupFields(this.columnDefs);
6171
6283
  if (rowGroupColumnFields.length) {
6284
+ this._appliedGroupFields = rowGroupColumnFields;
6172
6285
  this.treeData = true;
6173
6286
  this.treeDataChildrenField = ONE_GROUP_CHILDREN_FIELD;
6174
- // The group column: host-tuned through autoGroupColumnDef, but its
6175
- // field is always the synthetic label — that is where a group row
6176
- // carries its display value.
6177
- this.autoGroupColumnDef = {
6178
- headerName: 'Group',
6179
- minWidth: 200,
6180
- ...this.autoGroupColumnDef,
6181
- field: ONE_GROUP_VALUE_FIELD,
6182
- // The marker the tree machinery keys on: the expander renders on this
6183
- // column and group rows print their label here.
6184
- autoGroupField: true,
6185
- };
6186
- // A grouped-by column's value is on the group row; showing the flat
6187
- // column too repeats it down every leaf. Hidden, not removed — the
6188
- // host can bring it back with api.setColumnVisible.
6189
- this.columnDefs = this.columnDefs.map(column => column.rowGroup ? { ...column, hide: true } : column);
6287
+ this.autoGroupColumnDef = this.buildAutoGroupColumnDef();
6288
+ this.columnDefs = this.groupedColumnDefs(rowGroupColumnFields);
6190
6289
  }
6191
6290
  /**
6192
6291
  * Initialize One Grid API
@@ -6201,11 +6300,21 @@ class OneGridComponent {
6201
6300
  masterDetail: this.masterDetail,
6202
6301
  detailCellRendererParams: this.detailCellRendererParams,
6203
6302
  detailRendererComponent: this.detailRendererComponent,
6204
- treeData: this.treeData,
6303
+ // The host's own tree data, not the tree grouping builds: `setRowGrouping`
6304
+ // turns that on below, and the API has to be able to tell whose tree it
6305
+ // is before it may replace one.
6306
+ treeData: this._hostTreeData,
6205
6307
  });
6206
6308
  if (rowGroupColumnFields.length) {
6207
6309
  this.api.setRowGrouping(rowGroupColumnFields, aggColumns(this.columnDefs));
6208
6310
  }
6311
+ // Regrouping at runtime. `api.setRowGrouping` owns the row model half of
6312
+ // the switch; this is the other half — the columns the grid registers and
6313
+ // the shape the body renders. Subscribed after the initial call above,
6314
+ // which `ngOnInit` has already applied.
6315
+ this.subscriptions.add(this.eventService.addEventListener('rowGroupingChanged', (payload) => {
6316
+ this.applyRowGroupingColumns(payload.groupFields);
6317
+ }));
6209
6318
  if (this.undoRedoCellEditing) {
6210
6319
  this.api.enableUndoRedoCellEditing(this.undoRedoCellEditingLimit);
6211
6320
  }
@@ -6219,7 +6328,7 @@ class OneGridComponent {
6219
6328
  this.api.setGroupDefaultExpanded(this.groupDefaultExpanded);
6220
6329
  this.api.setGetDetailRowData(this.detailCellRendererParams?.getDetailRowData ?? null);
6221
6330
  if (this.rowNumber) {
6222
- const columnDefs = _.concat(this._oneRowNumberColumnDef, this.columnDefs);
6331
+ const columnDefs = lodash.concat(this._oneRowNumberColumnDef, this.columnDefs);
6223
6332
  setTimeout(() => this.api.setColumnDefs(columnDefs));
6224
6333
  }
6225
6334
  }
@@ -6228,10 +6337,9 @@ class OneGridComponent {
6228
6337
  */
6229
6338
  if (this.treeData) {
6230
6339
  this.gridType = 'tree';
6231
- const columnDefs = this.rowNumber ? _.concat(this._oneRowNumberColumnDef, this.autoGroupColumnDef, this.columnDefs) : _.concat(this.autoGroupColumnDef, this.columnDefs);
6232
6340
  this.api.setTreeDataChildrenField(this.treeDataChildrenField);
6233
6341
  this.api.setGroupDefaultExpanded(this.groupDefaultExpanded);
6234
- setTimeout(() => this.api.setColumnDefs(columnDefs));
6342
+ setTimeout(() => this.api.setColumnDefs(this.columnStack()));
6235
6343
  }
6236
6344
  // Listen Selection Changed Event
6237
6345
  // Columns can be replaced at runtime through `api.setColumnDefs`, and
@@ -6257,6 +6365,69 @@ class OneGridComponent {
6257
6365
  this.columnResized.emit(payload);
6258
6366
  }));
6259
6367
  }
6368
+ /**
6369
+ * The group column: host-tuned through `autoGroupColumnDef`, but its field
6370
+ * is always the synthetic label — that is where a group row carries its
6371
+ * display value — and it always carries the marker the tree machinery keys
6372
+ * on, which is what puts the expander on this column.
6373
+ */
6374
+ buildAutoGroupColumnDef() {
6375
+ return {
6376
+ headerName: 'Group',
6377
+ minWidth: 200,
6378
+ ...this._hostAutoGroupColumnDef,
6379
+ field: ONE_GROUP_VALUE_FIELD,
6380
+ autoGroupField: true,
6381
+ };
6382
+ }
6383
+ /**
6384
+ * The host's columns with the grouped-by ones hidden.
6385
+ *
6386
+ * A grouped-by column's value is on the group row; showing the flat column
6387
+ * too repeats it down every leaf. Hidden, not removed — the host can bring
6388
+ * one back with `api.setColumnVisible`, and ungrouping restores whatever
6389
+ * visibility the host itself declared.
6390
+ */
6391
+ groupedColumnDefs(groupFields) {
6392
+ return this._hostColumnDefs.map(column => column.field && groupFields.includes(column.field) ? { ...column, hide: true } : column);
6393
+ }
6394
+ /** The columns the grid registers: row numbers, the group column, the host's. */
6395
+ columnStack() {
6396
+ return lodash.compact([
6397
+ this.rowNumber ? this._oneRowNumberColumnDef : null,
6398
+ this.treeData ? this.autoGroupColumnDef : null,
6399
+ ...this.columnDefs,
6400
+ ]);
6401
+ }
6402
+ /**
6403
+ * Re-plumb everything grouping owns outside the row model: tree mode, the
6404
+ * children field, the group column, and which of the host's columns are
6405
+ * hidden because their value now shows on a group row.
6406
+ *
6407
+ * Driven by `rowGroupingChanged`, so a host regroups through one call —
6408
+ * `api.setRowGrouping(['country', 'city'])` — instead of re-creating the
6409
+ * grid. Grouping and host tree data are mutually exclusive, which the API
6410
+ * enforces, so this only ever switches a tree that grouping itself built.
6411
+ */
6412
+ applyRowGroupingColumns(groupFields) {
6413
+ const unchanged = groupFields.length === this._appliedGroupFields.length
6414
+ && groupFields.every((field, index) => field === this._appliedGroupFields[index]);
6415
+ if (unchanged)
6416
+ return;
6417
+ const grouping = groupFields.length > 0;
6418
+ this._appliedGroupFields = [...groupFields];
6419
+ this.treeData = grouping;
6420
+ this.treeDataChildrenField = grouping ? ONE_GROUP_CHILDREN_FIELD : null;
6421
+ this.gridType = grouping ? 'tree' : this._hostGridType;
6422
+ this.autoGroupColumnDef = grouping ? this.buildAutoGroupColumnDef() : this._hostAutoGroupColumnDef;
6423
+ this.columnDefs = this.groupedColumnDefs(groupFields);
6424
+ this.api.setGroupDefaultExpanded(this.groupDefaultExpanded);
6425
+ this.api.setColumnDefs(this.columnStack());
6426
+ // The grid type, the tree flag and the group column are all rendered
6427
+ // through bindings, and this runs from an event rather than from a
6428
+ // template callback.
6429
+ this.cdr.markForCheck();
6430
+ }
6260
6431
  ngAfterViewInit() {
6261
6432
  // The header follows the body through (bodyScroll) -> [scrollLeft]. A second
6262
6433
  // subscription used to live here that queried '.one-grid-header-column' — a