@dolusoft/query-table 3.6.0 → 3.7.1

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.
@@ -1,5 +1,5 @@
1
1
  import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createSlots, createStaticVNode, createTextVNode, createVNode, defineComponent, effectScope, getCurrentInstance, getCurrentScope, guardReactiveProps, h, inject, mergeProps, nextTick, normalizeProps, normalizeStyle, onActivated, onBeforeUnmount, onBeforeUpdate, onDeactivated, onMounted, onScopeDispose, openBlock, provide, renderList, renderSlot, shallowRef, toDisplayString, toHandlers, toValue, unref, useSlots, watch, watchEffect, withCtx, withKeys, withModifiers } from "vue";
2
- import { columnTypeOf, columnTypeOf as columnTypeOf$1, conditionLabel, conditionOptions, isCursorQuery, parseFilterInput as parseFilterInput$1, rulesOf, searchOf } from "@dolusoft/query-protocol";
2
+ import { cloneQuery, columnTypeOf, columnTypeOf as columnTypeOf$1, conditionLabel, conditionOptions, isCursorQuery, parseFilterInput as parseFilterInput$1, rulesOf, sameQuery, searchOf } from "@dolusoft/query-protocol";
3
3
  import { createRowChangeTracker } from "@dolusoft/query-table-core/row-changes";
4
4
  import { dispose, filterInputFeature, projectServerQuery, serverQueryFeature } from "@dolusoft/query-table-core";
5
5
  import { columnFilteringFeature, columnOrderingFeature, columnPinningFeature, columnVisibilityFeature, functionalUpdate, globalFilteringFeature, rowExpandingFeature, rowPaginationFeature, rowPinningFeature, rowSelectionFeature, rowSortingFeature, tableFeatures, useTable } from "@tanstack/vue-table";
@@ -1257,6 +1257,68 @@ var measureColumnWidths = (table, fields) => {
1257
1257
  return widths;
1258
1258
  };
1259
1259
  //#endregion
1260
+ //#region src/resize/width-scale.ts
1261
+ var same = (width) => width;
1262
+ var sum = (widths) => widths.reduce((a, b) => a + b, 0);
1263
+ /** The declared width is the drawn one. */
1264
+ var identityScale = {
1265
+ declared: same,
1266
+ rendered: same
1267
+ };
1268
+ /**
1269
+ * A table whose cells take `space` (a container's width, with
1270
+ * `min-width: 100%`) and whose columns are declared narrower: the fixed
1271
+ * layout spreads the rest over the columns by their declared widths, so a
1272
+ * column declared `w` next to `others` is drawn `w * space / (w + others)`
1273
+ * wide until the columns fill the space, and `w` from there on. With no other
1274
+ * column the one column takes the space whatever its width: the identity.
1275
+ */
1276
+ var stretchedScale = (space, others) => others > 0 && space > others ? {
1277
+ declared: (rendered) => rendered >= space - others ? rendered : rendered * others / (space - rendered),
1278
+ rendered: (declared) => declared + others >= space ? declared : declared * space / (declared + others)
1279
+ } : identityScale;
1280
+ /**
1281
+ * How the declared width of the column of `th` maps to its drawn width, read
1282
+ * from the layout. The column is drawn once at the width it is drawn at now.
1283
+ * Wider there, the table stretches it: its row's cells take the whole space,
1284
+ * and the other columns' declared widths follow from the stretch; the fit
1285
+ * must then give back the width the column is drawn at now. Not wider, the
1286
+ * column is drawn once more at `low`, the narrowest width a resize writes. A
1287
+ * table that stretches it there widens every cell of the row by one factor,
1288
+ * the column at least as much (content the fixed layout keeps wider than
1289
+ * `low` only adds to it), and its cells then take the space the columns can
1290
+ * shrink into before the table stretches them. Any other layout gets the
1291
+ * identity: one that never stretches, a column without a width that takes
1292
+ * what is left, one that spreads the space otherwise. Every style is put back before the
1293
+ * function returns (C-31), so nothing is painted in between.
1294
+ */
1295
+ var measureWidthScale = (th, low) => {
1296
+ const now = th.getBoundingClientRect().width;
1297
+ const others = Array.from(th.parentElement?.children ?? []).filter((cell) => cell !== th);
1298
+ const widths = () => others.map((cell) => cell.getBoundingClientRect().width);
1299
+ const swap = styleSwap();
1300
+ /** Draws the column `width` wide; the width it takes. */
1301
+ const draw = (width) => {
1302
+ swap.set(th, "width", `${width}px`);
1303
+ return th.getBoundingClientRect().width;
1304
+ };
1305
+ try {
1306
+ const drawn = draw(now);
1307
+ const before = widths();
1308
+ const rest = sum(before);
1309
+ if (drawn > now + .5) {
1310
+ const scale = stretchedScale(drawn + rest, now * rest / drawn);
1311
+ return Math.abs(draw(scale.declared(now)) - now) <= 1 ? scale : identityScale;
1312
+ }
1313
+ const narrow = draw(low);
1314
+ const after = widths();
1315
+ const factor = sum(after) / rest;
1316
+ return factor > 1 && narrow >= low * factor - 1 && after.every((width, i) => Math.abs(width - before[i] * factor) <= 1) ? stretchedScale(narrow + sum(after), rest) : identityScale;
1317
+ } finally {
1318
+ swap.restore();
1319
+ }
1320
+ };
1321
+ //#endregion
1260
1322
  //#region src/resize/use-column-resize.ts
1261
1323
  /** Default `Column.minWidth`, in pixels. */
1262
1324
  var defaultMinWidth = 40;
@@ -1286,18 +1348,27 @@ var cellOf = (event) => event.currentTarget.closest("th");
1286
1348
  */
1287
1349
  var edgeOf = (column) => column.pinned === "right" ? -1 : 1;
1288
1350
  /**
1351
+ * The scale of a column's widths, read from the layout once per drag or key:
1352
+ * a table stretched to its container draws a column wider than declared.
1353
+ */
1354
+ var scaleOf = (th, column) => measureWidthScale(th, clampWidth(column, 0));
1355
+ /**
1289
1356
  * Resizing a column (C-48 to C-50). The table holds a width only while a
1290
1357
  * drag is under way (the preview, P2); every committed width goes out as a
1291
1358
  * `columnResize` event and comes back, if the consumer wants it, as
1292
1359
  * `Column.width`. The handle captures the pointer, so all drag events arrive
1293
- * at the handle itself, and it has the focus, so Escape does too.
1360
+ * at the handle itself, and it has the focus, so Escape does too. The pointer
1361
+ * and the arrow keys move the drawn width; what the table writes and emits is
1362
+ * the declared width that draws it (`measureWidthScale`).
1294
1363
  */
1295
1364
  var useColumnResize = (options) => {
1365
+ /** `width` is declared (the inline style), `rendered` drawn. */
1296
1366
  const preview = shallowRef(null);
1297
1367
  let drag = null;
1298
1368
  const isResizable = (column) => options.resizable() && column.resizable !== false;
1299
1369
  /** The width the header shows: the preview while dragging, else measured. */
1300
- const widthOf = (column) => preview.value?.field === column.field ? preview.value.width : Math.round(options.measuredWidth(column.field) ?? 0);
1370
+ const widthOf = (column) => preview.value?.field === column.field ? preview.value.rendered : Math.round(options.measuredWidth(column.field) ?? 0);
1371
+ /** Emits the declared width `width`, unless it is the declared `from`. */
1301
1372
  const commit = (column, from, width) => {
1302
1373
  const next = clampWidth(column, width);
1303
1374
  if (next !== Math.round(from)) options.emit({
@@ -1305,14 +1376,15 @@ var useColumnResize = (options) => {
1305
1376
  width: next
1306
1377
  });
1307
1378
  };
1308
- const dragged = (current) => current.startWidth + edgeOf(current.column) * (current.lastX - current.startX);
1379
+ /** The declared width that draws the column where the pointer is. */
1380
+ const dragged = (current) => current.scale.declared(current.startWidth + edgeOf(current.column) * (current.lastX - current.startX));
1309
1381
  const end = (apply) => {
1310
1382
  const current = drag;
1311
1383
  if (current) {
1312
1384
  drag = null;
1313
1385
  cancelAnimationFrame(current.frame);
1314
1386
  preview.value = null;
1315
- if (apply) commit(current.column, current.startWidth, dragged(current));
1387
+ if (apply) commit(current.column, current.scale.declared(current.startWidth), dragged(current));
1316
1388
  }
1317
1389
  };
1318
1390
  /** Every pointer event of a handle; one listener for the whole drag. */
@@ -1329,6 +1401,7 @@ var useColumnResize = (options) => {
1329
1401
  pointerId: event.pointerId,
1330
1402
  startX: event.clientX,
1331
1403
  startWidth: th.getBoundingClientRect().width,
1404
+ scale: scaleOf(th, column),
1332
1405
  lastX: event.clientX,
1333
1406
  frame: 0
1334
1407
  };
@@ -1339,15 +1412,20 @@ var useColumnResize = (options) => {
1339
1412
  if (event.type === "pointermove") drag.frame ||= requestAnimationFrame(() => {
1340
1413
  if (drag) {
1341
1414
  drag.frame = 0;
1415
+ const width = clampWidth(drag.column, dragged(drag));
1342
1416
  preview.value = {
1343
1417
  field: drag.column.field,
1344
- width: clampWidth(drag.column, dragged(drag))
1418
+ width,
1419
+ rendered: Math.round(drag.scale.rendered(width))
1345
1420
  };
1346
1421
  }
1347
1422
  });
1348
1423
  else end(event.type === "pointerup");
1349
1424
  };
1350
- /** Arrow keys step the width, Enter fits it; both from the rendered width. */
1425
+ /**
1426
+ * Arrow keys step the drawn width, Enter fits it to the content (the
1427
+ * declared width `measureColumnWidths` gives, as it is).
1428
+ */
1351
1429
  const onKey = (event, column) => {
1352
1430
  const { key } = event;
1353
1431
  if (drag) {
@@ -1362,8 +1440,11 @@ var useColumnResize = (options) => {
1362
1440
  const th = cellOf(event);
1363
1441
  if (th && (sign || key === "Enter" || event.type === "dblclick")) {
1364
1442
  event.preventDefault();
1365
- const from = th.getBoundingClientRect().width;
1366
- const to = sign ? Math.round(from) + sign * (event.shiftKey ? 50 : 10) : measureColumnWidths(th.closest("table"), [column.field])[column.field];
1443
+ const drawn = th.getBoundingClientRect().width;
1444
+ const scale = scaleOf(th, column);
1445
+ const from = scale.declared(drawn);
1446
+ const step = scale.declared(Math.round(drawn) + sign * (event.shiftKey ? 50 : 10));
1447
+ const to = sign ? from + sign * Math.max(1, Math.abs(step - from)) : measureColumnWidths(th.closest("table"), [column.field])[column.field];
1367
1448
  if (to !== void 0) commit(column, from, to);
1368
1449
  }
1369
1450
  };
@@ -1536,6 +1617,7 @@ var table_header_default = /* @__PURE__ */ defineComponent({
1536
1617
  utilities: {},
1537
1618
  hasPinned: { type: Boolean },
1538
1619
  offsets: {},
1620
+ autoWidths: {},
1539
1621
  slotNames: {}
1540
1622
  },
1541
1623
  setup(__props) {
@@ -1564,9 +1646,14 @@ var table_header_default = /* @__PURE__ */ defineComponent({
1564
1646
  const hasFilter = (column) => props.filterable && column.filterable !== false;
1565
1647
  const hostsClearAll = (utility) => props.filterable && utility === props.utilities.find((candidate) => candidate !== "select");
1566
1648
  const utilityAttrs = (utility) => pinAttrs(props.hasPinned ? "left" : false, props.offsets[utilityKey(utility)]);
1649
+ const widthOf = (column) => {
1650
+ if (column.width) return column.width;
1651
+ const auto = props.autoWidths[column.field];
1652
+ return auto === void 0 ? void 0 : `${auto}px`;
1653
+ };
1567
1654
  const cellAttrs = (column) => {
1568
1655
  const preview = resize.preview.value;
1569
- return pinAttrs(sideOf(column), props.offsets[column.field], preview?.field === column.field ? `${preview.width}px` : column.width);
1656
+ return pinAttrs(sideOf(column), props.offsets[column.field], preview?.field === column.field ? `${preview.width}px` : widthOf(column));
1570
1657
  };
1571
1658
  const headerSlotProps = (column) => ({
1572
1659
  column,
@@ -1921,6 +2008,119 @@ var useColumnReorder = (options) => {
1921
2008
  };
1922
2009
  };
1923
2010
  //#endregion
2011
+ //#region src/resize/use-auto-width.ts
2012
+ /**
2013
+ * The query without what moves inside one result: the paging position, the
2014
+ * page size and the sort. Two queries alike in the rest ask for the same
2015
+ * rows, in another order or slice (C-100).
2016
+ */
2017
+ var resultOf = (query) => isCursorQuery(query) ? {
2018
+ ...query,
2019
+ cursor: null,
2020
+ pageSize: 0,
2021
+ sort: null
2022
+ } : {
2023
+ ...query,
2024
+ page: 1,
2025
+ pageSize: 0,
2026
+ sort: null
2027
+ };
2028
+ var sameWidths = (a, b) => {
2029
+ const keys = Object.keys(a);
2030
+ return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]);
2031
+ };
2032
+ var none = Object.freeze({});
2033
+ /**
2034
+ * Automatic column widths (C-100, ADR 0014). With `autoWidth`, every visible
2035
+ * column without `Column.width` is measured (C-99) after the drawn rows
2036
+ * change, clamped as a resize is (C-49), and drawn at that width. The widths
2037
+ * are UI state (P2): never emitted, never written to `columns`, gone when
2038
+ * `autoWidth` is off or the column gets a width. Within one result (the query
2039
+ * apart from its paging position, page size and sort) they only grow, so a
2040
+ * page, a sort or a live tick never narrows a column.
2041
+ */
2042
+ var useAutoWidth = (options) => {
2043
+ const widths = shallowRef(none);
2044
+ /** The result the widths were last measured for, `null` before one. */
2045
+ let result = null;
2046
+ /** The next measure starts over: the result moved before it could run. */
2047
+ let fromScratch = false;
2048
+ /** A measure found the table not displayed: measure once it is. */
2049
+ let waiting = false;
2050
+ let observer = null;
2051
+ const autoColumns = () => options.columns().filter((column) => !column.width);
2052
+ const clear = () => {
2053
+ widths.value = none;
2054
+ result = null;
2055
+ fromScratch = false;
2056
+ waiting = false;
2057
+ observer?.disconnect();
2058
+ observer = null;
2059
+ };
2060
+ const measure = (rowsChanged) => {
2061
+ const table = options.table.value;
2062
+ const columns = autoColumns();
2063
+ if (!table || columns.length === 0) return;
2064
+ const query = options.query();
2065
+ if (result === null || rowsChanged && (options.rowsUpdate() === "reset" || !sameQuery(resultOf(result), resultOf(query)))) {
2066
+ fromScratch = true;
2067
+ result = cloneQuery(query);
2068
+ }
2069
+ if (options.rows().length === 0) return;
2070
+ const measured = measureColumnWidths(table, columns.map((column) => column.field));
2071
+ if (Object.keys(measured).length === 0) {
2072
+ waiting = true;
2073
+ return;
2074
+ }
2075
+ waiting = false;
2076
+ const previous = widths.value;
2077
+ const next = {};
2078
+ for (const column of columns) {
2079
+ const width = measured[column.field];
2080
+ if (width === void 0) continue;
2081
+ const fitted = clampWidth({
2082
+ ...column,
2083
+ maxWidth: limitOf(column.maxWidth) ?? options.max()
2084
+ }, width);
2085
+ next[column.field] = fromScratch ? fitted : Math.max(fitted, previous[column.field] ?? 0);
2086
+ }
2087
+ fromScratch = false;
2088
+ if (!sameWidths(next, previous)) widths.value = next;
2089
+ };
2090
+ const observe = (table) => {
2091
+ observer?.disconnect();
2092
+ observer = null;
2093
+ if (!table || typeof ResizeObserver === "undefined") return;
2094
+ observer = new ResizeObserver(() => {
2095
+ if (waiting) measure(false);
2096
+ });
2097
+ observer.observe(table);
2098
+ };
2099
+ watch([
2100
+ () => options.enabled(),
2101
+ () => options.max(),
2102
+ () => options.table.value,
2103
+ () => options.rows(),
2104
+ () => autoColumns().map((column) => column.field).join("|")
2105
+ ], ([enabled, max, table, rows], [wasEnabled, oldMax, oldTable, oldRows]) => {
2106
+ if (!enabled) {
2107
+ if (wasEnabled) clear();
2108
+ return;
2109
+ }
2110
+ if (!wasEnabled || max !== oldMax) fromScratch = true;
2111
+ if (table !== oldTable || !wasEnabled) observe(table);
2112
+ measure(rows !== oldRows);
2113
+ }, {
2114
+ immediate: true,
2115
+ flush: "post"
2116
+ });
2117
+ onBeforeUnmount(() => {
2118
+ observer?.disconnect();
2119
+ observer = null;
2120
+ });
2121
+ return { widths };
2122
+ };
2123
+ //#endregion
1924
2124
  //#region src/use-query-table.ts
1925
2125
  /** The TanStack features of a Query Table, the two plugins included. */
1926
2126
  var features = tableFeatures({
@@ -3100,6 +3300,11 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3100
3300
  type: Boolean,
3101
3301
  default: false
3102
3302
  },
3303
+ autoWidth: {
3304
+ type: Boolean,
3305
+ default: false
3306
+ },
3307
+ autoWidthMax: { default: void 0 },
3103
3308
  reorderable: {
3104
3309
  type: Boolean,
3105
3310
  default: false
@@ -3230,6 +3435,15 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3230
3435
  if (next) emit("update:columns", next, "resize");
3231
3436
  }
3232
3437
  });
3438
+ const autoWidth = useAutoWidth({
3439
+ enabled: () => props.autoWidth,
3440
+ max: () => props.autoWidthMax,
3441
+ table: tableEl,
3442
+ columns: () => visibleColumns.value,
3443
+ rows: () => props.rows,
3444
+ query: () => props.query,
3445
+ rowsUpdate: () => props.rowsUpdate
3446
+ });
3233
3447
  const rowWindow = useRowWindow({
3234
3448
  virtual: () => props.virtual,
3235
3449
  table: tableEl,
@@ -3341,6 +3555,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3341
3555
  utilities: utilities.value,
3342
3556
  "has-pinned": unref(hasPinned),
3343
3557
  offsets: unref(offsets),
3558
+ "auto-widths": unref(autoWidth).widths.value,
3344
3559
  "slot-names": headerSlotNames(),
3345
3560
  "aria-rowindex": unref(rowWindow).enabled() ? 1 : void 0
3346
3561
  }, {
@@ -3355,6 +3570,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3355
3570
  "utilities",
3356
3571
  "has-pinned",
3357
3572
  "offsets",
3573
+ "auto-widths",
3358
3574
  "slot-names",
3359
3575
  "aria-rowindex"
3360
3576
  ])], 32),