@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.
- package/LICENSE +0 -0
- package/README.md +375 -37
- package/dist/frost-ui-sortable.esm.js +361 -0
- package/dist/frost-ui-sortable.esm.js.map +1 -0
- package/dist/frost-ui-sortable.esm.min.js +2 -0
- package/dist/frost-ui-sortable.esm.min.js.map +1 -0
- package/dist/frost-ui-sortable.js +394 -323
- package/dist/frost-ui-sortable.js.map +1 -1
- package/dist/frost-ui-sortable.min.js +2 -1
- package/dist/frost-ui-sortable.min.js.map +1 -1
- package/package.json +56 -24
- package/src/{wrapper.js → browser.js} +0 -0
- package/src/helpers.js +53 -0
- package/src/index.js +0 -28
- package/src/sortable.js +475 -27
- package/src/prototype/events.js +0 -150
- package/src/prototype/helpers.js +0 -80
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { BaseComponent, getPosition, initComponent } from "@fr0st/ui";
|
|
2
|
+
import $ from "@fr0st/query";
|
|
3
|
+
|
|
4
|
+
//#region src/helpers.js
|
|
5
|
+
/** @import { SortableEventData } from './sortable.js'; */
|
|
6
|
+
var cursorLocks = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
/**
|
|
8
|
+
* Resets the cursor style for a collection of elements.
|
|
9
|
+
* @param {Iterable<HTMLElement>} nodes The elements to restore.
|
|
10
|
+
*/
|
|
11
|
+
function resetCursor(nodes) {
|
|
12
|
+
for (const node of nodes) {
|
|
13
|
+
const release = cursorLocks.get(node);
|
|
14
|
+
if (!release) continue;
|
|
15
|
+
release();
|
|
16
|
+
cursorLocks.delete(node);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Sets the cursor style for a collection of elements.
|
|
21
|
+
* @param {Iterable<HTMLElement>} nodes The elements to update.
|
|
22
|
+
* @param {string} cursor The cursor style.
|
|
23
|
+
*/
|
|
24
|
+
function setCursor(nodes, cursor) {
|
|
25
|
+
for (const node of nodes) if (cursorLocks.has(node)) $.setStyle(node, { cursor });
|
|
26
|
+
else cursorLocks.set(node, $.setStyleLock(node, "cursor", cursor));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Triggers a Sortable event with its data and detail payloads.
|
|
30
|
+
* @param {HTMLElement} node The event target.
|
|
31
|
+
* @param {string} name The event name without its namespace.
|
|
32
|
+
* @param {SortableEventData} data The sorting event data.
|
|
33
|
+
*/
|
|
34
|
+
function triggerSortableEvent(node, name, data) {
|
|
35
|
+
$.triggerEvent(node, `${name}.ui.sortable`, {
|
|
36
|
+
data,
|
|
37
|
+
detail: {
|
|
38
|
+
target: data.item,
|
|
39
|
+
...data
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/sortable.js
|
|
46
|
+
var document = $.getContext();
|
|
47
|
+
var window = $.getWindow();
|
|
48
|
+
/** @typedef {'x'|'y'} SortableAxis */
|
|
49
|
+
/**
|
|
50
|
+
* @typedef {object} SortableOptions
|
|
51
|
+
* @property {SortableAxis|null} [axis=null] The axis used to determine item placement.
|
|
52
|
+
* @property {string} [cancel='input, textarea, button, select, option'] The selector for elements that cannot start a sort.
|
|
53
|
+
* @property {string|null} [connectWith=null] The selector for connected Sortable containers.
|
|
54
|
+
* @property {string} [cursor='auto'] The cursor applied to sortable items or handles.
|
|
55
|
+
* @property {boolean} [disabled=false] Whether sorting is initially disabled.
|
|
56
|
+
* @property {boolean} [dropOnEmpty=true] Whether connected empty containers accept items.
|
|
57
|
+
* @property {string|null} [handle=null] The handle selector within each sortable item.
|
|
58
|
+
* @property {string|null} [helperClass='bg-body-tertiary'] The class applied to an item while it is being sorted.
|
|
59
|
+
* @property {string} [items=':scope > li'] The selector for sortable items.
|
|
60
|
+
* @property {boolean} [scroll=true] Whether to scroll the window while sorting near an edge.
|
|
61
|
+
* @property {number} [scrollSensitivity=20] The distance from a window edge that starts scrolling, in pixels.
|
|
62
|
+
* @property {number} [scrollSpeed=5] The scroll distance applied per animation frame, in pixels.
|
|
63
|
+
* @property {number} [swapThreshold=0.25] The fraction of an item crossed before swapping, from `0` to `1`.
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* @typedef {object} SortableEventData
|
|
67
|
+
* @property {HTMLElement} item The item being sorted.
|
|
68
|
+
* @property {HTMLElement} from The previous or original Sortable container.
|
|
69
|
+
* @property {HTMLElement} to The next or final Sortable container.
|
|
70
|
+
* @property {number} oldIndex The previous or original item index.
|
|
71
|
+
* @property {number} newIndex The next or final item index.
|
|
72
|
+
*/
|
|
73
|
+
/**
|
|
74
|
+
* Controls a sortable collection of elements.
|
|
75
|
+
* @augments {BaseComponent<SortableOptions>}
|
|
76
|
+
*/
|
|
77
|
+
var Sortable = class extends BaseComponent {
|
|
78
|
+
/** @type {SortableOptions} */
|
|
79
|
+
static defaults = {
|
|
80
|
+
axis: null,
|
|
81
|
+
cancel: "input, textarea, button, select, option",
|
|
82
|
+
connectWith: null,
|
|
83
|
+
cursor: "auto",
|
|
84
|
+
disabled: false,
|
|
85
|
+
dropOnEmpty: true,
|
|
86
|
+
handle: null,
|
|
87
|
+
helperClass: "bg-body-tertiary",
|
|
88
|
+
items: ":scope > li",
|
|
89
|
+
scroll: true,
|
|
90
|
+
scrollSensitivity: 20,
|
|
91
|
+
scrollSpeed: 5,
|
|
92
|
+
swapThreshold: .25
|
|
93
|
+
};
|
|
94
|
+
#currentIndex;
|
|
95
|
+
#currentSortable;
|
|
96
|
+
#dragging;
|
|
97
|
+
#enabled;
|
|
98
|
+
#pos;
|
|
99
|
+
#scrollFrame;
|
|
100
|
+
#selector;
|
|
101
|
+
#startIndex;
|
|
102
|
+
#target;
|
|
103
|
+
/**
|
|
104
|
+
* Creates a Sortable.
|
|
105
|
+
* @param {HTMLElement} node The sortable container.
|
|
106
|
+
* @param {SortableOptions} [options] The Sortable options.
|
|
107
|
+
*/
|
|
108
|
+
constructor(node, options) {
|
|
109
|
+
super(node, options);
|
|
110
|
+
this.#selector = this.options.handle ? `:is(${this.options.items}) :is(${this.options.handle})` : this.options.items;
|
|
111
|
+
this.#dragging = false;
|
|
112
|
+
this.#enabled = !this.options.disabled;
|
|
113
|
+
setCursor($.find(this.#selector, this.node), this.options.cursor);
|
|
114
|
+
this.#events();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Disables the Sortable.
|
|
118
|
+
*/
|
|
119
|
+
disable() {
|
|
120
|
+
this.#enabled = false;
|
|
121
|
+
}
|
|
122
|
+
/** @inheritdoc */
|
|
123
|
+
dispose() {
|
|
124
|
+
if (!this.node) return;
|
|
125
|
+
$.removeEventDelegate(this.node, "mousedown.ui.sortable touchstart.ui.sortable");
|
|
126
|
+
$.removeEvent(this.node, "receive.ui.sortable");
|
|
127
|
+
resetCursor($.find(this.#selector, this.node));
|
|
128
|
+
if (this.#target && this.options.helperClass) $.removeClass(this.#target, this.options.helperClass);
|
|
129
|
+
this.#resetDrag();
|
|
130
|
+
super.dispose();
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Enables the Sortable.
|
|
134
|
+
*/
|
|
135
|
+
enable() {
|
|
136
|
+
this.#enabled = true;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Gets the Sortable item elements.
|
|
140
|
+
* @returns {HTMLElement[]} The Sortable item elements.
|
|
141
|
+
*/
|
|
142
|
+
items() {
|
|
143
|
+
return $.find(this.options.items, this.node);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Checks whether the window should scroll while sorting.
|
|
147
|
+
*/
|
|
148
|
+
#checkScroll() {
|
|
149
|
+
this.#scrollFrame = null;
|
|
150
|
+
if (!this.node || !this.#dragging || !this.#target || !this.#pos) return;
|
|
151
|
+
const originalX = $.getScrollX(window);
|
|
152
|
+
const originalY = $.getScrollY(window);
|
|
153
|
+
const windowHeight = $.height(window);
|
|
154
|
+
const windowWidth = $.width(window);
|
|
155
|
+
let offsetX = originalX;
|
|
156
|
+
let offsetY = originalY;
|
|
157
|
+
if (this.#pos.y < originalY + this.options.scrollSensitivity) offsetY = Math.max(0, originalY - this.options.scrollSpeed);
|
|
158
|
+
else if (this.#pos.y > originalY + windowHeight - this.options.scrollSensitivity) {
|
|
159
|
+
const docHeight = $.height(document, { boxSize: $.SCROLL_BOX });
|
|
160
|
+
const maxY = Math.max(0, docHeight - windowHeight);
|
|
161
|
+
offsetY = Math.min(maxY, originalY + this.options.scrollSpeed);
|
|
162
|
+
}
|
|
163
|
+
if (this.#pos.x < originalX + this.options.scrollSensitivity) offsetX = Math.max(0, originalX - this.options.scrollSpeed);
|
|
164
|
+
else if (this.#pos.x > originalX + windowWidth - this.options.scrollSensitivity) {
|
|
165
|
+
const docWidth = $.width(document, { boxSize: $.SCROLL_BOX });
|
|
166
|
+
const maxX = Math.max(0, docWidth - windowWidth);
|
|
167
|
+
offsetX = Math.min(maxX, originalX + this.options.scrollSpeed);
|
|
168
|
+
}
|
|
169
|
+
if (offsetX === originalX && offsetY === originalY) return;
|
|
170
|
+
window.scrollTo({
|
|
171
|
+
left: offsetX,
|
|
172
|
+
top: offsetY,
|
|
173
|
+
behavior: "instant"
|
|
174
|
+
});
|
|
175
|
+
const deltaX = offsetX - originalX;
|
|
176
|
+
const deltaY = offsetY - originalY;
|
|
177
|
+
this.#pos.x += deltaX;
|
|
178
|
+
this.#pos.y += deltaY;
|
|
179
|
+
this.#updatePlacement(deltaX, deltaY);
|
|
180
|
+
this.#scheduleScroll();
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Attaches events for the Sortable.
|
|
184
|
+
*/
|
|
185
|
+
#events() {
|
|
186
|
+
const downEvent = (e) => {
|
|
187
|
+
const cancelled = this.options.cancel && ($.is(e.target, this.options.cancel) || $.is(e.currentTarget, this.options.cancel) || $.closest(e.target, this.options.cancel, this.node).length);
|
|
188
|
+
if (e.button || !this.#enabled || cancelled || $.getDataset(this.node, "uiDragging")) return false;
|
|
189
|
+
this.#target = this.options.handle ? this.items().find((node) => $.hasDescendent(node, e.currentTarget)) : e.currentTarget;
|
|
190
|
+
if (!this.#target) return false;
|
|
191
|
+
};
|
|
192
|
+
const moveEvent = (e) => {
|
|
193
|
+
if (!this.node || !this.#target) return;
|
|
194
|
+
const previousPos = this.#pos;
|
|
195
|
+
this.#pos = getPosition(e);
|
|
196
|
+
const deltaX = previousPos ? this.#pos.x - previousPos.x : 0;
|
|
197
|
+
const deltaY = previousPos ? this.#pos.y - previousPos.y : 0;
|
|
198
|
+
if (!this.#dragging) {
|
|
199
|
+
this.#dragging = true;
|
|
200
|
+
this.#currentSortable = this.node;
|
|
201
|
+
this.#startIndex = this.items().indexOf(this.#target);
|
|
202
|
+
this.#currentIndex = this.#startIndex;
|
|
203
|
+
$.setDataset(this.node, { uiDragging: true });
|
|
204
|
+
/** @type {SortableEventData} */
|
|
205
|
+
const eventData = {
|
|
206
|
+
item: this.#target,
|
|
207
|
+
from: this.node,
|
|
208
|
+
to: this.node,
|
|
209
|
+
oldIndex: this.#startIndex,
|
|
210
|
+
newIndex: this.#startIndex
|
|
211
|
+
};
|
|
212
|
+
triggerSortableEvent(this.node, "sort", eventData);
|
|
213
|
+
if (!this.node) return;
|
|
214
|
+
if (this.options.helperClass) $.addClass(this.#target, this.options.helperClass);
|
|
215
|
+
}
|
|
216
|
+
this.#updatePlacement(deltaX, deltaY);
|
|
217
|
+
this.#scheduleScroll();
|
|
218
|
+
};
|
|
219
|
+
const upEvent = (e) => {
|
|
220
|
+
if (!this.node) return;
|
|
221
|
+
if (!this.#dragging) {
|
|
222
|
+
this.#target = null;
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (e.type === "mouseup") e.preventDefault();
|
|
226
|
+
const source = this.node;
|
|
227
|
+
const destination = this.#currentSortable;
|
|
228
|
+
/** @type {SortableEventData} */
|
|
229
|
+
const eventData = {
|
|
230
|
+
item: this.#target,
|
|
231
|
+
from: source,
|
|
232
|
+
to: destination,
|
|
233
|
+
oldIndex: this.#startIndex,
|
|
234
|
+
newIndex: this.#currentIndex
|
|
235
|
+
};
|
|
236
|
+
triggerSortableEvent(destination, "sorted", eventData);
|
|
237
|
+
if (!this.node) return;
|
|
238
|
+
if (this.options.helperClass) $.removeClass(this.#target, this.options.helperClass);
|
|
239
|
+
if (source !== destination) {
|
|
240
|
+
triggerSortableEvent(source, "update", eventData);
|
|
241
|
+
if (this.node) triggerSortableEvent(destination, "update", eventData);
|
|
242
|
+
} else if (this.#currentIndex !== this.#startIndex) triggerSortableEvent(destination, "update", eventData);
|
|
243
|
+
if (this.node) this.#resetDrag();
|
|
244
|
+
};
|
|
245
|
+
const dragEvent = $.mouseDragFactory(downEvent, moveEvent, upEvent);
|
|
246
|
+
$.addEventDelegate(this.node, "mousedown.ui.sortable touchstart.ui.sortable", this.#selector, dragEvent);
|
|
247
|
+
$.addEvent(this.node, "receive.ui.sortable", (_) => {
|
|
248
|
+
setCursor($.find(this.#selector, this.node), this.options.cursor);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Resets the active drag state.
|
|
253
|
+
*/
|
|
254
|
+
#resetDrag() {
|
|
255
|
+
if (this.#scrollFrame !== null && this.#scrollFrame !== void 0) window.cancelAnimationFrame(this.#scrollFrame);
|
|
256
|
+
if (this.#dragging && this.node) $.removeDataset(this.node, "uiDragging");
|
|
257
|
+
this.#currentIndex = null;
|
|
258
|
+
this.#currentSortable = null;
|
|
259
|
+
this.#dragging = false;
|
|
260
|
+
this.#pos = null;
|
|
261
|
+
this.#scrollFrame = null;
|
|
262
|
+
this.#startIndex = null;
|
|
263
|
+
this.#target = null;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Schedules the next auto-scroll update.
|
|
267
|
+
*/
|
|
268
|
+
#scheduleScroll() {
|
|
269
|
+
if (!this.node || !this.options.scroll || !this.#dragging || this.#scrollFrame !== null && this.#scrollFrame !== void 0) return;
|
|
270
|
+
this.#scrollFrame = window.requestAnimationFrame((_) => {
|
|
271
|
+
this.#checkScroll();
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Updates item placement and sorting events for the current pointer position.
|
|
276
|
+
* @param {number} deltaX The horizontal pointer movement.
|
|
277
|
+
* @param {number} deltaY The vertical pointer movement.
|
|
278
|
+
*/
|
|
279
|
+
#updatePlacement(deltaX, deltaY) {
|
|
280
|
+
let nearestSortable = this.node;
|
|
281
|
+
if (this.options.connectWith) {
|
|
282
|
+
const connected = $.find(this.options.connectWith).filter((node) => node !== this.node).filter((node) => {
|
|
283
|
+
const sortable = this.constructor.init(node);
|
|
284
|
+
return sortable.#enabled && (sortable.options.dropOnEmpty || sortable.items().length);
|
|
285
|
+
});
|
|
286
|
+
if (connected.length) nearestSortable = $.nearestTo([this.node, ...connected], this.#pos.x, this.#pos.y, { offset: true });
|
|
287
|
+
}
|
|
288
|
+
const nearestInstance = this.constructor.init(nearestSortable);
|
|
289
|
+
const nearestItems = nearestInstance.items().filter((node) => node !== this.#target);
|
|
290
|
+
const nearestItem = $.nearestTo(nearestItems, this.#pos.x, this.#pos.y, { offset: true });
|
|
291
|
+
if (nearestItem) this.#updateTarget(nearestItem, this.#pos.x, this.#pos.y, deltaX, deltaY);
|
|
292
|
+
else if (nearestSortable !== this.#currentSortable) $.append(nearestSortable, this.#target);
|
|
293
|
+
else return;
|
|
294
|
+
const newIndex = nearestInstance.items().indexOf(this.#target);
|
|
295
|
+
if (nearestSortable !== this.#currentSortable) {
|
|
296
|
+
const previousSortable = this.#currentSortable;
|
|
297
|
+
const { handle } = this.constructor.init(previousSortable).options;
|
|
298
|
+
resetCursor(handle ? $.find(handle, this.#target) : [this.#target]);
|
|
299
|
+
/** @type {SortableEventData} */
|
|
300
|
+
const eventData = {
|
|
301
|
+
item: this.#target,
|
|
302
|
+
from: previousSortable,
|
|
303
|
+
to: nearestSortable,
|
|
304
|
+
oldIndex: this.#currentIndex,
|
|
305
|
+
newIndex
|
|
306
|
+
};
|
|
307
|
+
triggerSortableEvent(previousSortable, "send", eventData);
|
|
308
|
+
if (!this.node) return;
|
|
309
|
+
triggerSortableEvent(nearestSortable, "receive", eventData);
|
|
310
|
+
} else if (this.#currentIndex !== newIndex) {
|
|
311
|
+
/** @type {SortableEventData} */
|
|
312
|
+
const eventData = {
|
|
313
|
+
item: this.#target,
|
|
314
|
+
from: nearestSortable,
|
|
315
|
+
to: nearestSortable,
|
|
316
|
+
oldIndex: this.#currentIndex,
|
|
317
|
+
newIndex
|
|
318
|
+
};
|
|
319
|
+
triggerSortableEvent(nearestSortable, "sorting", eventData);
|
|
320
|
+
}
|
|
321
|
+
if (!this.node) return;
|
|
322
|
+
this.#currentIndex = newIndex;
|
|
323
|
+
this.#currentSortable = nearestSortable;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Moves the target relative to the nearest item.
|
|
327
|
+
* @param {HTMLElement} nearestItem The nearest item.
|
|
328
|
+
* @param {number} x The mouse X position.
|
|
329
|
+
* @param {number} y The mouse Y position.
|
|
330
|
+
* @param {number} deltaX The horizontal pointer movement.
|
|
331
|
+
* @param {number} deltaY The vertical pointer movement.
|
|
332
|
+
*/
|
|
333
|
+
#updateTarget(nearestItem, x, y, deltaX, deltaY) {
|
|
334
|
+
const nearestCenter = $.center(nearestItem, { offset: true });
|
|
335
|
+
const nearestRect = $.rect(nearestItem, { offset: true });
|
|
336
|
+
const targetCenter = $.center(this.#target, { offset: true });
|
|
337
|
+
const threshold = Math.min(1, Math.max(0, this.options.swapThreshold));
|
|
338
|
+
let thresholdX = .5;
|
|
339
|
+
let thresholdY = .5;
|
|
340
|
+
if (deltaX) thresholdX = deltaX > 0 ? threshold : 1 - threshold;
|
|
341
|
+
if (deltaY) thresholdY = deltaY > 0 ? threshold : 1 - threshold;
|
|
342
|
+
let method;
|
|
343
|
+
if (this.options.axis === "y" || nearestCenter.x === targetCenter.x) method = y < nearestRect.top + nearestRect.height * thresholdY ? "before" : "after";
|
|
344
|
+
else if (this.options.axis === "x" || nearestCenter.y === targetCenter.y) method = x < nearestRect.left + nearestRect.width * thresholdX ? "before" : "after";
|
|
345
|
+
else {
|
|
346
|
+
const swapX = nearestRect.left + nearestRect.width * thresholdX;
|
|
347
|
+
const swapY = nearestRect.top + nearestRect.height * thresholdY;
|
|
348
|
+
method = x < swapX && y < swapY ? "before" : "after";
|
|
349
|
+
}
|
|
350
|
+
$[method](nearestItem, this.#target);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/index.js
|
|
356
|
+
initComponent("sortable", Sortable);
|
|
357
|
+
var src_default = Sortable;
|
|
358
|
+
|
|
359
|
+
//#endregion
|
|
360
|
+
export { src_default as default };
|
|
361
|
+
//# sourceMappingURL=frost-ui-sortable.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frost-ui-sortable.esm.js","names":[],"sources":["../src/helpers.js","../src/sortable.js","../src/index.js"],"sourcesContent":["/** @import { SortableEventData } from './sortable.js'; */\n\nimport $ from '@fr0st/query';\n\nconst cursorLocks = new WeakMap();\n\n/**\n * Resets the cursor style for a collection of elements.\n * @param {Iterable<HTMLElement>} nodes The elements to restore.\n */\nexport function resetCursor(nodes) {\n for (const node of nodes) {\n const release = cursorLocks.get(node);\n\n if (!release) {\n continue;\n }\n\n release();\n cursorLocks.delete(node);\n }\n};\n\n/**\n * Sets the cursor style for a collection of elements.\n * @param {Iterable<HTMLElement>} nodes The elements to update.\n * @param {string} cursor The cursor style.\n */\nexport function setCursor(nodes, cursor) {\n for (const node of nodes) {\n if (cursorLocks.has(node)) {\n $.setStyle(node, { cursor });\n } else {\n cursorLocks.set(node, $.setStyleLock(node, 'cursor', cursor));\n }\n }\n};\n\n/**\n * Triggers a Sortable event with its data and detail payloads.\n * @param {HTMLElement} node The event target.\n * @param {string} name The event name without its namespace.\n * @param {SortableEventData} data The sorting event data.\n */\nexport function triggerSortableEvent(node, name, data) {\n $.triggerEvent(node, `${name}.ui.sortable`, {\n data,\n detail: {\n target: data.item,\n ...data,\n },\n });\n};\n","import $ from '@fr0st/query';\nimport { BaseComponent, getPosition } from '@fr0st/ui';\nimport { resetCursor, setCursor, triggerSortableEvent } from './helpers.js';\n\nconst document = $.getContext();\nconst window = $.getWindow();\n\n/** @typedef {'x'|'y'} SortableAxis */\n\n/**\n * @typedef {object} SortableOptions\n * @property {SortableAxis|null} [axis=null] The axis used to determine item placement.\n * @property {string} [cancel='input, textarea, button, select, option'] The selector for elements that cannot start a sort.\n * @property {string|null} [connectWith=null] The selector for connected Sortable containers.\n * @property {string} [cursor='auto'] The cursor applied to sortable items or handles.\n * @property {boolean} [disabled=false] Whether sorting is initially disabled.\n * @property {boolean} [dropOnEmpty=true] Whether connected empty containers accept items.\n * @property {string|null} [handle=null] The handle selector within each sortable item.\n * @property {string|null} [helperClass='bg-body-tertiary'] The class applied to an item while it is being sorted.\n * @property {string} [items=':scope > li'] The selector for sortable items.\n * @property {boolean} [scroll=true] Whether to scroll the window while sorting near an edge.\n * @property {number} [scrollSensitivity=20] The distance from a window edge that starts scrolling, in pixels.\n * @property {number} [scrollSpeed=5] The scroll distance applied per animation frame, in pixels.\n * @property {number} [swapThreshold=0.25] The fraction of an item crossed before swapping, from `0` to `1`.\n */\n\n/**\n * @typedef {object} SortableEventData\n * @property {HTMLElement} item The item being sorted.\n * @property {HTMLElement} from The previous or original Sortable container.\n * @property {HTMLElement} to The next or final Sortable container.\n * @property {number} oldIndex The previous or original item index.\n * @property {number} newIndex The next or final item index.\n */\n\n/**\n * Controls a sortable collection of elements.\n * @augments {BaseComponent<SortableOptions>}\n */\nexport default class Sortable extends BaseComponent {\n /** @type {SortableOptions} */\n static defaults = {\n axis: null,\n cancel: 'input, textarea, button, select, option',\n connectWith: null,\n cursor: 'auto',\n disabled: false,\n dropOnEmpty: true,\n handle: null,\n helperClass: 'bg-body-tertiary',\n items: ':scope > li',\n scroll: true,\n scrollSensitivity: 20,\n scrollSpeed: 5,\n swapThreshold: 0.25,\n };\n\n #currentIndex;\n #currentSortable;\n #dragging;\n #enabled;\n #pos;\n #scrollFrame;\n #selector;\n #startIndex;\n #target;\n\n /**\n * Creates a Sortable.\n * @param {HTMLElement} node The sortable container.\n * @param {SortableOptions} [options] The Sortable options.\n */\n constructor(node, options) {\n super(node, options);\n\n this.#selector = this.options.handle ?\n `:is(${this.options.items}) :is(${this.options.handle})` :\n this.options.items;\n\n this.#dragging = false;\n this.#enabled = !this.options.disabled;\n\n setCursor($.find(this.#selector, this.node), this.options.cursor);\n this.#events();\n }\n\n /**\n * Disables the Sortable.\n */\n disable() {\n this.#enabled = false;\n }\n\n /** @inheritdoc */\n dispose() {\n if (!this.node) {\n return;\n }\n\n $.removeEventDelegate(this.node, 'mousedown.ui.sortable touchstart.ui.sortable');\n $.removeEvent(this.node, 'receive.ui.sortable');\n resetCursor($.find(this.#selector, this.node));\n\n if (this.#target && this.options.helperClass) {\n $.removeClass(this.#target, this.options.helperClass);\n }\n\n this.#resetDrag();\n\n super.dispose();\n }\n\n /**\n * Enables the Sortable.\n */\n enable() {\n this.#enabled = true;\n }\n\n /**\n * Gets the Sortable item elements.\n * @returns {HTMLElement[]} The Sortable item elements.\n */\n items() {\n return $.find(this.options.items, this.node);\n }\n\n /**\n * Checks whether the window should scroll while sorting.\n */\n #checkScroll() {\n this.#scrollFrame = null;\n\n if (!this.node || !this.#dragging || !this.#target || !this.#pos) {\n return;\n }\n\n const originalX = $.getScrollX(window);\n const originalY = $.getScrollY(window);\n const windowHeight = $.height(window);\n const windowWidth = $.width(window);\n\n let offsetX = originalX;\n let offsetY = originalY;\n\n if (this.#pos.y < originalY + this.options.scrollSensitivity) {\n offsetY = Math.max(0, originalY - this.options.scrollSpeed);\n } else if (this.#pos.y > originalY + windowHeight - this.options.scrollSensitivity) {\n const docHeight = $.height(document, { boxSize: $.SCROLL_BOX });\n const maxY = Math.max(0, docHeight - windowHeight);\n offsetY = Math.min(maxY, originalY + this.options.scrollSpeed);\n }\n\n if (this.#pos.x < originalX + this.options.scrollSensitivity) {\n offsetX = Math.max(0, originalX - this.options.scrollSpeed);\n } else if (this.#pos.x > originalX + windowWidth - this.options.scrollSensitivity) {\n const docWidth = $.width(document, { boxSize: $.SCROLL_BOX });\n const maxX = Math.max(0, docWidth - windowWidth);\n offsetX = Math.min(maxX, originalX + this.options.scrollSpeed);\n }\n\n if (offsetX === originalX && offsetY === originalY) {\n return;\n }\n\n window.scrollTo({\n left: offsetX,\n top: offsetY,\n behavior: 'instant',\n });\n\n const deltaX = offsetX - originalX;\n const deltaY = offsetY - originalY;\n\n this.#pos.x += deltaX;\n this.#pos.y += deltaY;\n\n this.#updatePlacement(deltaX, deltaY);\n this.#scheduleScroll();\n }\n\n /**\n * Attaches events for the Sortable.\n */\n #events() {\n const downEvent = (e) => {\n const cancelled = this.options.cancel &&\n (\n $.is(e.target, this.options.cancel) ||\n $.is(e.currentTarget, this.options.cancel) ||\n $.closest(e.target, this.options.cancel, this.node).length\n );\n\n if (\n e.button ||\n !this.#enabled ||\n cancelled ||\n $.getDataset(this.node, 'uiDragging')\n ) {\n return false;\n }\n\n this.#target = this.options.handle ?\n this.items().find((node) => $.hasDescendent(node, e.currentTarget)) :\n e.currentTarget;\n\n if (!this.#target) {\n return false;\n }\n };\n\n const moveEvent = (e) => {\n if (!this.node || !this.#target) {\n return;\n }\n\n const previousPos = this.#pos;\n this.#pos = getPosition(e);\n const deltaX = previousPos ?\n this.#pos.x - previousPos.x :\n 0;\n const deltaY = previousPos ?\n this.#pos.y - previousPos.y :\n 0;\n\n if (!this.#dragging) {\n this.#dragging = true;\n this.#currentSortable = this.node;\n this.#startIndex = this.items().indexOf(this.#target);\n this.#currentIndex = this.#startIndex;\n\n $.setDataset(this.node, { uiDragging: true });\n\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: this.node,\n to: this.node,\n oldIndex: this.#startIndex,\n newIndex: this.#startIndex,\n };\n\n triggerSortableEvent(this.node, 'sort', eventData);\n\n if (!this.node) {\n return;\n }\n\n if (this.options.helperClass) {\n $.addClass(this.#target, this.options.helperClass);\n }\n }\n\n this.#updatePlacement(deltaX, deltaY);\n this.#scheduleScroll();\n };\n\n const upEvent = (e) => {\n if (!this.node) {\n return;\n }\n\n if (!this.#dragging) {\n this.#target = null;\n return;\n }\n\n if (e.type === 'mouseup') {\n e.preventDefault();\n }\n\n const source = this.node;\n const destination = this.#currentSortable;\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: source,\n to: destination,\n oldIndex: this.#startIndex,\n newIndex: this.#currentIndex,\n };\n\n triggerSortableEvent(destination, 'sorted', eventData);\n\n if (!this.node) {\n return;\n }\n\n if (this.options.helperClass) {\n $.removeClass(this.#target, this.options.helperClass);\n }\n\n if (source !== destination) {\n triggerSortableEvent(source, 'update', eventData);\n\n if (this.node) {\n triggerSortableEvent(destination, 'update', eventData);\n }\n } else if (this.#currentIndex !== this.#startIndex) {\n triggerSortableEvent(destination, 'update', eventData);\n }\n\n if (this.node) {\n this.#resetDrag();\n }\n };\n\n const dragEvent = $.mouseDragFactory(downEvent, moveEvent, upEvent);\n\n $.addEventDelegate(\n this.node,\n 'mousedown.ui.sortable touchstart.ui.sortable',\n this.#selector,\n dragEvent,\n );\n\n $.addEvent(this.node, 'receive.ui.sortable', (_) => {\n setCursor($.find(this.#selector, this.node), this.options.cursor);\n });\n }\n\n /**\n * Resets the active drag state.\n */\n #resetDrag() {\n if (this.#scrollFrame !== null && this.#scrollFrame !== undefined) {\n window.cancelAnimationFrame(this.#scrollFrame);\n }\n\n if (this.#dragging && this.node) {\n $.removeDataset(this.node, 'uiDragging');\n }\n\n this.#currentIndex = null;\n this.#currentSortable = null;\n this.#dragging = false;\n this.#pos = null;\n this.#scrollFrame = null;\n this.#startIndex = null;\n this.#target = null;\n }\n\n /**\n * Schedules the next auto-scroll update.\n */\n #scheduleScroll() {\n if (\n !this.node ||\n !this.options.scroll ||\n !this.#dragging ||\n this.#scrollFrame !== null && this.#scrollFrame !== undefined\n ) {\n return;\n }\n\n this.#scrollFrame = window.requestAnimationFrame((_) => {\n this.#checkScroll();\n });\n }\n\n /**\n * Updates item placement and sorting events for the current pointer position.\n * @param {number} deltaX The horizontal pointer movement.\n * @param {number} deltaY The vertical pointer movement.\n */\n #updatePlacement(deltaX, deltaY) {\n let nearestSortable = this.node;\n\n if (this.options.connectWith) {\n const connected = $.find(this.options.connectWith)\n .filter((node) => node !== this.node)\n .filter((node) => {\n const sortable = this.constructor.init(node);\n\n return sortable.#enabled &&\n (sortable.options.dropOnEmpty || sortable.items().length);\n });\n\n if (connected.length) {\n nearestSortable = $.nearestTo(\n [this.node, ...connected],\n this.#pos.x,\n this.#pos.y,\n { offset: true },\n );\n }\n }\n\n const nearestInstance = this.constructor.init(nearestSortable);\n const nearestItems = nearestInstance.items()\n .filter((node) => node !== this.#target);\n const nearestItem = $.nearestTo(\n nearestItems,\n this.#pos.x,\n this.#pos.y,\n { offset: true },\n );\n\n if (nearestItem) {\n this.#updateTarget(\n nearestItem,\n this.#pos.x,\n this.#pos.y,\n deltaX,\n deltaY,\n );\n } else if (nearestSortable !== this.#currentSortable) {\n $.append(nearestSortable, this.#target);\n } else {\n return;\n }\n\n const newIndex = nearestInstance.items().indexOf(this.#target);\n\n if (nearestSortable !== this.#currentSortable) {\n const previousSortable = this.#currentSortable;\n const previousInstance = this.constructor.init(previousSortable);\n const { handle } = previousInstance.options;\n\n resetCursor(handle ?\n $.find(handle, this.#target) :\n [this.#target]);\n\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: previousSortable,\n to: nearestSortable,\n oldIndex: this.#currentIndex,\n newIndex,\n };\n\n triggerSortableEvent(previousSortable, 'send', eventData);\n\n if (!this.node) {\n return;\n }\n\n triggerSortableEvent(nearestSortable, 'receive', eventData);\n } else if (this.#currentIndex !== newIndex) {\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: nearestSortable,\n to: nearestSortable,\n oldIndex: this.#currentIndex,\n newIndex,\n };\n\n triggerSortableEvent(nearestSortable, 'sorting', eventData);\n }\n\n if (!this.node) {\n return;\n }\n\n this.#currentIndex = newIndex;\n this.#currentSortable = nearestSortable;\n }\n\n /**\n * Moves the target relative to the nearest item.\n * @param {HTMLElement} nearestItem The nearest item.\n * @param {number} x The mouse X position.\n * @param {number} y The mouse Y position.\n * @param {number} deltaX The horizontal pointer movement.\n * @param {number} deltaY The vertical pointer movement.\n */\n #updateTarget(nearestItem, x, y, deltaX, deltaY) {\n const nearestCenter = $.center(nearestItem, { offset: true });\n const nearestRect = $.rect(nearestItem, { offset: true });\n const targetCenter = $.center(this.#target, { offset: true });\n const threshold = Math.min(1, Math.max(0, this.options.swapThreshold));\n let thresholdX = 0.5;\n let thresholdY = 0.5;\n\n if (deltaX) {\n thresholdX = deltaX > 0 ?\n threshold :\n 1 - threshold;\n }\n\n if (deltaY) {\n thresholdY = deltaY > 0 ?\n threshold :\n 1 - threshold;\n }\n\n let method;\n if (this.options.axis === 'y' || nearestCenter.x === targetCenter.x) {\n const swapY = nearestRect.top + nearestRect.height * thresholdY;\n method = y < swapY ?\n 'before' :\n 'after';\n } else if (this.options.axis === 'x' || nearestCenter.y === targetCenter.y) {\n const swapX = nearestRect.left + nearestRect.width * thresholdX;\n method = x < swapX ?\n 'before' :\n 'after';\n } else {\n const swapX = nearestRect.left + nearestRect.width * thresholdX;\n const swapY = nearestRect.top + nearestRect.height * thresholdY;\n method = x < swapX && y < swapY ?\n 'before' :\n 'after';\n }\n\n $[method](nearestItem, this.#target);\n }\n}\n","import { initComponent } from '@fr0st/ui';\nimport Sortable from './sortable.js';\n\ninitComponent('sortable', Sortable);\n\nexport default Sortable;\n"],"mappings":";;;;;AAIA,IAAM,8BAAc,IAAI,QAAQ;;;;;AAMhC,SAAgB,YAAY,OAAO;CAC/B,KAAK,MAAM,QAAQ,OAAO;EACtB,MAAM,UAAU,YAAY,IAAI,IAAI;EAEp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EACrE,IAAI,aAAa;EACjB,IAAI,aAAa;EAEjB,IAAI,QACA,aAAa,SAAS,IAClB,YACA,IAAI;EAGZ,IAAI,QACA,aAAa,SAAS,IAClB,YACA,IAAI;EAGZ,IAAI;EACJ,IAAI,KAAK,QAAQ,SAAS,OAAO,cAAc,MAAM,aAAa,GAE9D,SAAS,IADK,YAAY,MAAM,YAAY,SAAS,aAEjD,WACA;OACD,IAAI,KAAK,QAAQ,SAAS,OAAO,cAAc,MAAM,aAAa,GAErE,SAAS,IADK,YAAY,OAAO,YAAY,QAAQ,aAEjD,WACA;OACD;GACH,MAAM,QAAQ,YAAY,OAAO,YAAY,QAAQ;GACrD,MAAM,QAAQ,YAAY,MAAM,YAAY,SAAS;GACrD,SAAS,IAAI,SAAS,IAAI,QACtB,WACA;EACR;EAEA,EAAE,OAAO,CAAC,aAAa,KAAK,OAAO;CACvC;AACJ;;;;AC1fA,cAAc,YAAY,QAAQ;AAElC,kBAAe"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{BaseComponent as e,getPosition as t,initComponent as n}from"@fr0st/ui";import r from"@fr0st/query";var i=new WeakMap;function a(e){for(let t of e){let e=i.get(t);e&&(e(),i.delete(t))}}function o(e,t){for(let n of e)i.has(n)?r.setStyle(n,{cursor:t}):i.set(n,r.setStyleLock(n,`cursor`,t))}function s(e,t,n){r.triggerEvent(e,`${t}.ui.sortable`,{data:n,detail:{target:n.item,...n}})}var c=r.getContext(),l=r.getWindow(),u=class extends e{static defaults={axis:null,cancel:`input, textarea, button, select, option`,connectWith:null,cursor:`auto`,disabled:!1,dropOnEmpty:!0,handle:null,helperClass:`bg-body-tertiary`,items:`:scope > li`,scroll:!0,scrollSensitivity:20,scrollSpeed:5,swapThreshold:.25};#e;#t;#n;#r;#i;#a;#o;#s;#c;constructor(e,t){super(e,t),this.#o=this.options.handle?`:is(${this.options.items}) :is(${this.options.handle})`:this.options.items,this.#n=!1,this.#r=!this.options.disabled,o(r.find(this.#o,this.node),this.options.cursor),this.#u()}disable(){this.#r=!1}dispose(){this.node&&(r.removeEventDelegate(this.node,`mousedown.ui.sortable touchstart.ui.sortable`),r.removeEvent(this.node,`receive.ui.sortable`),a(r.find(this.#o,this.node)),this.#c&&this.options.helperClass&&r.removeClass(this.#c,this.options.helperClass),this.#d(),super.dispose())}enable(){this.#r=!0}items(){return r.find(this.options.items,this.node)}#l(){if(this.#a=null,!this.node||!this.#n||!this.#c||!this.#i)return;let e=r.getScrollX(l),t=r.getScrollY(l),n=r.height(l),i=r.width(l),a=e,o=t;if(this.#i.y<t+this.options.scrollSensitivity)o=Math.max(0,t-this.options.scrollSpeed);else if(this.#i.y>t+n-this.options.scrollSensitivity){let e=r.height(c,{boxSize:r.SCROLL_BOX}),i=Math.max(0,e-n);o=Math.min(i,t+this.options.scrollSpeed)}if(this.#i.x<e+this.options.scrollSensitivity)a=Math.max(0,e-this.options.scrollSpeed);else if(this.#i.x>e+i-this.options.scrollSensitivity){let t=r.width(c,{boxSize:r.SCROLL_BOX}),n=Math.max(0,t-i);a=Math.min(n,e+this.options.scrollSpeed)}if(a===e&&o===t)return;l.scrollTo({left:a,top:o,behavior:`instant`});let s=a-e,u=o-t;this.#i.x+=s,this.#i.y+=u,this.#p(s,u),this.#f()}#u(){let e=r.mouseDragFactory(e=>{let t=this.options.cancel&&(r.is(e.target,this.options.cancel)||r.is(e.currentTarget,this.options.cancel)||r.closest(e.target,this.options.cancel,this.node).length);if(e.button||!this.#r||t||r.getDataset(this.node,`uiDragging`)||(this.#c=this.options.handle?this.items().find(t=>r.hasDescendent(t,e.currentTarget)):e.currentTarget,!this.#c))return!1},e=>{if(!this.node||!this.#c)return;let n=this.#i;this.#i=t(e);let i=n?this.#i.x-n.x:0,a=n?this.#i.y-n.y:0;if(!this.#n){this.#n=!0,this.#t=this.node,this.#s=this.items().indexOf(this.#c),this.#e=this.#s,r.setDataset(this.node,{uiDragging:!0});let e={item:this.#c,from:this.node,to:this.node,oldIndex:this.#s,newIndex:this.#s};if(s(this.node,`sort`,e),!this.node)return;this.options.helperClass&&r.addClass(this.#c,this.options.helperClass)}this.#p(i,a),this.#f()},e=>{if(!this.node)return;if(!this.#n){this.#c=null;return}e.type===`mouseup`&&e.preventDefault();let t=this.node,n=this.#t,i={item:this.#c,from:t,to:n,oldIndex:this.#s,newIndex:this.#e};s(n,`sorted`,i),this.node&&(this.options.helperClass&&r.removeClass(this.#c,this.options.helperClass),t===n?this.#e!==this.#s&&s(n,`update`,i):(s(t,`update`,i),this.node&&s(n,`update`,i)),this.node&&this.#d())});r.addEventDelegate(this.node,`mousedown.ui.sortable touchstart.ui.sortable`,this.#o,e),r.addEvent(this.node,`receive.ui.sortable`,e=>{o(r.find(this.#o,this.node),this.options.cursor)})}#d(){this.#a!==null&&this.#a!==void 0&&l.cancelAnimationFrame(this.#a),this.#n&&this.node&&r.removeDataset(this.node,`uiDragging`),this.#e=null,this.#t=null,this.#n=!1,this.#i=null,this.#a=null,this.#s=null,this.#c=null}#f(){!this.node||!this.options.scroll||!this.#n||this.#a!==null&&this.#a!==void 0||(this.#a=l.requestAnimationFrame(e=>{this.#l()}))}#p(e,t){let n=this.node;if(this.options.connectWith){let e=r.find(this.options.connectWith).filter(e=>e!==this.node).filter(e=>{let t=this.constructor.init(e);return t.#r&&(t.options.dropOnEmpty||t.items().length)});e.length&&(n=r.nearestTo([this.node,...e],this.#i.x,this.#i.y,{offset:!0}))}let i=this.constructor.init(n),o=i.items().filter(e=>e!==this.#c),c=r.nearestTo(o,this.#i.x,this.#i.y,{offset:!0});if(c)this.#m(c,this.#i.x,this.#i.y,e,t);else if(n!==this.#t)r.append(n,this.#c);else return;let l=i.items().indexOf(this.#c);if(n!==this.#t){let e=this.#t,{handle:t}=this.constructor.init(e).options;a(t?r.find(t,this.#c):[this.#c]);let i={item:this.#c,from:e,to:n,oldIndex:this.#e,newIndex:l};if(s(e,`send`,i),!this.node)return;s(n,`receive`,i)}else if(this.#e!==l){let e={item:this.#c,from:n,to:n,oldIndex:this.#e,newIndex:l};s(n,`sorting`,e)}this.node&&(this.#e=l,this.#t=n)}#m(e,t,n,i,a){let o=r.center(e,{offset:!0}),s=r.rect(e,{offset:!0}),c=r.center(this.#c,{offset:!0}),l=Math.min(1,Math.max(0,this.options.swapThreshold)),u=.5,d=.5;i&&(u=i>0?l:1-l),a&&(d=a>0?l:1-l);let f;if(this.options.axis===`y`||o.x===c.x)f=n<s.top+s.height*d?`before`:`after`;else if(this.options.axis===`x`||o.y===c.y)f=t<s.left+s.width*u?`before`:`after`;else{let e=s.left+s.width*u,r=s.top+s.height*d;f=t<e&&n<r?`before`:`after`}r[f](e,this.#c)}};n(`sortable`,u);var d=u;export{d as default};
|
|
2
|
+
//# sourceMappingURL=frost-ui-sortable.esm.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frost-ui-sortable.esm.min.js","names":[],"sources":["../src/helpers.js","../src/sortable.js","../src/index.js"],"sourcesContent":["/** @import { SortableEventData } from './sortable.js'; */\n\nimport $ from '@fr0st/query';\n\nconst cursorLocks = new WeakMap();\n\n/**\n * Resets the cursor style for a collection of elements.\n * @param {Iterable<HTMLElement>} nodes The elements to restore.\n */\nexport function resetCursor(nodes) {\n for (const node of nodes) {\n const release = cursorLocks.get(node);\n\n if (!release) {\n continue;\n }\n\n release();\n cursorLocks.delete(node);\n }\n};\n\n/**\n * Sets the cursor style for a collection of elements.\n * @param {Iterable<HTMLElement>} nodes The elements to update.\n * @param {string} cursor The cursor style.\n */\nexport function setCursor(nodes, cursor) {\n for (const node of nodes) {\n if (cursorLocks.has(node)) {\n $.setStyle(node, { cursor });\n } else {\n cursorLocks.set(node, $.setStyleLock(node, 'cursor', cursor));\n }\n }\n};\n\n/**\n * Triggers a Sortable event with its data and detail payloads.\n * @param {HTMLElement} node The event target.\n * @param {string} name The event name without its namespace.\n * @param {SortableEventData} data The sorting event data.\n */\nexport function triggerSortableEvent(node, name, data) {\n $.triggerEvent(node, `${name}.ui.sortable`, {\n data,\n detail: {\n target: data.item,\n ...data,\n },\n });\n};\n","import $ from '@fr0st/query';\nimport { BaseComponent, getPosition } from '@fr0st/ui';\nimport { resetCursor, setCursor, triggerSortableEvent } from './helpers.js';\n\nconst document = $.getContext();\nconst window = $.getWindow();\n\n/** @typedef {'x'|'y'} SortableAxis */\n\n/**\n * @typedef {object} SortableOptions\n * @property {SortableAxis|null} [axis=null] The axis used to determine item placement.\n * @property {string} [cancel='input, textarea, button, select, option'] The selector for elements that cannot start a sort.\n * @property {string|null} [connectWith=null] The selector for connected Sortable containers.\n * @property {string} [cursor='auto'] The cursor applied to sortable items or handles.\n * @property {boolean} [disabled=false] Whether sorting is initially disabled.\n * @property {boolean} [dropOnEmpty=true] Whether connected empty containers accept items.\n * @property {string|null} [handle=null] The handle selector within each sortable item.\n * @property {string|null} [helperClass='bg-body-tertiary'] The class applied to an item while it is being sorted.\n * @property {string} [items=':scope > li'] The selector for sortable items.\n * @property {boolean} [scroll=true] Whether to scroll the window while sorting near an edge.\n * @property {number} [scrollSensitivity=20] The distance from a window edge that starts scrolling, in pixels.\n * @property {number} [scrollSpeed=5] The scroll distance applied per animation frame, in pixels.\n * @property {number} [swapThreshold=0.25] The fraction of an item crossed before swapping, from `0` to `1`.\n */\n\n/**\n * @typedef {object} SortableEventData\n * @property {HTMLElement} item The item being sorted.\n * @property {HTMLElement} from The previous or original Sortable container.\n * @property {HTMLElement} to The next or final Sortable container.\n * @property {number} oldIndex The previous or original item index.\n * @property {number} newIndex The next or final item index.\n */\n\n/**\n * Controls a sortable collection of elements.\n * @augments {BaseComponent<SortableOptions>}\n */\nexport default class Sortable extends BaseComponent {\n /** @type {SortableOptions} */\n static defaults = {\n axis: null,\n cancel: 'input, textarea, button, select, option',\n connectWith: null,\n cursor: 'auto',\n disabled: false,\n dropOnEmpty: true,\n handle: null,\n helperClass: 'bg-body-tertiary',\n items: ':scope > li',\n scroll: true,\n scrollSensitivity: 20,\n scrollSpeed: 5,\n swapThreshold: 0.25,\n };\n\n #currentIndex;\n #currentSortable;\n #dragging;\n #enabled;\n #pos;\n #scrollFrame;\n #selector;\n #startIndex;\n #target;\n\n /**\n * Creates a Sortable.\n * @param {HTMLElement} node The sortable container.\n * @param {SortableOptions} [options] The Sortable options.\n */\n constructor(node, options) {\n super(node, options);\n\n this.#selector = this.options.handle ?\n `:is(${this.options.items}) :is(${this.options.handle})` :\n this.options.items;\n\n this.#dragging = false;\n this.#enabled = !this.options.disabled;\n\n setCursor($.find(this.#selector, this.node), this.options.cursor);\n this.#events();\n }\n\n /**\n * Disables the Sortable.\n */\n disable() {\n this.#enabled = false;\n }\n\n /** @inheritdoc */\n dispose() {\n if (!this.node) {\n return;\n }\n\n $.removeEventDelegate(this.node, 'mousedown.ui.sortable touchstart.ui.sortable');\n $.removeEvent(this.node, 'receive.ui.sortable');\n resetCursor($.find(this.#selector, this.node));\n\n if (this.#target && this.options.helperClass) {\n $.removeClass(this.#target, this.options.helperClass);\n }\n\n this.#resetDrag();\n\n super.dispose();\n }\n\n /**\n * Enables the Sortable.\n */\n enable() {\n this.#enabled = true;\n }\n\n /**\n * Gets the Sortable item elements.\n * @returns {HTMLElement[]} The Sortable item elements.\n */\n items() {\n return $.find(this.options.items, this.node);\n }\n\n /**\n * Checks whether the window should scroll while sorting.\n */\n #checkScroll() {\n this.#scrollFrame = null;\n\n if (!this.node || !this.#dragging || !this.#target || !this.#pos) {\n return;\n }\n\n const originalX = $.getScrollX(window);\n const originalY = $.getScrollY(window);\n const windowHeight = $.height(window);\n const windowWidth = $.width(window);\n\n let offsetX = originalX;\n let offsetY = originalY;\n\n if (this.#pos.y < originalY + this.options.scrollSensitivity) {\n offsetY = Math.max(0, originalY - this.options.scrollSpeed);\n } else if (this.#pos.y > originalY + windowHeight - this.options.scrollSensitivity) {\n const docHeight = $.height(document, { boxSize: $.SCROLL_BOX });\n const maxY = Math.max(0, docHeight - windowHeight);\n offsetY = Math.min(maxY, originalY + this.options.scrollSpeed);\n }\n\n if (this.#pos.x < originalX + this.options.scrollSensitivity) {\n offsetX = Math.max(0, originalX - this.options.scrollSpeed);\n } else if (this.#pos.x > originalX + windowWidth - this.options.scrollSensitivity) {\n const docWidth = $.width(document, { boxSize: $.SCROLL_BOX });\n const maxX = Math.max(0, docWidth - windowWidth);\n offsetX = Math.min(maxX, originalX + this.options.scrollSpeed);\n }\n\n if (offsetX === originalX && offsetY === originalY) {\n return;\n }\n\n window.scrollTo({\n left: offsetX,\n top: offsetY,\n behavior: 'instant',\n });\n\n const deltaX = offsetX - originalX;\n const deltaY = offsetY - originalY;\n\n this.#pos.x += deltaX;\n this.#pos.y += deltaY;\n\n this.#updatePlacement(deltaX, deltaY);\n this.#scheduleScroll();\n }\n\n /**\n * Attaches events for the Sortable.\n */\n #events() {\n const downEvent = (e) => {\n const cancelled = this.options.cancel &&\n (\n $.is(e.target, this.options.cancel) ||\n $.is(e.currentTarget, this.options.cancel) ||\n $.closest(e.target, this.options.cancel, this.node).length\n );\n\n if (\n e.button ||\n !this.#enabled ||\n cancelled ||\n $.getDataset(this.node, 'uiDragging')\n ) {\n return false;\n }\n\n this.#target = this.options.handle ?\n this.items().find((node) => $.hasDescendent(node, e.currentTarget)) :\n e.currentTarget;\n\n if (!this.#target) {\n return false;\n }\n };\n\n const moveEvent = (e) => {\n if (!this.node || !this.#target) {\n return;\n }\n\n const previousPos = this.#pos;\n this.#pos = getPosition(e);\n const deltaX = previousPos ?\n this.#pos.x - previousPos.x :\n 0;\n const deltaY = previousPos ?\n this.#pos.y - previousPos.y :\n 0;\n\n if (!this.#dragging) {\n this.#dragging = true;\n this.#currentSortable = this.node;\n this.#startIndex = this.items().indexOf(this.#target);\n this.#currentIndex = this.#startIndex;\n\n $.setDataset(this.node, { uiDragging: true });\n\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: this.node,\n to: this.node,\n oldIndex: this.#startIndex,\n newIndex: this.#startIndex,\n };\n\n triggerSortableEvent(this.node, 'sort', eventData);\n\n if (!this.node) {\n return;\n }\n\n if (this.options.helperClass) {\n $.addClass(this.#target, this.options.helperClass);\n }\n }\n\n this.#updatePlacement(deltaX, deltaY);\n this.#scheduleScroll();\n };\n\n const upEvent = (e) => {\n if (!this.node) {\n return;\n }\n\n if (!this.#dragging) {\n this.#target = null;\n return;\n }\n\n if (e.type === 'mouseup') {\n e.preventDefault();\n }\n\n const source = this.node;\n const destination = this.#currentSortable;\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: source,\n to: destination,\n oldIndex: this.#startIndex,\n newIndex: this.#currentIndex,\n };\n\n triggerSortableEvent(destination, 'sorted', eventData);\n\n if (!this.node) {\n return;\n }\n\n if (this.options.helperClass) {\n $.removeClass(this.#target, this.options.helperClass);\n }\n\n if (source !== destination) {\n triggerSortableEvent(source, 'update', eventData);\n\n if (this.node) {\n triggerSortableEvent(destination, 'update', eventData);\n }\n } else if (this.#currentIndex !== this.#startIndex) {\n triggerSortableEvent(destination, 'update', eventData);\n }\n\n if (this.node) {\n this.#resetDrag();\n }\n };\n\n const dragEvent = $.mouseDragFactory(downEvent, moveEvent, upEvent);\n\n $.addEventDelegate(\n this.node,\n 'mousedown.ui.sortable touchstart.ui.sortable',\n this.#selector,\n dragEvent,\n );\n\n $.addEvent(this.node, 'receive.ui.sortable', (_) => {\n setCursor($.find(this.#selector, this.node), this.options.cursor);\n });\n }\n\n /**\n * Resets the active drag state.\n */\n #resetDrag() {\n if (this.#scrollFrame !== null && this.#scrollFrame !== undefined) {\n window.cancelAnimationFrame(this.#scrollFrame);\n }\n\n if (this.#dragging && this.node) {\n $.removeDataset(this.node, 'uiDragging');\n }\n\n this.#currentIndex = null;\n this.#currentSortable = null;\n this.#dragging = false;\n this.#pos = null;\n this.#scrollFrame = null;\n this.#startIndex = null;\n this.#target = null;\n }\n\n /**\n * Schedules the next auto-scroll update.\n */\n #scheduleScroll() {\n if (\n !this.node ||\n !this.options.scroll ||\n !this.#dragging ||\n this.#scrollFrame !== null && this.#scrollFrame !== undefined\n ) {\n return;\n }\n\n this.#scrollFrame = window.requestAnimationFrame((_) => {\n this.#checkScroll();\n });\n }\n\n /**\n * Updates item placement and sorting events for the current pointer position.\n * @param {number} deltaX The horizontal pointer movement.\n * @param {number} deltaY The vertical pointer movement.\n */\n #updatePlacement(deltaX, deltaY) {\n let nearestSortable = this.node;\n\n if (this.options.connectWith) {\n const connected = $.find(this.options.connectWith)\n .filter((node) => node !== this.node)\n .filter((node) => {\n const sortable = this.constructor.init(node);\n\n return sortable.#enabled &&\n (sortable.options.dropOnEmpty || sortable.items().length);\n });\n\n if (connected.length) {\n nearestSortable = $.nearestTo(\n [this.node, ...connected],\n this.#pos.x,\n this.#pos.y,\n { offset: true },\n );\n }\n }\n\n const nearestInstance = this.constructor.init(nearestSortable);\n const nearestItems = nearestInstance.items()\n .filter((node) => node !== this.#target);\n const nearestItem = $.nearestTo(\n nearestItems,\n this.#pos.x,\n this.#pos.y,\n { offset: true },\n );\n\n if (nearestItem) {\n this.#updateTarget(\n nearestItem,\n this.#pos.x,\n this.#pos.y,\n deltaX,\n deltaY,\n );\n } else if (nearestSortable !== this.#currentSortable) {\n $.append(nearestSortable, this.#target);\n } else {\n return;\n }\n\n const newIndex = nearestInstance.items().indexOf(this.#target);\n\n if (nearestSortable !== this.#currentSortable) {\n const previousSortable = this.#currentSortable;\n const previousInstance = this.constructor.init(previousSortable);\n const { handle } = previousInstance.options;\n\n resetCursor(handle ?\n $.find(handle, this.#target) :\n [this.#target]);\n\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: previousSortable,\n to: nearestSortable,\n oldIndex: this.#currentIndex,\n newIndex,\n };\n\n triggerSortableEvent(previousSortable, 'send', eventData);\n\n if (!this.node) {\n return;\n }\n\n triggerSortableEvent(nearestSortable, 'receive', eventData);\n } else if (this.#currentIndex !== newIndex) {\n /** @type {SortableEventData} */\n const eventData = {\n item: this.#target,\n from: nearestSortable,\n to: nearestSortable,\n oldIndex: this.#currentIndex,\n newIndex,\n };\n\n triggerSortableEvent(nearestSortable, 'sorting', eventData);\n }\n\n if (!this.node) {\n return;\n }\n\n this.#currentIndex = newIndex;\n this.#currentSortable = nearestSortable;\n }\n\n /**\n * Moves the target relative to the nearest item.\n * @param {HTMLElement} nearestItem The nearest item.\n * @param {number} x The mouse X position.\n * @param {number} y The mouse Y position.\n * @param {number} deltaX The horizontal pointer movement.\n * @param {number} deltaY The vertical pointer movement.\n */\n #updateTarget(nearestItem, x, y, deltaX, deltaY) {\n const nearestCenter = $.center(nearestItem, { offset: true });\n const nearestRect = $.rect(nearestItem, { offset: true });\n const targetCenter = $.center(this.#target, { offset: true });\n const threshold = Math.min(1, Math.max(0, this.options.swapThreshold));\n let thresholdX = 0.5;\n let thresholdY = 0.5;\n\n if (deltaX) {\n thresholdX = deltaX > 0 ?\n threshold :\n 1 - threshold;\n }\n\n if (deltaY) {\n thresholdY = deltaY > 0 ?\n threshold :\n 1 - threshold;\n }\n\n let method;\n if (this.options.axis === 'y' || nearestCenter.x === targetCenter.x) {\n const swapY = nearestRect.top + nearestRect.height * thresholdY;\n method = y < swapY ?\n 'before' :\n 'after';\n } else if (this.options.axis === 'x' || nearestCenter.y === targetCenter.y) {\n const swapX = nearestRect.left + nearestRect.width * thresholdX;\n method = x < swapX ?\n 'before' :\n 'after';\n } else {\n const swapX = nearestRect.left + nearestRect.width * thresholdX;\n const swapY = nearestRect.top + nearestRect.height * thresholdY;\n method = x < swapX && y < swapY ?\n 'before' :\n 'after';\n }\n\n $[method](nearestItem, this.#target);\n }\n}\n","import { initComponent } from '@fr0st/ui';\nimport Sortable from './sortable.js';\n\ninitComponent('sortable', Sortable);\n\nexport default Sortable;\n"],"mappings":"0GAIA,IAAM,EAAc,IAAI,QAMxB,SAAgB,EAAY,EAAO,CAC/B,IAAK,IAAM,KAAQ,EAAO,CACtB,IAAM,EAAU,EAAY,IAAI,CAAI,EAE/B,IAIL,EAAQ,EACR,EAAY,OAAO,CAAI,EAC3B,CACJ,CAOA,SAAgB,EAAU,EAA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