@fr0st/ui-sortable 3.0.0 → 3.0.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/src/helpers.js CHANGED
@@ -19,7 +19,7 @@ export function resetCursor(nodes) {
19
19
  release();
20
20
  cursorLocks.delete(node);
21
21
  }
22
- };
22
+ }
23
23
 
24
24
  /**
25
25
  * Sets the cursor style for a collection of elements.
@@ -34,7 +34,7 @@ export function setCursor(nodes, cursor) {
34
34
  cursorLocks.set(node, $.setStyleLock(node, 'cursor', cursor));
35
35
  }
36
36
  }
37
- };
37
+ }
38
38
 
39
39
  /**
40
40
  * Triggers a Sortable event with its data and detail payloads.
@@ -50,4 +50,4 @@ export function triggerSortableEvent(node, name, data) {
50
50
  ...data,
51
51
  },
52
52
  });
53
- };
53
+ }
package/src/sortable.js CHANGED
@@ -56,14 +56,15 @@ export default class Sortable extends BaseComponent {
56
56
  };
57
57
 
58
58
  #currentIndex;
59
- #currentSortable;
59
+ #currentSortable = null;
60
60
  #dragging;
61
61
  #enabled;
62
- #pos;
63
- #scrollFrame;
62
+ #initialCursorNodes = [];
63
+ #pos = null;
64
+ #scrollFrame = null;
64
65
  #selector;
65
66
  #startIndex;
66
- #target;
67
+ #target = null;
67
68
 
68
69
  /**
69
70
  * Creates a Sortable.
@@ -73,15 +74,22 @@ export default class Sortable extends BaseComponent {
73
74
  constructor(node, options) {
74
75
  super(node, options);
75
76
 
76
- this.#selector = this.options.handle ?
77
- `:is(${this.options.items}) :is(${this.options.handle})` :
78
- this.options.items;
79
-
80
- this.#dragging = false;
81
- this.#enabled = !this.options.disabled;
82
-
83
- setCursor($.find(this.#selector, this.node), this.options.cursor);
84
- this.#events();
77
+ try {
78
+ this.#selector = this.options.handle ?
79
+ `:is(${this.options.items}) :is(${this.options.handle})` :
80
+ this.options.items;
81
+
82
+ this.#dragging = false;
83
+ this.#enabled = !this.options.disabled;
84
+
85
+ this.#initialCursorNodes = $.find(this.#selector, this.node);
86
+ setCursor(this.#initialCursorNodes, this.options.cursor);
87
+ this.#events();
88
+ this.#initialCursorNodes = null;
89
+ } catch (error) {
90
+ this.dispose();
91
+ throw error;
92
+ }
85
93
  }
86
94
 
87
95
  /**
@@ -97,9 +105,7 @@ export default class Sortable extends BaseComponent {
97
105
  return;
98
106
  }
99
107
 
100
- $.removeEventDelegate(this.node, 'mousedown.ui.sortable touchstart.ui.sortable');
101
- $.removeEvent(this.node, 'receive.ui.sortable');
102
- resetCursor($.find(this.#selector, this.node));
108
+ resetCursor(this.#initialCursorNodes ?? $.find(this.#selector, this.node));
103
109
 
104
110
  if (this.#target && this.options.helperClass) {
105
111
  $.removeClass(this.#target, this.options.helperClass);
@@ -107,6 +113,8 @@ export default class Sortable extends BaseComponent {
107
113
 
108
114
  this.#resetDrag();
109
115
 
116
+ this.#initialCursorNodes = null;
117
+
110
118
  super.dispose();
111
119
  }
112
120
 
@@ -183,16 +191,16 @@ export default class Sortable extends BaseComponent {
183
191
  * Attaches events for the Sortable.
184
192
  */
185
193
  #events() {
186
- const downEvent = (e) => {
194
+ const downHandler = (event) => {
187
195
  const cancelled = this.options.cancel &&
188
196
  (
189
- $.is(e.target, this.options.cancel) ||
190
- $.is(e.currentTarget, this.options.cancel) ||
191
- $.closest(e.target, this.options.cancel, this.node).length
197
+ $.is(event.target, this.options.cancel) ||
198
+ $.is(event.currentTarget, this.options.cancel) ||
199
+ $.closest(event.target, this.options.cancel, this.node).length
192
200
  );
193
201
 
194
202
  if (
195
- e.button ||
203
+ event.button ||
196
204
  !this.#enabled ||
197
205
  cancelled ||
198
206
  $.getDataset(this.node, 'uiDragging')
@@ -201,21 +209,21 @@ export default class Sortable extends BaseComponent {
201
209
  }
202
210
 
203
211
  this.#target = this.options.handle ?
204
- this.items().find((node) => $.hasDescendent(node, e.currentTarget)) :
205
- e.currentTarget;
212
+ this.items().find((node) => $.hasDescendent(node, event.currentTarget)) :
213
+ event.currentTarget;
206
214
 
207
215
  if (!this.#target) {
208
216
  return false;
209
217
  }
210
218
  };
211
219
 
212
- const moveEvent = (e) => {
220
+ const moveHandler = (event) => {
213
221
  if (!this.node || !this.#target) {
214
222
  return;
215
223
  }
216
224
 
217
225
  const previousPos = this.#pos;
218
- this.#pos = getPosition(e);
226
+ this.#pos = getPosition(event);
219
227
  const deltaX = previousPos ?
220
228
  this.#pos.x - previousPos.x :
221
229
  0;
@@ -255,7 +263,7 @@ export default class Sortable extends BaseComponent {
255
263
  this.#scheduleScroll();
256
264
  };
257
265
 
258
- const upEvent = (e) => {
266
+ const upHandler = (event) => {
259
267
  if (!this.node) {
260
268
  return;
261
269
  }
@@ -265,8 +273,8 @@ export default class Sortable extends BaseComponent {
265
273
  return;
266
274
  }
267
275
 
268
- if (e.type === 'mouseup') {
269
- e.preventDefault();
276
+ if (event.type === 'mouseup') {
277
+ event.preventDefault();
270
278
  }
271
279
 
272
280
  const source = this.node;
@@ -305,16 +313,16 @@ export default class Sortable extends BaseComponent {
305
313
  }
306
314
  };
307
315
 
308
- const dragEvent = $.mouseDragFactory(downEvent, moveEvent, upEvent);
316
+ const dragHandler = $.mouseDragFactory(downHandler, moveHandler, upHandler);
309
317
 
310
- $.addEventDelegate(
318
+ this.listen(
311
319
  this.node,
312
320
  'mousedown.ui.sortable touchstart.ui.sortable',
313
- this.#selector,
314
- dragEvent,
321
+ dragHandler,
322
+ { delegate: this.#selector },
315
323
  );
316
324
 
317
- $.addEvent(this.node, 'receive.ui.sortable', (_) => {
325
+ this.listen(this.node, 'receive.ui.sortable', () => {
318
326
  setCursor($.find(this.#selector, this.node), this.options.cursor);
319
327
  });
320
328
  }
@@ -323,7 +331,7 @@ export default class Sortable extends BaseComponent {
323
331
  * Resets the active drag state.
324
332
  */
325
333
  #resetDrag() {
326
- if (this.#scrollFrame !== null && this.#scrollFrame !== undefined) {
334
+ if (this.#scrollFrame !== null) {
327
335
  window.cancelAnimationFrame(this.#scrollFrame);
328
336
  }
329
337
 
@@ -348,12 +356,12 @@ export default class Sortable extends BaseComponent {
348
356
  !this.node ||
349
357
  !this.options.scroll ||
350
358
  !this.#dragging ||
351
- this.#scrollFrame !== null && this.#scrollFrame !== undefined
359
+ this.#scrollFrame !== null
352
360
  ) {
353
361
  return;
354
362
  }
355
363
 
356
- this.#scrollFrame = window.requestAnimationFrame((_) => {
364
+ this.#scrollFrame = window.requestAnimationFrame(() => {
357
365
  this.#checkScroll();
358
366
  });
359
367
  }