@itrocks/table 0.2.6 → 0.2.7
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 +2 -1
- package/column-reorder.d.ts +7 -2
- package/column-reorder.js +51 -12
- package/package.json +1 -1
package/column-reorder-freeze.js
CHANGED
|
@@ -11,7 +11,8 @@ export class TableColumnReorderFreeze extends Plugin {
|
|
|
11
11
|
return;
|
|
12
12
|
reorder.options.isReorderable = header => {
|
|
13
13
|
const index = this.of.cellColumnNumber(header);
|
|
14
|
-
|
|
14
|
+
const row = header.parentElement;
|
|
15
|
+
return (index >= freeze.leftColumnCount) && (index < row.cells.length - freeze.rightColumnCount);
|
|
15
16
|
};
|
|
16
17
|
}
|
|
17
18
|
}
|
package/column-reorder.d.ts
CHANGED
|
@@ -10,14 +10,17 @@ interface Drag extends Press {
|
|
|
10
10
|
initial: number;
|
|
11
11
|
maximum: number;
|
|
12
12
|
minimum: number;
|
|
13
|
+
remove: boolean;
|
|
13
14
|
scroll?: HTMLElement;
|
|
14
15
|
time?: number;
|
|
15
16
|
x: number;
|
|
17
|
+
y: number;
|
|
16
18
|
}
|
|
17
19
|
interface Press {
|
|
18
20
|
header: HTMLTableCellElement;
|
|
19
21
|
id: number;
|
|
20
22
|
origin: number;
|
|
23
|
+
y: number;
|
|
21
24
|
}
|
|
22
25
|
export declare class TableColumnReorder extends Plugin<Table, TableColumnReorderOptions> {
|
|
23
26
|
protected drag?: Drag;
|
|
@@ -34,13 +37,15 @@ export declare class TableColumnReorder extends Plugin<Table, TableColumnReorder
|
|
|
34
37
|
protected rows(): HTMLTableRowElement[];
|
|
35
38
|
protected scroll(drag: Drag, time: number): Direction;
|
|
36
39
|
protected scrollContainer(): HTMLElement | undefined;
|
|
37
|
-
protected start(press: Press, x: number): void;
|
|
40
|
+
protected start(press: Press, x: number, y: number): void;
|
|
38
41
|
protected target(drag: Drag, direction: Direction): number;
|
|
39
42
|
protected triggerRow(): HTMLTableRowElement;
|
|
40
|
-
protected
|
|
43
|
+
protected updateState(drag: Drag): void;
|
|
44
|
+
protected updatePointer(x: number, y: number): void;
|
|
41
45
|
}
|
|
42
46
|
export declare class TableColumnReorderOptions extends PluginOptions {
|
|
43
47
|
isReorderable?: (header: HTMLTableCellElement) => boolean;
|
|
48
|
+
onRemove?: (header: HTMLTableCellElement) => void;
|
|
44
49
|
onReorder?: (header: HTMLTableCellElement) => void;
|
|
45
50
|
threshold: number;
|
|
46
51
|
}
|
package/column-reorder.js
CHANGED
|
@@ -13,6 +13,11 @@ export class TableColumnReorder extends Plugin {
|
|
|
13
13
|
animate(time) {
|
|
14
14
|
this.frame = undefined;
|
|
15
15
|
const drag = this.drag;
|
|
16
|
+
this.updateState(drag);
|
|
17
|
+
if (drag.remove) {
|
|
18
|
+
drag.time = undefined;
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
16
21
|
const pointerDirection = drag.direction;
|
|
17
22
|
drag.direction = 0;
|
|
18
23
|
const scrollDirection = this.scroll(drag, time);
|
|
@@ -37,15 +42,27 @@ export class TableColumnReorder extends Plugin {
|
|
|
37
42
|
const changed = drag.index !== drag.initial;
|
|
38
43
|
if (!commit && changed)
|
|
39
44
|
this.move(drag, drag.initial);
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
const removed = commit && drag.remove;
|
|
46
|
+
if (removed) {
|
|
47
|
+
for (const row of this.rows()) {
|
|
48
|
+
row.cells[drag.index]?.remove();
|
|
49
|
+
}
|
|
50
|
+
drag.col?.remove();
|
|
51
|
+
}
|
|
52
|
+
for (const cell of drag.cells) {
|
|
53
|
+
cell.classList.remove('column-reorder', 'moving', 'removing');
|
|
54
|
+
}
|
|
42
55
|
document.documentElement.classList.remove('column-reorder');
|
|
43
56
|
activeDrag = undefined;
|
|
44
57
|
this.drag = undefined;
|
|
45
58
|
if (this.of.element.hasPointerCapture(drag.id))
|
|
46
59
|
this.of.element.releasePointerCapture(drag.id);
|
|
47
|
-
if (
|
|
60
|
+
if (removed) {
|
|
61
|
+
this.options.onRemove?.(drag.header);
|
|
62
|
+
}
|
|
63
|
+
else if (commit && changed) {
|
|
48
64
|
this.options.onReorder?.(drag.header);
|
|
65
|
+
}
|
|
49
66
|
}
|
|
50
67
|
init() {
|
|
51
68
|
const table = this.of;
|
|
@@ -82,6 +99,7 @@ export class TableColumnReorder extends Plugin {
|
|
|
82
99
|
after ? target.after(drag.col) : target.before(drag.col);
|
|
83
100
|
}
|
|
84
101
|
drag.index = targetIndex;
|
|
102
|
+
this.updateState(drag);
|
|
85
103
|
}
|
|
86
104
|
onPointer(event) {
|
|
87
105
|
if (event.type === 'pointerdown') {
|
|
@@ -90,7 +108,7 @@ export class TableColumnReorder extends Plugin {
|
|
|
90
108
|
const header = event.target.closest('th, td');
|
|
91
109
|
if (!header || (header.parentElement !== this.triggerRow()))
|
|
92
110
|
return;
|
|
93
|
-
this.press = { header, id: event.pointerId, origin: event.clientX };
|
|
111
|
+
this.press = { header, id: event.pointerId, origin: event.clientX, y: event.clientY };
|
|
94
112
|
return;
|
|
95
113
|
}
|
|
96
114
|
const state = this.drag ?? this.press;
|
|
@@ -106,7 +124,7 @@ export class TableColumnReorder extends Plugin {
|
|
|
106
124
|
return;
|
|
107
125
|
}
|
|
108
126
|
event.preventDefault();
|
|
109
|
-
this.updatePointer(event.clientX);
|
|
127
|
+
this.updatePointer(event.clientX, event.clientY);
|
|
110
128
|
if (this.frame !== undefined) {
|
|
111
129
|
cancelAnimationFrame(this.frame);
|
|
112
130
|
this.frame = undefined;
|
|
@@ -118,15 +136,15 @@ export class TableColumnReorder extends Plugin {
|
|
|
118
136
|
return;
|
|
119
137
|
}
|
|
120
138
|
if (!this.drag) {
|
|
121
|
-
if (Math.
|
|
139
|
+
if (Math.hypot(event.clientX - state.origin, event.clientY - state.y) < this.options.threshold)
|
|
122
140
|
return;
|
|
123
141
|
this.press = undefined;
|
|
124
|
-
this.start(state, event.clientX);
|
|
142
|
+
this.start(state, event.clientX, event.clientY);
|
|
125
143
|
}
|
|
126
144
|
if (!this.drag)
|
|
127
145
|
return;
|
|
128
146
|
event.preventDefault();
|
|
129
|
-
this.updatePointer(event.clientX);
|
|
147
|
+
this.updatePointer(event.clientX, event.clientY);
|
|
130
148
|
}
|
|
131
149
|
requestFrame() {
|
|
132
150
|
if (this.frame === undefined)
|
|
@@ -168,7 +186,7 @@ export class TableColumnReorder extends Plugin {
|
|
|
168
186
|
}
|
|
169
187
|
return undefined;
|
|
170
188
|
}
|
|
171
|
-
start(press, x) {
|
|
189
|
+
start(press, x, y) {
|
|
172
190
|
if (activeDrag)
|
|
173
191
|
return;
|
|
174
192
|
const headers = Array.from(this.triggerRow().cells);
|
|
@@ -194,14 +212,17 @@ export class TableColumnReorder extends Plugin {
|
|
|
194
212
|
initial,
|
|
195
213
|
maximum,
|
|
196
214
|
minimum,
|
|
215
|
+
remove: false,
|
|
197
216
|
scroll: this.scrollContainer(),
|
|
198
|
-
x
|
|
217
|
+
x,
|
|
218
|
+
y
|
|
199
219
|
};
|
|
200
220
|
this.drag = drag;
|
|
201
221
|
activeDrag = this;
|
|
202
222
|
document.documentElement.classList.add('column-reorder');
|
|
203
223
|
for (const cell of drag.cells)
|
|
204
224
|
cell.classList.add('column-reorder');
|
|
225
|
+
this.updateState(drag);
|
|
205
226
|
this.requestFrame();
|
|
206
227
|
}
|
|
207
228
|
target(drag, direction) {
|
|
@@ -219,17 +240,35 @@ export class TableColumnReorder extends Plugin {
|
|
|
219
240
|
triggerRow() {
|
|
220
241
|
return this.of.element.tHead?.rows[0] ?? this.of.element.tBodies[0]?.rows[0];
|
|
221
242
|
}
|
|
222
|
-
|
|
243
|
+
updateState(drag) {
|
|
244
|
+
if (this.options.onRemove) {
|
|
245
|
+
const rect = this.of.element.getBoundingClientRect();
|
|
246
|
+
const headers = this.triggerRow().cells;
|
|
247
|
+
const first = headers[drag.minimum].getBoundingClientRect();
|
|
248
|
+
const last = headers[drag.maximum].getBoundingClientRect();
|
|
249
|
+
const left = Math.max(rect.left, first.left, headers[drag.minimum - 1]?.getBoundingClientRect().right ?? rect.left);
|
|
250
|
+
const right = Math.min(rect.right, last.right, headers[drag.maximum + 1]?.getBoundingClientRect().left ?? rect.right);
|
|
251
|
+
drag.remove = (drag.x < left) || (drag.x > right) || (drag.y < rect.top) || (drag.y > rect.bottom);
|
|
252
|
+
}
|
|
253
|
+
for (const cell of drag.cells) {
|
|
254
|
+
cell.classList.toggle('removing', drag.remove);
|
|
255
|
+
cell.classList.toggle('moving', !drag.remove && (drag.index !== drag.initial));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
updatePointer(x, y) {
|
|
223
259
|
const drag = this.drag;
|
|
224
|
-
if (x === drag.x)
|
|
260
|
+
if ((x === drag.x) && (y === drag.y))
|
|
225
261
|
return;
|
|
226
262
|
drag.direction = Math.sign(x - drag.x);
|
|
227
263
|
drag.x = x;
|
|
264
|
+
drag.y = y;
|
|
265
|
+
this.updateState(drag);
|
|
228
266
|
this.requestFrame();
|
|
229
267
|
}
|
|
230
268
|
}
|
|
231
269
|
export class TableColumnReorderOptions extends PluginOptions {
|
|
232
270
|
isReorderable;
|
|
271
|
+
onRemove;
|
|
233
272
|
onReorder;
|
|
234
273
|
threshold = 10;
|
|
235
274
|
}
|
package/package.json
CHANGED