@dolusoft/query-table 3.6.0 → 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";
@@ -1536,6 +1536,7 @@ var table_header_default = /* @__PURE__ */ defineComponent({
1536
1536
  utilities: {},
1537
1537
  hasPinned: { type: Boolean },
1538
1538
  offsets: {},
1539
+ autoWidths: {},
1539
1540
  slotNames: {}
1540
1541
  },
1541
1542
  setup(__props) {
@@ -1564,9 +1565,14 @@ var table_header_default = /* @__PURE__ */ defineComponent({
1564
1565
  const hasFilter = (column) => props.filterable && column.filterable !== false;
1565
1566
  const hostsClearAll = (utility) => props.filterable && utility === props.utilities.find((candidate) => candidate !== "select");
1566
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
+ };
1567
1573
  const cellAttrs = (column) => {
1568
1574
  const preview = resize.preview.value;
1569
- 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));
1570
1576
  };
1571
1577
  const headerSlotProps = (column) => ({
1572
1578
  column,
@@ -1921,6 +1927,119 @@ var useColumnReorder = (options) => {
1921
1927
  };
1922
1928
  };
1923
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
1924
2043
  //#region src/use-query-table.ts
1925
2044
  /** The TanStack features of a Query Table, the two plugins included. */
1926
2045
  var features = tableFeatures({
@@ -3100,6 +3219,11 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3100
3219
  type: Boolean,
3101
3220
  default: false
3102
3221
  },
3222
+ autoWidth: {
3223
+ type: Boolean,
3224
+ default: false
3225
+ },
3226
+ autoWidthMax: { default: void 0 },
3103
3227
  reorderable: {
3104
3228
  type: Boolean,
3105
3229
  default: false
@@ -3230,6 +3354,15 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3230
3354
  if (next) emit("update:columns", next, "resize");
3231
3355
  }
3232
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
+ });
3233
3366
  const rowWindow = useRowWindow({
3234
3367
  virtual: () => props.virtual,
3235
3368
  table: tableEl,
@@ -3341,6 +3474,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3341
3474
  utilities: utilities.value,
3342
3475
  "has-pinned": unref(hasPinned),
3343
3476
  offsets: unref(offsets),
3477
+ "auto-widths": unref(autoWidth).widths.value,
3344
3478
  "slot-names": headerSlotNames(),
3345
3479
  "aria-rowindex": unref(rowWindow).enabled() ? 1 : void 0
3346
3480
  }, {
@@ -3355,6 +3489,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3355
3489
  "utilities",
3356
3490
  "has-pinned",
3357
3491
  "offsets",
3492
+ "auto-widths",
3358
3493
  "slot-names",
3359
3494
  "aria-rowindex"
3360
3495
  ])], 32),