@itrocks/table 0.1.9 → 0.2.1
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.
- package/column-reorder-freeze.d.ts +5 -0
- package/column-reorder-freeze.js +17 -0
- package/column-reorder.d.ts +46 -4
- package/column-reorder.js +305 -37
- package/freeze.js +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Plugin } from '../plugin/plugin.js';
|
|
2
|
+
import { TableColumnReorder } from './column-reorder.js';
|
|
3
|
+
import { TableFreeze } from './freeze.js';
|
|
4
|
+
export class TableColumnReorderFreeze extends Plugin {
|
|
5
|
+
init() {
|
|
6
|
+
const freeze = this.of.plugins.TableFreeze;
|
|
7
|
+
const reorder = this.of.plugins.TableColumnReorder;
|
|
8
|
+
if (!(freeze instanceof TableFreeze) || !(reorder instanceof TableColumnReorder))
|
|
9
|
+
return;
|
|
10
|
+
if (reorder.options.isReorderable !== undefined)
|
|
11
|
+
return;
|
|
12
|
+
reorder.options.isReorderable = header => {
|
|
13
|
+
const index = this.of.cellColumnNumber(header);
|
|
14
|
+
return (index >= freeze.leftColumnCount) && (index < freeze.columns.length - freeze.rightColumnCount);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
package/column-reorder.d.ts
CHANGED
|
@@ -1,8 +1,50 @@
|
|
|
1
1
|
import { Plugin } from '../plugin/plugin.js';
|
|
2
|
-
import {
|
|
2
|
+
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
3
|
import { Table } from './table.js';
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
type Direction = -1 | 0 | 1;
|
|
5
|
+
interface Drag extends Press {
|
|
6
|
+
cells: HTMLTableCellElement[];
|
|
7
|
+
col?: HTMLTableColElement;
|
|
8
|
+
count: number;
|
|
9
|
+
direction: Direction;
|
|
10
|
+
index: number;
|
|
11
|
+
initial: number;
|
|
12
|
+
maximum: number;
|
|
13
|
+
minimum: number;
|
|
14
|
+
scroll?: HTMLElement;
|
|
15
|
+
time?: number;
|
|
16
|
+
}
|
|
17
|
+
interface Press {
|
|
18
|
+
header: HTMLTableCellElement;
|
|
19
|
+
id: number;
|
|
20
|
+
origin: number;
|
|
21
|
+
x: number;
|
|
22
|
+
}
|
|
23
|
+
export declare class TableColumnReorder extends Plugin<Table, TableColumnReorderOptions> {
|
|
24
|
+
protected drag?: Drag;
|
|
25
|
+
protected frame?: number;
|
|
26
|
+
protected press?: Press;
|
|
27
|
+
protected suppressClick: boolean;
|
|
28
|
+
protected animate(time: number): void;
|
|
29
|
+
protected colGroups(): HTMLTableColElement[];
|
|
30
|
+
defaultOptions(): TableColumnReorderOptions;
|
|
31
|
+
protected finish(commit: boolean): void;
|
|
6
32
|
init(): void;
|
|
7
|
-
protected
|
|
33
|
+
protected isCurrent(drag: Drag): boolean;
|
|
34
|
+
protected move(drag: Drag, targetIndex: number): void;
|
|
35
|
+
protected onPointer(event: PointerEvent): void;
|
|
36
|
+
protected requestFrame(): void;
|
|
37
|
+
protected rows(): HTMLTableRowElement[];
|
|
38
|
+
protected scroll(drag: Drag, time: number): Direction;
|
|
39
|
+
protected scrollContainer(): HTMLElement | undefined;
|
|
40
|
+
protected start(press: Press, x: number): void;
|
|
41
|
+
protected target(drag: Drag, direction: Direction): number;
|
|
42
|
+
protected triggerRow(): HTMLTableRowElement;
|
|
43
|
+
protected updatePointer(x: number): void;
|
|
44
|
+
}
|
|
45
|
+
export declare class TableColumnReorderOptions extends PluginOptions {
|
|
46
|
+
isReorderable?: (header: HTMLTableCellElement) => boolean;
|
|
47
|
+
onReorder?: (header: HTMLTableCellElement) => void;
|
|
48
|
+
threshold: number;
|
|
8
49
|
}
|
|
50
|
+
export {};
|
package/column-reorder.js
CHANGED
|
@@ -1,48 +1,316 @@
|
|
|
1
1
|
import { Plugin } from '../plugin/plugin.js';
|
|
2
|
+
import { PluginOptions } from '../plugin/plugin.js';
|
|
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
|
+
const maxScrollSpeed = 800;
|
|
7
|
+
const penetration = 10;
|
|
8
|
+
const rowSelector = ':scope > tbody > tr, :scope > tfoot > tr, :scope > thead > tr';
|
|
9
|
+
const scrollZone = 50;
|
|
10
|
+
let activeDrag;
|
|
2
11
|
export class TableColumnReorder extends Plugin {
|
|
3
|
-
|
|
12
|
+
drag;
|
|
13
|
+
frame;
|
|
14
|
+
press;
|
|
15
|
+
suppressClick = false;
|
|
16
|
+
animate(time) {
|
|
17
|
+
this.frame = undefined;
|
|
18
|
+
const drag = this.drag;
|
|
19
|
+
if (!drag)
|
|
20
|
+
return;
|
|
21
|
+
if (!this.isCurrent(drag)) {
|
|
22
|
+
this.finish(false);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const pointerDirection = drag.direction;
|
|
26
|
+
drag.direction = 0;
|
|
27
|
+
const scrollDirection = this.scroll(drag, time);
|
|
28
|
+
const direction = pointerDirection || scrollDirection;
|
|
29
|
+
const target = direction ? this.target(drag, direction) : drag.index;
|
|
30
|
+
if (target !== drag.index)
|
|
31
|
+
this.move(drag, target);
|
|
32
|
+
if (scrollDirection)
|
|
33
|
+
this.requestFrame();
|
|
34
|
+
}
|
|
35
|
+
colGroups() {
|
|
36
|
+
return Array.from(this.of.element.children).filter(element => element.tagName === 'COLGROUP');
|
|
37
|
+
}
|
|
38
|
+
defaultOptions() {
|
|
39
|
+
return new TableColumnReorderOptions();
|
|
40
|
+
}
|
|
41
|
+
finish(commit) {
|
|
42
|
+
this.press = undefined;
|
|
43
|
+
if (this.frame !== undefined)
|
|
44
|
+
cancelAnimationFrame(this.frame);
|
|
45
|
+
this.frame = undefined;
|
|
46
|
+
const drag = this.drag;
|
|
47
|
+
if (!drag)
|
|
48
|
+
return;
|
|
49
|
+
const changed = drag.index !== drag.initial;
|
|
50
|
+
const current = this.isCurrent(drag);
|
|
51
|
+
if (!current)
|
|
52
|
+
commit = false;
|
|
53
|
+
if (!commit && changed && current)
|
|
54
|
+
this.move(drag, drag.initial);
|
|
55
|
+
for (const cell of drag.cells)
|
|
56
|
+
cell.classList.remove('column-reorder');
|
|
57
|
+
if (activeDrag === this) {
|
|
58
|
+
activeDrag = undefined;
|
|
59
|
+
document.documentElement.classList.remove('column-reorder');
|
|
60
|
+
}
|
|
61
|
+
this.drag = undefined;
|
|
62
|
+
if (this.of.element.hasPointerCapture(drag.id))
|
|
63
|
+
this.of.element.releasePointerCapture(drag.id);
|
|
64
|
+
if (commit && changed)
|
|
65
|
+
this.options.onReorder?.(drag.header);
|
|
66
|
+
}
|
|
4
67
|
init() {
|
|
5
|
-
this.reorderCells = this.getReorderCells();
|
|
6
|
-
let downed;
|
|
7
|
-
let dragging;
|
|
8
|
-
let mouse = new DOMRect;
|
|
9
|
-
let mouseFrom = new DOMRect;
|
|
10
68
|
const table = this.of;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
table.addEventListener(document, '
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
69
|
+
table.addEventListener(table.element, 'click', event => {
|
|
70
|
+
if (!this.suppressClick)
|
|
71
|
+
return;
|
|
72
|
+
this.suppressClick = false;
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
event.stopImmediatePropagation();
|
|
75
|
+
}, { capture: true });
|
|
76
|
+
table.addEventListener(document, 'keydown', event => {
|
|
77
|
+
if ((event.key !== 'Escape') || !this.drag)
|
|
78
|
+
return;
|
|
79
|
+
event.preventDefault();
|
|
80
|
+
event.stopImmediatePropagation();
|
|
81
|
+
this.finish(false);
|
|
82
|
+
}, { capture: true });
|
|
83
|
+
table.addEventListener(table.element, 'lostpointercapture', event => this.onPointer(event));
|
|
84
|
+
table.addEventListener(document, 'pointercancel', event => this.onPointer(event), { capture: true });
|
|
85
|
+
table.addEventListener(table.element, 'pointerdown', event => this.onPointer(event));
|
|
86
|
+
table.addEventListener(document, 'pointermove', event => this.onPointer(event), { capture: true });
|
|
87
|
+
table.addEventListener(document, 'pointerup', event => this.onPointer(event), { capture: true });
|
|
88
|
+
table.onReset.push(() => {
|
|
89
|
+
this.finish(false);
|
|
90
|
+
this.suppressClick = false;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
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
|
+
move(drag, targetIndex) {
|
|
107
|
+
const after = drag.index < targetIndex;
|
|
108
|
+
for (const cell of drag.cells) {
|
|
109
|
+
const target = cell.parentElement.cells[targetIndex];
|
|
110
|
+
after ? target.after(cell) : target.before(cell);
|
|
111
|
+
}
|
|
112
|
+
if (drag.col) {
|
|
113
|
+
const target = drag.col.parentElement.children[targetIndex];
|
|
114
|
+
after ? target.after(drag.col) : target.before(drag.col);
|
|
115
|
+
}
|
|
116
|
+
drag.index = targetIndex;
|
|
117
|
+
}
|
|
118
|
+
onPointer(event) {
|
|
119
|
+
if (event.type === 'pointerdown') {
|
|
120
|
+
if (activeDrag || this.drag || this.press || (event.button !== 0) || !event.isPrimary)
|
|
121
|
+
return;
|
|
122
|
+
if (!(event.target instanceof Element))
|
|
123
|
+
return;
|
|
124
|
+
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)))
|
|
130
|
+
return;
|
|
131
|
+
this.press = { header, id: event.pointerId, origin: event.clientX, x: event.clientX };
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const state = this.drag ?? this.press;
|
|
135
|
+
if (!state || (event.pointerId !== state.id))
|
|
136
|
+
return;
|
|
137
|
+
if (event.type === 'lostpointercapture') {
|
|
138
|
+
if (this.drag)
|
|
139
|
+
this.finish(false);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (event.type === 'pointercancel') {
|
|
143
|
+
if (this.drag) {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
event.stopImmediatePropagation();
|
|
25
146
|
}
|
|
26
|
-
|
|
27
|
-
|
|
147
|
+
this.finish(false);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (event.type === 'pointerup') {
|
|
151
|
+
if (!this.drag) {
|
|
152
|
+
this.press = undefined;
|
|
153
|
+
return;
|
|
28
154
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
event.stopImmediatePropagation();
|
|
157
|
+
this.updatePointer(event.clientX);
|
|
158
|
+
if ((this.frame !== undefined) || this.drag.direction) {
|
|
159
|
+
if (this.frame !== undefined)
|
|
160
|
+
cancelAnimationFrame(this.frame);
|
|
161
|
+
this.frame = undefined;
|
|
162
|
+
this.animate(performance.now());
|
|
33
163
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
164
|
+
this.suppressClick = true;
|
|
165
|
+
setTimeout(() => this.suppressClick = false);
|
|
166
|
+
this.finish(true);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (!this.drag) {
|
|
170
|
+
if (Math.abs(event.clientX - state.origin) < Math.max(0, this.options.threshold)) {
|
|
171
|
+
state.x = event.clientX;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
this.press = undefined;
|
|
175
|
+
this.start(state, event.clientX);
|
|
176
|
+
}
|
|
177
|
+
if (!this.drag)
|
|
178
|
+
return;
|
|
179
|
+
event.preventDefault();
|
|
180
|
+
this.updatePointer(event.clientX);
|
|
181
|
+
}
|
|
182
|
+
requestFrame() {
|
|
183
|
+
if (this.frame === undefined)
|
|
184
|
+
this.frame = requestAnimationFrame(time => this.animate(time));
|
|
185
|
+
}
|
|
186
|
+
rows() {
|
|
187
|
+
return Array.from(this.of.element.querySelectorAll(rowSelector));
|
|
37
188
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
189
|
+
scroll(drag, time) {
|
|
190
|
+
const container = drag.scroll;
|
|
191
|
+
if (!container)
|
|
192
|
+
return 0;
|
|
193
|
+
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;
|
|
197
|
+
if (!direction) {
|
|
198
|
+
drag.time = undefined;
|
|
199
|
+
return 0;
|
|
45
200
|
}
|
|
46
|
-
|
|
201
|
+
const previousTime = drag.time;
|
|
202
|
+
drag.time = time;
|
|
203
|
+
if (previousTime === undefined)
|
|
204
|
+
return direction;
|
|
205
|
+
const previousScroll = container.scrollLeft;
|
|
206
|
+
container.scrollLeft += direction * maxScrollSpeed * Math.max(left, right) * (time - previousTime) / 1000;
|
|
207
|
+
if (container.scrollLeft !== previousScroll)
|
|
208
|
+
return direction;
|
|
209
|
+
drag.time = undefined;
|
|
210
|
+
return 0;
|
|
47
211
|
}
|
|
212
|
+
scrollContainer() {
|
|
213
|
+
let element = this.of.element;
|
|
214
|
+
while (element) {
|
|
215
|
+
if (['auto', 'overlay', 'scroll'].includes(getComputedStyle(element).overflowX)
|
|
216
|
+
&& (element.scrollWidth > element.clientWidth)) {
|
|
217
|
+
return element;
|
|
218
|
+
}
|
|
219
|
+
element = element.parentElement;
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
start(press, x) {
|
|
224
|
+
if (activeDrag)
|
|
225
|
+
return;
|
|
226
|
+
const row = this.triggerRow();
|
|
227
|
+
const headers = Array.from(row?.cells ?? []);
|
|
228
|
+
const initial = headers.indexOf(press.header);
|
|
229
|
+
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;
|
|
243
|
+
const reorderable = headers.map(header => this.options.isReorderable?.(header) ?? true);
|
|
244
|
+
if (!reorderable[initial])
|
|
245
|
+
return;
|
|
246
|
+
let maximum = initial;
|
|
247
|
+
let minimum = initial;
|
|
248
|
+
while ((maximum < headers.length - 1) && reorderable[maximum + 1])
|
|
249
|
+
maximum++;
|
|
250
|
+
while ((minimum > 0) && reorderable[minimum - 1])
|
|
251
|
+
minimum--;
|
|
252
|
+
try {
|
|
253
|
+
this.of.element.setPointerCapture(press.id);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const drag = {
|
|
259
|
+
...press,
|
|
260
|
+
cells: rows.map(row => row.cells[initial]),
|
|
261
|
+
col: columns[initial],
|
|
262
|
+
count: headers.length,
|
|
263
|
+
direction: Math.sign(x - press.x),
|
|
264
|
+
index: initial,
|
|
265
|
+
initial,
|
|
266
|
+
maximum,
|
|
267
|
+
minimum,
|
|
268
|
+
scroll: this.scrollContainer(),
|
|
269
|
+
x
|
|
270
|
+
};
|
|
271
|
+
this.drag = drag;
|
|
272
|
+
activeDrag = this;
|
|
273
|
+
document.documentElement.classList.add('column-reorder');
|
|
274
|
+
for (const cell of drag.cells)
|
|
275
|
+
cell.classList.add('column-reorder');
|
|
276
|
+
this.requestFrame();
|
|
277
|
+
}
|
|
278
|
+
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;
|
|
285
|
+
let target = drag.index;
|
|
286
|
+
for (let index = drag.minimum; index <= drag.maximum; index++) {
|
|
287
|
+
if (index === drag.index)
|
|
288
|
+
continue;
|
|
289
|
+
const rect = headers[index].getBoundingClientRect();
|
|
290
|
+
const delta = (rect.left + rect.right) / 2 - sourceCenter;
|
|
291
|
+
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
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return target;
|
|
299
|
+
}
|
|
300
|
+
triggerRow() {
|
|
301
|
+
return this.of.element.tHead?.rows[0] ?? this.of.element.tBodies[0]?.rows[0];
|
|
302
|
+
}
|
|
303
|
+
updatePointer(x) {
|
|
304
|
+
const drag = this.drag;
|
|
305
|
+
if (!drag || (x === drag.x))
|
|
306
|
+
return;
|
|
307
|
+
drag.direction = Math.sign(x - drag.x);
|
|
308
|
+
drag.x = x;
|
|
309
|
+
this.requestFrame();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
export class TableColumnReorderOptions extends PluginOptions {
|
|
313
|
+
isReorderable;
|
|
314
|
+
onReorder;
|
|
315
|
+
threshold = 10;
|
|
48
316
|
}
|
package/freeze.js
CHANGED
|
@@ -90,7 +90,7 @@ export class TableFreeze extends Plugin {
|
|
|
90
90
|
const bodySel = [];
|
|
91
91
|
const cornerSel = [];
|
|
92
92
|
let counter = 1, left = .0, previousLeft = table.element.getBoundingClientRect().left;
|
|
93
|
-
for (const colCell of Array.from(this.columns).slice(this.leftColumnCount)) {
|
|
93
|
+
for (const colCell of Array.from(this.columns).slice(0, this.leftColumnCount)) {
|
|
94
94
|
const actualLeft = colCell.getBoundingClientRect().left;
|
|
95
95
|
left += actualLeft - previousLeft;
|
|
96
96
|
previousLeft = actualLeft;
|
|
@@ -123,7 +123,7 @@ export class TableFreeze extends Plugin {
|
|
|
123
123
|
const bodySel = [];
|
|
124
124
|
const cornerSel = [];
|
|
125
125
|
let counter = 1, right = .0, previousRight = table.element.getBoundingClientRect().right;
|
|
126
|
-
for (const colCell of Array.from(this.columns).reverse().slice(this.rightColumnCount)) {
|
|
126
|
+
for (const colCell of Array.from(this.columns).reverse().slice(0, this.rightColumnCount)) {
|
|
127
127
|
const actualRight = colCell.getBoundingClientRect().right;
|
|
128
128
|
right += previousRight - actualRight;
|
|
129
129
|
previousRight = actualRight;
|
package/package.json
CHANGED