@itrocks/table 0.2.9 → 0.4.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/README.md +95 -0
- package/column-add.css +33 -0
- package/column-add.d.ts +79 -0
- package/column-add.js +361 -0
- package/column-reorder.d.ts +29 -2
- package/column-reorder.js +146 -32
- package/freeze.d.ts +4 -1
- package/freeze.js +27 -14
- package/package.json +3 -2
- package/table.d.ts +1 -0
- package/table.js +3 -0
package/README.md
CHANGED
|
@@ -224,3 +224,98 @@ Finds all tables matching the CSS selector and returns the corresponding
|
|
|
224
224
|
multiple rows directly in a table.
|
|
225
225
|
- Implement custom plugins on top of `HasPlugins<Table>` to add domain‑
|
|
226
226
|
specific features (validation, inline actions, totals rows, etc.).
|
|
227
|
+
|
|
228
|
+
## Adding columns
|
|
229
|
+
|
|
230
|
+
Import the public browser entry and stylesheet. Configure `TableColumnAdd` after `TableColumnReorder` and any
|
|
231
|
+
freeze plugins. The addition plugin consumes opaque string ids and callback-rendered cells; it has no dependency on
|
|
232
|
+
business objects, selectors, routes or storage.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { TableColumnAdd } from '@itrocks/table/column-add.js'
|
|
236
|
+
import { TableColumnReorder } from '@itrocks/table/column-reorder.js'
|
|
237
|
+
import { Table } from '@itrocks/table/table.js'
|
|
238
|
+
|
|
239
|
+
const addition = new TableColumnAdd({
|
|
240
|
+
onChange: change => console.log(change.id, change.state)
|
|
241
|
+
})
|
|
242
|
+
const table = new Table(document.querySelector('table')!, {
|
|
243
|
+
plugins: [TableColumnReorder, addition]
|
|
244
|
+
})
|
|
245
|
+
const result = await addition.add({
|
|
246
|
+
id: 'opaque-column-id',
|
|
247
|
+
create: () => ({
|
|
248
|
+
header: Object.assign(document.createElement('th'), { textContent: 'Metric' }),
|
|
249
|
+
cells: Array.from(table.element.tBodies[0].rows, () => document.createElement('td'))
|
|
250
|
+
})
|
|
251
|
+
})
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Include `<link rel="stylesheet" href="/node_modules/@itrocks/table/column-add.css">` in the consuming document,
|
|
255
|
+
adapting its public URL to your asset host. See [the standalone fixture](demo/column-add.html) for asynchronous values,
|
|
256
|
+
retry, reset, sorting and frozen columns. The fixture is development material and is excluded from the package tarball.
|
|
257
|
+
|
|
258
|
+
`ColumnSource.create(): ColumnContent` supplies one detached header and one detached cell for every other table row,
|
|
259
|
+
in DOM order, including footer rows. Addition supports rectangular tables with exactly one header row and no spans.
|
|
260
|
+
Invalid creation rejects before insertion. A `colgroup`, when present, must have one `col` per column.
|
|
261
|
+
`ColumnSource.load?(signal): Promise<ColumnContent>` optionally supplies replacement header/cells asynchronously.
|
|
262
|
+
Callbacks own safe rendering, row identity and data access. If row elements change while loading, the plugin reports an
|
|
263
|
+
error rather than applying stale positional values. Before retry, the consumer updates its callbacks' row data to
|
|
264
|
+
match the current view.
|
|
265
|
+
|
|
266
|
+
- `add(source): Promise<'added' | 'existing'>` appends before right-frozen columns, reveals and highlights. It resolves
|
|
267
|
+
once the real placeholder column is committed, independently of optional values. A repeated id reveals/highlights its
|
|
268
|
+
existing position, without invoking its creation/loading callbacks or issuing another commit.
|
|
269
|
+
- `reveal(id): void` scrolls the horizontal container only as needed, accounting for frozen bounds.
|
|
270
|
+
- `highlight(id): void` applies the same green/light/theme flash for new and existing columns, cleans up after 900 ms,
|
|
271
|
+
honours reduced motion and preserves keyboard focus and row selection.
|
|
272
|
+
- `retry(id): Promise<void>` retries a failed value callback. Failure retains the committed column and marks it with
|
|
273
|
+
`.column-add-error`; the consumer presents the error and a retry control. It never confirms another addition.
|
|
274
|
+
- `begin(source): ColumnTransaction` creates a real provisional column without committing it. Its `ready` promise waits
|
|
275
|
+
for optional values and resolves after error notification as well. `moveTo(index)` reuses public reorder placement
|
|
276
|
+
and its existing boundaries. `commit()` validates it and returns `added` or `existing`; repeating commit returns
|
|
277
|
+
`existing`. `cancel()` aborts values, removes new provisional cells or restores an existing column's position.
|
|
278
|
+
Completed/cancelled handles cannot move a column. `begin()` itself installs no pointer transport.
|
|
279
|
+
- `dispose(): void` cancels provisional operations, aborts outstanding values and removes flash resources. Committed
|
|
280
|
+
columns remain. `Table.reset()` invokes disposal automatically; call disposal when removing the owning view.
|
|
281
|
+
|
|
282
|
+
`TableColumnAddOptions.identify(header)` defaults to `header.dataset.columnId`. Added and loaded headers carry this
|
|
283
|
+
opaque attribute so identity survives plugin resets. A custom callback can identify pre-existing columns differently.
|
|
284
|
+
`onChange(change)` runs synchronously for `provisional`, `committed`, `loaded`, `error` and `cancelled` observations;
|
|
285
|
+
`change` contains `id`, `header`, `state` and an optional `error`. Consumers persist only `committed` changes and must
|
|
286
|
+
not throw from this observer. Loading uses `.column-add-loading` and `aria-busy`, independently of provisional state.
|
|
287
|
+
An aborted or removed operation cannot be recreated by late values.
|
|
288
|
+
|
|
289
|
+
`TableColumnReorder.moveColumn(header, index)` is public placement without header gesture callbacks. It rejects an
|
|
290
|
+
active header interaction and clamps movement to that header's reorderable boundaries.
|
|
291
|
+
`TableColumnReorder.replaceCell(previous, replacement)`
|
|
292
|
+
preserves an active header gesture during asynchronous cell replacement. `TableFreeze.refresh()` recalculates sticky
|
|
293
|
+
offsets after structural changes. `Table.refreshStyleSheets()` reapplies the managed styles.
|
|
294
|
+
|
|
295
|
+
`TableColumnReorder.scrollContainer()` exposes the existing horizontal scroll owner for table plugins.
|
|
296
|
+
`TableFreeze.visibleInnerRect()` returns the area between rendered sticky cells, including tables configured through
|
|
297
|
+
`colgroup`. `reveal()` fully exposes columns that fit that area and uses its maximum width for wider columns, with
|
|
298
|
+
minimal scrolling in either direction. Freeze refresh measures natural cell positions before recalculating offsets,
|
|
299
|
+
including header-backed tables after addition, asynchronous replacement or reset.
|
|
300
|
+
|
|
301
|
+
### External pointer transport
|
|
302
|
+
|
|
303
|
+
`beginExternal(source, sample): ColumnOperation` immediately creates a provisional real column, or reuses an existing
|
|
304
|
+
id. The structural `PointerSample` contains `pointerId`, `pointerType`, `clientX` and `clientY`. The source owns
|
|
305
|
+
capture.
|
|
306
|
+
`move(sample)` uses the existing reorder engine, including frozen boundaries and visible-edge autoscroll.
|
|
307
|
+
`finish(sample)` resolves to `added`, `moved` or `cancelled`; an outside drop cancels without a fallback insertion.
|
|
308
|
+
`cancel()` restores an existing column's original position or removes a new preview. Neither operation invokes the
|
|
309
|
+
header-origin removal callback. Disposal and reset invalidate the handle and discard late loaded content.
|
|
310
|
+
|
|
311
|
+
Optional `ColumnSource.accept(signal)` validates host admission independently of values. It starts once alongside
|
|
312
|
+
`load`; a valid drop waits for admission, with geometry held and other column gestures excluded, then commits without
|
|
313
|
+
waiting for `load`. Rejection rolls back and rejects `finish`. An omitted callback accepts immediately. Value errors
|
|
314
|
+
remain retryable and do not reject a valid drop. The host must keep admission independent of its data request.
|
|
315
|
+
|
|
316
|
+
`TableColumnReorder.beginExternal(header, sample): ExternalColumnOperation` is the lower-level public extension for
|
|
317
|
+
an already attached column. Its `move`, `hold`, `finish` and `cancel` methods share header-origin geometry and
|
|
318
|
+
placement.
|
|
319
|
+
`hold(sample)` settles and pauses autoscroll while retaining exclusivity; it returns whether the target is valid.
|
|
320
|
+
`finish(sample)` releases the gesture and returns acceptance. It never calls header reorder/removal notifications;
|
|
321
|
+
the owning addition transaction emits its distinct commit notification. Header-origin gestures retain their callbacks.
|
package/column-add.css
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-highlight {
|
|
2
|
+
animation: itrocks-column-add-flash 850ms ease-out;
|
|
3
|
+
}
|
|
4
|
+
table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-loading {
|
|
5
|
+
cursor: progress;
|
|
6
|
+
}
|
|
7
|
+
table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-provisional {
|
|
8
|
+
background-color: color-mix(in srgb, #58b46b 5%, Canvas);
|
|
9
|
+
}
|
|
10
|
+
table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-error {
|
|
11
|
+
outline: 1px solid #c33;
|
|
12
|
+
outline-offset: -2px;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@keyframes itrocks-column-add-flash {
|
|
16
|
+
0% {
|
|
17
|
+
background-color: color-mix(in srgb, #58b46b 35%, Canvas);
|
|
18
|
+
}
|
|
19
|
+
45% {
|
|
20
|
+
background-color: color-mix(in srgb, white 20%, Canvas);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
@media (prefers-reduced-motion: reduce) {
|
|
24
|
+
table.itrocks > :is(thead, tbody, tfoot) > tr > :is(th, td).column-add-highlight {
|
|
25
|
+
animation: none;
|
|
26
|
+
outline: 2px solid color-mix(in srgb, #58b46b 65%, Canvas);
|
|
27
|
+
outline-offset: -2px;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
table.itrocks > thead > tr > .column-add-provisional::before {
|
|
31
|
+
content: "+ ";
|
|
32
|
+
color: #28783b;
|
|
33
|
+
}
|
package/column-add.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Plugin } from '../plugin/plugin.js';
|
|
2
|
+
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
|
+
import type { PointerSample } from './column-reorder.js';
|
|
4
|
+
import { TableColumnReorder } from './column-reorder.js';
|
|
5
|
+
import { Table } from './table.js';
|
|
6
|
+
export type ColumnChange = {
|
|
7
|
+
error?: unknown;
|
|
8
|
+
header: HTMLTableCellElement;
|
|
9
|
+
id: string;
|
|
10
|
+
state: ColumnState;
|
|
11
|
+
};
|
|
12
|
+
export type ColumnContent = {
|
|
13
|
+
cells: readonly HTMLTableCellElement[];
|
|
14
|
+
header: HTMLTableCellElement;
|
|
15
|
+
};
|
|
16
|
+
export interface ColumnOperation {
|
|
17
|
+
cancel(): void;
|
|
18
|
+
finish(sample: PointerSample): Promise<'added' | 'moved' | 'cancelled'>;
|
|
19
|
+
move(sample: PointerSample): void;
|
|
20
|
+
}
|
|
21
|
+
export type ColumnResult = 'added' | 'existing';
|
|
22
|
+
export type ColumnSource = {
|
|
23
|
+
accept?(signal: AbortSignal): Promise<void>;
|
|
24
|
+
create(): ColumnContent;
|
|
25
|
+
id: string;
|
|
26
|
+
load?(signal: AbortSignal): Promise<ColumnContent>;
|
|
27
|
+
};
|
|
28
|
+
export type ColumnState = 'cancelled' | 'committed' | 'error' | 'loaded' | 'provisional';
|
|
29
|
+
export interface ColumnTransaction {
|
|
30
|
+
readonly header: HTMLTableCellElement;
|
|
31
|
+
readonly id: string;
|
|
32
|
+
readonly ready: Promise<void>;
|
|
33
|
+
readonly state: 'cancelled' | 'committed' | 'provisional';
|
|
34
|
+
cancel(): void;
|
|
35
|
+
commit(): ColumnResult;
|
|
36
|
+
moveTo(index: number): void;
|
|
37
|
+
}
|
|
38
|
+
interface Pending {
|
|
39
|
+
abort: AbortController;
|
|
40
|
+
col?: HTMLTableColElement;
|
|
41
|
+
content: ColumnContent;
|
|
42
|
+
initial: number;
|
|
43
|
+
rows: HTMLTableRowElement[];
|
|
44
|
+
settled: boolean;
|
|
45
|
+
source: ColumnSource;
|
|
46
|
+
transaction: ColumnTransaction;
|
|
47
|
+
}
|
|
48
|
+
export declare class TableColumnAdd extends Plugin<Table, TableColumnAddOptions> {
|
|
49
|
+
protected disposed: boolean;
|
|
50
|
+
protected external?: ColumnOperation;
|
|
51
|
+
protected highlights: Map<HTMLTableCellElement, number>;
|
|
52
|
+
protected identities: WeakMap<HTMLTableCellElement, string>;
|
|
53
|
+
protected pending: Map<string, Pending>;
|
|
54
|
+
protected reorder: TableColumnReorder;
|
|
55
|
+
add(source: ColumnSource): Promise<ColumnResult>;
|
|
56
|
+
/** Create a provisional column without installing pointer transport. */
|
|
57
|
+
begin(source: ColumnSource): ColumnTransaction;
|
|
58
|
+
/** Add or move an opaque column through the existing reorder geometry. */
|
|
59
|
+
beginExternal(source: ColumnSource, sample: PointerSample): ColumnOperation;
|
|
60
|
+
defaultOptions(): TableColumnAddOptions;
|
|
61
|
+
dispose(): void;
|
|
62
|
+
protected find(id: string): HTMLTableCellElement | undefined;
|
|
63
|
+
protected headers(): HTMLTableRowElement;
|
|
64
|
+
highlight(id: string): void;
|
|
65
|
+
init(): void;
|
|
66
|
+
protected live(entry: Pending): boolean;
|
|
67
|
+
protected load(entry: Pending): Promise<void>;
|
|
68
|
+
protected mark(entry: Pending, state: 'error' | 'loading' | 'provisional', present: boolean): void;
|
|
69
|
+
protected notify(entry: Pending, state: ColumnState, error?: unknown): void;
|
|
70
|
+
protected refresh(): void;
|
|
71
|
+
reveal(id: string): void;
|
|
72
|
+
retry(id: string): Promise<void>;
|
|
73
|
+
protected validate(content: ColumnContent, rows: HTMLTableRowElement[]): void;
|
|
74
|
+
}
|
|
75
|
+
export declare class TableColumnAddOptions extends PluginOptions {
|
|
76
|
+
identify: (header: HTMLTableCellElement) => string | undefined;
|
|
77
|
+
onChange?: (change: ColumnChange) => void;
|
|
78
|
+
}
|
|
79
|
+
export {};
|
package/column-add.js
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { Plugin } from '../plugin/plugin.js';
|
|
2
|
+
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
|
+
import { TableColumnReorder } from './column-reorder.js';
|
|
4
|
+
import { TableFreeze } from './freeze.js';
|
|
5
|
+
export class TableColumnAdd extends Plugin {
|
|
6
|
+
disposed = false;
|
|
7
|
+
external;
|
|
8
|
+
highlights = new Map();
|
|
9
|
+
identities = new WeakMap();
|
|
10
|
+
pending = new Map();
|
|
11
|
+
reorder;
|
|
12
|
+
add(source) {
|
|
13
|
+
try {
|
|
14
|
+
if (this.disposed)
|
|
15
|
+
throw new Error('Column addition is disposed');
|
|
16
|
+
if (this.find(source.id)) {
|
|
17
|
+
this.reveal(source.id);
|
|
18
|
+
this.highlight(source.id);
|
|
19
|
+
return Promise.resolve('existing');
|
|
20
|
+
}
|
|
21
|
+
const operation = this.begin(source);
|
|
22
|
+
const result = operation.commit();
|
|
23
|
+
this.reveal(source.id);
|
|
24
|
+
this.highlight(source.id);
|
|
25
|
+
return Promise.resolve(result);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
return Promise.reject(error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Create a provisional column without installing pointer transport. */
|
|
32
|
+
begin(source) {
|
|
33
|
+
if (this.disposed)
|
|
34
|
+
throw new Error('Column addition is disposed');
|
|
35
|
+
const pending = this.pending.get(source.id);
|
|
36
|
+
if (pending && pending.content.header.parentElement === this.headers())
|
|
37
|
+
return pending.transaction;
|
|
38
|
+
if (pending) {
|
|
39
|
+
pending.abort.abort();
|
|
40
|
+
this.pending.delete(source.id);
|
|
41
|
+
}
|
|
42
|
+
const headers = this.headers();
|
|
43
|
+
const existing = this.find(source.id);
|
|
44
|
+
const rows = Array.from(this.of.element.rows).filter(row => row !== headers);
|
|
45
|
+
const content = existing
|
|
46
|
+
? { header: existing, cells: rows.map(row => row.cells[existing.cellIndex]) }
|
|
47
|
+
: source.create();
|
|
48
|
+
this.validate(content, rows);
|
|
49
|
+
if (!existing && [content.header, ...content.cells].some(cell => cell.parentElement))
|
|
50
|
+
throw new Error('New column content must be detached');
|
|
51
|
+
const abort = new AbortController();
|
|
52
|
+
let state = 'provisional';
|
|
53
|
+
let ready = Promise.resolve();
|
|
54
|
+
const transaction = {
|
|
55
|
+
get header() { return entry.content.header; },
|
|
56
|
+
id: source.id,
|
|
57
|
+
get ready() { return ready; },
|
|
58
|
+
get state() { return state; },
|
|
59
|
+
cancel: () => {
|
|
60
|
+
if (state !== 'provisional')
|
|
61
|
+
return;
|
|
62
|
+
state = 'cancelled';
|
|
63
|
+
abort.abort();
|
|
64
|
+
this.pending.delete(source.id);
|
|
65
|
+
if (existing)
|
|
66
|
+
this.reorder.moveColumn(entry.content.header, entry.initial);
|
|
67
|
+
else {
|
|
68
|
+
for (const cell of [entry.content.header, ...entry.content.cells])
|
|
69
|
+
cell.remove();
|
|
70
|
+
entry.col?.remove();
|
|
71
|
+
}
|
|
72
|
+
this.refresh();
|
|
73
|
+
this.notify(entry, 'cancelled');
|
|
74
|
+
},
|
|
75
|
+
commit: () => {
|
|
76
|
+
if (state === 'cancelled')
|
|
77
|
+
throw new Error('Column transaction was cancelled');
|
|
78
|
+
if (state === 'committed')
|
|
79
|
+
return 'existing';
|
|
80
|
+
state = 'committed';
|
|
81
|
+
this.mark(entry, 'provisional', false);
|
|
82
|
+
if (!existing || (entry.content.header.cellIndex !== entry.initial))
|
|
83
|
+
this.notify(entry, 'committed');
|
|
84
|
+
if (existing || !source.load || (entry.settled && !entry.content.header.classList.contains('column-add-error')))
|
|
85
|
+
this.pending.delete(source.id);
|
|
86
|
+
return existing ? 'existing' : 'added';
|
|
87
|
+
},
|
|
88
|
+
moveTo: index => {
|
|
89
|
+
if (!Number.isFinite(index))
|
|
90
|
+
throw new Error('Column index must be finite');
|
|
91
|
+
if (state !== 'provisional')
|
|
92
|
+
return;
|
|
93
|
+
this.reorder.moveColumn(entry.content.header, index);
|
|
94
|
+
this.refresh();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const entry = { abort, content, initial: existing?.cellIndex ?? -1,
|
|
98
|
+
rows, settled: false, source, transaction };
|
|
99
|
+
this.pending.set(source.id, entry);
|
|
100
|
+
if (existing)
|
|
101
|
+
return transaction;
|
|
102
|
+
this.identities.set(content.header, source.id);
|
|
103
|
+
content.header.dataset.columnId = source.id;
|
|
104
|
+
const freeze = this.of.plugins.TableFreeze;
|
|
105
|
+
const index = headers.cells.length - (freeze instanceof TableFreeze ? freeze.rightColumnCount : 0);
|
|
106
|
+
headers.insertBefore(content.header, headers.cells[index] ?? null);
|
|
107
|
+
rows.forEach((row, number) => row.insertBefore(content.cells[number], row.cells[index] ?? null));
|
|
108
|
+
const group = this.of.element.querySelector('colgroup');
|
|
109
|
+
if (group) {
|
|
110
|
+
entry.col = document.createElement('col');
|
|
111
|
+
group.insertBefore(entry.col, group.children[index] ?? null);
|
|
112
|
+
}
|
|
113
|
+
this.mark(entry, 'provisional', true);
|
|
114
|
+
this.refresh();
|
|
115
|
+
this.notify(entry, 'provisional');
|
|
116
|
+
if (source.load)
|
|
117
|
+
ready = this.load(entry);
|
|
118
|
+
return transaction;
|
|
119
|
+
}
|
|
120
|
+
/** Add or move an opaque column through the existing reorder geometry. */
|
|
121
|
+
beginExternal(source, sample) {
|
|
122
|
+
if (this.disposed || this.external)
|
|
123
|
+
throw new Error('Column addition is unavailable');
|
|
124
|
+
if (this.pending.get(source.id)?.transaction.state === 'provisional')
|
|
125
|
+
throw new Error('Column addition is pending');
|
|
126
|
+
const existing = this.find(source.id);
|
|
127
|
+
const initial = existing?.cellIndex;
|
|
128
|
+
const transaction = existing ? undefined : this.begin(source);
|
|
129
|
+
let reorder;
|
|
130
|
+
try {
|
|
131
|
+
reorder = this.reorder.beginExternal(existing ?? transaction.header, sample);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
transaction?.cancel();
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
let complete = false;
|
|
138
|
+
let finishing = false;
|
|
139
|
+
const abort = new AbortController();
|
|
140
|
+
const acceptance = Promise.resolve().then(() => source.accept?.(abort.signal));
|
|
141
|
+
// Keep a rejected admission observable at finish without an unhandled rejection during transport.
|
|
142
|
+
void acceptance.catch(() => { });
|
|
143
|
+
const operation = {
|
|
144
|
+
cancel: () => {
|
|
145
|
+
if (complete)
|
|
146
|
+
return;
|
|
147
|
+
complete = true;
|
|
148
|
+
abort.abort();
|
|
149
|
+
reorder.cancel();
|
|
150
|
+
if (initial !== undefined)
|
|
151
|
+
this.reorder.moveColumn(this.find(source.id), initial);
|
|
152
|
+
transaction?.cancel();
|
|
153
|
+
this.external = undefined;
|
|
154
|
+
this.refresh();
|
|
155
|
+
},
|
|
156
|
+
finish: async (point) => {
|
|
157
|
+
if (complete || finishing)
|
|
158
|
+
return 'cancelled';
|
|
159
|
+
finishing = true;
|
|
160
|
+
if (!reorder.hold(point)) {
|
|
161
|
+
operation.cancel();
|
|
162
|
+
return 'cancelled';
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
await acceptance;
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
operation.cancel();
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
if (complete)
|
|
172
|
+
return 'cancelled';
|
|
173
|
+
if (!reorder.finish(point)) {
|
|
174
|
+
operation.cancel();
|
|
175
|
+
return 'cancelled';
|
|
176
|
+
}
|
|
177
|
+
complete = true;
|
|
178
|
+
this.external = undefined;
|
|
179
|
+
if (transaction)
|
|
180
|
+
transaction.commit();
|
|
181
|
+
else
|
|
182
|
+
this.options.onChange?.({ header: this.find(source.id), id: source.id, state: 'committed' });
|
|
183
|
+
this.refresh();
|
|
184
|
+
return existing ? 'moved' : 'added';
|
|
185
|
+
},
|
|
186
|
+
move: point => { if (!complete)
|
|
187
|
+
reorder.move(point); }
|
|
188
|
+
};
|
|
189
|
+
this.external = operation;
|
|
190
|
+
return operation;
|
|
191
|
+
}
|
|
192
|
+
defaultOptions() {
|
|
193
|
+
return new TableColumnAddOptions();
|
|
194
|
+
}
|
|
195
|
+
dispose() {
|
|
196
|
+
this.external?.cancel();
|
|
197
|
+
this.disposed = true;
|
|
198
|
+
for (const entry of [...this.pending.values()]) {
|
|
199
|
+
entry.transaction.cancel();
|
|
200
|
+
entry.abort.abort();
|
|
201
|
+
this.mark(entry, 'loading', false);
|
|
202
|
+
}
|
|
203
|
+
this.pending.clear();
|
|
204
|
+
for (const [cell, timer] of this.highlights) {
|
|
205
|
+
clearTimeout(timer);
|
|
206
|
+
cell.classList.remove('column-add-highlight');
|
|
207
|
+
}
|
|
208
|
+
this.highlights.clear();
|
|
209
|
+
}
|
|
210
|
+
find(id) {
|
|
211
|
+
return Array.from(this.headers().cells).find(header => (this.identities.get(header) ?? this.options.identify(header)) === id);
|
|
212
|
+
}
|
|
213
|
+
headers() {
|
|
214
|
+
const row = this.of.element.tHead?.rows[0];
|
|
215
|
+
if (!row)
|
|
216
|
+
throw new Error('Column addition requires one header row');
|
|
217
|
+
return row;
|
|
218
|
+
}
|
|
219
|
+
highlight(id) {
|
|
220
|
+
const header = this.find(id);
|
|
221
|
+
if (!header || this.disposed)
|
|
222
|
+
return;
|
|
223
|
+
for (const row of Array.from(this.of.element.rows)) {
|
|
224
|
+
const cell = row.cells[header.cellIndex];
|
|
225
|
+
if (!cell)
|
|
226
|
+
continue;
|
|
227
|
+
clearTimeout(this.highlights.get(cell));
|
|
228
|
+
cell.classList.remove('column-add-highlight');
|
|
229
|
+
void cell.offsetWidth;
|
|
230
|
+
cell.classList.add('column-add-highlight');
|
|
231
|
+
this.highlights.set(cell, setTimeout(() => {
|
|
232
|
+
cell.classList.remove('column-add-highlight');
|
|
233
|
+
this.highlights.delete(cell);
|
|
234
|
+
}, 900));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
init() {
|
|
238
|
+
this.disposed = false;
|
|
239
|
+
const reorder = this.of.plugins.TableColumnReorder;
|
|
240
|
+
if (!(reorder instanceof TableColumnReorder))
|
|
241
|
+
throw new Error('Configure TableColumnReorder before TableColumnAdd');
|
|
242
|
+
this.reorder = reorder;
|
|
243
|
+
this.of.onReset.push(() => this.dispose());
|
|
244
|
+
}
|
|
245
|
+
live(entry) {
|
|
246
|
+
return !this.disposed && !entry.abort.signal.aborted && (this.pending.get(entry.source.id) === entry)
|
|
247
|
+
&& entry.content.header.parentElement === this.headers();
|
|
248
|
+
}
|
|
249
|
+
async load(entry) {
|
|
250
|
+
this.mark(entry, 'loading', true);
|
|
251
|
+
try {
|
|
252
|
+
const content = await entry.source.load(entry.abort.signal);
|
|
253
|
+
if (!this.live(entry))
|
|
254
|
+
return;
|
|
255
|
+
this.validate(content, entry.rows);
|
|
256
|
+
if ([content.header, ...content.cells].some(cell => cell.parentElement))
|
|
257
|
+
throw new Error('Loaded column content must be detached');
|
|
258
|
+
// A consumer must reload changed row views; stale positional data is never applied.
|
|
259
|
+
const rows = Array.from(this.of.element.rows).filter(row => row !== this.headers());
|
|
260
|
+
if ((rows.length !== entry.rows.length) || rows.some((row, index) => row !== entry.rows[index])) {
|
|
261
|
+
throw new Error('Column row view changed during loading');
|
|
262
|
+
}
|
|
263
|
+
const old = entry.content;
|
|
264
|
+
this.reorder.replaceCell(old.header, content.header);
|
|
265
|
+
old.cells.forEach((cell, index) => this.reorder.replaceCell(cell, content.cells[index]));
|
|
266
|
+
entry.content = content;
|
|
267
|
+
this.identities.set(content.header, entry.source.id);
|
|
268
|
+
content.header.dataset.columnId = entry.source.id;
|
|
269
|
+
const highlighted = old.header.classList.contains('column-add-highlight');
|
|
270
|
+
this.mark(entry, 'provisional', entry.transaction.state === 'provisional');
|
|
271
|
+
this.mark(entry, 'loading', false);
|
|
272
|
+
this.refresh();
|
|
273
|
+
this.notify(entry, 'loaded');
|
|
274
|
+
if (highlighted)
|
|
275
|
+
this.highlight(entry.source.id);
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
if (!this.live(entry))
|
|
279
|
+
return;
|
|
280
|
+
this.mark(entry, 'loading', false);
|
|
281
|
+
this.mark(entry, 'error', true);
|
|
282
|
+
this.notify(entry, 'error', error);
|
|
283
|
+
}
|
|
284
|
+
finally {
|
|
285
|
+
entry.settled = true;
|
|
286
|
+
if (entry.transaction.state !== 'provisional' && this.pending.get(entry.source.id) === entry
|
|
287
|
+
&& !entry.content.header.classList.contains('column-add-error'))
|
|
288
|
+
this.pending.delete(entry.source.id);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
mark(entry, state, present) {
|
|
292
|
+
for (const cell of [entry.content.header, ...entry.content.cells]) {
|
|
293
|
+
cell.classList.toggle(`column-add-${state}`, present);
|
|
294
|
+
if (state === 'loading')
|
|
295
|
+
present ? cell.setAttribute('aria-busy', 'true') : cell.removeAttribute('aria-busy');
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
notify(entry, state, error) {
|
|
299
|
+
this.options.onChange?.({ error, header: entry.content.header, id: entry.source.id, state });
|
|
300
|
+
}
|
|
301
|
+
refresh() {
|
|
302
|
+
const freeze = this.of.plugins.TableFreeze;
|
|
303
|
+
if (freeze instanceof TableFreeze)
|
|
304
|
+
freeze.refresh();
|
|
305
|
+
this.of.refreshStyleSheets();
|
|
306
|
+
}
|
|
307
|
+
reveal(id) {
|
|
308
|
+
const header = this.find(id);
|
|
309
|
+
if (!header || this.disposed)
|
|
310
|
+
return;
|
|
311
|
+
const scroll = this.reorder.scrollContainer();
|
|
312
|
+
if (!scroll)
|
|
313
|
+
return;
|
|
314
|
+
const bounds = scroll.getBoundingClientRect();
|
|
315
|
+
const freeze = this.of.plugins.TableFreeze;
|
|
316
|
+
const inner = freeze instanceof TableFreeze ? freeze.visibleInnerRect() : bounds;
|
|
317
|
+
const rect = header.getBoundingClientRect();
|
|
318
|
+
const left = Math.max(bounds.left, inner.left);
|
|
319
|
+
const right = Math.min(bounds.right, inner.right);
|
|
320
|
+
if (right <= left)
|
|
321
|
+
return;
|
|
322
|
+
// A wide column already covering the inner area needs no scroll in either direction.
|
|
323
|
+
if ((rect.left < left) && (rect.right < right))
|
|
324
|
+
scroll.scrollLeft += Math.max(rect.left - left, rect.right - right);
|
|
325
|
+
else if ((rect.right > right) && (rect.left > left))
|
|
326
|
+
scroll.scrollLeft += Math.min(rect.right - right, rect.left - left);
|
|
327
|
+
}
|
|
328
|
+
retry(id) {
|
|
329
|
+
const entry = this.pending.get(id);
|
|
330
|
+
if (!entry || !this.live(entry) || !entry.content.header.classList.contains('column-add-error'))
|
|
331
|
+
return Promise.resolve();
|
|
332
|
+
entry.rows = Array.from(this.of.element.rows).filter(row => row !== this.headers());
|
|
333
|
+
entry.content = {
|
|
334
|
+
cells: entry.rows.map(row => row.cells[entry.content.header.cellIndex]), header: entry.content.header
|
|
335
|
+
};
|
|
336
|
+
this.mark(entry, 'error', false);
|
|
337
|
+
return this.load(entry);
|
|
338
|
+
}
|
|
339
|
+
validate(content, rows) {
|
|
340
|
+
const headers = this.headers();
|
|
341
|
+
const columns = this.of.element.querySelectorAll(':scope > colgroup > col');
|
|
342
|
+
if (this.of.element.querySelectorAll(':scope > colgroup').length > 1)
|
|
343
|
+
throw new Error('Column addition supports one column group');
|
|
344
|
+
if (columns.length && (columns.length !== headers.cells.length
|
|
345
|
+
|| Array.from(columns).some(column => column.span !== 1)))
|
|
346
|
+
throw new Error('Column groups must contain one unspanned col per column');
|
|
347
|
+
if ((this.of.element.tHead.rows.length !== 1) || (content.cells.length !== rows.length))
|
|
348
|
+
throw new Error('Column content must match the current single-header row view');
|
|
349
|
+
const cells = [content.header, ...content.cells];
|
|
350
|
+
if ((new Set(cells).size !== cells.length) || cells.some(cell => !['TD', 'TH'].includes(cell.tagName)
|
|
351
|
+
|| cell.colSpan !== 1 || cell.rowSpan !== 1))
|
|
352
|
+
throw new Error('Column cells must be distinct, unspanned cells');
|
|
353
|
+
if (Array.from(this.of.element.rows).some(row => row.cells.length !== headers.cells.length
|
|
354
|
+
|| Array.from(row.cells).some(cell => cell.colSpan !== 1 || cell.rowSpan !== 1)))
|
|
355
|
+
throw new Error('Column addition requires a rectangular table without spans');
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
export class TableColumnAddOptions extends PluginOptions {
|
|
359
|
+
identify = header => header.dataset.columnId;
|
|
360
|
+
onChange;
|
|
361
|
+
}
|
package/column-reorder.d.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import { Plugin } from '../plugin/plugin.js';
|
|
2
2
|
import { PluginOptions } from '../plugin/plugin.js';
|
|
3
3
|
import { Table } from './table.js';
|
|
4
|
+
export type PointerSample = {
|
|
5
|
+
pointerId: number;
|
|
6
|
+
pointerType: string;
|
|
7
|
+
clientX: number;
|
|
8
|
+
clientY: number;
|
|
9
|
+
};
|
|
10
|
+
export interface ExternalColumnOperation {
|
|
11
|
+
cancel(): void;
|
|
12
|
+
finish(sample: PointerSample): boolean;
|
|
13
|
+
hold(sample: PointerSample): boolean;
|
|
14
|
+
move(sample: PointerSample): void;
|
|
15
|
+
}
|
|
4
16
|
type Direction = -1 | 0 | 1;
|
|
5
17
|
interface Drag extends Press {
|
|
18
|
+
external: boolean;
|
|
6
19
|
cells: HTMLTableCellElement[];
|
|
7
20
|
col?: HTMLTableColElement;
|
|
8
21
|
direction: Direction;
|
|
@@ -11,6 +24,7 @@ interface Drag extends Press {
|
|
|
11
24
|
maximum: number;
|
|
12
25
|
minimum: number;
|
|
13
26
|
remove: boolean;
|
|
27
|
+
rows: HTMLTableRowElement[];
|
|
14
28
|
scroll?: HTMLElement;
|
|
15
29
|
time?: number;
|
|
16
30
|
x: number;
|
|
@@ -28,18 +42,31 @@ export declare class TableColumnReorder extends Plugin<Table, TableColumnReorder
|
|
|
28
42
|
protected press?: Press;
|
|
29
43
|
protected suppressClick: boolean;
|
|
30
44
|
protected animate(time: number): void;
|
|
45
|
+
/** The source owns pointer capture; this operation owns target geometry and rollback. */
|
|
46
|
+
beginExternal(header: HTMLTableCellElement, sample: PointerSample): ExternalColumnOperation;
|
|
31
47
|
defaultOptions(): TableColumnReorderOptions;
|
|
48
|
+
/** Move a column inside its existing reorderable boundaries, without committing a header gesture. */
|
|
49
|
+
moveColumn(header: HTMLTableCellElement, targetIndex: number): void;
|
|
50
|
+
/** Retain the active gesture when asynchronous rendering replaces one of its cells. */
|
|
51
|
+
replaceCell(previous: HTMLTableCellElement, replacement: HTMLTableCellElement): void;
|
|
32
52
|
protected finish(commit: boolean): void;
|
|
33
53
|
init(): void;
|
|
34
54
|
protected move(drag: Drag, targetIndex: number): void;
|
|
55
|
+
protected moveCells(index: number, targetIndex: number, rows?: HTMLTableRowElement[]): void;
|
|
35
56
|
protected onPointer(event: PointerEvent): void;
|
|
36
57
|
protected requestFrame(): void;
|
|
37
58
|
protected rows(): HTMLTableRowElement[];
|
|
38
59
|
protected scroll(drag: Drag, time: number): Direction;
|
|
39
|
-
|
|
40
|
-
protected start(press: Press, x: number, y: number): void;
|
|
60
|
+
scrollContainer(): HTMLElement | undefined;
|
|
61
|
+
protected start(press: Press, x: number, y: number, external?: boolean): void;
|
|
41
62
|
protected target(drag: Drag, direction: Direction): number;
|
|
42
63
|
protected triggerRow(): HTMLTableRowElement;
|
|
64
|
+
protected bounds(drag: Drag): {
|
|
65
|
+
left: number;
|
|
66
|
+
right: number;
|
|
67
|
+
top: number;
|
|
68
|
+
bottom: number;
|
|
69
|
+
};
|
|
43
70
|
protected updateState(drag: Drag): void;
|
|
44
71
|
protected updatePointer(x: number, y: number): void;
|
|
45
72
|
}
|
package/column-reorder.js
CHANGED
|
@@ -24,9 +24,84 @@ export class TableColumnReorder extends Plugin {
|
|
|
24
24
|
if (target !== drag.index)
|
|
25
25
|
this.move(drag, target);
|
|
26
26
|
}
|
|
27
|
+
/** The source owns pointer capture; this operation owns target geometry and rollback. */
|
|
28
|
+
beginExternal(header, sample) {
|
|
29
|
+
if (activeDrag || this.press)
|
|
30
|
+
throw new Error('A column interaction is active');
|
|
31
|
+
this.start({ header, id: sample.pointerId, origin: header.getBoundingClientRect().left, y: sample.clientY }, sample.clientX, sample.clientY, true);
|
|
32
|
+
const drag = this.drag;
|
|
33
|
+
if (!drag)
|
|
34
|
+
throw new Error('Column cannot be reordered');
|
|
35
|
+
let held = false;
|
|
36
|
+
const live = () => this.drag === drag;
|
|
37
|
+
const move = (point) => {
|
|
38
|
+
if (!live() || held || point.pointerId !== drag.id)
|
|
39
|
+
return;
|
|
40
|
+
this.updatePointer(point.clientX, point.clientY);
|
|
41
|
+
};
|
|
42
|
+
const hold = (point) => {
|
|
43
|
+
if (!live() || point.pointerId !== drag.id)
|
|
44
|
+
return false;
|
|
45
|
+
move(point);
|
|
46
|
+
if (this.frame !== undefined)
|
|
47
|
+
cancelAnimationFrame(this.frame);
|
|
48
|
+
this.frame = undefined;
|
|
49
|
+
this.animate(performance.now());
|
|
50
|
+
if (this.frame !== undefined)
|
|
51
|
+
cancelAnimationFrame(this.frame);
|
|
52
|
+
this.frame = undefined;
|
|
53
|
+
held = true;
|
|
54
|
+
return !drag.remove && this.of.element.isConnected;
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
cancel: () => { if (live())
|
|
58
|
+
this.finish(false); },
|
|
59
|
+
finish: point => {
|
|
60
|
+
const accepted = hold(point);
|
|
61
|
+
if (live())
|
|
62
|
+
this.finish(accepted);
|
|
63
|
+
return accepted;
|
|
64
|
+
},
|
|
65
|
+
hold,
|
|
66
|
+
move
|
|
67
|
+
};
|
|
68
|
+
}
|
|
27
69
|
defaultOptions() {
|
|
28
70
|
return new TableColumnReorderOptions();
|
|
29
71
|
}
|
|
72
|
+
/** Move a column inside its existing reorderable boundaries, without committing a header gesture. */
|
|
73
|
+
moveColumn(header, targetIndex) {
|
|
74
|
+
if (!Number.isFinite(targetIndex))
|
|
75
|
+
throw new Error('Column index must be finite');
|
|
76
|
+
if (this.drag || this.press)
|
|
77
|
+
throw new Error('A header interaction is active');
|
|
78
|
+
const headers = Array.from(this.triggerRow().cells);
|
|
79
|
+
const index = headers.indexOf(header);
|
|
80
|
+
if ((index < 0) || !(this.options.isReorderable?.(header) ?? true))
|
|
81
|
+
return;
|
|
82
|
+
let maximum = index;
|
|
83
|
+
let minimum = index;
|
|
84
|
+
while ((maximum < headers.length - 1) && (this.options.isReorderable?.(headers[maximum + 1]) ?? true))
|
|
85
|
+
maximum++;
|
|
86
|
+
while ((minimum > 0) && (this.options.isReorderable?.(headers[minimum - 1]) ?? true))
|
|
87
|
+
minimum--;
|
|
88
|
+
this.moveCells(index, Math.max(minimum, Math.min(maximum, Math.trunc(targetIndex))));
|
|
89
|
+
}
|
|
90
|
+
/** Retain the active gesture when asynchronous rendering replaces one of its cells. */
|
|
91
|
+
replaceCell(previous, replacement) {
|
|
92
|
+
if (this.press?.header === previous)
|
|
93
|
+
this.press.header = replacement;
|
|
94
|
+
if (this.drag) {
|
|
95
|
+
if (this.drag.header === previous)
|
|
96
|
+
this.drag.header = replacement;
|
|
97
|
+
this.drag.cells = this.drag.cells.map(cell => cell === previous ? replacement : cell);
|
|
98
|
+
for (const name of ['column-reorder', 'moving', 'removing']) {
|
|
99
|
+
if (previous.classList.contains(name))
|
|
100
|
+
replacement.classList.add(name);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
previous.replaceWith(replacement);
|
|
104
|
+
}
|
|
30
105
|
finish(commit) {
|
|
31
106
|
this.press = undefined;
|
|
32
107
|
if (this.frame !== undefined) {
|
|
@@ -39,7 +114,7 @@ export class TableColumnReorder extends Plugin {
|
|
|
39
114
|
const changed = drag.index !== drag.initial;
|
|
40
115
|
if (!commit && changed)
|
|
41
116
|
this.move(drag, drag.initial);
|
|
42
|
-
const removed = commit && drag.remove;
|
|
117
|
+
const removed = commit && drag.remove && !drag.external;
|
|
43
118
|
if (removed) {
|
|
44
119
|
for (const row of this.rows()) {
|
|
45
120
|
row.cells[drag.index]?.remove();
|
|
@@ -52,30 +127,43 @@ export class TableColumnReorder extends Plugin {
|
|
|
52
127
|
document.documentElement.classList.remove('column-reorder');
|
|
53
128
|
activeDrag = undefined;
|
|
54
129
|
this.drag = undefined;
|
|
55
|
-
if (this.of.element.hasPointerCapture(drag.id))
|
|
130
|
+
if (!drag.external && this.of.element.hasPointerCapture(drag.id))
|
|
56
131
|
this.of.element.releasePointerCapture(drag.id);
|
|
57
132
|
if (removed) {
|
|
58
133
|
this.options.onRemove?.(drag.header);
|
|
59
134
|
}
|
|
60
|
-
else if (commit && changed) {
|
|
135
|
+
else if (commit && changed && !drag.external) {
|
|
61
136
|
this.options.onReorder?.(drag.header);
|
|
62
137
|
}
|
|
63
138
|
}
|
|
64
139
|
init() {
|
|
65
140
|
const table = this.of;
|
|
141
|
+
const section = table.element.tHead?.rows.length ? 'thead' : 'tbody:first-of-type';
|
|
142
|
+
// Reserve both axes before pointerdown, including vertical drags used to remove a column.
|
|
143
|
+
table.styleSheet.push(`
|
|
144
|
+
${table.selector} > ${section} > tr:first-child > :is(th, td) {
|
|
145
|
+
touch-action: none;
|
|
146
|
+
-webkit-user-select: none;
|
|
147
|
+
user-select: none;
|
|
148
|
+
}
|
|
149
|
+
`);
|
|
66
150
|
table.addEventListener(table.element, 'click', event => {
|
|
67
|
-
if (!this.suppressClick)
|
|
151
|
+
if (!this.suppressClick || (event.detail === 0))
|
|
68
152
|
return;
|
|
69
153
|
this.suppressClick = false;
|
|
70
154
|
event.preventDefault();
|
|
71
155
|
event.stopImmediatePropagation();
|
|
72
156
|
}, { capture: true });
|
|
73
157
|
table.addEventListener(document, 'keydown', event => {
|
|
74
|
-
if ((event.key !== 'Escape') || !this.drag)
|
|
158
|
+
if ((event.key !== 'Escape') || !this.drag || this.drag.external)
|
|
75
159
|
return;
|
|
76
160
|
event.preventDefault();
|
|
77
161
|
this.finish(false);
|
|
78
162
|
}, { capture: true });
|
|
163
|
+
table.addEventListener(table.element, 'lostpointercapture', event => {
|
|
164
|
+
if (event.target === table.element)
|
|
165
|
+
this.onPointer(event);
|
|
166
|
+
});
|
|
79
167
|
table.addEventListener(document, 'pointercancel', event => this.onPointer(event), { capture: true });
|
|
80
168
|
table.addEventListener(table.element, 'pointerdown', event => this.onPointer(event));
|
|
81
169
|
table.addEventListener(document, 'pointermove', event => this.onPointer(event), { capture: true });
|
|
@@ -86,33 +174,42 @@ export class TableColumnReorder extends Plugin {
|
|
|
86
174
|
});
|
|
87
175
|
}
|
|
88
176
|
move(drag, targetIndex) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
177
|
+
this.moveCells(drag.index, targetIndex, drag.external ? drag.rows : undefined);
|
|
178
|
+
drag.index = targetIndex;
|
|
179
|
+
this.updateState(drag);
|
|
180
|
+
}
|
|
181
|
+
moveCells(index, targetIndex, rows = this.rows()) {
|
|
182
|
+
if (index === targetIndex)
|
|
183
|
+
return;
|
|
184
|
+
const after = index < targetIndex;
|
|
185
|
+
for (const row of rows) {
|
|
186
|
+
const cell = row.cells[index];
|
|
92
187
|
const target = row.cells[targetIndex];
|
|
93
188
|
after ? target.after(cell) : target.before(cell);
|
|
94
189
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
190
|
+
const columns = this.of.element.querySelectorAll(':scope > colgroup > col');
|
|
191
|
+
if (columns[index]) {
|
|
192
|
+
const target = columns[targetIndex];
|
|
193
|
+
after ? target.after(columns[index]) : target.before(columns[index]);
|
|
98
194
|
}
|
|
99
|
-
drag.index = targetIndex;
|
|
100
|
-
this.updateState(drag);
|
|
101
195
|
}
|
|
102
196
|
onPointer(event) {
|
|
103
197
|
if (event.type === 'pointerdown') {
|
|
104
|
-
if (activeDrag || this.press || (event.button !== 0))
|
|
198
|
+
if (activeDrag || this.press || !event.isPrimary || (event.button !== 0))
|
|
105
199
|
return;
|
|
200
|
+
this.suppressClick = false;
|
|
106
201
|
const header = event.target.closest('th, td');
|
|
107
202
|
if (!header || (header.parentElement !== this.triggerRow()))
|
|
108
203
|
return;
|
|
109
204
|
this.press = { header, id: event.pointerId, origin: event.clientX, y: event.clientY };
|
|
110
205
|
return;
|
|
111
206
|
}
|
|
207
|
+
if (this.drag?.external)
|
|
208
|
+
return;
|
|
112
209
|
const state = this.drag ?? this.press;
|
|
113
210
|
if (!state || (event.pointerId !== state.id))
|
|
114
211
|
return;
|
|
115
|
-
if (event.type === 'pointercancel') {
|
|
212
|
+
if ((event.type === 'pointercancel') || (event.type === 'lostpointercapture')) {
|
|
116
213
|
this.finish(false);
|
|
117
214
|
return;
|
|
118
215
|
}
|
|
@@ -128,8 +225,6 @@ export class TableColumnReorder extends Plugin {
|
|
|
128
225
|
this.frame = undefined;
|
|
129
226
|
this.animate(performance.now());
|
|
130
227
|
}
|
|
131
|
-
this.suppressClick = true;
|
|
132
|
-
setTimeout(() => this.suppressClick = false);
|
|
133
228
|
this.finish(true);
|
|
134
229
|
return;
|
|
135
230
|
}
|
|
@@ -184,14 +279,16 @@ export class TableColumnReorder extends Plugin {
|
|
|
184
279
|
}
|
|
185
280
|
return undefined;
|
|
186
281
|
}
|
|
187
|
-
start(press, x, y) {
|
|
282
|
+
start(press, x, y, external = false) {
|
|
188
283
|
if (activeDrag)
|
|
189
284
|
return;
|
|
190
285
|
const headers = Array.from(this.triggerRow().cells);
|
|
191
286
|
const initial = headers.indexOf(press.header);
|
|
192
287
|
const rows = this.rows();
|
|
193
288
|
const columns = this.of.element.querySelectorAll(':scope > colgroup > col');
|
|
194
|
-
const
|
|
289
|
+
const source = press.header.getBoundingClientRect();
|
|
290
|
+
const reorderable = headers.map(header => (external || !header.classList.contains('column-add-provisional'))
|
|
291
|
+
&& (this.options.isReorderable?.(header) ?? true));
|
|
195
292
|
if (!reorderable[initial])
|
|
196
293
|
return;
|
|
197
294
|
let maximum = initial;
|
|
@@ -200,23 +297,29 @@ export class TableColumnReorder extends Plugin {
|
|
|
200
297
|
maximum++;
|
|
201
298
|
while ((minimum > 0) && reorderable[minimum - 1])
|
|
202
299
|
minimum--;
|
|
203
|
-
|
|
300
|
+
if (!external)
|
|
301
|
+
this.of.element.setPointerCapture(press.id);
|
|
204
302
|
const drag = {
|
|
205
303
|
...press,
|
|
304
|
+
external,
|
|
206
305
|
cells: rows.map(row => row.cells[initial]),
|
|
207
306
|
col: columns[initial],
|
|
208
|
-
direction:
|
|
307
|
+
direction: external ? (x < source.left ? -1 : x > source.right ? 1 : 0)
|
|
308
|
+
: Math.sign(x - press.origin),
|
|
209
309
|
index: initial,
|
|
210
310
|
initial,
|
|
211
311
|
maximum,
|
|
212
312
|
minimum,
|
|
213
313
|
remove: false,
|
|
314
|
+
rows,
|
|
214
315
|
scroll: this.scrollContainer(),
|
|
215
316
|
x,
|
|
216
317
|
y
|
|
217
318
|
};
|
|
218
319
|
this.drag = drag;
|
|
219
320
|
activeDrag = this;
|
|
321
|
+
// Keep suppression until the compatibility click arrives, even if touch delays it.
|
|
322
|
+
this.suppressClick = !external;
|
|
220
323
|
document.documentElement.classList.add('column-reorder');
|
|
221
324
|
for (const cell of drag.cells)
|
|
222
325
|
cell.classList.add('column-reorder');
|
|
@@ -238,26 +341,37 @@ export class TableColumnReorder extends Plugin {
|
|
|
238
341
|
triggerRow() {
|
|
239
342
|
return this.of.element.tHead?.rows[0] ?? this.of.element.tBodies[0]?.rows[0];
|
|
240
343
|
}
|
|
344
|
+
bounds(drag) {
|
|
345
|
+
const rect = this.of.element.getBoundingClientRect();
|
|
346
|
+
const headers = this.triggerRow().cells;
|
|
347
|
+
const first = headers[drag.minimum].getBoundingClientRect();
|
|
348
|
+
const last = headers[drag.maximum].getBoundingClientRect();
|
|
349
|
+
const viewport = drag.external ? drag.scroll?.getBoundingClientRect() : undefined;
|
|
350
|
+
return {
|
|
351
|
+
left: Math.max(rect.left, first.left, headers[drag.minimum - 1]?.getBoundingClientRect().right ?? rect.left, viewport?.left ?? rect.left),
|
|
352
|
+
right: Math.min(rect.right, last.right, headers[drag.maximum + 1]?.getBoundingClientRect().left ?? rect.right, viewport?.right ?? rect.right),
|
|
353
|
+
top: Math.max(rect.top, viewport?.top ?? rect.top),
|
|
354
|
+
bottom: Math.min(rect.bottom, viewport?.bottom ?? rect.bottom)
|
|
355
|
+
};
|
|
356
|
+
}
|
|
241
357
|
updateState(drag) {
|
|
242
|
-
if (this.options.onRemove) {
|
|
243
|
-
const rect = this.
|
|
244
|
-
|
|
245
|
-
const first = headers[drag.minimum].getBoundingClientRect();
|
|
246
|
-
const last = headers[drag.maximum].getBoundingClientRect();
|
|
247
|
-
const left = Math.max(rect.left, first.left, headers[drag.minimum - 1]?.getBoundingClientRect().right ?? rect.left);
|
|
248
|
-
const right = Math.min(rect.right, last.right, headers[drag.maximum + 1]?.getBoundingClientRect().left ?? rect.right);
|
|
249
|
-
drag.remove = (drag.x < left) || (drag.x > right) || (drag.y < rect.top) || (drag.y > rect.bottom);
|
|
358
|
+
if (this.options.onRemove || drag.external) {
|
|
359
|
+
const rect = this.bounds(drag);
|
|
360
|
+
drag.remove = (drag.x < rect.left) || (drag.x > rect.right) || (drag.y < rect.top) || (drag.y > rect.bottom);
|
|
250
361
|
}
|
|
251
362
|
for (const cell of drag.cells) {
|
|
252
|
-
cell.classList.toggle('removing', drag.remove);
|
|
253
|
-
cell.classList.toggle('moving', !drag.remove && (drag.index !== drag.initial)
|
|
363
|
+
cell.classList.toggle('removing', drag.remove && !drag.external);
|
|
364
|
+
cell.classList.toggle('moving', !drag.remove && (drag.index !== drag.initial)
|
|
365
|
+
&& !drag.header.classList.contains('column-add-provisional'));
|
|
254
366
|
}
|
|
255
367
|
}
|
|
256
368
|
updatePointer(x, y) {
|
|
257
369
|
const drag = this.drag;
|
|
258
370
|
if ((x === drag.x) && (y === drag.y))
|
|
259
371
|
return;
|
|
260
|
-
|
|
372
|
+
const source = drag.external ? drag.header.getBoundingClientRect() : undefined;
|
|
373
|
+
drag.direction = source ? (x === drag.x ? 0 : x < source.left ? -1 : x > source.right ? 1 : 0)
|
|
374
|
+
: Math.sign(x - drag.x);
|
|
261
375
|
drag.x = x;
|
|
262
376
|
drag.y = y;
|
|
263
377
|
this.updateState(drag);
|
package/freeze.d.ts
CHANGED
|
@@ -12,7 +12,10 @@ export declare class TableFreeze extends Plugin<Table> {
|
|
|
12
12
|
leftColumnCount: number;
|
|
13
13
|
rightColumnCount: number;
|
|
14
14
|
zIndex: string;
|
|
15
|
+
protected rules: string[];
|
|
15
16
|
init(): void;
|
|
17
|
+
/** Recalculate sticky offsets after structural changes without resetting other plugins. */
|
|
18
|
+
refresh(): void;
|
|
16
19
|
closestScrollable(element: Element): HTMLElement | (Window & typeof globalThis) | undefined;
|
|
17
20
|
protected countLeftColumns(): number;
|
|
18
21
|
protected countRightColumns(): number;
|
|
@@ -20,7 +23,7 @@ export declare class TableFreeze extends Plugin<Table> {
|
|
|
20
23
|
protected freezeHeadRows(): void;
|
|
21
24
|
protected freezeLeftColumns(): void;
|
|
22
25
|
protected freezeRightColumns(): void;
|
|
23
|
-
protected getColumns(): NodeListOf<
|
|
26
|
+
protected getColumns(): NodeListOf<HTMLTableColElement>;
|
|
24
27
|
position(position: number, _counter: number, _colCell: HTMLTableFreezeElement, _side: 'bottom' | 'left' | 'right' | 'top'): string;
|
|
25
28
|
visibleInnerRect(): DOMRect;
|
|
26
29
|
}
|
package/freeze.js
CHANGED
|
@@ -5,7 +5,9 @@ export class TableFreeze extends Plugin {
|
|
|
5
5
|
leftColumnCount;
|
|
6
6
|
rightColumnCount;
|
|
7
7
|
zIndex = '1';
|
|
8
|
+
rules = [];
|
|
8
9
|
init() {
|
|
10
|
+
const offset = this.of.styleSheet.length;
|
|
9
11
|
this.columns = this.getColumns();
|
|
10
12
|
this.leftColumnCount = this.countLeftColumns();
|
|
11
13
|
this.rightColumnCount = this.countRightColumns();
|
|
@@ -13,6 +15,19 @@ export class TableFreeze extends Plugin {
|
|
|
13
15
|
this.freezeHeadRows();
|
|
14
16
|
this.freezeLeftColumns();
|
|
15
17
|
this.freezeRightColumns();
|
|
18
|
+
this.rules = this.of.styleSheet.slice(offset);
|
|
19
|
+
}
|
|
20
|
+
/** Recalculate sticky offsets after structural changes without resetting other plugins. */
|
|
21
|
+
refresh() {
|
|
22
|
+
const styles = this.of.styleSheet;
|
|
23
|
+
for (const rule of this.rules) {
|
|
24
|
+
const index = styles.indexOf(rule);
|
|
25
|
+
if (index >= 0)
|
|
26
|
+
styles.splice(index, 1);
|
|
27
|
+
}
|
|
28
|
+
// Measure natural positions, including header-backed columns that were previously sticky.
|
|
29
|
+
this.of.refreshStyleSheets();
|
|
30
|
+
this.init();
|
|
16
31
|
}
|
|
17
32
|
closestScrollable(element) {
|
|
18
33
|
let parent = element.closest('table')?.parentElement;
|
|
@@ -152,8 +167,6 @@ export class TableFreeze extends Plugin {
|
|
|
152
167
|
`);
|
|
153
168
|
}
|
|
154
169
|
getColumns() {
|
|
155
|
-
if (this.columns)
|
|
156
|
-
return this.columns;
|
|
157
170
|
const table = this.of;
|
|
158
171
|
let columns = table.element.querySelectorAll(':scope > colgroup > col');
|
|
159
172
|
if (!columns.length) {
|
|
@@ -170,18 +183,18 @@ export class TableFreeze extends Plugin {
|
|
|
170
183
|
visibleInnerRect() {
|
|
171
184
|
const tableElement = this.of.element;
|
|
172
185
|
const rect = tableElement.getBoundingClientRect();
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
186
|
+
// Cols supply configuration and layout widths; only rendered cells have sticky viewport positions.
|
|
187
|
+
const cells = tableElement.tHead?.rows[0]?.cells ?? tableElement.tBodies[0]?.rows[0]?.cells;
|
|
188
|
+
const left = this.leftColumnCount && cells?.[this.leftColumnCount - 1]
|
|
189
|
+
? cells[this.leftColumnCount - 1].getBoundingClientRect().right : rect.left;
|
|
190
|
+
const right = this.rightColumnCount && cells?.[cells.length - this.rightColumnCount]
|
|
191
|
+
? cells[cells.length - this.rightColumnCount].getBoundingClientRect().left : rect.right;
|
|
192
|
+
const top = tableElement.tHead?.lastElementChild?.firstElementChild?.getBoundingClientRect().bottom ?? rect.top;
|
|
193
|
+
const bottom = tableElement.tFoot?.firstElementChild?.firstElementChild?.getBoundingClientRect().top ?? rect.bottom;
|
|
194
|
+
rect.x = left;
|
|
195
|
+
rect.y = top;
|
|
196
|
+
rect.width = Math.max(0, right - left);
|
|
197
|
+
rect.height = Math.max(0, bottom - top);
|
|
185
198
|
return rect;
|
|
186
199
|
}
|
|
187
200
|
}
|
package/package.json
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
17
|
"LICENSE",
|
|
18
|
+
"column-add.css",
|
|
18
19
|
"README.md",
|
|
19
20
|
"**/*.d.ts",
|
|
20
21
|
"**/*.js",
|
|
@@ -50,7 +51,7 @@
|
|
|
50
51
|
"url": "git+https://github.com/itrocks-ts/table.git"
|
|
51
52
|
},
|
|
52
53
|
"scripts": {
|
|
53
|
-
"build": "tsc && sass --no-source-map demo:demo && node ../prepare-module/prepare-module.js"
|
|
54
|
+
"build": "tsc && sass --no-source-map demo:demo src/column-add.scss:column-add.css && node ../prepare-module/prepare-module.js"
|
|
54
55
|
},
|
|
55
|
-
"version": "0.
|
|
56
|
+
"version": "0.4.1"
|
|
56
57
|
}
|
package/table.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare class Table extends HasPlugins<Table> {
|
|
|
13
13
|
addEventListener<T extends keyof GlobalEventHandlersEventMap>(element: Document | Element | Window, type: T, listener: (this: Element, ev: GlobalEventHandlersEventMap[T]) => any, options?: AddEventListenerOptions | boolean): void;
|
|
14
14
|
cellColumnNumber(cell: HTMLTableCellElement): number;
|
|
15
15
|
reset(): Table;
|
|
16
|
+
refreshStyleSheets(): void;
|
|
16
17
|
}
|
|
17
18
|
export declare function tableByElement(element: HTMLTableElement, options?: Partial<Options<Table>>): Table;
|
|
18
19
|
export declare function tableByElements(elements: Array<HTMLTableElement> | NodeListOf<HTMLTableElement>, options?: Partial<Options<Table>>): Table[];
|
package/table.js
CHANGED
|
@@ -63,6 +63,9 @@ export class Table extends HasPlugins {
|
|
|
63
63
|
onReset.call(this);
|
|
64
64
|
return tableByElement(this.element, this.options);
|
|
65
65
|
}
|
|
66
|
+
refreshStyleSheets() {
|
|
67
|
+
applyStyleSheets();
|
|
68
|
+
}
|
|
66
69
|
}
|
|
67
70
|
export function tableByElement(element, options = {}) {
|
|
68
71
|
return new Table(element, options);
|