@dolusoft/query-table 3.5.1 → 3.7.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.
@@ -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";
@@ -1187,6 +1187,76 @@ var reorder_handle_default = /* @__PURE__ */ defineComponent({
1187
1187
  }
1188
1188
  });
1189
1189
  //#endregion
1190
+ //#region src/resize/measure-widths.ts
1191
+ /**
1192
+ * What must not count, inside the table: the filter row and the resize handle
1193
+ * of a header cell (C-50 counts the label only), the footer, and every body
1194
+ * row that is not a data row (subtable, virtual spacer, loading, empty,
1195
+ * load-more). A subtable or a spanning cell would otherwise widen the
1196
+ * columns it spans with content that is not theirs.
1197
+ */
1198
+ var notCounted = [
1199
+ ":scope > thead > tr > th[data-field] > .qt-filter",
1200
+ ":scope > thead > tr > th[data-field] > .qt-resize-handle",
1201
+ ":scope > tbody > tr:not([data-row-index])",
1202
+ ":scope > tfoot"
1203
+ ].join(", ");
1204
+ /**
1205
+ * Sets inline declarations for one read and puts every `style` attribute
1206
+ * back exactly as it was, absent included, so the markup after the call is
1207
+ * the markup before it (C-31). `!important` so a consumer's `!important`
1208
+ * rule cannot keep the fixed layout during the read.
1209
+ */
1210
+ var styleSwap = () => {
1211
+ const saved = /* @__PURE__ */ new Map();
1212
+ return {
1213
+ set(element, property, value) {
1214
+ if (!saved.has(element)) saved.set(element, element.getAttribute("style"));
1215
+ element.style.setProperty(property, value, "important");
1216
+ },
1217
+ restore() {
1218
+ for (const [element, style] of saved) if (style === null) {
1219
+ element.getAttribute("style");
1220
+ element.removeAttribute("style");
1221
+ } else element.setAttribute("style", style);
1222
+ saved.clear();
1223
+ }
1224
+ };
1225
+ };
1226
+ /**
1227
+ * The max-content width of each drawn column, by `field`: header label and
1228
+ * the cells of the drawn data rows, padding and border included, in whole
1229
+ * pixels rounded up. The browser lays the table out once with its automatic
1230
+ * layout and no widths, the parts that do not count taken out, and every
1231
+ * style is put back before the function returns: nothing is painted in
1232
+ * between, and slot content that draws a block or a flex box counts with the
1233
+ * width its content needs, not the width its cell gives it. Only `fields`
1234
+ * when given; a column with no box (the table not displayed) is left out.
1235
+ */
1236
+ var measureColumnWidths = (table, fields) => {
1237
+ const widths = {};
1238
+ if (!table?.tHead) return widths;
1239
+ const wanted = fields ? new Set(fields) : null;
1240
+ const headers = Array.from(table.querySelectorAll(":scope > thead > tr > th[data-field]"));
1241
+ const swap = styleSwap();
1242
+ try {
1243
+ swap.set(table, "table-layout", "auto");
1244
+ swap.set(table, "width", "max-content");
1245
+ swap.set(table, "min-width", "0");
1246
+ for (const th of headers) swap.set(th, "width", "auto");
1247
+ for (const part of Array.from(table.querySelectorAll(notCounted))) swap.set(part, "display", "none");
1248
+ for (const th of headers) {
1249
+ const field = th.dataset.field;
1250
+ if (field === void 0 || wanted && !wanted.has(field)) continue;
1251
+ const width = th.getBoundingClientRect().width;
1252
+ if (width > 0) widths[field] = Math.ceil(width);
1253
+ }
1254
+ } finally {
1255
+ swap.restore();
1256
+ }
1257
+ return widths;
1258
+ };
1259
+ //#endregion
1190
1260
  //#region src/resize/use-column-resize.ts
1191
1261
  /** Default `Column.minWidth`, in pixels. */
1192
1262
  var defaultMinWidth = 40;
@@ -1207,29 +1277,6 @@ var maxWidthOf = (column) => {
1207
1277
  return max === void 0 ? void 0 : Math.max(max, minWidthOf(column));
1208
1278
  };
1209
1279
  var clampWidth = (column, width) => Math.round(Math.min(maxWidthOf(column) ?? Infinity, Math.max(minWidthOf(column), width)));
1210
- /** Width of the content of a cell up to `until` (exclusive), padding and border included. */
1211
- var contentWidth = (cell, until) => {
1212
- const range = document.createRange();
1213
- range.selectNodeContents(cell);
1214
- if (until) range.setEndBefore(until);
1215
- const style = getComputedStyle(cell);
1216
- return [
1217
- style.paddingLeft,
1218
- style.paddingRight,
1219
- style.borderLeftWidth,
1220
- style.borderRightWidth
1221
- ].reduce((sum, value) => sum + (parseFloat(value) || 0), range.getBoundingClientRect().width);
1222
- };
1223
- /**
1224
- * The widest rendered content of a column: the header label (the part before
1225
- * the filter row and the handle) and the column's cells in this table's own
1226
- * body rows. Only the rows given are measured (C-50).
1227
- */
1228
- var autofitWidth = (th, field) => {
1229
- let widest = contentWidth(th, th.querySelector(":scope > .qt-filter, :scope > .qt-resize-handle"));
1230
- for (const cell of th.closest("table")?.tBodies[0]?.querySelectorAll(`:scope > tr[data-row-index] > td[data-field="${CSS.escape(field)}"]`) ?? []) widest = Math.max(widest, contentWidth(cell));
1231
- return Math.ceil(widest);
1232
- };
1233
1280
  var cellOf = (event) => event.currentTarget.closest("th");
1234
1281
  /**
1235
1282
  * Which edge of the column the handle stands for: `1` for the right edge,
@@ -1316,7 +1363,8 @@ var useColumnResize = (options) => {
1316
1363
  if (th && (sign || key === "Enter" || event.type === "dblclick")) {
1317
1364
  event.preventDefault();
1318
1365
  const from = th.getBoundingClientRect().width;
1319
- commit(column, from, sign ? Math.round(from) + sign * (event.shiftKey ? 50 : 10) : autofitWidth(th, column.field));
1366
+ const to = sign ? Math.round(from) + sign * (event.shiftKey ? 50 : 10) : measureColumnWidths(th.closest("table"), [column.field])[column.field];
1367
+ if (to !== void 0) commit(column, from, to);
1320
1368
  }
1321
1369
  };
1322
1370
  /**
@@ -1488,6 +1536,7 @@ var table_header_default = /* @__PURE__ */ defineComponent({
1488
1536
  utilities: {},
1489
1537
  hasPinned: { type: Boolean },
1490
1538
  offsets: {},
1539
+ autoWidths: {},
1491
1540
  slotNames: {}
1492
1541
  },
1493
1542
  setup(__props) {
@@ -1516,9 +1565,14 @@ var table_header_default = /* @__PURE__ */ defineComponent({
1516
1565
  const hasFilter = (column) => props.filterable && column.filterable !== false;
1517
1566
  const hostsClearAll = (utility) => props.filterable && utility === props.utilities.find((candidate) => candidate !== "select");
1518
1567
  const utilityAttrs = (utility) => pinAttrs(props.hasPinned ? "left" : false, props.offsets[utilityKey(utility)]);
1568
+ const widthOf = (column) => {
1569
+ if (column.width) return column.width;
1570
+ const auto = props.autoWidths[column.field];
1571
+ return auto === void 0 ? void 0 : `${auto}px`;
1572
+ };
1519
1573
  const cellAttrs = (column) => {
1520
1574
  const preview = resize.preview.value;
1521
- return pinAttrs(sideOf(column), props.offsets[column.field], preview?.field === column.field ? `${preview.width}px` : column.width);
1575
+ return pinAttrs(sideOf(column), props.offsets[column.field], preview?.field === column.field ? `${preview.width}px` : widthOf(column));
1522
1576
  };
1523
1577
  const headerSlotProps = (column) => ({
1524
1578
  column,
@@ -1873,6 +1927,119 @@ var useColumnReorder = (options) => {
1873
1927
  };
1874
1928
  };
1875
1929
  //#endregion
1930
+ //#region src/resize/use-auto-width.ts
1931
+ /**
1932
+ * The query without what moves inside one result: the paging position, the
1933
+ * page size and the sort. Two queries alike in the rest ask for the same
1934
+ * rows, in another order or slice (C-100).
1935
+ */
1936
+ var resultOf = (query) => isCursorQuery(query) ? {
1937
+ ...query,
1938
+ cursor: null,
1939
+ pageSize: 0,
1940
+ sort: null
1941
+ } : {
1942
+ ...query,
1943
+ page: 1,
1944
+ pageSize: 0,
1945
+ sort: null
1946
+ };
1947
+ var sameWidths = (a, b) => {
1948
+ const keys = Object.keys(a);
1949
+ return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]);
1950
+ };
1951
+ var none = Object.freeze({});
1952
+ /**
1953
+ * Automatic column widths (C-100, ADR 0014). With `autoWidth`, every visible
1954
+ * column without `Column.width` is measured (C-99) after the drawn rows
1955
+ * change, clamped as a resize is (C-49), and drawn at that width. The widths
1956
+ * are UI state (P2): never emitted, never written to `columns`, gone when
1957
+ * `autoWidth` is off or the column gets a width. Within one result (the query
1958
+ * apart from its paging position, page size and sort) they only grow, so a
1959
+ * page, a sort or a live tick never narrows a column.
1960
+ */
1961
+ var useAutoWidth = (options) => {
1962
+ const widths = shallowRef(none);
1963
+ /** The result the widths were last measured for, `null` before one. */
1964
+ let result = null;
1965
+ /** The next measure starts over: the result moved before it could run. */
1966
+ let fromScratch = false;
1967
+ /** A measure found the table not displayed: measure once it is. */
1968
+ let waiting = false;
1969
+ let observer = null;
1970
+ const autoColumns = () => options.columns().filter((column) => !column.width);
1971
+ const clear = () => {
1972
+ widths.value = none;
1973
+ result = null;
1974
+ fromScratch = false;
1975
+ waiting = false;
1976
+ observer?.disconnect();
1977
+ observer = null;
1978
+ };
1979
+ const measure = (rowsChanged) => {
1980
+ const table = options.table.value;
1981
+ const columns = autoColumns();
1982
+ if (!table || columns.length === 0) return;
1983
+ const query = options.query();
1984
+ if (result === null || rowsChanged && (options.rowsUpdate() === "reset" || !sameQuery(resultOf(result), resultOf(query)))) {
1985
+ fromScratch = true;
1986
+ result = cloneQuery(query);
1987
+ }
1988
+ if (options.rows().length === 0) return;
1989
+ const measured = measureColumnWidths(table, columns.map((column) => column.field));
1990
+ if (Object.keys(measured).length === 0) {
1991
+ waiting = true;
1992
+ return;
1993
+ }
1994
+ waiting = false;
1995
+ const previous = widths.value;
1996
+ const next = {};
1997
+ for (const column of columns) {
1998
+ const width = measured[column.field];
1999
+ if (width === void 0) continue;
2000
+ const fitted = clampWidth({
2001
+ ...column,
2002
+ maxWidth: limitOf(column.maxWidth) ?? options.max()
2003
+ }, width);
2004
+ next[column.field] = fromScratch ? fitted : Math.max(fitted, previous[column.field] ?? 0);
2005
+ }
2006
+ fromScratch = false;
2007
+ if (!sameWidths(next, previous)) widths.value = next;
2008
+ };
2009
+ const observe = (table) => {
2010
+ observer?.disconnect();
2011
+ observer = null;
2012
+ if (!table || typeof ResizeObserver === "undefined") return;
2013
+ observer = new ResizeObserver(() => {
2014
+ if (waiting) measure(false);
2015
+ });
2016
+ observer.observe(table);
2017
+ };
2018
+ watch([
2019
+ () => options.enabled(),
2020
+ () => options.max(),
2021
+ () => options.table.value,
2022
+ () => options.rows(),
2023
+ () => autoColumns().map((column) => column.field).join("|")
2024
+ ], ([enabled, max, table, rows], [wasEnabled, oldMax, oldTable, oldRows]) => {
2025
+ if (!enabled) {
2026
+ if (wasEnabled) clear();
2027
+ return;
2028
+ }
2029
+ if (!wasEnabled || max !== oldMax) fromScratch = true;
2030
+ if (table !== oldTable || !wasEnabled) observe(table);
2031
+ measure(rows !== oldRows);
2032
+ }, {
2033
+ immediate: true,
2034
+ flush: "post"
2035
+ });
2036
+ onBeforeUnmount(() => {
2037
+ observer?.disconnect();
2038
+ observer = null;
2039
+ });
2040
+ return { widths };
2041
+ };
2042
+ //#endregion
1876
2043
  //#region src/use-query-table.ts
1877
2044
  /** The TanStack features of a Query Table, the two plugins included. */
1878
2045
  var features = tableFeatures({
@@ -3052,6 +3219,11 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3052
3219
  type: Boolean,
3053
3220
  default: false
3054
3221
  },
3222
+ autoWidth: {
3223
+ type: Boolean,
3224
+ default: false
3225
+ },
3226
+ autoWidthMax: { default: void 0 },
3055
3227
  reorderable: {
3056
3228
  type: Boolean,
3057
3229
  default: false
@@ -3182,6 +3354,15 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3182
3354
  if (next) emit("update:columns", next, "resize");
3183
3355
  }
3184
3356
  });
3357
+ const autoWidth = useAutoWidth({
3358
+ enabled: () => props.autoWidth,
3359
+ max: () => props.autoWidthMax,
3360
+ table: tableEl,
3361
+ columns: () => visibleColumns.value,
3362
+ rows: () => props.rows,
3363
+ query: () => props.query,
3364
+ rowsUpdate: () => props.rowsUpdate
3365
+ });
3185
3366
  const rowWindow = useRowWindow({
3186
3367
  virtual: () => props.virtual,
3187
3368
  table: tableEl,
@@ -3269,7 +3450,8 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3269
3450
  filters.flushAll();
3270
3451
  },
3271
3452
  scrollToIndex: rowWindow.scrollToIndex,
3272
- loadMore: infinite.loadMore
3453
+ loadMore: infinite.loadMore,
3454
+ measureColumnWidths: (fields) => measureColumnWidths(tableEl.value, fields)
3273
3455
  });
3274
3456
  return (_ctx, _cache) => {
3275
3457
  return openBlock(), createElementBlock("div", {
@@ -3292,6 +3474,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3292
3474
  utilities: utilities.value,
3293
3475
  "has-pinned": unref(hasPinned),
3294
3476
  offsets: unref(offsets),
3477
+ "auto-widths": unref(autoWidth).widths.value,
3295
3478
  "slot-names": headerSlotNames(),
3296
3479
  "aria-rowindex": unref(rowWindow).enabled() ? 1 : void 0
3297
3480
  }, {
@@ -3306,6 +3489,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3306
3489
  "utilities",
3307
3490
  "has-pinned",
3308
3491
  "offsets",
3492
+ "auto-widths",
3309
3493
  "slot-names",
3310
3494
  "aria-rowindex"
3311
3495
  ])], 32),