@itrocks/table 0.2.1 → 0.2.3

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.
@@ -5,7 +5,6 @@ type Direction = -1 | 0 | 1;
5
5
  interface Drag extends Press {
6
6
  cells: HTMLTableCellElement[];
7
7
  col?: HTMLTableColElement;
8
- count: number;
9
8
  direction: Direction;
10
9
  index: number;
11
10
  initial: number;
@@ -13,12 +12,12 @@ interface Drag extends Press {
13
12
  minimum: number;
14
13
  scroll?: HTMLElement;
15
14
  time?: number;
15
+ x: number;
16
16
  }
17
17
  interface Press {
18
18
  header: HTMLTableCellElement;
19
19
  id: number;
20
20
  origin: number;
21
- x: number;
22
21
  }
23
22
  export declare class TableColumnReorder extends Plugin<Table, TableColumnReorderOptions> {
24
23
  protected drag?: Drag;
@@ -26,11 +25,9 @@ export declare class TableColumnReorder extends Plugin<Table, TableColumnReorder
26
25
  protected press?: Press;
27
26
  protected suppressClick: boolean;
28
27
  protected animate(time: number): void;
29
- protected colGroups(): HTMLTableColElement[];
30
28
  defaultOptions(): TableColumnReorderOptions;
31
29
  protected finish(commit: boolean): void;
32
30
  init(): void;
33
- protected isCurrent(drag: Drag): boolean;
34
31
  protected move(drag: Drag, targetIndex: number): void;
35
32
  protected onPointer(event: PointerEvent): void;
36
33
  protected requestFrame(): void;
package/column-reorder.js CHANGED
@@ -1,11 +1,8 @@
1
1
  import { Plugin } from '../plugin/plugin.js';
2
2
  import { PluginOptions } from '../plugin/plugin.js';
3
3
  const clamp = (value) => Math.max(0, Math.min(1, value));
4
- const interactiveSelector = 'a, button, input, select, textarea, '
5
- + '[contenteditable]:not([contenteditable="false"]), [draggable="true"]';
6
4
  const maxScrollSpeed = 800;
7
5
  const penetration = 10;
8
- const rowSelector = ':scope > tbody > tr, :scope > tfoot > tr, :scope > thead > tr';
9
6
  const scrollZone = 50;
10
7
  let activeDrag;
11
8
  export class TableColumnReorder extends Plugin {
@@ -16,12 +13,6 @@ export class TableColumnReorder extends Plugin {
16
13
  animate(time) {
17
14
  this.frame = undefined;
18
15
  const drag = this.drag;
19
- if (!drag)
20
- return;
21
- if (!this.isCurrent(drag)) {
22
- this.finish(false);
23
- return;
24
- }
25
16
  const pointerDirection = drag.direction;
26
17
  drag.direction = 0;
27
18
  const scrollDirection = this.scroll(drag, time);
@@ -32,9 +23,6 @@ export class TableColumnReorder extends Plugin {
32
23
  if (scrollDirection)
33
24
  this.requestFrame();
34
25
  }
35
- colGroups() {
36
- return Array.from(this.of.element.children).filter(element => element.tagName === 'COLGROUP');
37
- }
38
26
  defaultOptions() {
39
27
  return new TableColumnReorderOptions();
40
28
  }
@@ -47,17 +35,12 @@ export class TableColumnReorder extends Plugin {
47
35
  if (!drag)
48
36
  return;
49
37
  const changed = drag.index !== drag.initial;
50
- const current = this.isCurrent(drag);
51
- if (!current)
52
- commit = false;
53
- if (!commit && changed && current)
38
+ if (!commit && changed)
54
39
  this.move(drag, drag.initial);
55
40
  for (const cell of drag.cells)
56
41
  cell.classList.remove('column-reorder');
57
- if (activeDrag === this) {
58
- activeDrag = undefined;
59
- document.documentElement.classList.remove('column-reorder');
60
- }
42
+ document.documentElement.classList.remove('column-reorder');
43
+ activeDrag = undefined;
61
44
  this.drag = undefined;
62
45
  if (this.of.element.hasPointerCapture(drag.id))
63
46
  this.of.element.releasePointerCapture(drag.id);
@@ -77,10 +60,8 @@ export class TableColumnReorder extends Plugin {
77
60
  if ((event.key !== 'Escape') || !this.drag)
78
61
  return;
79
62
  event.preventDefault();
80
- event.stopImmediatePropagation();
81
63
  this.finish(false);
82
64
  }, { capture: true });
83
- table.addEventListener(table.element, 'lostpointercapture', event => this.onPointer(event));
84
65
  table.addEventListener(document, 'pointercancel', event => this.onPointer(event), { capture: true });
85
66
  table.addEventListener(table.element, 'pointerdown', event => this.onPointer(event));
86
67
  table.addEventListener(document, 'pointermove', event => this.onPointer(event), { capture: true });
@@ -90,19 +71,6 @@ export class TableColumnReorder extends Plugin {
90
71
  this.suppressClick = false;
91
72
  });
92
73
  }
93
- isCurrent(drag) {
94
- const rows = this.rows();
95
- if ((drag.header.parentElement !== this.triggerRow()) || (rows.length !== drag.cells.length))
96
- return false;
97
- if (rows.some((row, index) => (row !== drag.cells[index].parentElement)
98
- || (row.cells.length !== drag.count) || (row.cells[drag.index] !== drag.cells[index])))
99
- return false;
100
- const groups = this.colGroups();
101
- if (!drag.col)
102
- return !groups.length;
103
- return (groups.length === 1) && (groups[0] === drag.col.parentElement)
104
- && (groups[0].children.length === drag.count) && (groups[0].children[drag.index] === drag.col);
105
- }
106
74
  move(drag, targetIndex) {
107
75
  const after = drag.index < targetIndex;
108
76
  for (const cell of drag.cells) {
@@ -117,33 +85,18 @@ export class TableColumnReorder extends Plugin {
117
85
  }
118
86
  onPointer(event) {
119
87
  if (event.type === 'pointerdown') {
120
- if (activeDrag || this.drag || this.press || (event.button !== 0) || !event.isPrimary)
121
- return;
122
- if (!(event.target instanceof Element))
88
+ if (activeDrag || this.press || (event.button !== 0))
123
89
  return;
124
90
  const header = event.target.closest('th, td');
125
- const interactive = event.target.closest(interactiveSelector);
126
- if (!header
127
- || (header.closest('table') !== this.of.element)
128
- || (header.parentElement !== this.triggerRow())
129
- || (interactive && header.contains(interactive)))
91
+ if (!header || (header.parentElement !== this.triggerRow()))
130
92
  return;
131
- this.press = { header, id: event.pointerId, origin: event.clientX, x: event.clientX };
93
+ this.press = { header, id: event.pointerId, origin: event.clientX };
132
94
  return;
133
95
  }
134
96
  const state = this.drag ?? this.press;
135
97
  if (!state || (event.pointerId !== state.id))
136
98
  return;
137
- if (event.type === 'lostpointercapture') {
138
- if (this.drag)
139
- this.finish(false);
140
- return;
141
- }
142
99
  if (event.type === 'pointercancel') {
143
- if (this.drag) {
144
- event.preventDefault();
145
- event.stopImmediatePropagation();
146
- }
147
100
  this.finish(false);
148
101
  return;
149
102
  }
@@ -153,11 +106,9 @@ export class TableColumnReorder extends Plugin {
153
106
  return;
154
107
  }
155
108
  event.preventDefault();
156
- event.stopImmediatePropagation();
157
109
  this.updatePointer(event.clientX);
158
- if ((this.frame !== undefined) || this.drag.direction) {
159
- if (this.frame !== undefined)
160
- cancelAnimationFrame(this.frame);
110
+ if (this.frame !== undefined) {
111
+ cancelAnimationFrame(this.frame);
161
112
  this.frame = undefined;
162
113
  this.animate(performance.now());
163
114
  }
@@ -167,10 +118,8 @@ export class TableColumnReorder extends Plugin {
167
118
  return;
168
119
  }
169
120
  if (!this.drag) {
170
- if (Math.abs(event.clientX - state.origin) < Math.max(0, this.options.threshold)) {
171
- state.x = event.clientX;
121
+ if (Math.abs(event.clientX - state.origin) < this.options.threshold)
172
122
  return;
173
- }
174
123
  this.press = undefined;
175
124
  this.start(state, event.clientX);
176
125
  }
@@ -184,16 +133,17 @@ export class TableColumnReorder extends Plugin {
184
133
  this.frame = requestAnimationFrame(time => this.animate(time));
185
134
  }
186
135
  rows() {
187
- return Array.from(this.of.element.querySelectorAll(rowSelector));
136
+ return Array.from(this.of.element.rows);
188
137
  }
189
138
  scroll(drag, time) {
190
139
  const container = drag.scroll;
191
140
  if (!container)
192
141
  return 0;
193
142
  const rect = container.getBoundingClientRect();
194
- const left = 1 - clamp((drag.x - rect.left) / scrollZone);
195
- const right = 1 - clamp((rect.right - drag.x) / scrollZone);
196
- const direction = ((left > 0) || (right > 0)) ? ((right > left) ? 1 : -1) : 0;
143
+ const left = clamp((rect.left + scrollZone - drag.x) / scrollZone);
144
+ const right = clamp((drag.x - rect.right + scrollZone) / scrollZone);
145
+ const speed = right - left;
146
+ const direction = Math.sign(speed);
197
147
  if (!direction) {
198
148
  drag.time = undefined;
199
149
  return 0;
@@ -203,43 +153,28 @@ export class TableColumnReorder extends Plugin {
203
153
  if (previousTime === undefined)
204
154
  return direction;
205
155
  const previousScroll = container.scrollLeft;
206
- container.scrollLeft += direction * maxScrollSpeed * Math.max(left, right) * (time - previousTime) / 1000;
156
+ container.scrollLeft += speed * maxScrollSpeed * (time - previousTime) / 1000;
207
157
  if (container.scrollLeft !== previousScroll)
208
158
  return direction;
209
159
  drag.time = undefined;
210
160
  return 0;
211
161
  }
212
162
  scrollContainer() {
213
- let element = this.of.element;
214
- while (element) {
163
+ for (let element = this.of.element; element; element = element.parentElement) {
215
164
  if (['auto', 'overlay', 'scroll'].includes(getComputedStyle(element).overflowX)
216
165
  && (element.scrollWidth > element.clientWidth)) {
217
166
  return element;
218
167
  }
219
- element = element.parentElement;
220
168
  }
221
169
  return undefined;
222
170
  }
223
171
  start(press, x) {
224
172
  if (activeDrag)
225
173
  return;
226
- const row = this.triggerRow();
227
- const headers = Array.from(row?.cells ?? []);
174
+ const headers = Array.from(this.triggerRow().cells);
228
175
  const initial = headers.indexOf(press.header);
229
176
  const rows = this.rows();
230
- if ((initial < 0)
231
- || !rows.length
232
- || rows.some(row => (row.cells.length !== headers.length)
233
- || Array.from(row.cells).some(cell => (cell.colSpan !== 1) || (cell.rowSpan !== 1))))
234
- return;
235
- const groups = this.colGroups();
236
- if (groups.length > 1)
237
- return;
238
- const columns = Array.from(groups[0]?.children ?? []);
239
- if (groups.length
240
- && ((columns.length !== headers.length)
241
- || columns.some(column => (column.tagName !== 'COL') || (column.span !== 1))))
242
- return;
177
+ const columns = this.of.element.querySelectorAll(':scope > colgroup > col');
243
178
  const reorderable = headers.map(header => this.options.isReorderable?.(header) ?? true);
244
179
  if (!reorderable[initial])
245
180
  return;
@@ -249,18 +184,12 @@ export class TableColumnReorder extends Plugin {
249
184
  maximum++;
250
185
  while ((minimum > 0) && reorderable[minimum - 1])
251
186
  minimum--;
252
- try {
253
- this.of.element.setPointerCapture(press.id);
254
- }
255
- catch {
256
- return;
257
- }
187
+ this.of.element.setPointerCapture(press.id);
258
188
  const drag = {
259
189
  ...press,
260
190
  cells: rows.map(row => row.cells[initial]),
261
191
  col: columns[initial],
262
- count: headers.length,
263
- direction: Math.sign(x - press.x),
192
+ direction: Math.sign(x - press.origin),
264
193
  index: initial,
265
194
  initial,
266
195
  maximum,
@@ -276,24 +205,14 @@ export class TableColumnReorder extends Plugin {
276
205
  this.requestFrame();
277
206
  }
278
207
  target(drag, direction) {
279
- const headers = Array.from(this.triggerRow()?.cells ?? []);
280
- const source = headers[drag.index]?.getBoundingClientRect();
281
- if (!source)
282
- return drag.index;
283
- const sourceCenter = (source.left + source.right) / 2;
284
- let distance = 0;
208
+ const headers = this.triggerRow().cells;
285
209
  let target = drag.index;
286
- for (let index = drag.minimum; index <= drag.maximum; index++) {
287
- if (index === drag.index)
288
- continue;
210
+ for (let index = drag.index + direction; (index >= drag.minimum) && (index <= drag.maximum); index += direction) {
289
211
  const rect = headers[index].getBoundingClientRect();
290
- const delta = (rect.left + rect.right) / 2 - sourceCenter;
291
212
  const edge = direction > 0 ? rect.left : rect.right;
292
- if ((Math.sign(delta) === direction) && (direction * (drag.x - edge) >= penetration)
293
- && (Math.abs(delta) > distance)) {
294
- distance = Math.abs(delta);
295
- target = index;
296
- }
213
+ if (direction * (drag.x - edge) < penetration)
214
+ break;
215
+ target = index;
297
216
  }
298
217
  return target;
299
218
  }
@@ -302,7 +221,7 @@ export class TableColumnReorder extends Plugin {
302
221
  }
303
222
  updatePointer(x) {
304
223
  const drag = this.drag;
305
- if (!drag || (x === drag.x))
224
+ if (x === drag.x)
306
225
  return;
307
226
  drag.direction = Math.sign(x - drag.x);
308
227
  drag.x = x;
package/package.json CHANGED
@@ -52,5 +52,5 @@
52
52
  "scripts": {
53
53
  "build": "tsc && sass --no-source-map demo:demo && ../prepare-module/prepare-module.js"
54
54
  },
55
- "version": "0.2.1"
55
+ "version": "0.2.3"
56
56
  }