@itrocks/table 0.2.6 → 0.2.8

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