uiik 1.1.0 → 1.3.0-alpha

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/sortable.js DELETED
@@ -1,441 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _Sortable_removeListenItems;
13
- /* eslint-disable max-len */
14
- /**
15
- * sortable
16
- * @author holyhigh2
17
- */
18
- import { each, map, flatMap, reject, size, toArray } from 'myfx/collection';
19
- import { alphaId } from 'myfx/utils';
20
- import { merge } from 'myfx/object';
21
- import { compact, findIndex } from 'myfx/array';
22
- import { split } from 'myfx/string';
23
- import { isEmpty, isFunction } from 'myfx/is';
24
- import { Uii } from "./types";
25
- import { lockPage, restoreCursor, saveCursor, unlockPage } from "./utils";
26
- const SORTABLE_GROUPS = {};
27
- const CLASS_SORTABLE_CONTAINER = "uii-sortable-container";
28
- const CLASS_SORTABLE_GHOST = "uii-sortable-ghost";
29
- const CLASS_SORTABLE_ACTIVE = "uii-sortable-active";
30
- const ATTR_SORTABLE_ACTIVE = "uii-sortable-active";
31
- const THRESHOLD = 2;
32
- /**
33
- * 用于表示一类排序容器的定义
34
- * > 可用CSS接口
35
- * - .uii-sortable-container
36
- * - .uii-sortable-ghost
37
- * - .uii-sortable-active
38
- * @public
39
- */
40
- export class Sortable extends Uii {
41
- constructor(container, opts) {
42
- super(container, merge({
43
- move: {
44
- from: true,
45
- to: true,
46
- },
47
- scroll: true,
48
- sort: true
49
- }, opts));
50
- _Sortable_removeListenItems.set(this, void 0);
51
- if (size(this.ele) > 1 && !this.opts.group) {
52
- this.opts.group = "uii_sortable_" + alphaId();
53
- }
54
- each(this.ele, (el) => {
55
- el.classList.add(CLASS_SORTABLE_CONTAINER);
56
- el.style.position = "relative";
57
- el.style.pointerEvents = "initial";
58
- bindContainer(this.registerEvent.bind(this), el, this.opts);
59
- });
60
- //put into group
61
- if (this.opts.group) {
62
- if (!SORTABLE_GROUPS[this.opts.group]) {
63
- SORTABLE_GROUPS[this.opts.group] = [];
64
- }
65
- SORTABLE_GROUPS[this.opts.group].push([this, this.ele]);
66
- }
67
- }
68
- /**
69
- * 调用active表示移出策略肯定是true | 'copy'
70
- * @internal
71
- */
72
- active(draggingItem, fromContainer, toContainers, toOpts) {
73
- var _a;
74
- //check move
75
- const moveFrom = (_a = toOpts.move) === null || _a === void 0 ? void 0 : _a.from;
76
- const acceptFn = isFunction(moveFrom) ? moveFrom : () => !!moveFrom;
77
- //验证移入策略
78
- const activableContainers = flatMap(toContainers, (el) => {
79
- const valid = acceptFn(draggingItem, fromContainer, el);
80
- return valid ? el : [];
81
- });
82
- each(activableContainers, (el) => {
83
- el.setAttribute(ATTR_SORTABLE_ACTIVE, "1");
84
- if (toOpts.activeClass) {
85
- each(split(toOpts.activeClass || "", " "), (cls) => {
86
- el.classList.toggle(cls, true);
87
- });
88
- }
89
- });
90
- __classPrivateFieldSet(this, _Sortable_removeListenItems, map(activableContainers, (con) => {
91
- const filteredItems = con.querySelectorAll(":scope > *");
92
- return listenItems(toOpts, con, draggingItem, filteredItems);
93
- }), "f");
94
- toOpts.onActive &&
95
- toOpts.onActive({ item: draggingItem, from: fromContainer });
96
- }
97
- /**
98
- * @internal
99
- */
100
- deactive(draggingItem, fromContainer, toContainers, opts) {
101
- each(toContainers, (el) => {
102
- el.removeAttribute(ATTR_SORTABLE_ACTIVE);
103
- if (opts.activeClass) {
104
- each(split(opts.activeClass || "", " "), (cls) => {
105
- el.classList.toggle(cls, false);
106
- });
107
- }
108
- });
109
- each(__classPrivateFieldGet(this, _Sortable_removeListenItems, "f"), (fn) => {
110
- fn();
111
- });
112
- opts.onDeactive &&
113
- opts.onDeactive({ item: draggingItem, from: fromContainer });
114
- }
115
- /**
116
- * @internal
117
- */
118
- onOptionChanged() { }
119
- }
120
- _Sortable_removeListenItems = new WeakMap();
121
- let DraggingData = null;
122
- function bindContainer(registerEvent, container, opts) {
123
- registerEvent(container, "mousedown", (e) => {
124
- var _a;
125
- let con = e.currentTarget;
126
- let t = e.target;
127
- if (t === con)
128
- return;
129
- // filter & handle
130
- const filterStr = opts.filter ? `:not(${opts.filter})` : "";
131
- const filteredItems = con.querySelectorAll(":scope > *" + filterStr);
132
- const handles = opts.handle
133
- ? map(filteredItems, (el) => el.querySelector(opts.handle || ""))
134
- : toArray(filteredItems);
135
- const i = findIndex(handles, (handle) => handle.contains(t));
136
- if (i < 0)
137
- return;
138
- const draggingItem = filteredItems[i];
139
- const ghostContainer = opts.ghostContainer || con;
140
- const onStart = opts.onStart;
141
- const onEnd = opts.onEnd;
142
- const ghostClass = opts.ghostClass;
143
- const group = opts.group;
144
- let moveTo = (_a = opts.move) === null || _a === void 0 ? void 0 : _a.to;
145
- const toCopy = moveTo === "copy";
146
- const toOutFn = isFunction(moveTo) ? moveTo : () => !!moveTo;
147
- const moveMode = toOutFn(draggingItem, con);
148
- const sort = opts.sort;
149
- const scroll = opts.scroll;
150
- const scrollSpeed = opts.scrollSpeed || 10;
151
- let hitPosX = e.offsetX + con.scrollLeft, hitPosY = e.offsetY + con.scrollTop;
152
- saveCursor();
153
- let dragging = false;
154
- let ghostNode = null;
155
- let removeListenItems = null;
156
- const dragListener = (ev) => {
157
- const newX = ev.clientX;
158
- const newY = ev.clientY;
159
- let offsetx = newX - hitPosX;
160
- let offsety = newY - hitPosY;
161
- if (!dragging) {
162
- if (Math.abs(offsetx) > THRESHOLD || Math.abs(offsety) > THRESHOLD) {
163
- dragging = true;
164
- ghostNode = draggingItem.cloneNode(true);
165
- ghostNode.style.opacity = "0.3";
166
- ghostNode.style.pointerEvents = "none";
167
- ghostNode.style.position = "fixed";
168
- ghostNode.style.zIndex = "999";
169
- ghostNode.style.left = draggingItem.style.left;
170
- ghostNode.style.top = draggingItem.style.top;
171
- if (ghostClass) {
172
- ghostNode.classList.add(...compact(split(ghostClass, " ")));
173
- }
174
- ghostNode.classList.toggle(CLASS_SORTABLE_GHOST, true);
175
- ghostContainer.appendChild(ghostNode);
176
- if (!toCopy)
177
- draggingItem.classList.toggle(CLASS_SORTABLE_ACTIVE, true);
178
- let copy = undefined;
179
- if (toCopy) {
180
- copy = draggingItem.cloneNode(true);
181
- copy.classList.toggle(CLASS_SORTABLE_ACTIVE, true);
182
- }
183
- DraggingData = {
184
- item: draggingItem,
185
- fromIndex: i,
186
- fromContainer: con,
187
- toContainer: con,
188
- moveTo: toCopy ? "copy" : moveMode,
189
- spill: opts.spill,
190
- copy
191
- };
192
- onStart && onStart({ item: draggingItem, from: con, index: i }, ev);
193
- lockPage();
194
- if (sort) {
195
- removeListenItems = listenItems(opts, con, toCopy ? draggingItem : copy, filteredItems, i);
196
- }
197
- //active
198
- if (moveMode && group && SORTABLE_GROUPS[group]) {
199
- each(SORTABLE_GROUPS[group], ([sortable, ele]) => {
200
- const filtered = reject(ele, (el) => el === container);
201
- if (isEmpty(filtered))
202
- return;
203
- sortable.active(toCopy ? draggingItem : copy, container, filtered, sortable.getOptions());
204
- });
205
- }
206
- }
207
- else {
208
- ev.preventDefault();
209
- return false;
210
- }
211
- }
212
- ghostNode.style.left = newX + "px";
213
- ghostNode.style.top = newY + "px";
214
- ev.preventDefault();
215
- return false;
216
- };
217
- const dragEndListener = (ev) => {
218
- var _a;
219
- document.removeEventListener("mousemove", dragListener);
220
- document.removeEventListener("mouseup", dragEndListener);
221
- window.removeEventListener("blur", dragEndListener);
222
- if (dragging) {
223
- unlockPage();
224
- restoreCursor();
225
- if (ghostNode)
226
- ghostContainer.removeChild(ghostNode);
227
- const toContainer = DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.toContainer;
228
- DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.item.classList.remove(CLASS_SORTABLE_ACTIVE);
229
- (_a = DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.copy) === null || _a === void 0 ? void 0 : _a.classList.remove(CLASS_SORTABLE_ACTIVE);
230
- DraggingData = null;
231
- if (removeListenItems)
232
- removeListenItems();
233
- //deactive
234
- if (group && SORTABLE_GROUPS[group]) {
235
- each(SORTABLE_GROUPS[group], ([sortable, ele]) => {
236
- const filtered = reject(ele, (el) => el === container);
237
- if (isEmpty(filtered))
238
- return;
239
- sortable.deactive(draggingItem, container, filtered, sortable.getOptions());
240
- });
241
- }
242
- onEnd && onEnd({ item: draggingItem, from: container, to: toContainer }, e);
243
- }
244
- };
245
- document.addEventListener("mousemove", dragListener);
246
- document.addEventListener("mouseup", dragEndListener);
247
- window.addEventListener("blur", dragEndListener);
248
- e.preventDefault();
249
- return false;
250
- });
251
- registerEvent(container, "mouseleave", (e) => {
252
- var _a, _b;
253
- if (!DraggingData)
254
- return;
255
- opts.onLeave &&
256
- opts.onLeave({
257
- item: DraggingData.item,
258
- from: DraggingData.fromContainer,
259
- to: container,
260
- }, e);
261
- if (DraggingData.moveTo !== 'copy') {
262
- if (DraggingData.spill === 'remove') {
263
- (_a = DraggingData.item.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(DraggingData.item);
264
- }
265
- else if (DraggingData.spill === 'revert') {
266
- (_b = DraggingData.item.parentElement) === null || _b === void 0 ? void 0 : _b.removeChild(DraggingData.item);
267
- const nextSibling = DraggingData.fromContainer.children[DraggingData.fromIndex];
268
- DraggingData.fromContainer.insertBefore(DraggingData.item, nextSibling);
269
- }
270
- }
271
- });
272
- //总是先触发容器enter,之后才是itementer
273
- registerEvent(container, "mouseenter", (e) => {
274
- var _a;
275
- if (!DraggingData)
276
- return;
277
- let draggingItem = DraggingData.item;
278
- let parent = draggingItem.parentElement;
279
- const cx = e.clientX;
280
- const cy = e.clientY;
281
- const rect = container.getBoundingClientRect();
282
- const centerX = rect.width / 2;
283
- const centerY = rect.height / 2;
284
- const offsetX = cx - rect.x;
285
- const offsetY = cy - rect.y;
286
- let dir = "";
287
- if (offsetX < centerX && offsetY < centerY) {
288
- dir = "tl";
289
- }
290
- else if (offsetX > centerX && offsetY > centerY) {
291
- dir = "br";
292
- }
293
- else if (offsetX < centerX && offsetY > centerY) {
294
- dir = "bl";
295
- }
296
- else if (offsetX > centerX && offsetY < centerY) {
297
- dir = "tr";
298
- }
299
- opts.onEnter &&
300
- opts.onEnter({
301
- item: DraggingData.item,
302
- from: DraggingData.fromContainer,
303
- to: container,
304
- dir,
305
- }, e);
306
- DraggingData.toContainer = container;
307
- if (container.getAttribute(ATTR_SORTABLE_ACTIVE)) {
308
- let valid = true;
309
- //check move
310
- const moveFrom = (_a = opts.move) === null || _a === void 0 ? void 0 : _a.from;
311
- const acceptFn = isFunction(moveFrom) ? moveFrom : () => !!moveFrom;
312
- valid = acceptFn(DraggingData.item, DraggingData.fromContainer, container);
313
- if (!valid)
314
- return;
315
- if (container.contains(draggingItem)) {
316
- return;
317
- }
318
- //此处检测移出策略
319
- const moveTo = DraggingData.moveTo;
320
- if (moveTo === "copy") {
321
- draggingItem = DraggingData.copy;
322
- }
323
- if (draggingItem.parentElement)
324
- draggingItem.parentElement.removeChild(draggingItem);
325
- let toIndex = 0;
326
- if (dir[0] === "t") {
327
- container.insertBefore(draggingItem, container.children[0]);
328
- }
329
- else {
330
- container.appendChild(draggingItem);
331
- toIndex = container.children.length - 1;
332
- }
333
- opts.onAdd &&
334
- opts.onAdd({
335
- item: draggingItem,
336
- from: DraggingData.fromContainer,
337
- to: container,
338
- index: toIndex,
339
- }, e);
340
- }
341
- else if (container === DraggingData.fromContainer) {
342
- if (DraggingData.copy) {
343
- let parent = DraggingData.copy.parentElement;
344
- if (parent)
345
- parent.removeChild(DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.copy);
346
- }
347
- else {
348
- if (draggingItem.parentElement)
349
- draggingItem.parentElement.removeChild(draggingItem);
350
- let toIndex = 0;
351
- if (dir[0] === "t") {
352
- container.insertBefore(draggingItem, container.children[0]);
353
- }
354
- else {
355
- container.appendChild(draggingItem);
356
- toIndex = container.children.length - 1;
357
- }
358
- }
359
- }
360
- });
361
- }
362
- function listenItems(opts, toContainer, draggingItem, items, fromIndex = 0) {
363
- //sorting listener
364
- const listener = (e) => {
365
- const ct = e.currentTarget;
366
- if (ct.style.transform) {
367
- return;
368
- }
369
- const toIndex = ct._uiik_i;
370
- let draggingItem = (DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.copy) || (DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.item);
371
- if (toContainer === (DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.fromContainer)) {
372
- draggingItem = DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.item;
373
- }
374
- let parent = draggingItem.parentElement;
375
- parent === null || parent === void 0 ? void 0 : parent.removeChild(draggingItem);
376
- const sameContainer = parent === ct.parentElement;
377
- if (!sameContainer) {
378
- parent = ct.parentElement;
379
- }
380
- const oldIndex = fromIndex;
381
- if (toIndex > fromIndex) {
382
- fromIndex = toIndex;
383
- parent === null || parent === void 0 ? void 0 : parent.insertBefore(draggingItem, ct.nextElementSibling);
384
- }
385
- else {
386
- fromIndex = toIndex - 1;
387
- parent === null || parent === void 0 ? void 0 : parent.insertBefore(draggingItem, ct);
388
- }
389
- opts.onChange &&
390
- opts.onChange({
391
- item: draggingItem,
392
- from: DraggingData === null || DraggingData === void 0 ? void 0 : DraggingData.fromContainer,
393
- to: toContainer,
394
- fromIndex: oldIndex,
395
- toIndex: fromIndex,
396
- }, e);
397
- const toPos = { x: ct.offsetLeft, y: ct.offsetTop };
398
- const fromPos = { x: draggingItem.offsetLeft, y: draggingItem.offsetTop };
399
- ct.style.transform = `translate3d(${fromPos.x - toPos.x}px,${fromPos.y - toPos.y}px,0)`;
400
- draggingItem.style.transform = `translate3d(${toPos.x - fromPos.x}px,${toPos.y - fromPos.y}px,0)`;
401
- draggingItem.offsetHeight;
402
- ct.offsetHeight;
403
- draggingItem.style.transition = "transform .15s";
404
- draggingItem.style.transform = `translate3d(0px,0px,0)`;
405
- ct.style.transition = "transform .15s";
406
- ct.style.transform = `translate3d(0px,0px,0)`;
407
- setTimeout(() => {
408
- ct.style.transition = "";
409
- ct.style.transform = ``;
410
- draggingItem.style.transition = "";
411
- draggingItem.style.transform = ``;
412
- }, 150);
413
- e.stopPropagation();
414
- e.preventDefault();
415
- };
416
- each(items, (item, i) => {
417
- item.style.position = "relative";
418
- if (item === draggingItem)
419
- return;
420
- item.style.pointerEvents = "initial";
421
- item._uiik_i = i;
422
- item.addEventListener("mouseenter", listener);
423
- });
424
- return () => {
425
- //解绑enter事件
426
- each(items, (item, i) => {
427
- if (item === draggingItem)
428
- return;
429
- item.removeEventListener("mouseenter", listener);
430
- });
431
- };
432
- }
433
- /**
434
- * make elements within the container sortable
435
- * @param container css selector or html element(array)
436
- * @param opts
437
- * @returns
438
- */
439
- export function newSortable(container, opts) {
440
- return new Sortable(container, opts);
441
- }