@dolusoft/query-table 3.11.0 → 3.12.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.
@@ -3,6 +3,174 @@ import { cloneQuery, columnTypeOf, columnTypeOf as columnTypeOf$1, conditionLabe
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";
6
+ //#region src/body/use-row-navigation.ts
7
+ /**
8
+ * The first focusable element of a row, when no `focus` selector is given.
9
+ * Controls that use the arrow keys themselves (text fields, `select`, radio
10
+ * buttons, editable content) are left out: they keep their keys (C-109).
11
+ */
12
+ var focusable$1 = [
13
+ "a[href]",
14
+ "button:not([disabled])",
15
+ "input[type=\"checkbox\"]:not([disabled])",
16
+ "[tabindex]:not([tabindex=\"-1\"]):not(input, select, textarea, [contenteditable])"
17
+ ].join(", ");
18
+ var keys = /* @__PURE__ */ new Set([
19
+ "ArrowUp",
20
+ "ArrowDown",
21
+ "Home",
22
+ "End"
23
+ ]);
24
+ /** Frames a move waits for its row to be drawn and to keep the focus. */
25
+ var maxFrames = 30;
26
+ /** Frames the focus must stay on the target before a move is done. */
27
+ var keepFrames = 2;
28
+ /**
29
+ * Keyboard navigation between the data rows (C-109 to C-111): one `keydown`
30
+ * listener on the `tbody`, only while `rowNavigation` is on. A move goes by
31
+ * the row's key, not its index: the rows may change while a virtual body
32
+ * draws the row (C-110), and the focus is put back when moving DOM nodes
33
+ * drops it.
34
+ */
35
+ var useRowNavigation = (o) => {
36
+ const settings = computed(() => {
37
+ const value = o.rowNavigation();
38
+ return value === true ? {} : value || null;
39
+ });
40
+ const enabled = () => settings.value !== null;
41
+ const body = () => o.table.value?.tBodies[0] ?? null;
42
+ /** The focus target of a drawn row, or `null`. */
43
+ const targetOf = (tr) => tr.querySelector(settings.value?.focus ?? focusable$1);
44
+ /** The data row of the body that holds `element`, or `null`. */
45
+ const rowOf = (element, tbody) => {
46
+ let tr = element?.closest?.("tr") ?? null;
47
+ while (tr && tr.parentElement !== tbody) tr = tr.parentElement?.closest("tr") ?? null;
48
+ return tr?.hasAttribute("data-row-index") ? tr : null;
49
+ };
50
+ /** Position in drawing order of the move's row, or -1. */
51
+ const positionOf = (rows, m) => o.hasRowKey() ? rows.findIndex((item) => o.keyOf(item.row, item.index) === m.key) : rows.findIndex((item) => item.row === m.row);
52
+ /** The next stop from `from` for `key`: a row `skip` does not name. */
53
+ const stopFrom = (rows, from, key) => {
54
+ const skip = settings.value?.skip;
55
+ const stop = (p) => !skip || !skip(rows[p].row, rows[p].index);
56
+ const step = key === "ArrowUp" || key === "End" ? -1 : 1;
57
+ let p = key === "Home" ? 0 : key === "End" ? rows.length - 1 : from + step;
58
+ while (p >= 0 && p < rows.length && !stop(p)) p += step;
59
+ return p >= 0 && p < rows.length ? p : -1;
60
+ };
61
+ let move = null;
62
+ let frame = 0;
63
+ const stop = () => {
64
+ move = null;
65
+ cancelAnimationFrame(frame);
66
+ frame = 0;
67
+ removeEventListener("pointerdown", stop, true);
68
+ };
69
+ const again = () => {
70
+ if (!frame) frame = requestAnimationFrame(tick);
71
+ };
72
+ /**
73
+ * The focus is where the move left it, or nowhere: a click or a script
74
+ * that moved it elsewhere ends the move.
75
+ */
76
+ const free = (m) => {
77
+ const active = document.activeElement;
78
+ return !active || active === document.body || active === m.origin || active === m.landed || !active.isConnected;
79
+ };
80
+ /**
81
+ * Puts the focus on the move's row; `true` when it has it. A row of a
82
+ * virtual body that is not drawn is scrolled to (C-110): again when the
83
+ * window dropped the scroll because `rows` changed (C-87), and right away
84
+ * with `scroll`.
85
+ */
86
+ const land = (m, scroll = false) => {
87
+ const tbody = body();
88
+ const rows = o.bodyRows();
89
+ const p = positionOf(rows, m);
90
+ if (!tbody || p < 0) {
91
+ stop();
92
+ return false;
93
+ }
94
+ const { index, pinned } = rows[p];
95
+ const tr = tbody.querySelector(`:scope > tr[data-row-index="${index}"]`);
96
+ if (!tr) {
97
+ if (o.virtual() && !pinned && (scroll || !o.busy())) o.scrollToIndex(index, { align: "auto" });
98
+ return false;
99
+ }
100
+ const target = targetOf(tr);
101
+ if (!target) {
102
+ stop();
103
+ return false;
104
+ }
105
+ target.focus({ preventScroll: true });
106
+ if (document.activeElement !== target) return false;
107
+ const first = m.landed === null;
108
+ m.landed = target;
109
+ if (first) {
110
+ o.reveal(index);
111
+ o.onFocus({
112
+ row: rows[p].row,
113
+ rowIndex: index,
114
+ key: m.pressed
115
+ });
116
+ }
117
+ return true;
118
+ };
119
+ function tick() {
120
+ frame = 0;
121
+ const m = move;
122
+ if (!m) return;
123
+ if (++m.frames > maxFrames || !free(m)) {
124
+ stop();
125
+ return;
126
+ }
127
+ if (m.landed && document.activeElement === m.landed) {
128
+ if (!o.busy() && ++m.kept >= keepFrames) {
129
+ stop();
130
+ return;
131
+ }
132
+ again();
133
+ return;
134
+ }
135
+ if (!o.busy()) land(m);
136
+ if (move) again();
137
+ }
138
+ const onKeyDown = (event) => {
139
+ if (!enabled() || !keys.has(event.key) || event.defaultPrevented || event.isComposing || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
140
+ const tbody = event.currentTarget;
141
+ const target = event.target;
142
+ const waiting = move && !move.landed && target === move.origin ? move : null;
143
+ const tr = rowOf(target, tbody);
144
+ if (!waiting && (!tr || targetOf(tr) !== target)) return;
145
+ const rows = o.bodyRows();
146
+ const from = waiting ? positionOf(rows, waiting) : rows.findIndex((item) => item.index === Number(tr.getAttribute("data-row-index")));
147
+ if (from < 0) return;
148
+ event.preventDefault();
149
+ const to = stopFrom(rows, from, event.key);
150
+ if (to < 0 || to === from) return;
151
+ stop();
152
+ const { row, index } = rows[to];
153
+ const m = {
154
+ key: o.keyOf(row, index),
155
+ row,
156
+ pressed: event.key,
157
+ origin: waiting ? waiting.origin : target,
158
+ landed: null,
159
+ frames: 0,
160
+ kept: 0
161
+ };
162
+ move = m;
163
+ addEventListener("pointerdown", stop, true);
164
+ land(m, true);
165
+ if (move) again();
166
+ };
167
+ onBeforeUnmount(stop);
168
+ return {
169
+ enabled,
170
+ onKeyDown
171
+ };
172
+ };
173
+ //#endregion
6
174
  //#region src/columns/column-layout.ts
7
175
  var sideOf = (column) => column.pinned === "left" ? "left" : column.pinned === "right" ? "right" : false;
8
176
  /** A copy of `column` without `key`: a default is removed, never set (C-68). */
@@ -2496,6 +2664,7 @@ var isDevelopment$1 = () => {
2496
2664
  return true;
2497
2665
  }
2498
2666
  };
2667
+ var automaticLayout = "[QueryTable] `scrollBody`: the body table has the automatic layout, so its columns follow its rows, not the header. Give the body and footer tables `table-layout: fixed` and a width that is a length or a percentage, such as `width: 100%`: with `width: auto` or `max-content` the browser uses the automatic layout whatever `table-layout` says (C-101).";
2499
2668
  /**
2500
2669
  * Header, body and footer of `scrollBody` scroll sideways together (C-101):
2501
2670
  * one passive `scroll` listener on each wrapper sets the `scrollLeft` of the
@@ -2544,12 +2713,43 @@ var useScrollBody = (options) => {
2544
2713
  const table = options.bodyTable();
2545
2714
  if (table && !warned && isDevelopment$1() && getComputedStyle(table).tableLayout !== "fixed") {
2546
2715
  warned = true;
2547
- console.warn("[QueryTable] `scrollBody` with `table-layout: auto` on the body table: its columns follow its rows, not the header. Set `table-layout: fixed` (C-101).");
2716
+ console.warn(automaticLayout);
2548
2717
  }
2549
2718
  }, {
2550
2719
  flush: "post",
2551
2720
  immediate: true
2552
2721
  });
2722
+ /**
2723
+ * `table-layout: fixed` applies only to a table with a definite width:
2724
+ * under `width: auto` or `max-content` the browser lays the body out
2725
+ * automatically while `tableLayout` still reads `fixed`, and a cell wider
2726
+ * than its `col` widens its column and the table. That shows once the
2727
+ * rows hold such a cell, so it is checked whenever the rows or the `col`
2728
+ * widths change, against `col` widths that match the header cells (not
2729
+ * ones a change of the drawn columns has made stale until measured).
2730
+ */
2731
+ const checkLayout = () => {
2732
+ const head = options.headTable();
2733
+ const body = options.bodyTable();
2734
+ if (warned || !head || !body) return;
2735
+ const cells = head.querySelectorAll(":scope > thead > tr > *");
2736
+ const widths = options.colWidths();
2737
+ if (cells.length === 0 || cells.length !== widths.length || [...cells].some((cell, i) => {
2738
+ const width = widths[i];
2739
+ return width === void 0 || Math.abs(cell.getBoundingClientRect().width - width) >= .5;
2740
+ })) return;
2741
+ if (body.getBoundingClientRect().width - head.getBoundingClientRect().width >= 1) {
2742
+ warned = true;
2743
+ console.warn(automaticLayout);
2744
+ }
2745
+ };
2746
+ if (isDevelopment$1()) watch(() => options.enabled() ? [
2747
+ options.colWidths(),
2748
+ options.rows(),
2749
+ options.bodyTable()
2750
+ ] : null, (sources) => {
2751
+ if (sources) checkLayout();
2752
+ }, { flush: "post" });
2553
2753
  onBeforeUnmount(detach);
2554
2754
  };
2555
2755
  //#endregion
@@ -3462,12 +3662,12 @@ var useRowWindow = (o) => {
3462
3662
  let pending = null;
3463
3663
  /** Frames a `scrollToIndex` waits for its row before it is given up. */
3464
3664
  const pendingFrames = 6;
3465
- const viewport = () => {
3466
- if (container === null || container === window) return {
3665
+ const viewport = (target = container) => {
3666
+ if (target === null || target === window) return {
3467
3667
  top: 0,
3468
3668
  height: globalThis.innerHeight
3469
3669
  };
3470
- const element = container;
3670
+ const element = target;
3471
3671
  return {
3472
3672
  top: element.getBoundingClientRect().top + element.clientTop,
3473
3673
  height: element.clientHeight
@@ -3479,16 +3679,16 @@ var useRowWindow = (o) => {
3479
3679
  return tbody.getBoundingClientRect().bottom;
3480
3680
  };
3481
3681
  const scrollTopOf = (target) => target === window ? window.scrollY : target.scrollTop;
3482
- const scrollBy = (delta) => {
3483
- if (Math.abs(delta) < 1 || !container) return;
3484
- container.scrollTo({
3485
- top: scrollTopOf(container) + delta,
3682
+ const scrollBy = (delta, target = container) => {
3683
+ if (Math.abs(delta) < 1 || !target) return;
3684
+ target.scrollTo({
3685
+ top: scrollTopOf(target) + delta,
3486
3686
  behavior: "instant"
3487
3687
  });
3488
3688
  };
3489
3689
  /** How far to scroll to show an item at `top` (client) with `height`. */
3490
- const alignDelta = (top, height, align, groupCover = 0) => {
3491
- const view = viewport();
3690
+ const alignDelta = (top, height, align, groupCover = 0, target = container) => {
3691
+ const view = viewport(target);
3492
3692
  const head = o.table.value?.tHead?.getBoundingClientRect();
3493
3693
  const cover = head && head.top <= view.top + 1 && head.bottom > view.top ? head.bottom - view.top : 0;
3494
3694
  const viewTop = view.top + cover + groupCover;
@@ -3515,6 +3715,16 @@ var useRowWindow = (o) => {
3515
3715
  const g = groups.value;
3516
3716
  return g && g.groupOf[position] >= 0 && !startsGroup(position) ? stickyGroupCover(tbody) : 0;
3517
3717
  };
3718
+ /**
3719
+ * Scrolls `target` so the drawn row `tr` (at a center position, or `-1`
3720
+ * for a pinned row) is shown as `align` asks, below a sticky header and a
3721
+ * sticky group row (C-87, C-104).
3722
+ */
3723
+ const alignRow = (tbody, tr, position, align, target, whole = true) => {
3724
+ const above = startsGroup(position) ? tr.previousElementSibling : null;
3725
+ const head = above?.classList.contains("qt-group-row") ? above : null;
3726
+ scrollBy(alignDelta((head ?? tr).getBoundingClientRect().top, (whole ? itemHeight(tr) : tr.getBoundingClientRect().height) + (head ? head.getBoundingClientRect().height : 0), align, head ? 0 : coverOf(position, tbody), target), target);
3727
+ };
3518
3728
  /** After a render: correct the drift (C-84) or a `scrollToIndex` (C-87). */
3519
3729
  const settle = () => {
3520
3730
  const tbody = body();
@@ -3528,9 +3738,7 @@ var useRowWindow = (o) => {
3528
3738
  if (tr) {
3529
3739
  const { align, position } = pending;
3530
3740
  pending = null;
3531
- const above = startsGroup(position) ? tr.previousElementSibling : null;
3532
- const head = above?.classList.contains("qt-group-row") ? above : null;
3533
- scrollBy(alignDelta((head ?? tr).getBoundingClientRect().top, itemHeight(tr) + (head ? head.getBoundingClientRect().height : 0), align, head ? 0 : coverOf(position, tbody)));
3741
+ alignRow(tbody, tr, position, align, container);
3534
3742
  return;
3535
3743
  }
3536
3744
  if (++pending.tries <= pendingFrames) {
@@ -3842,6 +4050,24 @@ var useRowWindow = (o) => {
3842
4050
  scrollBy(alignDelta(itemsTop(tbody) + offsets[position], heights[position], align, coverOf(position, tbody)));
3843
4051
  schedule();
3844
4052
  };
4053
+ /**
4054
+ * Shows the drawn row with this index in `rows` whole, below a sticky
4055
+ * header and a sticky group row, in its scroll container (C-110); with
4056
+ * `virtual` that is the container of C-85, else the nearest scrolling
4057
+ * ancestor of the table or the window. Only the row: an open subtable row
4058
+ * taller than the view would push it out. A `scrollToIndex` still waiting
4059
+ * is dropped, so it cannot move the view away after. A row not drawn does
4060
+ * nothing.
4061
+ */
4062
+ const reveal = (index) => {
4063
+ const tbody = body();
4064
+ const table = o.table.value;
4065
+ const tr = tbody?.querySelector(`:scope > tr[data-row-index="${index}"]`);
4066
+ if (!tbody || !table || !tr) return;
4067
+ pending = null;
4068
+ const target = enabled() && container ? container : findContainer(table);
4069
+ alignRow(tbody, tr, tr.hasAttribute("data-pinned-row") ? -1 : positionOf(index), "auto", target, false);
4070
+ };
3845
4071
  return {
3846
4072
  enabled,
3847
4073
  drawn,
@@ -3852,6 +4078,9 @@ var useRowWindow = (o) => {
3852
4078
  /** Group rows of the body, drawn or not (C-104). */
3853
4079
  groupCount,
3854
4080
  scrollToIndex,
4081
+ reveal,
4082
+ /** A `scrollToIndex` or a window update is still to come (C-110). */
4083
+ busy: () => pending !== null || frame !== 0,
3855
4084
  onFocusIn,
3856
4085
  onFocusOut
3857
4086
  };
@@ -3940,6 +4169,10 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3940
4169
  type: [Boolean, Object],
3941
4170
  default: false
3942
4171
  },
4172
+ rowNavigation: {
4173
+ type: [Boolean, Object],
4174
+ default: false
4175
+ },
3943
4176
  scrollBody: {
3944
4177
  type: Boolean,
3945
4178
  default: false
@@ -3962,7 +4195,8 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3962
4195
  "headerContextMenu",
3963
4196
  "columnResize",
3964
4197
  "update:columns",
3965
- "update:rowPinning"
4198
+ "update:rowPinning",
4199
+ "rowFocus"
3966
4200
  ],
3967
4201
  setup(__props, { expose: __expose, emit: __emit }) {
3968
4202
  const props = __props;
@@ -4051,7 +4285,10 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4051
4285
  body: bodyWrap.value,
4052
4286
  foot: footWrap.value
4053
4287
  }),
4054
- bodyTable: () => bodyTableEl.value
4288
+ headTable: () => tableEl.value,
4289
+ bodyTable: () => bodyTableEl.value,
4290
+ colWidths: () => colWidths.value,
4291
+ rows: () => props.rows
4055
4292
  });
4056
4293
  const resize = useColumnResize({
4057
4294
  resizable: () => props.resizable,
@@ -4082,6 +4319,20 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4082
4319
  isExpanded: expansion.isExpanded,
4083
4320
  groupBy: () => props.groupBy
4084
4321
  });
4322
+ const navigation = useRowNavigation({
4323
+ rowNavigation: () => props.rowNavigation,
4324
+ table: bodyTable,
4325
+ bodyRows: () => state.rowPinning.rows.value,
4326
+ keyOf: expansion.keyOf,
4327
+ hasRowKey: () => props.rowKey !== void 0,
4328
+ virtual: rowWindow.enabled,
4329
+ scrollToIndex: rowWindow.scrollToIndex,
4330
+ reveal: rowWindow.reveal,
4331
+ busy: rowWindow.busy,
4332
+ onFocus: (payload) => emit("rowFocus", payload)
4333
+ });
4334
+ /** The `tbody` listener of the navigation: none while it is off (C-111). */
4335
+ const navigationListeners = () => navigation.enabled() ? { keydown: navigation.onKeyDown } : {};
4085
4336
  const infinite = useLoadMore({
4086
4337
  infinite: () => props.infinite,
4087
4338
  rowKey: () => props.rowKey,
@@ -4232,7 +4483,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4232
4483
  key: i,
4233
4484
  style: normalizeStyle(width === void 0 ? void 0 : { width: `${width}px` })
4234
4485
  }, null, 4);
4235
- }), 128))]), createVNode(table_body_default, {
4486
+ }), 128))]), createVNode(table_body_default, mergeProps({
4236
4487
  rows: __props.rows,
4237
4488
  "body-rows": unref(rowWindow).drawn.value,
4238
4489
  "pin-row": unref(state).rowPinning.pin,
@@ -4261,12 +4512,12 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4261
4512
  onCellContextMenu: _cache[2] || (_cache[2] = (payload) => emit("cellContextMenu", payload)),
4262
4513
  onFocusin: unref(rowWindow).onFocusIn,
4263
4514
  onFocusout: unref(rowWindow).onFocusOut
4264
- }, createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4515
+ }, toHandlers(navigationListeners())), createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4265
4516
  return {
4266
4517
  name,
4267
4518
  fn: withCtx((p) => [renderSlot(_ctx.$slots, name, normalizeProps(guardReactiveProps(p)))])
4268
4519
  };
4269
- })]), 1032, [
4520
+ })]), 1040, [
4270
4521
  "rows",
4271
4522
  "body-rows",
4272
4523
  "pin-row",
@@ -4351,7 +4602,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4351
4602
  "slot-names",
4352
4603
  "aria-rowindex"
4353
4604
  ])], 32),
4354
- createVNode(table_body_default, {
4605
+ createVNode(table_body_default, mergeProps({
4355
4606
  rows: __props.rows,
4356
4607
  "body-rows": unref(rowWindow).drawn.value,
4357
4608
  "pin-row": unref(state).rowPinning.pin,
@@ -4379,12 +4630,12 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4379
4630
  onCellContextMenu: _cache[5] || (_cache[5] = (payload) => emit("cellContextMenu", payload)),
4380
4631
  onFocusin: unref(rowWindow).onFocusIn,
4381
4632
  onFocusout: unref(rowWindow).onFocusOut
4382
- }, createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4633
+ }, toHandlers(navigationListeners())), createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4383
4634
  return {
4384
4635
  name,
4385
4636
  fn: withCtx((p) => [renderSlot(_ctx.$slots, name, normalizeProps(guardReactiveProps(p)))])
4386
4637
  };
4387
- })]), 1032, [
4638
+ })]), 1040, [
4388
4639
  "rows",
4389
4640
  "body-rows",
4390
4641
  "pin-row",