@dolusoft/query-table 3.11.1 → 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). */
@@ -3494,12 +3662,12 @@ var useRowWindow = (o) => {
3494
3662
  let pending = null;
3495
3663
  /** Frames a `scrollToIndex` waits for its row before it is given up. */
3496
3664
  const pendingFrames = 6;
3497
- const viewport = () => {
3498
- if (container === null || container === window) return {
3665
+ const viewport = (target = container) => {
3666
+ if (target === null || target === window) return {
3499
3667
  top: 0,
3500
3668
  height: globalThis.innerHeight
3501
3669
  };
3502
- const element = container;
3670
+ const element = target;
3503
3671
  return {
3504
3672
  top: element.getBoundingClientRect().top + element.clientTop,
3505
3673
  height: element.clientHeight
@@ -3511,16 +3679,16 @@ var useRowWindow = (o) => {
3511
3679
  return tbody.getBoundingClientRect().bottom;
3512
3680
  };
3513
3681
  const scrollTopOf = (target) => target === window ? window.scrollY : target.scrollTop;
3514
- const scrollBy = (delta) => {
3515
- if (Math.abs(delta) < 1 || !container) return;
3516
- container.scrollTo({
3517
- 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,
3518
3686
  behavior: "instant"
3519
3687
  });
3520
3688
  };
3521
3689
  /** How far to scroll to show an item at `top` (client) with `height`. */
3522
- const alignDelta = (top, height, align, groupCover = 0) => {
3523
- const view = viewport();
3690
+ const alignDelta = (top, height, align, groupCover = 0, target = container) => {
3691
+ const view = viewport(target);
3524
3692
  const head = o.table.value?.tHead?.getBoundingClientRect();
3525
3693
  const cover = head && head.top <= view.top + 1 && head.bottom > view.top ? head.bottom - view.top : 0;
3526
3694
  const viewTop = view.top + cover + groupCover;
@@ -3547,6 +3715,16 @@ var useRowWindow = (o) => {
3547
3715
  const g = groups.value;
3548
3716
  return g && g.groupOf[position] >= 0 && !startsGroup(position) ? stickyGroupCover(tbody) : 0;
3549
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
+ };
3550
3728
  /** After a render: correct the drift (C-84) or a `scrollToIndex` (C-87). */
3551
3729
  const settle = () => {
3552
3730
  const tbody = body();
@@ -3560,9 +3738,7 @@ var useRowWindow = (o) => {
3560
3738
  if (tr) {
3561
3739
  const { align, position } = pending;
3562
3740
  pending = null;
3563
- const above = startsGroup(position) ? tr.previousElementSibling : null;
3564
- const head = above?.classList.contains("qt-group-row") ? above : null;
3565
- 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);
3566
3742
  return;
3567
3743
  }
3568
3744
  if (++pending.tries <= pendingFrames) {
@@ -3874,6 +4050,24 @@ var useRowWindow = (o) => {
3874
4050
  scrollBy(alignDelta(itemsTop(tbody) + offsets[position], heights[position], align, coverOf(position, tbody)));
3875
4051
  schedule();
3876
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
+ };
3877
4071
  return {
3878
4072
  enabled,
3879
4073
  drawn,
@@ -3884,6 +4078,9 @@ var useRowWindow = (o) => {
3884
4078
  /** Group rows of the body, drawn or not (C-104). */
3885
4079
  groupCount,
3886
4080
  scrollToIndex,
4081
+ reveal,
4082
+ /** A `scrollToIndex` or a window update is still to come (C-110). */
4083
+ busy: () => pending !== null || frame !== 0,
3887
4084
  onFocusIn,
3888
4085
  onFocusOut
3889
4086
  };
@@ -3972,6 +4169,10 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3972
4169
  type: [Boolean, Object],
3973
4170
  default: false
3974
4171
  },
4172
+ rowNavigation: {
4173
+ type: [Boolean, Object],
4174
+ default: false
4175
+ },
3975
4176
  scrollBody: {
3976
4177
  type: Boolean,
3977
4178
  default: false
@@ -3994,7 +4195,8 @@ var query_table_default = /* @__PURE__ */ defineComponent({
3994
4195
  "headerContextMenu",
3995
4196
  "columnResize",
3996
4197
  "update:columns",
3997
- "update:rowPinning"
4198
+ "update:rowPinning",
4199
+ "rowFocus"
3998
4200
  ],
3999
4201
  setup(__props, { expose: __expose, emit: __emit }) {
4000
4202
  const props = __props;
@@ -4117,6 +4319,20 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4117
4319
  isExpanded: expansion.isExpanded,
4118
4320
  groupBy: () => props.groupBy
4119
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 } : {};
4120
4336
  const infinite = useLoadMore({
4121
4337
  infinite: () => props.infinite,
4122
4338
  rowKey: () => props.rowKey,
@@ -4267,7 +4483,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4267
4483
  key: i,
4268
4484
  style: normalizeStyle(width === void 0 ? void 0 : { width: `${width}px` })
4269
4485
  }, null, 4);
4270
- }), 128))]), createVNode(table_body_default, {
4486
+ }), 128))]), createVNode(table_body_default, mergeProps({
4271
4487
  rows: __props.rows,
4272
4488
  "body-rows": unref(rowWindow).drawn.value,
4273
4489
  "pin-row": unref(state).rowPinning.pin,
@@ -4296,12 +4512,12 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4296
4512
  onCellContextMenu: _cache[2] || (_cache[2] = (payload) => emit("cellContextMenu", payload)),
4297
4513
  onFocusin: unref(rowWindow).onFocusIn,
4298
4514
  onFocusout: unref(rowWindow).onFocusOut
4299
- }, createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4515
+ }, toHandlers(navigationListeners())), createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4300
4516
  return {
4301
4517
  name,
4302
4518
  fn: withCtx((p) => [renderSlot(_ctx.$slots, name, normalizeProps(guardReactiveProps(p)))])
4303
4519
  };
4304
- })]), 1032, [
4520
+ })]), 1040, [
4305
4521
  "rows",
4306
4522
  "body-rows",
4307
4523
  "pin-row",
@@ -4386,7 +4602,7 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4386
4602
  "slot-names",
4387
4603
  "aria-rowindex"
4388
4604
  ])], 32),
4389
- createVNode(table_body_default, {
4605
+ createVNode(table_body_default, mergeProps({
4390
4606
  rows: __props.rows,
4391
4607
  "body-rows": unref(rowWindow).drawn.value,
4392
4608
  "pin-row": unref(state).rowPinning.pin,
@@ -4414,12 +4630,12 @@ var query_table_default = /* @__PURE__ */ defineComponent({
4414
4630
  onCellContextMenu: _cache[5] || (_cache[5] = (payload) => emit("cellContextMenu", payload)),
4415
4631
  onFocusin: unref(rowWindow).onFocusIn,
4416
4632
  onFocusout: unref(rowWindow).onFocusOut
4417
- }, createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4633
+ }, toHandlers(navigationListeners())), createSlots({ _: 2 }, [renderList(bodySlotNames(), (name) => {
4418
4634
  return {
4419
4635
  name,
4420
4636
  fn: withCtx((p) => [renderSlot(_ctx.$slots, name, normalizeProps(guardReactiveProps(p)))])
4421
4637
  };
4422
- })]), 1032, [
4638
+ })]), 1040, [
4423
4639
  "rows",
4424
4640
  "body-rows",
4425
4641
  "pin-row",