@itrocks/table 0.2.0 → 0.2.2
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.js +3 -6
- package/column-reorder.d.ts +32 -53
- package/column-reorder.js +188 -376
- package/package.json +1 -1
package/column-reorder-freeze.js
CHANGED
|
@@ -3,18 +3,15 @@ import { TableColumnReorder } from './column-reorder.js';
|
|
|
3
3
|
import { TableFreeze } from './freeze.js';
|
|
4
4
|
export class TableColumnReorderFreeze extends Plugin {
|
|
5
5
|
init() {
|
|
6
|
-
const reorder = this.of.plugins.TableColumnReorder;
|
|
7
6
|
const freeze = this.of.plugins.TableFreeze;
|
|
8
|
-
|
|
7
|
+
const reorder = this.of.plugins.TableColumnReorder;
|
|
8
|
+
if (!(freeze instanceof TableFreeze) || !(reorder instanceof TableColumnReorder))
|
|
9
9
|
return;
|
|
10
10
|
if (reorder.options.isReorderable !== undefined)
|
|
11
11
|
return;
|
|
12
12
|
reorder.options.isReorderable = header => {
|
|
13
13
|
const index = this.of.cellColumnNumber(header);
|
|
14
|
-
|
|
15
|
-
const reorderable = (index >= freeze.leftColumnCount) && (index < freeze.columns.length - freeze.rightColumnCount);
|
|
16
|
-
console.debug('reorderable of', index, '=', reorderable ? 'true' : 'false');
|
|
17
|
-
return reorderable;
|
|
14
|
+
return (index >= freeze.leftColumnCount) && (index < freeze.columns.length - freeze.rightColumnCount);
|
|
18
15
|
};
|
|
19
16
|
}
|
|
20
17
|
}
|
package/column-reorder.d.ts
CHANGED
|
@@ -2,67 +2,46 @@ import { Plugin } from '../plugin/plugin.js';
|
|
|
2
2
|
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
3
|
import { Table } from './table.js';
|
|
4
4
|
type Direction = -1 | 0 | 1;
|
|
5
|
-
interface
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
pointerId: number;
|
|
9
|
-
startX: number;
|
|
10
|
-
status: 'armed';
|
|
11
|
-
}
|
|
12
|
-
interface Grid {
|
|
13
|
-
colGroup?: HTMLTableColElement;
|
|
14
|
-
columnCount: number;
|
|
15
|
-
rows: HTMLTableRowElement[];
|
|
16
|
-
sourceCells: HTMLTableCellElement[];
|
|
17
|
-
sourceCol?: HTMLTableColElement;
|
|
18
|
-
}
|
|
19
|
-
interface DraggingState extends Grid {
|
|
20
|
-
currentIndex: number;
|
|
5
|
+
interface Drag extends Press {
|
|
6
|
+
cells: HTMLTableCellElement[];
|
|
7
|
+
col?: HTMLTableColElement;
|
|
21
8
|
direction: Direction;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
status: 'dragging';
|
|
30
|
-
scrollContainer?: HTMLElement;
|
|
9
|
+
index: number;
|
|
10
|
+
initial: number;
|
|
11
|
+
maximum: number;
|
|
12
|
+
minimum: number;
|
|
13
|
+
scroll?: HTMLElement;
|
|
14
|
+
time?: number;
|
|
15
|
+
x: number;
|
|
31
16
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
threshold: number;
|
|
17
|
+
interface Press {
|
|
18
|
+
header: HTMLTableCellElement;
|
|
19
|
+
id: number;
|
|
20
|
+
origin: number;
|
|
37
21
|
}
|
|
38
22
|
export declare class TableColumnReorder extends Plugin<Table, TableColumnReorderOptions> {
|
|
39
|
-
protected
|
|
40
|
-
protected
|
|
41
|
-
protected
|
|
23
|
+
protected drag?: Drag;
|
|
24
|
+
protected frame?: number;
|
|
25
|
+
protected press?: Press;
|
|
42
26
|
protected suppressClick: boolean;
|
|
27
|
+
protected animate(time: number): void;
|
|
43
28
|
defaultOptions(): TableColumnReorderOptions;
|
|
44
|
-
init(): void;
|
|
45
|
-
protected autoScroll(state: DraggingState, time: number): Direction;
|
|
46
|
-
protected clamp(value: number): number;
|
|
47
|
-
protected closestHorizontalScrollable(): HTMLElement | undefined;
|
|
48
|
-
protected collectGrid(header: HTMLTableCellElement): Grid | undefined;
|
|
49
|
-
protected destroy(): void;
|
|
50
29
|
protected finish(commit: boolean): void;
|
|
51
|
-
|
|
52
|
-
protected
|
|
53
|
-
protected
|
|
54
|
-
protected onClick(event: MouseEvent): void;
|
|
55
|
-
protected onKeyDown(event: KeyboardEvent): void;
|
|
56
|
-
protected onLostPointerCapture(event: PointerEvent): void;
|
|
57
|
-
protected onPointerCancel(event: PointerEvent): void;
|
|
58
|
-
protected onPointerDown(event: PointerEvent): void;
|
|
59
|
-
protected onPointerMove(event: PointerEvent): void;
|
|
60
|
-
protected onPointerUp(event: PointerEvent): void;
|
|
30
|
+
init(): void;
|
|
31
|
+
protected move(drag: Drag, targetIndex: number): void;
|
|
32
|
+
protected onPointer(event: PointerEvent): void;
|
|
61
33
|
protected requestFrame(): void;
|
|
62
|
-
protected
|
|
63
|
-
protected
|
|
64
|
-
protected
|
|
65
|
-
protected
|
|
34
|
+
protected rows(): HTMLTableRowElement[];
|
|
35
|
+
protected scroll(drag: Drag, time: number): Direction;
|
|
36
|
+
protected scrollContainer(): HTMLElement | undefined;
|
|
37
|
+
protected start(press: Press, x: number): void;
|
|
38
|
+
protected target(drag: Drag, direction: Direction): number;
|
|
66
39
|
protected triggerRow(): HTMLTableRowElement;
|
|
40
|
+
protected updatePointer(x: number): void;
|
|
41
|
+
}
|
|
42
|
+
export declare class TableColumnReorderOptions extends PluginOptions {
|
|
43
|
+
isReorderable?: (header: HTMLTableCellElement) => boolean;
|
|
44
|
+
onReorder?: (header: HTMLTableCellElement) => void;
|
|
45
|
+
threshold: number;
|
|
67
46
|
}
|
|
68
47
|
export {};
|
package/column-reorder.js
CHANGED
|
@@ -1,423 +1,235 @@
|
|
|
1
1
|
import { Plugin } from '../plugin/plugin.js';
|
|
2
2
|
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
|
-
const
|
|
4
|
-
'a',
|
|
5
|
-
'button',
|
|
6
|
-
'input',
|
|
7
|
-
'select',
|
|
8
|
-
'textarea',
|
|
9
|
-
'[contenteditable]:not([contenteditable="false"])',
|
|
10
|
-
'[draggable="true"]'
|
|
11
|
-
].join(', ');
|
|
3
|
+
const clamp = (value) => Math.max(0, Math.min(1, value));
|
|
12
4
|
const maxScrollSpeed = 800;
|
|
13
5
|
const penetration = 10;
|
|
14
6
|
const scrollZone = 50;
|
|
15
|
-
let
|
|
16
|
-
export class TableColumnReorderOptions extends PluginOptions {
|
|
17
|
-
isReorderable;
|
|
18
|
-
onReorder;
|
|
19
|
-
threshold = 10;
|
|
20
|
-
}
|
|
7
|
+
let activeDrag;
|
|
21
8
|
export class TableColumnReorder extends Plugin {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
9
|
+
drag;
|
|
10
|
+
frame;
|
|
11
|
+
press;
|
|
25
12
|
suppressClick = false;
|
|
13
|
+
animate(time) {
|
|
14
|
+
this.frame = undefined;
|
|
15
|
+
const drag = this.drag;
|
|
16
|
+
const pointerDirection = drag.direction;
|
|
17
|
+
drag.direction = 0;
|
|
18
|
+
const scrollDirection = this.scroll(drag, time);
|
|
19
|
+
const direction = pointerDirection || scrollDirection;
|
|
20
|
+
const target = direction ? this.target(drag, direction) : drag.index;
|
|
21
|
+
if (target !== drag.index)
|
|
22
|
+
this.move(drag, target);
|
|
23
|
+
if (scrollDirection)
|
|
24
|
+
this.requestFrame();
|
|
25
|
+
}
|
|
26
26
|
defaultOptions() {
|
|
27
27
|
return new TableColumnReorderOptions();
|
|
28
28
|
}
|
|
29
|
+
finish(commit) {
|
|
30
|
+
this.press = undefined;
|
|
31
|
+
if (this.frame !== undefined)
|
|
32
|
+
cancelAnimationFrame(this.frame);
|
|
33
|
+
this.frame = undefined;
|
|
34
|
+
const drag = this.drag;
|
|
35
|
+
if (!drag)
|
|
36
|
+
return;
|
|
37
|
+
const changed = drag.index !== drag.initial;
|
|
38
|
+
if (!commit && changed)
|
|
39
|
+
this.move(drag, drag.initial);
|
|
40
|
+
for (const cell of drag.cells)
|
|
41
|
+
cell.classList.remove('column-reorder');
|
|
42
|
+
document.documentElement.classList.remove('column-reorder');
|
|
43
|
+
activeDrag = undefined;
|
|
44
|
+
this.drag = undefined;
|
|
45
|
+
if (this.of.element.hasPointerCapture(drag.id))
|
|
46
|
+
this.of.element.releasePointerCapture(drag.id);
|
|
47
|
+
if (commit && changed)
|
|
48
|
+
this.options.onReorder?.(drag.header);
|
|
49
|
+
}
|
|
29
50
|
init() {
|
|
30
51
|
const table = this.of;
|
|
31
|
-
table.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
table.addEventListener(document, 'keydown', event =>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
table.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const rightProximity = 1 - this.clamp((rect.right - state.pointerX) / scrollZone);
|
|
53
|
-
let direction = 0;
|
|
54
|
-
let proximity = 0;
|
|
55
|
-
if ((leftProximity > 0) || (rightProximity > 0)) {
|
|
56
|
-
direction = (rightProximity > leftProximity) ? 1 : -1;
|
|
57
|
-
proximity = Math.max(leftProximity, rightProximity);
|
|
58
|
-
}
|
|
59
|
-
if (!direction) {
|
|
60
|
-
state.lastFrameTime = undefined;
|
|
61
|
-
return 0;
|
|
62
|
-
}
|
|
63
|
-
const previousTime = state.lastFrameTime;
|
|
64
|
-
state.lastFrameTime = time;
|
|
65
|
-
if (previousTime === undefined)
|
|
66
|
-
return direction;
|
|
67
|
-
const previousScroll = container.scrollLeft;
|
|
68
|
-
const elapsed = Math.max(0, time - previousTime) / 1000;
|
|
69
|
-
container.scrollLeft += direction * maxScrollSpeed * proximity * elapsed;
|
|
70
|
-
if (container.scrollLeft === previousScroll) {
|
|
71
|
-
state.lastFrameTime = undefined;
|
|
72
|
-
return 0;
|
|
73
|
-
}
|
|
74
|
-
return direction;
|
|
75
|
-
}
|
|
76
|
-
clamp(value) {
|
|
77
|
-
return Math.max(0, Math.min(1, value));
|
|
78
|
-
}
|
|
79
|
-
closestHorizontalScrollable() {
|
|
80
|
-
let element = this.of.element;
|
|
81
|
-
while (element) {
|
|
82
|
-
const overflow = getComputedStyle(element).overflowX;
|
|
83
|
-
if (['auto', 'overlay', 'scroll'].includes(overflow) && (element.scrollWidth > element.clientWidth)) {
|
|
84
|
-
return element;
|
|
85
|
-
}
|
|
86
|
-
element = element.parentElement;
|
|
87
|
-
}
|
|
88
|
-
return undefined;
|
|
52
|
+
table.addEventListener(table.element, 'click', event => {
|
|
53
|
+
if (!this.suppressClick)
|
|
54
|
+
return;
|
|
55
|
+
this.suppressClick = false;
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
event.stopImmediatePropagation();
|
|
58
|
+
}, { capture: true });
|
|
59
|
+
table.addEventListener(document, 'keydown', event => {
|
|
60
|
+
if ((event.key !== 'Escape') || !this.drag)
|
|
61
|
+
return;
|
|
62
|
+
event.preventDefault();
|
|
63
|
+
this.finish(false);
|
|
64
|
+
}, { capture: true });
|
|
65
|
+
table.addEventListener(document, 'pointercancel', event => this.onPointer(event), { capture: true });
|
|
66
|
+
table.addEventListener(table.element, 'pointerdown', event => this.onPointer(event));
|
|
67
|
+
table.addEventListener(document, 'pointermove', event => this.onPointer(event), { capture: true });
|
|
68
|
+
table.addEventListener(document, 'pointerup', event => this.onPointer(event), { capture: true });
|
|
69
|
+
table.onReset.push(() => {
|
|
70
|
+
this.finish(false);
|
|
71
|
+
this.suppressClick = false;
|
|
72
|
+
});
|
|
89
73
|
}
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const initialIndex = Array.from(headerRow.cells).indexOf(header);
|
|
96
|
-
const rows = this.tableRows();
|
|
97
|
-
if ((initialIndex < 0) || !rows.length)
|
|
98
|
-
return undefined;
|
|
99
|
-
for (const row of rows) {
|
|
100
|
-
if ((row.cells.length !== columnCount) || Array.from(row.cells).some(cell => {
|
|
101
|
-
return (cell.colSpan !== 1) || (cell.rowSpan !== 1);
|
|
102
|
-
})) {
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
74
|
+
move(drag, targetIndex) {
|
|
75
|
+
const after = drag.index < targetIndex;
|
|
76
|
+
for (const cell of drag.cells) {
|
|
77
|
+
const target = cell.parentElement.cells[targetIndex];
|
|
78
|
+
after ? target.after(cell) : target.before(cell);
|
|
105
79
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const colGroup = colGroups[0];
|
|
110
|
-
let sourceCol;
|
|
111
|
-
if (colGroup) {
|
|
112
|
-
const columns = Array.from(colGroup.children);
|
|
113
|
-
if ((columns.length !== columnCount)
|
|
114
|
-
|| columns.some(column => (column.tagName !== 'COL') || (column.span !== 1))) {
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
sourceCol = columns[initialIndex];
|
|
80
|
+
if (drag.col) {
|
|
81
|
+
const target = drag.col.parentElement.children[targetIndex];
|
|
82
|
+
after ? target.after(drag.col) : target.before(drag.col);
|
|
118
83
|
}
|
|
119
|
-
|
|
120
|
-
colGroup,
|
|
121
|
-
columnCount,
|
|
122
|
-
rows,
|
|
123
|
-
sourceCells: rows.map(row => row.cells[initialIndex]),
|
|
124
|
-
sourceCol
|
|
125
|
-
};
|
|
84
|
+
drag.index = targetIndex;
|
|
126
85
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
finish(commit) {
|
|
136
|
-
const state = this.state;
|
|
137
|
-
if (!state)
|
|
86
|
+
onPointer(event) {
|
|
87
|
+
if (event.type === 'pointerdown') {
|
|
88
|
+
if (activeDrag || this.press || (event.button !== 0))
|
|
89
|
+
return;
|
|
90
|
+
const header = event.target.closest('th, td');
|
|
91
|
+
if (!header || (header.parentElement !== this.triggerRow()))
|
|
92
|
+
return;
|
|
93
|
+
this.press = { header, id: event.pointerId, origin: event.clientX };
|
|
138
94
|
return;
|
|
139
|
-
if (this.animationFrame !== undefined) {
|
|
140
|
-
cancelAnimationFrame(this.animationFrame);
|
|
141
|
-
this.animationFrame = undefined;
|
|
142
95
|
}
|
|
143
|
-
|
|
144
|
-
if (state.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
this.moveColumn(state, state.initialIndex);
|
|
150
|
-
for (const cell of state.sourceCells)
|
|
151
|
-
cell.classList.remove('column-reorder');
|
|
152
|
-
if (cursorOwner === this) {
|
|
153
|
-
cursorOwner = undefined;
|
|
154
|
-
document.documentElement.classList.remove('column-reorder');
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
this.state = undefined;
|
|
158
|
-
if ((state.status === 'dragging') && this.of.element.hasPointerCapture(state.pointerId)) {
|
|
159
|
-
this.of.element.releasePointerCapture(state.pointerId);
|
|
96
|
+
const state = this.drag ?? this.press;
|
|
97
|
+
if (!state || (event.pointerId !== state.id))
|
|
98
|
+
return;
|
|
99
|
+
if (event.type === 'pointercancel') {
|
|
100
|
+
this.finish(false);
|
|
101
|
+
return;
|
|
160
102
|
}
|
|
161
|
-
if (
|
|
162
|
-
this.
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const headers = Array.from(this.triggerRow()?.cells ?? []);
|
|
166
|
-
const sourceRect = headers[state.currentIndex]?.getBoundingClientRect();
|
|
167
|
-
if (!sourceRect)
|
|
168
|
-
return state.currentIndex;
|
|
169
|
-
const sourceCenter = (sourceRect.left + sourceRect.right) / 2;
|
|
170
|
-
let distance = 0;
|
|
171
|
-
let target = state.currentIndex;
|
|
172
|
-
for (let index = state.minimumIndex; index <= state.maximumIndex; index++) {
|
|
173
|
-
if (index === state.currentIndex)
|
|
174
|
-
continue;
|
|
175
|
-
const rect = headers[index].getBoundingClientRect();
|
|
176
|
-
const center = (rect.left + rect.right) / 2;
|
|
177
|
-
if ((direction > 0) && (center > sourceCenter) && (state.pointerX >= rect.left + penetration)) {
|
|
178
|
-
const candidateDistance = center - sourceCenter;
|
|
179
|
-
if (candidateDistance > distance) {
|
|
180
|
-
distance = candidateDistance;
|
|
181
|
-
target = index;
|
|
182
|
-
}
|
|
103
|
+
if (event.type === 'pointerup') {
|
|
104
|
+
if (!this.drag) {
|
|
105
|
+
this.press = undefined;
|
|
106
|
+
return;
|
|
183
107
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
this.updatePointer(event.clientX);
|
|
110
|
+
if (this.frame !== undefined) {
|
|
111
|
+
cancelAnimationFrame(this.frame);
|
|
112
|
+
this.frame = undefined;
|
|
113
|
+
this.animate(performance.now());
|
|
190
114
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
moveColumn(state, targetIndex) {
|
|
195
|
-
if ((targetIndex === state.currentIndex) || !this.structureIsCurrent(state)) {
|
|
196
|
-
return targetIndex === state.currentIndex;
|
|
197
|
-
}
|
|
198
|
-
const moveAfter = state.currentIndex < targetIndex;
|
|
199
|
-
for (let index = 0; index < state.rows.length; index++) {
|
|
200
|
-
const source = state.sourceCells[index];
|
|
201
|
-
const target = state.rows[index].cells[targetIndex];
|
|
202
|
-
moveAfter ? target.after(source) : target.before(source);
|
|
203
|
-
}
|
|
204
|
-
if (state.sourceCol && state.colGroup) {
|
|
205
|
-
const target = state.colGroup.children[targetIndex];
|
|
206
|
-
moveAfter ? target.after(state.sourceCol) : target.before(state.sourceCol);
|
|
207
|
-
}
|
|
208
|
-
state.currentIndex = targetIndex;
|
|
209
|
-
return true;
|
|
210
|
-
}
|
|
211
|
-
onAnimationFrame(time) {
|
|
212
|
-
this.animationFrame = undefined;
|
|
213
|
-
const state = this.state;
|
|
214
|
-
if (!state || (state.status !== 'dragging'))
|
|
215
|
-
return;
|
|
216
|
-
if (!this.structureIsCurrent(state)) {
|
|
217
|
-
this.finish(false);
|
|
115
|
+
this.suppressClick = true;
|
|
116
|
+
setTimeout(() => this.suppressClick = false);
|
|
117
|
+
this.finish(true);
|
|
218
118
|
return;
|
|
219
119
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const targetIndex = this.headerAtPointer(state, direction);
|
|
226
|
-
if (targetIndex !== state.currentIndex)
|
|
227
|
-
this.moveColumn(state, targetIndex);
|
|
120
|
+
if (!this.drag) {
|
|
121
|
+
if (Math.abs(event.clientX - state.origin) < this.options.threshold)
|
|
122
|
+
return;
|
|
123
|
+
this.press = undefined;
|
|
124
|
+
this.start(state, event.clientX);
|
|
228
125
|
}
|
|
229
|
-
if (
|
|
230
|
-
this.requestFrame();
|
|
231
|
-
}
|
|
232
|
-
onClick(event) {
|
|
233
|
-
if (!this.suppressClick)
|
|
234
|
-
return;
|
|
235
|
-
this.suppressClick = false;
|
|
236
|
-
event.preventDefault();
|
|
237
|
-
event.stopImmediatePropagation();
|
|
238
|
-
}
|
|
239
|
-
onKeyDown(event) {
|
|
240
|
-
if ((event.key !== 'Escape') || (this.state?.status !== 'dragging'))
|
|
126
|
+
if (!this.drag)
|
|
241
127
|
return;
|
|
242
128
|
event.preventDefault();
|
|
243
|
-
event.
|
|
244
|
-
this.finish(false);
|
|
129
|
+
this.updatePointer(event.clientX);
|
|
245
130
|
}
|
|
246
|
-
|
|
247
|
-
if (
|
|
248
|
-
|
|
249
|
-
this.finish(false);
|
|
250
|
-
}
|
|
251
|
-
onPointerCancel(event) {
|
|
252
|
-
if (!this.state || (event.pointerId !== this.state.pointerId))
|
|
253
|
-
return;
|
|
254
|
-
if (this.state.status === 'dragging') {
|
|
255
|
-
event.preventDefault();
|
|
256
|
-
event.stopImmediatePropagation();
|
|
257
|
-
}
|
|
258
|
-
this.finish(false);
|
|
131
|
+
requestFrame() {
|
|
132
|
+
if (this.frame === undefined)
|
|
133
|
+
this.frame = requestAnimationFrame(time => this.animate(time));
|
|
259
134
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return;
|
|
263
|
-
if (!(event.target instanceof Element))
|
|
264
|
-
return;
|
|
265
|
-
const header = event.target.closest('th, td');
|
|
266
|
-
const row = this.triggerRow();
|
|
267
|
-
if (!header || !row || (header.parentElement !== row) || (header.closest('table') !== this.of.element))
|
|
268
|
-
return;
|
|
269
|
-
const interactive = event.target.closest(interactiveSelector);
|
|
270
|
-
if (interactive && header.contains(interactive))
|
|
271
|
-
return;
|
|
272
|
-
this.state = {
|
|
273
|
-
header,
|
|
274
|
-
lastX: event.clientX,
|
|
275
|
-
pointerId: event.pointerId,
|
|
276
|
-
startX: event.clientX,
|
|
277
|
-
status: 'armed'
|
|
278
|
-
};
|
|
135
|
+
rows() {
|
|
136
|
+
return Array.from(this.of.element.rows);
|
|
279
137
|
}
|
|
280
|
-
|
|
281
|
-
const
|
|
282
|
-
if (!
|
|
283
|
-
return;
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const dragging = this.state;
|
|
293
|
-
if (!dragging || (dragging.status !== 'dragging'))
|
|
294
|
-
return;
|
|
295
|
-
event.preventDefault();
|
|
296
|
-
if (event.clientX !== dragging.pointerX) {
|
|
297
|
-
dragging.direction = (event.clientX > dragging.pointerX) ? 1 : -1;
|
|
298
|
-
dragging.pointerX = event.clientX;
|
|
299
|
-
this.requestFrame();
|
|
138
|
+
scroll(drag, time) {
|
|
139
|
+
const container = drag.scroll;
|
|
140
|
+
if (!container)
|
|
141
|
+
return 0;
|
|
142
|
+
const rect = container.getBoundingClientRect();
|
|
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);
|
|
147
|
+
if (!direction) {
|
|
148
|
+
drag.time = undefined;
|
|
149
|
+
return 0;
|
|
300
150
|
}
|
|
151
|
+
const previousTime = drag.time;
|
|
152
|
+
drag.time = time;
|
|
153
|
+
if (previousTime === undefined)
|
|
154
|
+
return direction;
|
|
155
|
+
const previousScroll = container.scrollLeft;
|
|
156
|
+
container.scrollLeft += speed * maxScrollSpeed * (time - previousTime) / 1000;
|
|
157
|
+
if (container.scrollLeft !== previousScroll)
|
|
158
|
+
return direction;
|
|
159
|
+
drag.time = undefined;
|
|
160
|
+
return 0;
|
|
301
161
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
event.preventDefault();
|
|
310
|
-
event.stopImmediatePropagation();
|
|
311
|
-
const dragging = this.state;
|
|
312
|
-
if (event.clientX !== dragging.pointerX) {
|
|
313
|
-
dragging.direction = (event.clientX > dragging.pointerX) ? 1 : -1;
|
|
314
|
-
dragging.pointerX = event.clientX;
|
|
315
|
-
}
|
|
316
|
-
if ((this.animationFrame !== undefined) || dragging.direction) {
|
|
317
|
-
if (this.animationFrame !== undefined) {
|
|
318
|
-
cancelAnimationFrame(this.animationFrame);
|
|
319
|
-
this.animationFrame = undefined;
|
|
162
|
+
scrollContainer() {
|
|
163
|
+
for (let element = this.of.element; element; element = element.parentElement) {
|
|
164
|
+
if (['auto', 'overlay', 'scroll'].includes(getComputedStyle(element).overflowX)
|
|
165
|
+
&& (element.scrollWidth > element.clientWidth)) {
|
|
166
|
+
return element;
|
|
320
167
|
}
|
|
321
|
-
this.onAnimationFrame(performance.now());
|
|
322
168
|
}
|
|
323
|
-
|
|
324
|
-
this.finish(true);
|
|
169
|
+
return undefined;
|
|
325
170
|
}
|
|
326
|
-
|
|
327
|
-
if (
|
|
171
|
+
start(press, x) {
|
|
172
|
+
if (activeDrag)
|
|
328
173
|
return;
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
this.finish(false);
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
const grid = this.collectGrid(armed.header);
|
|
337
|
-
if (!grid) {
|
|
338
|
-
this.finish(false);
|
|
339
|
-
return false;
|
|
340
|
-
}
|
|
341
|
-
const headerRow = this.triggerRow();
|
|
342
|
-
const headers = Array.from(headerRow.cells);
|
|
343
|
-
const initialIndex = headers.indexOf(armed.header);
|
|
174
|
+
const headers = Array.from(this.triggerRow().cells);
|
|
175
|
+
const initial = headers.indexOf(press.header);
|
|
176
|
+
const rows = this.rows();
|
|
177
|
+
const columns = this.of.element.querySelectorAll(':scope > colgroup > col');
|
|
344
178
|
const reorderable = headers.map(header => this.options.isReorderable?.(header) ?? true);
|
|
345
|
-
if (
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
while ((
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
currentIndex: initialIndex,
|
|
366
|
-
direction,
|
|
367
|
-
header: armed.header,
|
|
368
|
-
initialIndex,
|
|
369
|
-
maximumIndex,
|
|
370
|
-
minimumIndex,
|
|
371
|
-
pointerId: armed.pointerId,
|
|
372
|
-
pointerX,
|
|
373
|
-
scrollContainer: this.closestHorizontalScrollable(),
|
|
374
|
-
status: 'dragging'
|
|
179
|
+
if (!reorderable[initial])
|
|
180
|
+
return;
|
|
181
|
+
let maximum = initial;
|
|
182
|
+
let minimum = initial;
|
|
183
|
+
while ((maximum < headers.length - 1) && reorderable[maximum + 1])
|
|
184
|
+
maximum++;
|
|
185
|
+
while ((minimum > 0) && reorderable[minimum - 1])
|
|
186
|
+
minimum--;
|
|
187
|
+
this.of.element.setPointerCapture(press.id);
|
|
188
|
+
const drag = {
|
|
189
|
+
...press,
|
|
190
|
+
cells: rows.map(row => row.cells[initial]),
|
|
191
|
+
col: columns[initial],
|
|
192
|
+
direction: Math.sign(x - press.origin),
|
|
193
|
+
index: initial,
|
|
194
|
+
initial,
|
|
195
|
+
maximum,
|
|
196
|
+
minimum,
|
|
197
|
+
scroll: this.scrollContainer(),
|
|
198
|
+
x
|
|
375
199
|
};
|
|
376
|
-
|
|
200
|
+
this.drag = drag;
|
|
201
|
+
activeDrag = this;
|
|
377
202
|
document.documentElement.classList.add('column-reorder');
|
|
378
|
-
for (const cell of
|
|
203
|
+
for (const cell of drag.cells)
|
|
379
204
|
cell.classList.add('column-reorder');
|
|
380
205
|
this.requestFrame();
|
|
381
|
-
return true;
|
|
382
206
|
}
|
|
383
|
-
|
|
384
|
-
const
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|| (row.cells[state.currentIndex] !== state.sourceCells[index])) {
|
|
393
|
-
return false;
|
|
394
|
-
}
|
|
207
|
+
target(drag, direction) {
|
|
208
|
+
const headers = this.triggerRow().cells;
|
|
209
|
+
let target = drag.index;
|
|
210
|
+
for (let index = drag.index + direction; (index >= drag.minimum) && (index <= drag.maximum); index += direction) {
|
|
211
|
+
const rect = headers[index].getBoundingClientRect();
|
|
212
|
+
const edge = direction > 0 ? rect.left : rect.right;
|
|
213
|
+
if (direction * (drag.x - edge) < penetration)
|
|
214
|
+
break;
|
|
215
|
+
target = index;
|
|
395
216
|
}
|
|
396
|
-
|
|
397
|
-
if (!state.colGroup)
|
|
398
|
-
return colGroups.length === 0;
|
|
399
|
-
return (colGroups.length === 1)
|
|
400
|
-
&& (colGroups[0] === state.colGroup)
|
|
401
|
-
&& (state.colGroup.children.length === state.columnCount)
|
|
402
|
-
&& (state.colGroup.children[state.currentIndex] === state.sourceCol);
|
|
403
|
-
}
|
|
404
|
-
suppressNextClick() {
|
|
405
|
-
this.suppressClick = true;
|
|
406
|
-
if (this.clickTimer !== undefined)
|
|
407
|
-
clearTimeout(this.clickTimer);
|
|
408
|
-
this.clickTimer = window.setTimeout(() => {
|
|
409
|
-
this.clickTimer = undefined;
|
|
410
|
-
this.suppressClick = false;
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
tableRows() {
|
|
414
|
-
return Array.from(this.of.element.querySelectorAll([
|
|
415
|
-
':scope > thead > tr',
|
|
416
|
-
':scope > tbody > tr',
|
|
417
|
-
':scope > tfoot > tr'
|
|
418
|
-
].join(', ')));
|
|
217
|
+
return target;
|
|
419
218
|
}
|
|
420
219
|
triggerRow() {
|
|
421
220
|
return this.of.element.tHead?.rows[0] ?? this.of.element.tBodies[0]?.rows[0];
|
|
422
221
|
}
|
|
222
|
+
updatePointer(x) {
|
|
223
|
+
const drag = this.drag;
|
|
224
|
+
if (x === drag.x)
|
|
225
|
+
return;
|
|
226
|
+
drag.direction = Math.sign(x - drag.x);
|
|
227
|
+
drag.x = x;
|
|
228
|
+
this.requestFrame();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
export class TableColumnReorderOptions extends PluginOptions {
|
|
232
|
+
isReorderable;
|
|
233
|
+
onReorder;
|
|
234
|
+
threshold = 10;
|
|
423
235
|
}
|
package/package.json
CHANGED