@fr0st/ui-sortable 2.0.5 → 3.0.0

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.
@@ -1,323 +1,394 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@fr0st/ui'), require('@fr0st/query')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@fr0st/ui', '@fr0st/query'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.UI = global.UI || {}, global.UI, global.fQuery));
5
- })(this, (function (exports, ui, $) { 'use strict';
6
-
7
- /**
8
- * Sortable Class
9
- * @class
10
- */
11
- class Sortable extends ui.BaseComponent {
12
- /**
13
- * New Sortable constructor.
14
- * @param {HTMLElement} node The input node.
15
- * @param {object} [options] The options to create the Sortable with.
16
- */
17
- constructor(node, options) {
18
- super(node, options);
19
-
20
- this._selector = this._options.items;
21
- if (this._options.handle) {
22
- this._selector += ' ' + this._options.handle;
23
- }
24
-
25
- this._enabled = !this._options.disabled;
26
-
27
- this._refreshCursors();
28
- this._events();
29
- }
30
-
31
- /**
32
- * Disable the Sortable.
33
- */
34
- disable() {
35
- this._enabled = false;
36
- }
37
-
38
- /**
39
- * Dispose the Sortable.
40
- */
41
- dispose() {
42
- $.removeEventDelegate(this._node, 'mousedown.ui.sortable touchstart.ui.sortable');
43
- $.removeEvent(this._node, 'receive.ui.sortable');
44
-
45
- this._target = null;
46
- this._currentSortable = null;
47
-
48
- super.dispose();
49
- }
50
-
51
- /**
52
- * Enable the Sortable.
53
- */
54
- enable() {
55
- this._enabled = true;
56
- }
57
-
58
- /**
59
- * Get the Sortable item elements.
60
- * @return {array} The Sortable item elements.
61
- */
62
- items() {
63
- return $.find(this._options.items, this._node);
64
- }
65
- }
66
-
67
- /**
68
- * Attach events for the Sortable.
69
- */
70
- function _events() {
71
- const downEvent = (e) => {
72
- if (
73
- e.button ||
74
- !this._enabled ||
75
- $.is(e.currentTarget, this._options.cancel) ||
76
- $.getDataset(this._node, 'uiDragging')
77
- ) {
78
- return false;
79
- }
80
-
81
- this._target = this._options.handle ?
82
- this.items().find((node) => $.hasDescendent(node, e.currentTarget)) :
83
- e.currentTarget;
84
- };
85
-
86
- const moveEvent = (e) => {
87
- this._pos = ui.getPosition(e);
88
-
89
- if (!$.getDataset(this._node, 'uiDragging')) {
90
- $.setDataset(this._node, { uiDragging: true });
91
-
92
- this._startIndex = $.index(this._target);
93
- this._currentIndex = this._startIndex;
94
-
95
- $.triggerEvent(this._node, 'sort.ui.sortable', {
96
- detail: {
97
- target: this._target,
98
- },
99
- });
100
-
101
- if (this._options.helperClass) {
102
- $.addClass(this._target, this._options.helperClass);
103
- }
104
-
105
- this._currentSortable = this._node;
106
-
107
- if (this._options.scroll) {
108
- this._checkScroll();
109
- }
110
- }
111
-
112
- let nearestSortable = this._node;
113
-
114
- if (this._options.connectWith) {
115
- const connected = $.find(this._options.connectWith)
116
- .filter((node) => {
117
- const sortable = this.constructor.init(node);
118
-
119
- return sortable._options.dropOnEmpty || sortable.items().length;
120
- });
121
-
122
- if (connected.length) {
123
- nearestSortable = $.nearestTo([this._node, ...connected], this._pos.x, this._pos.y, { offset: true });
124
- }
125
- }
126
-
127
- const nearestItems = $.isSame(this._node, nearestSortable) ?
128
- this.items() :
129
- this.constructor.init(nearestSortable).items();
130
-
131
- const nearestItem = $.nearestTo(nearestItems, this._pos.x, this._pos.y, { offset: true });
132
-
133
- if (!nearestItem) {
134
- return;
135
- }
136
-
137
- this._updateTarget(nearestItem, this._pos.x, this._pos.y);
138
-
139
- const newIndex = $.index(this._target);
140
-
141
- if (!$.isSame(nearestSortable, this._currentSortable)) {
142
- $.triggerEvent(this._currentSortable, 'send.ui.sortable', {
143
- detail: {
144
- target: this._target,
145
- },
146
- });
147
-
148
- $.triggerEvent(nearestSortable, 'receive.ui.sortable', {
149
- detail: {
150
- target: this._target,
151
- },
152
- });
153
- } else if (this._currentIndex !== newIndex) {
154
- $.triggerEvent(nearestSortable, 'sorting.ui.sortable', {
155
- detail: {
156
- target: this._target,
157
- },
158
- });
159
- }
160
-
161
- this._currentIndex = newIndex;
162
- this._currentSortable = nearestSortable;
163
- };
164
-
165
- const upEvent = (e) => {
166
- if (!$.getDataset(this._node, 'uiDragging')) {
167
- this._target = null;
168
- return;
169
- }
170
-
171
- if (e.type === 'mouseup') {
172
- e.preventDefault();
173
- }
174
-
175
- $.triggerEvent(this._currentSortable, 'sorted.ui.sortable', {
176
- detail: {
177
- target: this._target,
178
- },
179
- });
180
-
181
- if (this._options.helperClass) {
182
- $.removeClass(this._target, this._options.helperClass);
183
- }
184
-
185
- if (this._currentIndex !== this._startIndex || !$.isSame(this._currentSortable, this._node)) {
186
- $.triggerEvent(this._currentSortable, 'update.ui.sortable', {
187
- detail: {
188
- target: this._target,
189
- },
190
- });
191
- }
192
-
193
- $.removeDataset(this._node, 'uiDragging');
194
-
195
- if (this._checkScrollTimer) {
196
- clearTimeout(this._checkScrollTimer);
197
- this._checkScrollTimer = null;
198
- }
199
-
200
- this._target = null;
201
- this._currentSortable = null;
202
- this._currentIndex = null;
203
- this._startIndex = null;
204
- };
205
-
206
- const dragEvent = $.mouseDragFactory(downEvent, moveEvent, upEvent);
207
-
208
- $.addEventDelegate(this._node, 'mousedown.ui.sortable touchstart.ui.sortable', this._selector, dragEvent);
209
-
210
- $.addEvent(this._node, 'receive.ui.sortable', (_) => {
211
- this._refreshCursors();
212
- });
213
- }
214
-
215
- /**
216
- * Check for window scroll.
217
- */
218
- function _checkScroll() {
219
- $.hide(this._target);
220
-
221
- const originalX = $.getScrollX(window);
222
- const originalY = $.getScrollY(window);
223
-
224
- let offsetX = originalX;
225
- let offsetY = originalY;
226
-
227
- if (this._pos.y < originalY + this._options.scrollSensitivity) {
228
- offsetY = Math.max(0, originalY - this._options.scrollSpeed);
229
- } else if (this._pos.y > originalY + $.height(window) - this._options.scrollSensitivity) {
230
- const docHeight = $.height(document, { boxSize: $.SCROLL_BOX });
231
- offsetY = Math.min(docHeight, originalY + this._options.scrollSpeed);
232
- }
233
-
234
- if (this._pos.x < originalX + this._options.scrollSensitivity) {
235
- offsetX = Math.max(0, originalX - this._options.scrollSpeed);
236
- } else if (this._pos.x > originalX + $.width(window) - this._options.scrollSensitivity) {
237
- const docWidth = $.width(document, { boxSize: $.SCROLL_BOX });
238
- offsetX = Math.min(docWidth, originalX + this._options.scrollSpeed);
239
- }
240
-
241
- if (offsetX !== originalX || offsetY !== originalY) {
242
- $.setStyle(':root', { scrollBehavior: 'initial' });
243
- $.setScroll(document, offsetX, offsetY);
244
- $.setStyle(':root', { scrollBehavior: '' });
245
-
246
- this._pos.x += offsetX - originalX;
247
- this._pos.y += offsetY - originalY;
248
- }
249
-
250
- $.show(this._target);
251
-
252
- this._checkScrollTimer = setTimeout((_) => {
253
- this._checkScroll();
254
- }, 0);
255
- }
256
- /**
257
- * Refresh Sortable item cursors.
258
- */
259
- function _refreshCursors() {
260
- const elements = $.find(this._selector, this._node);
261
-
262
- $.setStyle(elements, { cursor: this._options.cursor });
263
- }
264
- /**
265
- * Update the target element.
266
- * @param {HTMLElement} nearestItem The current nearest item.
267
- * @param {number} x The mouse X position.
268
- * @param {number} y The mouse Y position.
269
- */
270
- function _updateTarget(nearestItem, x, y) {
271
- const nearestCenter = $.center(nearestItem, { offset: true });
272
- const targetCenter = $.center(this._target, { offset: true });
273
-
274
- let method;
275
- if (this._options.axis === 'y' || nearestCenter.x === targetCenter.x) {
276
- method = y < nearestCenter.y ?
277
- 'before' :
278
- 'after';
279
- } else if (this._options.axis === 'x' || nearestCenter.y === targetCenter.y) {
280
- method = x < nearestCenter.x ?
281
- 'before' :
282
- 'after';
283
- } else {
284
- method = x < nearestCenter.x && y < nearestCenter.y ?
285
- 'before' :
286
- 'after';
287
- }
288
-
289
- $[method](nearestItem, this._target);
290
- }
291
-
292
- // Sortable default options
293
- Sortable.defaults = {
294
- appendTo: 'body',
295
- axis: null,
296
- cancel: 'input, textarea, button, select, option',
297
- connectWith: null,
298
- cursor: 'auto',
299
- disabled: false,
300
- dropOnEmpty: true,
301
- handle: null,
302
- helperClass: 'bg-body-tertiary',
303
- items: ':scope > li',
304
- scroll: true,
305
- scrollSensitivity: 20,
306
- scrollSpeed: 5,
307
- };
308
-
309
- // Sortable prototype
310
- const proto = Sortable.prototype;
311
-
312
- proto._checkScroll = _checkScroll;
313
- proto._events = _events;
314
- proto._refreshCursors = _refreshCursors;
315
- proto._updateTarget = _updateTarget;
316
-
317
- // Sortable init
318
- ui.initComponent('sortable', Sortable);
319
-
320
- exports.Sortable = Sortable;
321
-
322
- }));
323
- //# sourceMappingURL=frost-ui-sortable.js.map
1
+ (function(global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@fr0st/ui'), require('@fr0st/query')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@fr0st/ui', '@fr0st/query'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.UI = global.UI || {}), global.UI,global.fQuery));
5
+ })(this, function(exports, _fr0st_ui, _fr0st_query) {
6
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
7
+ //#region \0rolldown/runtime.js
8
+ var __create = Object.create;
9
+ var __defProp = Object.defineProperty;
10
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
11
+ var __getOwnPropNames = Object.getOwnPropertyNames;
12
+ var __getProtoOf = Object.getPrototypeOf;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
17
+ key = keys[i];
18
+ if (!__hasOwnProp.call(to, key) && key !== except) {
19
+ __defProp(to, key, {
20
+ get: ((k) => from[k]).bind(null, key),
21
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
22
+ });
23
+ }
24
+ }
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
29
+ value: mod,
30
+ enumerable: true
31
+ }) : target, mod));
32
+
33
+ //#endregion
34
+ _fr0st_query = __toESM(_fr0st_query, 1);
35
+
36
+ //#region src/helpers.js
37
+ /** @import { SortableEventData } from './sortable.js'; */
38
+ var cursorLocks = /* @__PURE__ */ new WeakMap();
39
+ /**
40
+ * Resets the cursor style for a collection of elements.
41
+ * @param {Iterable<HTMLElement>} nodes The elements to restore.
42
+ */
43
+ function resetCursor(nodes) {
44
+ for (const node of nodes) {
45
+ const release = cursorLocks.get(node);
46
+ if (!release) continue;
47
+ release();
48
+ cursorLocks.delete(node);
49
+ }
50
+ }
51
+ /**
52
+ * Sets the cursor style for a collection of elements.
53
+ * @param {Iterable<HTMLElement>} nodes The elements to update.
54
+ * @param {string} cursor The cursor style.
55
+ */
56
+ function setCursor(nodes, cursor) {
57
+ for (const node of nodes) if (cursorLocks.has(node)) _fr0st_query.default.setStyle(node, { cursor });
58
+ else cursorLocks.set(node, _fr0st_query.default.setStyleLock(node, "cursor", cursor));
59
+ }
60
+ /**
61
+ * Triggers a Sortable event with its data and detail payloads.
62
+ * @param {HTMLElement} node The event target.
63
+ * @param {string} name The event name without its namespace.
64
+ * @param {SortableEventData} data The sorting event data.
65
+ */
66
+ function triggerSortableEvent(node, name, data) {
67
+ _fr0st_query.default.triggerEvent(node, `${name}.ui.sortable`, {
68
+ data,
69
+ detail: {
70
+ target: data.item,
71
+ ...data
72
+ }
73
+ });
74
+ }
75
+
76
+ //#endregion
77
+ //#region src/sortable.js
78
+ var document = _fr0st_query.default.getContext();
79
+ var window = _fr0st_query.default.getWindow();
80
+ /** @typedef {'x'|'y'} SortableAxis */
81
+ /**
82
+ * @typedef {object} SortableOptions
83
+ * @property {SortableAxis|null} [axis=null] The axis used to determine item placement.
84
+ * @property {string} [cancel='input, textarea, button, select, option'] The selector for elements that cannot start a sort.
85
+ * @property {string|null} [connectWith=null] The selector for connected Sortable containers.
86
+ * @property {string} [cursor='auto'] The cursor applied to sortable items or handles.
87
+ * @property {boolean} [disabled=false] Whether sorting is initially disabled.
88
+ * @property {boolean} [dropOnEmpty=true] Whether connected empty containers accept items.
89
+ * @property {string|null} [handle=null] The handle selector within each sortable item.
90
+ * @property {string|null} [helperClass='bg-body-tertiary'] The class applied to an item while it is being sorted.
91
+ * @property {string} [items=':scope > li'] The selector for sortable items.
92
+ * @property {boolean} [scroll=true] Whether to scroll the window while sorting near an edge.
93
+ * @property {number} [scrollSensitivity=20] The distance from a window edge that starts scrolling, in pixels.
94
+ * @property {number} [scrollSpeed=5] The scroll distance applied per animation frame, in pixels.
95
+ * @property {number} [swapThreshold=0.25] The fraction of an item crossed before swapping, from `0` to `1`.
96
+ */
97
+ /**
98
+ * @typedef {object} SortableEventData
99
+ * @property {HTMLElement} item The item being sorted.
100
+ * @property {HTMLElement} from The previous or original Sortable container.
101
+ * @property {HTMLElement} to The next or final Sortable container.
102
+ * @property {number} oldIndex The previous or original item index.
103
+ * @property {number} newIndex The next or final item index.
104
+ */
105
+ /**
106
+ * Controls a sortable collection of elements.
107
+ * @augments {BaseComponent<SortableOptions>}
108
+ */
109
+ var Sortable = class extends _fr0st_ui.BaseComponent {
110
+ /** @type {SortableOptions} */
111
+ static defaults = {
112
+ axis: null,
113
+ cancel: "input, textarea, button, select, option",
114
+ connectWith: null,
115
+ cursor: "auto",
116
+ disabled: false,
117
+ dropOnEmpty: true,
118
+ handle: null,
119
+ helperClass: "bg-body-tertiary",
120
+ items: ":scope > li",
121
+ scroll: true,
122
+ scrollSensitivity: 20,
123
+ scrollSpeed: 5,
124
+ swapThreshold: .25
125
+ };
126
+ #currentIndex;
127
+ #currentSortable;
128
+ #dragging;
129
+ #enabled;
130
+ #pos;
131
+ #scrollFrame;
132
+ #selector;
133
+ #startIndex;
134
+ #target;
135
+ /**
136
+ * Creates a Sortable.
137
+ * @param {HTMLElement} node The sortable container.
138
+ * @param {SortableOptions} [options] The Sortable options.
139
+ */
140
+ constructor(node, options) {
141
+ super(node, options);
142
+ this.#selector = this.options.handle ? `:is(${this.options.items}) :is(${this.options.handle})` : this.options.items;
143
+ this.#dragging = false;
144
+ this.#enabled = !this.options.disabled;
145
+ setCursor(_fr0st_query.default.find(this.#selector, this.node), this.options.cursor);
146
+ this.#events();
147
+ }
148
+ /**
149
+ * Disables the Sortable.
150
+ */
151
+ disable() {
152
+ this.#enabled = false;
153
+ }
154
+ /** @inheritdoc */
155
+ dispose() {
156
+ if (!this.node) return;
157
+ _fr0st_query.default.removeEventDelegate(this.node, "mousedown.ui.sortable touchstart.ui.sortable");
158
+ _fr0st_query.default.removeEvent(this.node, "receive.ui.sortable");
159
+ resetCursor(_fr0st_query.default.find(this.#selector, this.node));
160
+ if (this.#target && this.options.helperClass) _fr0st_query.default.removeClass(this.#target, this.options.helperClass);
161
+ this.#resetDrag();
162
+ super.dispose();
163
+ }
164
+ /**
165
+ * Enables the Sortable.
166
+ */
167
+ enable() {
168
+ this.#enabled = true;
169
+ }
170
+ /**
171
+ * Gets the Sortable item elements.
172
+ * @returns {HTMLElement[]} The Sortable item elements.
173
+ */
174
+ items() {
175
+ return _fr0st_query.default.find(this.options.items, this.node);
176
+ }
177
+ /**
178
+ * Checks whether the window should scroll while sorting.
179
+ */
180
+ #checkScroll() {
181
+ this.#scrollFrame = null;
182
+ if (!this.node || !this.#dragging || !this.#target || !this.#pos) return;
183
+ const originalX = _fr0st_query.default.getScrollX(window);
184
+ const originalY = _fr0st_query.default.getScrollY(window);
185
+ const windowHeight = _fr0st_query.default.height(window);
186
+ const windowWidth = _fr0st_query.default.width(window);
187
+ let offsetX = originalX;
188
+ let offsetY = originalY;
189
+ if (this.#pos.y < originalY + this.options.scrollSensitivity) offsetY = Math.max(0, originalY - this.options.scrollSpeed);
190
+ else if (this.#pos.y > originalY + windowHeight - this.options.scrollSensitivity) {
191
+ const docHeight = _fr0st_query.default.height(document, { boxSize: _fr0st_query.default.SCROLL_BOX });
192
+ const maxY = Math.max(0, docHeight - windowHeight);
193
+ offsetY = Math.min(maxY, originalY + this.options.scrollSpeed);
194
+ }
195
+ if (this.#pos.x < originalX + this.options.scrollSensitivity) offsetX = Math.max(0, originalX - this.options.scrollSpeed);
196
+ else if (this.#pos.x > originalX + windowWidth - this.options.scrollSensitivity) {
197
+ const docWidth = _fr0st_query.default.width(document, { boxSize: _fr0st_query.default.SCROLL_BOX });
198
+ const maxX = Math.max(0, docWidth - windowWidth);
199
+ offsetX = Math.min(maxX, originalX + this.options.scrollSpeed);
200
+ }
201
+ if (offsetX === originalX && offsetY === originalY) return;
202
+ window.scrollTo({
203
+ left: offsetX,
204
+ top: offsetY,
205
+ behavior: "instant"
206
+ });
207
+ const deltaX = offsetX - originalX;
208
+ const deltaY = offsetY - originalY;
209
+ this.#pos.x += deltaX;
210
+ this.#pos.y += deltaY;
211
+ this.#updatePlacement(deltaX, deltaY);
212
+ this.#scheduleScroll();
213
+ }
214
+ /**
215
+ * Attaches events for the Sortable.
216
+ */
217
+ #events() {
218
+ const downEvent = (e) => {
219
+ const cancelled = this.options.cancel && (_fr0st_query.default.is(e.target, this.options.cancel) || _fr0st_query.default.is(e.currentTarget, this.options.cancel) || _fr0st_query.default.closest(e.target, this.options.cancel, this.node).length);
220
+ if (e.button || !this.#enabled || cancelled || _fr0st_query.default.getDataset(this.node, "uiDragging")) return false;
221
+ this.#target = this.options.handle ? this.items().find((node) => _fr0st_query.default.hasDescendent(node, e.currentTarget)) : e.currentTarget;
222
+ if (!this.#target) return false;
223
+ };
224
+ const moveEvent = (e) => {
225
+ if (!this.node || !this.#target) return;
226
+ const previousPos = this.#pos;
227
+ this.#pos = (0, _fr0st_ui.getPosition)(e);
228
+ const deltaX = previousPos ? this.#pos.x - previousPos.x : 0;
229
+ const deltaY = previousPos ? this.#pos.y - previousPos.y : 0;
230
+ if (!this.#dragging) {
231
+ this.#dragging = true;
232
+ this.#currentSortable = this.node;
233
+ this.#startIndex = this.items().indexOf(this.#target);
234
+ this.#currentIndex = this.#startIndex;
235
+ _fr0st_query.default.setDataset(this.node, { uiDragging: true });
236
+ /** @type {SortableEventData} */
237
+ const eventData = {
238
+ item: this.#target,
239
+ from: this.node,
240
+ to: this.node,
241
+ oldIndex: this.#startIndex,
242
+ newIndex: this.#startIndex
243
+ };
244
+ triggerSortableEvent(this.node, "sort", eventData);
245
+ if (!this.node) return;
246
+ if (this.options.helperClass) _fr0st_query.default.addClass(this.#target, this.options.helperClass);
247
+ }
248
+ this.#updatePlacement(deltaX, deltaY);
249
+ this.#scheduleScroll();
250
+ };
251
+ const upEvent = (e) => {
252
+ if (!this.node) return;
253
+ if (!this.#dragging) {
254
+ this.#target = null;
255
+ return;
256
+ }
257
+ if (e.type === "mouseup") e.preventDefault();
258
+ const source = this.node;
259
+ const destination = this.#currentSortable;
260
+ /** @type {SortableEventData} */
261
+ const eventData = {
262
+ item: this.#target,
263
+ from: source,
264
+ to: destination,
265
+ oldIndex: this.#startIndex,
266
+ newIndex: this.#currentIndex
267
+ };
268
+ triggerSortableEvent(destination, "sorted", eventData);
269
+ if (!this.node) return;
270
+ if (this.options.helperClass) _fr0st_query.default.removeClass(this.#target, this.options.helperClass);
271
+ if (source !== destination) {
272
+ triggerSortableEvent(source, "update", eventData);
273
+ if (this.node) triggerSortableEvent(destination, "update", eventData);
274
+ } else if (this.#currentIndex !== this.#startIndex) triggerSortableEvent(destination, "update", eventData);
275
+ if (this.node) this.#resetDrag();
276
+ };
277
+ const dragEvent = _fr0st_query.default.mouseDragFactory(downEvent, moveEvent, upEvent);
278
+ _fr0st_query.default.addEventDelegate(this.node, "mousedown.ui.sortable touchstart.ui.sortable", this.#selector, dragEvent);
279
+ _fr0st_query.default.addEvent(this.node, "receive.ui.sortable", (_) => {
280
+ setCursor(_fr0st_query.default.find(this.#selector, this.node), this.options.cursor);
281
+ });
282
+ }
283
+ /**
284
+ * Resets the active drag state.
285
+ */
286
+ #resetDrag() {
287
+ if (this.#scrollFrame !== null && this.#scrollFrame !== void 0) window.cancelAnimationFrame(this.#scrollFrame);
288
+ if (this.#dragging && this.node) _fr0st_query.default.removeDataset(this.node, "uiDragging");
289
+ this.#currentIndex = null;
290
+ this.#currentSortable = null;
291
+ this.#dragging = false;
292
+ this.#pos = null;
293
+ this.#scrollFrame = null;
294
+ this.#startIndex = null;
295
+ this.#target = null;
296
+ }
297
+ /**
298
+ * Schedules the next auto-scroll update.
299
+ */
300
+ #scheduleScroll() {
301
+ if (!this.node || !this.options.scroll || !this.#dragging || this.#scrollFrame !== null && this.#scrollFrame !== void 0) return;
302
+ this.#scrollFrame = window.requestAnimationFrame((_) => {
303
+ this.#checkScroll();
304
+ });
305
+ }
306
+ /**
307
+ * Updates item placement and sorting events for the current pointer position.
308
+ * @param {number} deltaX The horizontal pointer movement.
309
+ * @param {number} deltaY The vertical pointer movement.
310
+ */
311
+ #updatePlacement(deltaX, deltaY) {
312
+ let nearestSortable = this.node;
313
+ if (this.options.connectWith) {
314
+ const connected = _fr0st_query.default.find(this.options.connectWith).filter((node) => node !== this.node).filter((node) => {
315
+ const sortable = this.constructor.init(node);
316
+ return sortable.#enabled && (sortable.options.dropOnEmpty || sortable.items().length);
317
+ });
318
+ if (connected.length) nearestSortable = _fr0st_query.default.nearestTo([this.node, ...connected], this.#pos.x, this.#pos.y, { offset: true });
319
+ }
320
+ const nearestInstance = this.constructor.init(nearestSortable);
321
+ const nearestItems = nearestInstance.items().filter((node) => node !== this.#target);
322
+ const nearestItem = _fr0st_query.default.nearestTo(nearestItems, this.#pos.x, this.#pos.y, { offset: true });
323
+ if (nearestItem) this.#updateTarget(nearestItem, this.#pos.x, this.#pos.y, deltaX, deltaY);
324
+ else if (nearestSortable !== this.#currentSortable) _fr0st_query.default.append(nearestSortable, this.#target);
325
+ else return;
326
+ const newIndex = nearestInstance.items().indexOf(this.#target);
327
+ if (nearestSortable !== this.#currentSortable) {
328
+ const previousSortable = this.#currentSortable;
329
+ const { handle } = this.constructor.init(previousSortable).options;
330
+ resetCursor(handle ? _fr0st_query.default.find(handle, this.#target) : [this.#target]);
331
+ /** @type {SortableEventData} */
332
+ const eventData = {
333
+ item: this.#target,
334
+ from: previousSortable,
335
+ to: nearestSortable,
336
+ oldIndex: this.#currentIndex,
337
+ newIndex
338
+ };
339
+ triggerSortableEvent(previousSortable, "send", eventData);
340
+ if (!this.node) return;
341
+ triggerSortableEvent(nearestSortable, "receive", eventData);
342
+ } else if (this.#currentIndex !== newIndex) {
343
+ /** @type {SortableEventData} */
344
+ const eventData = {
345
+ item: this.#target,
346
+ from: nearestSortable,
347
+ to: nearestSortable,
348
+ oldIndex: this.#currentIndex,
349
+ newIndex
350
+ };
351
+ triggerSortableEvent(nearestSortable, "sorting", eventData);
352
+ }
353
+ if (!this.node) return;
354
+ this.#currentIndex = newIndex;
355
+ this.#currentSortable = nearestSortable;
356
+ }
357
+ /**
358
+ * Moves the target relative to the nearest item.
359
+ * @param {HTMLElement} nearestItem The nearest item.
360
+ * @param {number} x The mouse X position.
361
+ * @param {number} y The mouse Y position.
362
+ * @param {number} deltaX The horizontal pointer movement.
363
+ * @param {number} deltaY The vertical pointer movement.
364
+ */
365
+ #updateTarget(nearestItem, x, y, deltaX, deltaY) {
366
+ const nearestCenter = _fr0st_query.default.center(nearestItem, { offset: true });
367
+ const nearestRect = _fr0st_query.default.rect(nearestItem, { offset: true });
368
+ const targetCenter = _fr0st_query.default.center(this.#target, { offset: true });
369
+ const threshold = Math.min(1, Math.max(0, this.options.swapThreshold));
370
+ let thresholdX = .5;
371
+ let thresholdY = .5;
372
+ if (deltaX) thresholdX = deltaX > 0 ? threshold : 1 - threshold;
373
+ if (deltaY) thresholdY = deltaY > 0 ? threshold : 1 - threshold;
374
+ let method;
375
+ if (this.options.axis === "y" || nearestCenter.x === targetCenter.x) method = y < nearestRect.top + nearestRect.height * thresholdY ? "before" : "after";
376
+ else if (this.options.axis === "x" || nearestCenter.y === targetCenter.y) method = x < nearestRect.left + nearestRect.width * thresholdX ? "before" : "after";
377
+ else {
378
+ const swapX = nearestRect.left + nearestRect.width * thresholdX;
379
+ const swapY = nearestRect.top + nearestRect.height * thresholdY;
380
+ method = x < swapX && y < swapY ? "before" : "after";
381
+ }
382
+ _fr0st_query.default[method](nearestItem, this.#target);
383
+ }
384
+ };
385
+
386
+ //#endregion
387
+ //#region src/index.js
388
+ (0, _fr0st_ui.initComponent)("sortable", Sortable);
389
+ var src_default = Sortable;
390
+
391
+ //#endregion
392
+ exports.Sortable = src_default;
393
+ });
394
+ //# sourceMappingURL=frost-ui-sortable.js.map