@vuu-ui/vuu-utils 2.1.19-beta.1 → 2.1.19-beta.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.
Files changed (82) hide show
  1. package/package.json +8 -9
  2. package/src/Clock.js +43 -0
  3. package/src/PageVisibilityObserver.js +42 -0
  4. package/src/ScaledDecimal.js +32 -0
  5. package/src/ShellContext.js +5 -0
  6. package/src/ThemeProvider.js +56 -0
  7. package/src/array-utils.js +51 -0
  8. package/src/box-utils.js +51 -0
  9. package/src/broadcast-channel.js +0 -0
  10. package/src/column-utils.js +790 -0
  11. package/src/common-types.js +11 -0
  12. package/src/component-registry.js +83 -0
  13. package/src/context-definitions/DataContext.js +15 -0
  14. package/src/context-definitions/DataProvider.js +13 -0
  15. package/src/context-definitions/DataSourceProvider.js +18 -0
  16. package/src/context-definitions/WorkspaceContext.js +14 -0
  17. package/src/cookie-utils.js +15 -0
  18. package/src/css-utils.js +5 -0
  19. package/src/data-editing/DataEditingProvider.js +13 -0
  20. package/src/data-editing/EditButtons.js +32 -0
  21. package/src/data-editing/EditModeProvider.js +18 -0
  22. package/src/data-editing/EditSession.js +149 -0
  23. package/src/data-editing/edit-utils.js +37 -0
  24. package/src/data-editing/useEditableTable.js +76 -0
  25. package/src/data-utils.js +55 -0
  26. package/src/datasource/BaseDataSource.js +233 -0
  27. package/src/datasource/datasource-action-utils.js +6 -0
  28. package/src/datasource/datasource-filter-utils.js +27 -0
  29. package/src/datasource/datasource-utils.js +148 -0
  30. package/src/date/date-utils.js +80 -0
  31. package/src/date/dateTimePattern.js +17 -0
  32. package/src/date/formatter.js +101 -0
  33. package/src/date/index.js +4 -0
  34. package/src/date/types.js +27 -0
  35. package/src/debug-utils.js +25 -0
  36. package/src/event-emitter.js +78 -0
  37. package/src/feature-utils.js +86 -0
  38. package/src/filters/filter-utils.js +136 -0
  39. package/src/filters/filterAsQuery.js +70 -0
  40. package/src/filters/index.js +2 -0
  41. package/src/form-utils.js +69 -0
  42. package/src/formatting-utils.js +42 -0
  43. package/src/getUniqueId.js +2 -0
  44. package/src/group-utils.js +16 -0
  45. package/src/html-utils.js +108 -0
  46. package/src/index.js +77 -0
  47. package/src/input-utils.js +3 -0
  48. package/src/invariant.js +9 -0
  49. package/src/itemToString.js +9 -0
  50. package/src/json-types.js +0 -0
  51. package/src/json-utils.js +108 -0
  52. package/src/keyboard-utils.js +14 -0
  53. package/src/keyset.js +57 -0
  54. package/src/layout-types.js +0 -0
  55. package/src/list-utils.js +5 -0
  56. package/src/local-storage-utils.js +23 -0
  57. package/src/logging-utils.js +52 -0
  58. package/src/module-utils.js +2 -0
  59. package/src/nanoid/index.js +13 -0
  60. package/src/perf-utils.js +28 -0
  61. package/src/promise-utils.js +26 -0
  62. package/src/protocol-message-utils.js +68 -0
  63. package/src/range-utils.js +109 -0
  64. package/src/react-utils.js +64 -0
  65. package/src/round-decimal.js +83 -0
  66. package/src/row-utils.js +43 -0
  67. package/src/selection-utils.js +25 -0
  68. package/src/shell-layout-types.js +9 -0
  69. package/src/sort-utils.js +75 -0
  70. package/src/table-schema-utils.js +11 -0
  71. package/src/text-utils.js +13 -0
  72. package/src/theme-utils.js +21 -0
  73. package/src/tree-types.js +0 -0
  74. package/src/tree-utils.js +96 -0
  75. package/src/ts-utils.js +6 -0
  76. package/src/typeahead-utils.js +4 -0
  77. package/src/url-utils.js +12 -0
  78. package/src/useId.js +6 -0
  79. package/src/useLayoutEffectSkipFirst.js +9 -0
  80. package/src/useResizeObserver.js +122 -0
  81. package/src/useStateRef.js +21 -0
  82. package/src/user-types.js +0 -0
@@ -0,0 +1,790 @@
1
+ import { moveItem } from "./array-utils.js";
2
+ import { queryClosest } from "./html-utils.js";
3
+ const SORT_ASC = "asc";
4
+ const NO_HEADINGS = [];
5
+ const DEFAULT_COL_WIDTH = 100;
6
+ const DEFAULT_MAX_WIDTH = 250;
7
+ const DEFAULT_MIN_WIDTH = 50;
8
+ const AggregationType = {
9
+ Average: 2,
10
+ Count: 3,
11
+ Distinct: 6,
12
+ Sum: 1,
13
+ High: 4,
14
+ Low: 5
15
+ };
16
+ function mapSortCriteria(sortCriteria, columnMap, metadataOffset = 0) {
17
+ return sortCriteria.map((s)=>{
18
+ if ("string" == typeof s) return [
19
+ columnMap[s] + metadataOffset,
20
+ "asc"
21
+ ];
22
+ if (Array.isArray(s)) {
23
+ const [columnName, sortDir] = s;
24
+ return [
25
+ columnMap[columnName] + metadataOffset,
26
+ sortDir || SORT_ASC
27
+ ];
28
+ }
29
+ throw Error("columnUtils.mapSortCriteria invalid input");
30
+ });
31
+ }
32
+ const numericTypes = [
33
+ "int",
34
+ "long",
35
+ "double"
36
+ ];
37
+ const getDefaultAlignment = (serverDataType)=>void 0 === serverDataType ? "left" : numericTypes.includes(serverDataType) ? "right" : "left";
38
+ const getRuntimeColumnWidth = (col, runtimeColumns)=>{
39
+ const runtimeColumn = runtimeColumns.find(({ name })=>name === col.name);
40
+ if (runtimeColumn) return runtimeColumn.width;
41
+ return DEFAULT_COL_WIDTH;
42
+ };
43
+ const applyRuntimeColumnWidthsToConfig = (tableConfig, columns)=>({
44
+ ...tableConfig,
45
+ columns: columns.map((column)=>({
46
+ ...column,
47
+ width: column.width ?? getRuntimeColumnWidth(column, columns)
48
+ })),
49
+ columnLayout: "manual"
50
+ });
51
+ const isValidColumnAlignment = (v)=>"left" === v || "right" === v;
52
+ const isValidPinLocation = (v)=>isValidColumnAlignment(v) || "floating" === v || "" === v;
53
+ const VUU_COLUMN_DATA_TYPES = [
54
+ "long",
55
+ "double",
56
+ "int",
57
+ "string",
58
+ "char",
59
+ "boolean",
60
+ "scaleddecimal2",
61
+ "scaleddecimal4",
62
+ "scaleddecimal6",
63
+ "scaleddecimal8",
64
+ "epochtimestamp"
65
+ ];
66
+ const isVuuColumnDataType = (value)=>VUU_COLUMN_DATA_TYPES.includes(value);
67
+ const fromServerDataType = (serverDataType)=>{
68
+ switch(serverDataType){
69
+ case "double":
70
+ case "int":
71
+ case "long":
72
+ return "number";
73
+ case "boolean":
74
+ return "boolean";
75
+ default:
76
+ return "string";
77
+ }
78
+ };
79
+ const isTimestampColumn = ({ serverDataType })=>"epochtimestamp" === serverDataType;
80
+ const isNumericColumn = ({ serverDataType, type })=>{
81
+ if ("int" === serverDataType || "long" === serverDataType || "double" === serverDataType) return true;
82
+ if ("string" == typeof type) return "number" === type;
83
+ if ("string" == typeof type?.name) return type?.name === "number";
84
+ return false;
85
+ };
86
+ const isDateTimeDataType = (column)=>(isTypeDescriptor(column.type) ? column.type.name : column.type) === "date/time";
87
+ const isTimeDataType = (column)=>(isTypeDescriptor(column.type) ? column.type.name : column.type) === "time";
88
+ const isDateTimeDataValue = (column, serverDataType)=>"epochtimestamp" === serverDataType || isTimestampColumn(column) || isDateTimeDataType(column);
89
+ const isTimeDataValue = (column)=>(isTypeDescriptor(column?.type) ? column.type.name : column?.type) === "time";
90
+ const isPinned = (column)=>"string" == typeof column.pin;
91
+ const hasHeadings = (column)=>Array.isArray(column.heading) && column.heading.length > 0;
92
+ const isResizing = (column)=>column.resizing;
93
+ const isTextColumn = ({ serverDataType })=>void 0 === serverDataType ? false : "char" === serverDataType || "string" === serverDataType;
94
+ const toColumnDescriptor = (name, serverDataType)=>({
95
+ name,
96
+ serverDataType
97
+ });
98
+ const isTypeDescriptor = (type)=>void 0 !== type && "string" != typeof type;
99
+ const EMPTY_COLUMN_MAP = {};
100
+ const isColumnTypeRenderer = (renderer)=>void 0 !== renderer?.name;
101
+ const hasCustomRenderer = (type)=>isTypeDescriptor(type) && isColumnTypeRenderer(type.renderer);
102
+ const isLookupRenderer = (renderer)=>void 0 !== renderer?.name && "lookup" in renderer;
103
+ const isValueListRenderer = (renderer)=>void 0 !== renderer?.name && Array.isArray(renderer.values);
104
+ const hasValidationRules = (type)=>isTypeDescriptor(type) && Array.isArray(type.rules) && type.rules.length > 0;
105
+ const isMappedValueTypeRenderer = (renderer)=>void 0 !== renderer && void 0 !== renderer?.map;
106
+ function buildColumnMap(columns) {
107
+ const start = metadataKeys.count;
108
+ if (columns) return columns.reduce((map, column, i)=>{
109
+ if ("string" == typeof column) map[column] = start + i;
110
+ else map[column.name] = start + i;
111
+ return map;
112
+ }, {});
113
+ return EMPTY_COLUMN_MAP;
114
+ }
115
+ function projectUpdates(updates) {
116
+ const results = [];
117
+ const metadataOffset = metadataKeys.count - 2;
118
+ for(let i = 0; i < updates.length; i += 3){
119
+ results[i] = updates[i] + metadataOffset;
120
+ results[i + 1] = updates[i + 1];
121
+ results[i + 2] = updates[i + 2];
122
+ }
123
+ return results;
124
+ }
125
+ const KEY = 6;
126
+ const metadataKeys = {
127
+ IDX: 0,
128
+ RENDER_IDX: 1,
129
+ IS_LEAF: 2,
130
+ IS_EXPANDED: 3,
131
+ DEPTH: 4,
132
+ COUNT: 5,
133
+ KEY: KEY,
134
+ SELECTED: 7,
135
+ TIMESTAMP: 8,
136
+ IS_NEW: 9,
137
+ count: 10,
138
+ PARENT_IDX: "parent_idx",
139
+ IDX_POINTER: "idx_pointer",
140
+ FILTER_COUNT: "filter_count",
141
+ NEXT_FILTER_IDX: "next_filter_idx"
142
+ };
143
+ const insertColumn = (columns, column)=>{
144
+ const { originalIdx } = column;
145
+ if ("number" == typeof originalIdx) for(let i = 0; i < columns.length; i++){
146
+ const { originalIdx: colIdx = -1 } = columns[i];
147
+ if (colIdx > originalIdx) {
148
+ columns.splice(i, 0, column);
149
+ return columns;
150
+ }
151
+ }
152
+ columns.push(column);
153
+ return columns;
154
+ };
155
+ const flattenColumnGroup = (columns)=>{
156
+ if (!columns[0]?.isGroup) return columns;
157
+ {
158
+ const [groupColumn, ...nonGroupedColumns] = columns;
159
+ groupColumn.columns.forEach((groupColumn)=>{
160
+ insertColumn(nonGroupedColumns, groupColumn);
161
+ });
162
+ return nonGroupedColumns;
163
+ }
164
+ };
165
+ function extractGroupColumn({ availableWidth, columns, groupBy, confirmed = true }) {
166
+ if (groupBy && groupBy.length > 0) {
167
+ const flattenedColumns = flattenColumnGroup(columns);
168
+ const [groupedColumns, rest] = flattenedColumns.reduce((result, column, i)=>{
169
+ const [g, r] = result;
170
+ if (groupBy.includes(column.name)) g.push({
171
+ ...column,
172
+ originalIdx: i
173
+ });
174
+ else r.push(column);
175
+ return result;
176
+ }, [
177
+ [],
178
+ []
179
+ ]);
180
+ if (groupedColumns.length !== groupBy.length) throw Error(`extractGroupColumn: no column definition found for all groupBy cols ${JSON.stringify(groupBy)} `);
181
+ const groupOnly = 0 === rest.length;
182
+ const groupCount = groupBy.length;
183
+ const groupCols = groupBy.map((name, idx)=>{
184
+ const column = groupedColumns.find((col)=>col.name === name);
185
+ return {
186
+ ...column,
187
+ groupLevel: groupCount - idx
188
+ };
189
+ });
190
+ const width = groupOnly ? availableWidth : Math.min(availableWidth, groupCols.map((c)=>c.width).reduce((a, b)=>a + b) + 100);
191
+ const groupCol = {
192
+ ariaColIndex: 1,
193
+ columns: groupCols,
194
+ heading: [
195
+ "group-col"
196
+ ],
197
+ isGroup: true,
198
+ groupConfirmed: confirmed,
199
+ name: "group-col",
200
+ width
201
+ };
202
+ const withAdjustedAriaIndex = [];
203
+ let colIndex = 2;
204
+ for (const column of rest){
205
+ withAdjustedAriaIndex.push({
206
+ ...column,
207
+ ariaColIndex: column.hidden ? -1 : colIndex
208
+ });
209
+ if (!column.hidden) colIndex += 1;
210
+ }
211
+ return [
212
+ groupCol,
213
+ withAdjustedAriaIndex
214
+ ];
215
+ }
216
+ return [
217
+ null,
218
+ flattenColumnGroup(columns)
219
+ ];
220
+ }
221
+ const isGroupColumn = (column)=>true === column.isGroup;
222
+ const checkConfirmationPending = (previousConfig)=>{
223
+ if (previousConfig) {
224
+ const [column] = previousConfig.columns;
225
+ if (void 0 !== column && isGroupColumn(column)) return column.groupConfirmed;
226
+ }
227
+ };
228
+ const isJsonAttribute = (value)=>"string" == typeof value && (value.endsWith("{") || value.endsWith("["));
229
+ const isJsonGroup = (column, dataRow)=>column.type?.name === "json" && isJsonAttribute(dataRow[column.name]);
230
+ const isJsonColumn = (column)=>column.type?.name === "json";
231
+ const sortPinnedColumns = (columns, selectionBookendWidth = 0)=>{
232
+ const leftPinnedColumns = [];
233
+ const rightPinnedColumns = [];
234
+ const restColumns = [];
235
+ let pinnedWidthLeft = selectionBookendWidth;
236
+ for (const column of columns)switch(column.pin){
237
+ case "left":
238
+ leftPinnedColumns.push({
239
+ ...column,
240
+ pinnedWidth: 0,
241
+ pinnedOffset: pinnedWidthLeft
242
+ });
243
+ pinnedWidthLeft += column.width;
244
+ break;
245
+ case "right":
246
+ rightPinnedColumns.unshift(column);
247
+ break;
248
+ default:
249
+ restColumns.push(column);
250
+ }
251
+ if (leftPinnedColumns.length) leftPinnedColumns.push({
252
+ ...leftPinnedColumns.pop(),
253
+ pinnedWidth: pinnedWidthLeft
254
+ });
255
+ let allColumns = leftPinnedColumns.length ? leftPinnedColumns.concat(restColumns) : restColumns;
256
+ if (rightPinnedColumns.length) {
257
+ const measuredRightPinnedColumns = [];
258
+ let pinnedWidthRight = selectionBookendWidth;
259
+ for (const column of rightPinnedColumns){
260
+ measuredRightPinnedColumns.unshift({
261
+ ...column,
262
+ pinnedOffset: pinnedWidthRight
263
+ });
264
+ pinnedWidthRight += column.width;
265
+ }
266
+ measuredRightPinnedColumns[rightPinnedColumns.length - 1].pinnedWidth = pinnedWidthRight;
267
+ allColumns = allColumns.concat(measuredRightPinnedColumns);
268
+ }
269
+ if (leftPinnedColumns.length || rightPinnedColumns.length) {
270
+ for(let i = 0; i < allColumns.length; i++)if (allColumns[i].ariaColIndex !== i + 1) allColumns[i] = {
271
+ ...allColumns[i],
272
+ ariaColIndex: i + 1
273
+ };
274
+ }
275
+ return allColumns;
276
+ };
277
+ const measurePinnedColumns = (columns, selectionEndSize)=>{
278
+ let pinnedWidthLeft = 0;
279
+ let pinnedWidthRight = 0;
280
+ let unpinnedWidth = 0;
281
+ for (const column of columns){
282
+ const { hidden, pin, width } = column;
283
+ const visibleWidth = hidden ? 0 : width;
284
+ if ("left" === pin) pinnedWidthLeft += visibleWidth;
285
+ else if ("right" === pin) pinnedWidthRight += visibleWidth;
286
+ else unpinnedWidth += visibleWidth;
287
+ }
288
+ return {
289
+ pinnedWidthLeft: pinnedWidthLeft + selectionEndSize,
290
+ pinnedWidthRight: pinnedWidthRight + selectionEndSize,
291
+ unpinnedWidth
292
+ };
293
+ };
294
+ const getTableHeadings = (columns)=>{
295
+ if (columns.some(hasHeadings)) {
296
+ const maxHeadingDepth = columns.reduce((max, { heading })=>Math.max(max, heading?.length ?? 0), 0);
297
+ let heading;
298
+ const tableHeadings = [];
299
+ let tableHeadingsRow;
300
+ for(let level = 0; level < maxHeadingDepth; level++){
301
+ tableHeadingsRow = [];
302
+ columns.forEach(({ heading: columnHeading = NO_HEADINGS, width })=>{
303
+ const label = columnHeading[level] ?? "";
304
+ if (heading && heading.label === label) heading.width += width;
305
+ else {
306
+ heading = {
307
+ label,
308
+ width
309
+ };
310
+ tableHeadingsRow.push(heading);
311
+ }
312
+ });
313
+ tableHeadings.push(tableHeadingsRow);
314
+ }
315
+ return tableHeadings;
316
+ }
317
+ return NO_HEADINGS;
318
+ };
319
+ const getColumnStyle = ({ pin, pinnedOffset = 0, pinnedWidth = 0, width })=>{
320
+ if (pinnedWidth && "left" === pin) return {
321
+ left: pinnedOffset,
322
+ width,
323
+ "--pin-width": `${pinnedWidth - 3}px`
324
+ };
325
+ if ("left" === pin) return {
326
+ left: pinnedOffset,
327
+ width
328
+ };
329
+ if (pinnedWidth && "right" === pin) return {
330
+ right: pinnedOffset,
331
+ width,
332
+ "--pin-width": `${pinnedWidth - 5}px`
333
+ };
334
+ if ("right" === pin) return {
335
+ right: pinnedOffset,
336
+ width
337
+ };
338
+ return {
339
+ width
340
+ };
341
+ };
342
+ const getPinnedWidthFromElement = (cell)=>{
343
+ const cellStyle = getComputedStyle(cell);
344
+ return parseInt(cellStyle.getPropertyValue("--pin-width"));
345
+ };
346
+ const getAllCellsInColumn = (container, colIndex)=>{
347
+ const byColIndex = `[aria-colindex='${colIndex}']`;
348
+ return Array.from(container?.querySelectorAll(`.vuuTableCell${byColIndex},.vuuTableHeaderCell${byColIndex},.vuuTableGroupHeaderCell${byColIndex}`) ?? []);
349
+ };
350
+ const getAriaRowIndex = (rowElement)=>{
351
+ const rowIndex = rowElement?.ariaRowIndex;
352
+ if (null != rowIndex) {
353
+ const index = parseInt(rowIndex);
354
+ if (!isNaN(index)) return index;
355
+ }
356
+ return -1;
357
+ };
358
+ const getAriaColIndex = (cellElement)=>{
359
+ const colIndex = cellElement?.ariaColIndex;
360
+ if (null != colIndex) {
361
+ const index = parseInt(colIndex);
362
+ if (!isNaN(index)) return index;
363
+ }
364
+ return -1;
365
+ };
366
+ const getPinDetails = (cell, className)=>{
367
+ if (cell.classList.contains("vuuEndPin")) return {
368
+ cell,
369
+ pinnedWidth: getPinnedWidthFromElement(cell)
370
+ };
371
+ {
372
+ const endPin = cell.parentElement?.querySelector(`.${className}.vuuEndPin`);
373
+ if (endPin) {
374
+ let pinnedCells;
375
+ if ("vuuPinLeft" === className) {
376
+ const container = queryClosest(cell, ".vuuTable-table", true);
377
+ const startIndex = getAriaColIndex(cell);
378
+ const endIndex = getAriaColIndex(endPin);
379
+ pinnedCells = [];
380
+ for(let colIndex = startIndex + 1; colIndex <= endIndex; colIndex++)pinnedCells = pinnedCells?.concat(getAllCellsInColumn(container, colIndex));
381
+ }
382
+ console.log({
383
+ pinnedCells
384
+ });
385
+ return {
386
+ cell: endPin,
387
+ pinnedCells,
388
+ pinnedWidth: getPinnedWidthFromElement(endPin)
389
+ };
390
+ }
391
+ throw Error("[column-utils] getPinDetailsFromElement, no endPin on multi column pin");
392
+ }
393
+ };
394
+ const getPinStateFromElement = (cell)=>{
395
+ if (cell.classList.contains("vuuPinLeft")) return getPinDetails(cell, "vuuPinLeft");
396
+ if (cell.classList.contains("vuuPinRight")) return getPinDetails(cell, "vuuPinRight");
397
+ };
398
+ const setAggregations = (aggregations, column, aggType)=>aggregations.filter((agg)=>agg.column !== column.name).concat({
399
+ column: column.name,
400
+ aggType
401
+ });
402
+ const applyGroupByToColumns = (props)=>{
403
+ if (props.groupBy.length) {
404
+ const [groupColumn, nonGroupedColumns] = extractGroupColumn(props);
405
+ if (groupColumn) return [
406
+ groupColumn
407
+ ].concat(nonGroupedColumns);
408
+ } else if (props.columns[0]?.isGroup) return flattenColumnGroup(props.columns);
409
+ return props.columns;
410
+ };
411
+ const applySortToColumns = (columns, sort)=>columns.map((column)=>{
412
+ const sorted = getSortType(column, sort);
413
+ if (void 0 !== sorted) return {
414
+ ...column,
415
+ sorted
416
+ };
417
+ if (column.sorted) return {
418
+ ...column,
419
+ sorted: void 0
420
+ };
421
+ return column;
422
+ });
423
+ const removeSort = (columns)=>columns.map((col)=>col.sorted ? {
424
+ ...col,
425
+ sorted: void 0
426
+ } : col);
427
+ const existingSort = (columns)=>columns.some((col)=>col.sorted);
428
+ const getSortType = (column, { sortDefs })=>{
429
+ const sortDef = sortDefs.find((sortCol)=>sortCol.column === column.name);
430
+ if (sortDef) return sortDefs.length > 1 ? (sortDefs.indexOf(sortDef) + 1) * ("A" === sortDef.sortType ? 1 : -1) : sortDef.sortType;
431
+ };
432
+ const getColumnName = (name)=>{
433
+ const pos = name.indexOf(":");
434
+ if (-1 === pos) return name;
435
+ return name.slice(0, pos);
436
+ };
437
+ const getColumnLabel = (column)=>{
438
+ if (column.label) return column.label;
439
+ if (!isCalculatedColumn(column.name)) return column.name;
440
+ {
441
+ const { name } = getCalculatedColumnDetails(column);
442
+ return name ?? column.name;
443
+ }
444
+ };
445
+ const findColumn = (columns, columnName)=>{
446
+ const column = columns.find((col)=>col.name === columnName);
447
+ if (column) return column;
448
+ {
449
+ const groupColumn = columns.find((col)=>col.isGroup);
450
+ if (groupColumn) return findColumn(groupColumn.columns, columnName);
451
+ }
452
+ };
453
+ function updateColumn(columns, column, options) {
454
+ const targetColumn = "string" == typeof column ? columns.find((col)=>col.name === column) : column;
455
+ if (targetColumn) {
456
+ const replacementColumn = options ? {
457
+ ...targetColumn,
458
+ ...options
459
+ } : targetColumn;
460
+ return columns.map((col)=>col.name === replacementColumn.name ? replacementColumn : col);
461
+ }
462
+ throw Error("column-utils.replaceColun, column not found");
463
+ }
464
+ const toDataSourceColumns = (column)=>column.name;
465
+ const dataSourceRowToDataRowDto = (row, columnMap)=>Object.entries(columnMap).reduce((map, [colName, key])=>{
466
+ map[colName] = row[key];
467
+ return map;
468
+ }, {});
469
+ const isDataLoading = (columns)=>isGroupColumn(columns[0]) && false === columns[0].groupConfirmed;
470
+ const getColumnsInViewport = (columns, vpStart, vpEnd)=>{
471
+ const visibleColumns = [];
472
+ let preSpan = 0;
473
+ let rightPinnedOnly = false;
474
+ for(let columnOffset = 0, i = 0; i < columns.length; i++){
475
+ const column = columns[i];
476
+ if (!column.hidden) {
477
+ if ("right" === column.pin) visibleColumns.push(column);
478
+ else if (rightPinnedOnly) continue;
479
+ else if (columnOffset + column.width < vpStart) if ("left" === column.pin) visibleColumns.push(column);
480
+ else if (columnOffset + column.width + columns[i + 1]?.width > vpStart) visibleColumns.push(column);
481
+ else preSpan += column.width;
482
+ else if (columnOffset > vpEnd) rightPinnedOnly = true;
483
+ else visibleColumns.push(column);
484
+ columnOffset += column.width;
485
+ }
486
+ }
487
+ return [
488
+ visibleColumns,
489
+ preSpan
490
+ ];
491
+ };
492
+ const isNotHidden = (column)=>true !== column.hidden;
493
+ const visibleColumnAtIndex = (columns, index)=>{
494
+ if (columns.every(isNotHidden)) return columns[index];
495
+ return columns.filter(isNotHidden).at(index);
496
+ };
497
+ const getGroupIcon = (columns, dataRow)=>{
498
+ const { depth, isLeaf } = dataRow;
499
+ if (isLeaf || depth > columns.length) return;
500
+ {
501
+ if (0 === depth) return;
502
+ const { getIcon } = columns[depth - 1];
503
+ return getIcon?.(dataRow);
504
+ }
505
+ };
506
+ const getGroupValue = (columns, dataRow)=>{
507
+ const { depth, isLeaf } = dataRow;
508
+ if (isLeaf || depth > columns.length) return null;
509
+ {
510
+ if (0 === depth) return "$root";
511
+ const { name, valueFormatter } = columns[depth - 1];
512
+ const value = valueFormatter(dataRow[name]);
513
+ return value;
514
+ }
515
+ };
516
+ const getDefaultColumnType = (serverDataType)=>{
517
+ switch(serverDataType){
518
+ case "int":
519
+ case "long":
520
+ case "double":
521
+ return "number";
522
+ case "scaleddecimal2":
523
+ case "scaleddecimal4":
524
+ case "scaleddecimal6":
525
+ case "scaleddecimal8":
526
+ return "scaleddecimal";
527
+ case "boolean":
528
+ return "boolean";
529
+ case "epochtimestamp":
530
+ return "date/time";
531
+ default:
532
+ return "string";
533
+ }
534
+ };
535
+ const updateColumnFormatting = (column, formatting)=>{
536
+ const { serverDataType, type = getDefaultColumnType(serverDataType) } = column;
537
+ if (isTypeDescriptor(type)) return {
538
+ ...column,
539
+ type: {
540
+ ...type,
541
+ formatting
542
+ }
543
+ };
544
+ return {
545
+ ...column,
546
+ type: {
547
+ name: type,
548
+ formatting
549
+ }
550
+ };
551
+ };
552
+ function updateColumnType(column, type) {
553
+ return isTypeDescriptor(column.type) ? {
554
+ ...column,
555
+ type: {
556
+ ...column.type,
557
+ name: type
558
+ }
559
+ } : {
560
+ ...column,
561
+ type
562
+ };
563
+ }
564
+ const updateColumnRenderProps = (column, renderer)=>{
565
+ const { serverDataType, type = getDefaultColumnType(serverDataType) } = column;
566
+ if (isTypeDescriptor(type)) return {
567
+ ...column,
568
+ type: {
569
+ ...type,
570
+ renderer
571
+ }
572
+ };
573
+ return {
574
+ ...column,
575
+ type: {
576
+ name: type,
577
+ renderer
578
+ }
579
+ };
580
+ };
581
+ const NO_TYPE_SETTINGS = {};
582
+ const getTypeFormattingFromColumn = (column)=>{
583
+ if (isTypeDescriptor(column.type)) return column.type.formatting ?? NO_TYPE_SETTINGS;
584
+ return NO_TYPE_SETTINGS;
585
+ };
586
+ const assertAllColumnsAreIncludedInSubscription = (columns, columnNames)=>{
587
+ const unsubscribedColumns = [];
588
+ for (const column of columns)if ("client" !== column.source && !columnNames?.includes(column.name)) unsubscribedColumns.push(column.name);
589
+ if (unsubscribedColumns.length > 0) throw Error(`[column-utils] assertAllColumnsAreIncludedInSubscription columns not included in subscription: ${unsubscribedColumns.join(", ")}`);
590
+ };
591
+ const subscribedOnly = (columnNames)=>(column)=>"client" === column.source || columnNames?.includes(column.name);
592
+ const addColumnToSubscribedColumns = (subscribedColumns, availableColumns, columnName)=>{
593
+ const byColName = (n = columnName)=>(column)=>column.name === n;
594
+ if (-1 !== subscribedColumns.findIndex(byColName())) throw Error(`[column-utils], addColumnToSubscribedColumns column ${columnName} is already subscribed`);
595
+ const indexOfAvailableColumn = availableColumns.findIndex(byColName());
596
+ if (-1 === indexOfAvailableColumn) throw Error(`[column-utils] addColumnToSubscribedColumns column ${columnName} is not available`);
597
+ const newColumn = {
598
+ ...availableColumns[indexOfAvailableColumn]
599
+ };
600
+ return subscribedColumns.concat(newColumn);
601
+ };
602
+ function getServerDataType(column, throwIfUndefined = false) {
603
+ if (isCalculatedColumn(column.name)) {
604
+ const { serverDataType } = getCalculatedColumnDetails(column);
605
+ if (isVuuColumnDataType(serverDataType)) return serverDataType;
606
+ if (throwIfUndefined) throw Error(`[column-utils] getServerDataType, calculated column does not have valid type ${column.name}`);
607
+ } else if (isVuuColumnDataType(column.serverDataType)) return column.serverDataType;
608
+ else if (throwIfUndefined) throw Error(`[column-utils] getServerDataType, columns does not have valid type ${column.serverDataType}`);
609
+ }
610
+ const CalculatedColumnPattern = /.*:.*:.*/;
611
+ const isCalculatedColumn = (columnName)=>void 0 !== columnName && CalculatedColumnPattern.test(columnName);
612
+ const getCalculatedColumnDetails = (column)=>{
613
+ const columnName = "string" == typeof column ? column : column.name;
614
+ if (isCalculatedColumn(columnName)) {
615
+ const [name, serverDataType, expression] = columnName.split(/:=?/);
616
+ if (serverDataType && !isVuuColumnDataType(serverDataType)) throw Error(`column-utils, getCalculatedColumnDetails ${serverDataType} is not valid type for column ${columnName}`);
617
+ return {
618
+ name: name ?? "",
619
+ expression: expression ?? "",
620
+ serverDataType: isVuuColumnDataType(serverDataType) ? serverDataType : void 0
621
+ };
622
+ }
623
+ throw Error("column.name is nor a calculated column");
624
+ };
625
+ const setCalculatedColumnName = (column, name)=>{
626
+ const [, type, expression] = column.name.split(":");
627
+ return {
628
+ ...column,
629
+ name: `${name}:${type}:${expression}`
630
+ };
631
+ };
632
+ const setCalculatedColumnType = (column, serverDataType)=>{
633
+ const [name, , expression] = column.name.split(":");
634
+ return {
635
+ ...column,
636
+ name: `${name}:${serverDataType}:${expression}`,
637
+ serverDataType
638
+ };
639
+ };
640
+ const setCalculatedColumnExpression = (column, expression)=>{
641
+ const [name, type] = column.name.split(":");
642
+ return {
643
+ ...column,
644
+ name: `${name}:${type}:=${expression}`
645
+ };
646
+ };
647
+ const moveColumnTo = (columns, column, newIndex)=>{
648
+ const index = columns.findIndex((col)=>col.name === column.name);
649
+ return moveItem(columns, index, newIndex);
650
+ };
651
+ function replaceColumn(columns, column) {
652
+ return columns.map((col)=>col.name === column.name ? column : col);
653
+ }
654
+ const vuuTimestampColumns = [
655
+ "vuuCreatedTimestamp",
656
+ "vuuUpdatedTimestamp"
657
+ ];
658
+ const notVuuTimestamps = (column)=>!vuuTimestampColumns.includes(column.name);
659
+ const applyDefaultColumnConfig = ({ columns: columnsProp, table }, getDefaultColumnConfig, includeVuuTimestampColumns = false)=>{
660
+ if ("function" != typeof getDefaultColumnConfig) return columnsProp;
661
+ {
662
+ const columns = includeVuuTimestampColumns ? columnsProp : columnsProp.filter(notVuuTimestamps);
663
+ return columns.map((column)=>{
664
+ const config = getDefaultColumnConfig(table.table, column.name);
665
+ if (config) return {
666
+ ...column,
667
+ ...config
668
+ };
669
+ return column;
670
+ });
671
+ }
672
+ };
673
+ const measureColumns = (columns, defaultMaxWidth, defaultMinWidth)=>columns.reduce((aggregated, column)=>{
674
+ if (true !== column.hidden) {
675
+ aggregated.totalMinWidth += column.minWidth ?? defaultMinWidth;
676
+ aggregated.totalMaxWidth += column.maxWidth ?? defaultMaxWidth;
677
+ aggregated.totalWidth += column.width;
678
+ aggregated.flexCount += column.flex ?? 0;
679
+ }
680
+ return aggregated;
681
+ }, {
682
+ totalMinWidth: 0,
683
+ totalMaxWidth: 0,
684
+ totalWidth: 0,
685
+ flexCount: 0
686
+ });
687
+ function applyWidthToColumns(columns, { availableWidth = 0, columnLayout = "static", defaultWidth = DEFAULT_COL_WIDTH, defaultMinWidth = DEFAULT_MIN_WIDTH, defaultMaxWidth = DEFAULT_MAX_WIDTH }) {
688
+ if ("fit" === columnLayout) {
689
+ const { totalMinWidth, totalMaxWidth, totalWidth, flexCount } = measureColumns(columns, defaultMaxWidth, defaultMinWidth);
690
+ if (totalMaxWidth < availableWidth) return assignMaxWidthToAll(columns, defaultMaxWidth);
691
+ if (totalMinWidth > availableWidth) ;
692
+ else if (totalWidth > availableWidth) return shrinkColumnsToFitAvailableSpace(columns, availableWidth, totalWidth, defaultMinWidth, defaultWidth, flexCount);
693
+ else if (totalWidth < availableWidth) return stretchColumnsToFillAvailableSpace(columns, availableWidth, totalWidth, defaultMaxWidth, defaultWidth);
694
+ }
695
+ return columns;
696
+ }
697
+ const assignMaxWidthToAll = (columns, defaultMaxWidth)=>columns.map((column)=>{
698
+ const { maxWidth = defaultMaxWidth } = column;
699
+ if (column.width === maxWidth) return column;
700
+ return {
701
+ ...column,
702
+ width: maxWidth
703
+ };
704
+ });
705
+ const shrinkColumnsToFitAvailableSpace = (columns, availableWidth, totalWidth, defaultMinWidth, defaultWidth, flexCount)=>{
706
+ const excessWidth = totalWidth - availableWidth;
707
+ const inFlexMode = flexCount > 0;
708
+ let excessWidthPerColumn = excessWidth / (flexCount || columns.length);
709
+ let columnsNotYetAtMinWidth = columns.length;
710
+ let unassignedExcess = 0;
711
+ let newColumns = columns.map((column)=>{
712
+ const { minWidth = defaultMinWidth, width = defaultWidth, flex = 0 } = column;
713
+ if (inFlexMode && 0 === flex) return column;
714
+ const adjustedWidth = width - excessWidthPerColumn;
715
+ if (!(adjustedWidth < minWidth)) return {
716
+ ...column,
717
+ width: adjustedWidth
718
+ };
719
+ columnsNotYetAtMinWidth -= 1;
720
+ unassignedExcess += minWidth - adjustedWidth;
721
+ return {
722
+ ...column,
723
+ width: minWidth
724
+ };
725
+ });
726
+ if (0 === unassignedExcess) return newColumns;
727
+ excessWidthPerColumn = unassignedExcess / columnsNotYetAtMinWidth;
728
+ newColumns = newColumns.map((column)=>{
729
+ const adjustedWidth = column.width - excessWidthPerColumn;
730
+ if (column.width !== column.minWidth) return {
731
+ ...column,
732
+ width: adjustedWidth
733
+ };
734
+ return column;
735
+ });
736
+ return newColumns;
737
+ };
738
+ const hasFlex = ({ flex })=>"number" == typeof flex;
739
+ const stretchColumnsToFillAvailableSpace = (columns, availableWidth, totalWidth, defaultMaxWidth, defaultWidth)=>{
740
+ let freeSpaceToBeFilled = availableWidth - totalWidth;
741
+ let adjustedColumns = columns;
742
+ const canGrow = ({ width = defaultWidth, maxWidth = defaultMaxWidth })=>width < maxWidth;
743
+ while(freeSpaceToBeFilled > 0){
744
+ const flexCols = adjustedColumns.filter((col)=>hasFlex(col) && canGrow(col));
745
+ const columnsNotYetAtMaxWidth = flexCols.length || adjustedColumns.filter(canGrow).length;
746
+ const additionalWidthPerColumn = Math.ceil(freeSpaceToBeFilled / columnsNotYetAtMaxWidth);
747
+ adjustedColumns = columns.map((column)=>{
748
+ const { maxWidth = defaultMaxWidth, width = defaultWidth, flex = 0 } = column;
749
+ if (flexCols.length > 0 && 0 === flex) return column;
750
+ const adjustmentAmount = Math.min(additionalWidthPerColumn, freeSpaceToBeFilled);
751
+ const adjustedWidth = width + adjustmentAmount;
752
+ if (adjustedWidth > maxWidth) {
753
+ freeSpaceToBeFilled -= adjustedWidth - maxWidth;
754
+ return {
755
+ ...column,
756
+ width: maxWidth
757
+ };
758
+ }
759
+ freeSpaceToBeFilled -= adjustmentAmount;
760
+ return {
761
+ ...column,
762
+ width: adjustedWidth
763
+ };
764
+ });
765
+ }
766
+ return adjustedColumns;
767
+ };
768
+ const dataAndColumnUnchanged = (p, p1)=>p.column === p1.column && p.column.valueFormatter(p.dataRow[p.column.name]) === p1.column.valueFormatter(p1.dataRow[p1.column.name]);
769
+ const dataColumnAndKeyUnchanged = (p, p1)=>p.column === p1.column && p.dataRow.key === p1.dataRow.key && p.column.valueFormatter(p.dataRow[p.column.name]) === p1.column.valueFormatter(p1.dataRow[p1.column.name]);
770
+ const toColumnName = (column)=>column.name;
771
+ const isStringColumn = (column)=>"string" === column.serverDataType;
772
+ const reorderColumnItems = (columnItems, orderedNames)=>{
773
+ const columns = [];
774
+ let previousName = "";
775
+ for (const name of orderedNames)if (previousName !== name) {
776
+ const columnItem = columnItems.find((c)=>c.name === name);
777
+ if (columnItem) columns.push(columnItem);
778
+ previousName = name;
779
+ }
780
+ return columns;
781
+ };
782
+ const columnByAriaIndex = (columns, ariaColIndex)=>{
783
+ const column = columns[ariaColIndex - 1];
784
+ if (column && column.ariaColIndex === ariaColIndex) return column;
785
+ if (column && column.ariaColIndex < ariaColIndex) {
786
+ if (columns[ariaColIndex]?.ariaColIndex === ariaColIndex) return columns[ariaColIndex];
787
+ }
788
+ throw Error(`no column with aria-colIndex ${ariaColIndex}`);
789
+ };
790
+ export { AggregationType, addColumnToSubscribedColumns, applyDefaultColumnConfig, applyGroupByToColumns, applyRuntimeColumnWidthsToConfig, applySortToColumns, applyWidthToColumns, assertAllColumnsAreIncludedInSubscription, buildColumnMap, checkConfirmationPending, columnByAriaIndex, dataAndColumnUnchanged, dataColumnAndKeyUnchanged, dataSourceRowToDataRowDto, existingSort, extractGroupColumn, findColumn, flattenColumnGroup, fromServerDataType, getAllCellsInColumn, getAriaColIndex, getAriaRowIndex, getCalculatedColumnDetails, getColumnLabel, getColumnName, getColumnStyle, getColumnsInViewport, getDefaultAlignment, getDefaultColumnType, getGroupIcon, getGroupValue, getPinStateFromElement, getRuntimeColumnWidth, getServerDataType, getTableHeadings, getTypeFormattingFromColumn, hasCustomRenderer, hasHeadings, hasValidationRules, isCalculatedColumn, isColumnTypeRenderer, isDataLoading, isDateTimeDataType, isDateTimeDataValue, isGroupColumn, isJsonAttribute, isJsonColumn, isJsonGroup, isLookupRenderer, isMappedValueTypeRenderer, isNotHidden, isNumericColumn, isPinned, isResizing, isStringColumn, isTextColumn, isTimeDataType, isTimeDataValue, isTimestampColumn, isTypeDescriptor, isValidColumnAlignment, isValidPinLocation, isValueListRenderer, isVuuColumnDataType, mapSortCriteria, measurePinnedColumns, metadataKeys, moveColumnTo, projectUpdates, removeSort, reorderColumnItems, replaceColumn, setAggregations, setCalculatedColumnExpression, setCalculatedColumnName, setCalculatedColumnType, sortPinnedColumns, subscribedOnly, toColumnDescriptor, toColumnName, toDataSourceColumns, updateColumn, updateColumnFormatting, updateColumnRenderProps, updateColumnType, visibleColumnAtIndex };