@fr0st/ui-sortable 2.0.4 → 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
|
@@ -1,323 +1,394 @@
|
|
|
1
|
-
(function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
})(this,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
})
|
|
323
|
-
|
|
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
|