-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmv-sorter.js
More file actions
2323 lines (1809 loc) · 59.3 KB
/
Copy pathmv-sorter.js
File metadata and controls
2323 lines (1809 loc) · 59.3 KB
1
2
3
4
5
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const log = console.log.bind(console);
function xlog(...logargs) {
console.log(...logargs);
const $log = document.getElementById('mv-debug');
if (!$log) return;
$log.insertAdjacentText('beforeend', logargs.join(" ")+"\n");
}
class MvSorter extends HTMLElement {
static is = 'mv-sorter';
static properties = {
row: {
type: Boolean,
observer: 'dir_changed',
},
column: {
type: Boolean,
observer: 'dir_changed',
},
lock: {
type: Boolean,
observer: 'lock_dir_changed',
},
group: {
type: String,
},
autosave: {
type: Boolean,
},
disabled: {
type: Boolean,
},
}
static _monostate = {
animQueue: new Set(),
items: new Map(),
containers: new Set(),
dirty: new Set(),
render_jobs: new Map(),
last_grabbed: null,
warned_container_resize: false,
}
/*
* Commits all items in current container, affecting other
* containers if items in this container has been moved to/from other
* container.
*/
commit() {
if (!this.is_altered) return;
if (1) { //debug
const els_old = [...this.children].map($el => MvSorter.desig($el));
const els_new = this.elements().map($el => MvSorter.desig($el));
//log("Commit", this.id, els_old, "=>", els_new);
}
this.replaceChildren(... this.elements());
}
/**
* Returns an array of elements currently placed in this container.
*/
elements() {
//log("homes", this.id, this.mv.homes);
return this.mv.homes.slice(0);
}
/**
* Is true if container has unsaved (not commited) changes
*/
get is_altered() {
const homes = this.mv.homes;
const children = this.children;
if (homes.length !== children.length) return true;
for (let i = 0; i < homes.length; i++ ) {
if (homes[i] !== children[i]) return true;
}
return false;
}
/*
* Returns an array of elements moved from this container.
*/
elements_removed() {
const mono = this.mv.monostate;
const items = mono.items;
const list = [];
const children = this.mv.$slot.assignedNodes();
for (let child of children) {
if (child.nodeType !== Node.ELEMENT_NODE) continue;
const item = items.get(child);
if (!item) continue; // may not track all elements
if (item.container === this) continue;
list.push(child);
}
return list;
}
/*
* Returns an array of elements moved to this container.
*/
elements_added() {
const mono = this.mv.monostate;
const items = mono.items;
const list = [];
const children = this.mv.$slot.assignedNodes();
for (const el of this.mv.homes) {
if( !children.includes(el) ) list.push(el);
}
return list;
}
/**
* Returns the original container this element belongs to, before
* reordering.
*/
element_origin(target) {
const mono = this.mv.monostate;
const items = mono.items;
const item = items.get(target);
if (!item) return null;
return target.parentElement;
}
/**
* Returns the current container this element belongs to,
* acknowledging reordering.
*/
element_home(target) {
const mono = this.mv.monostate;
const items = mono.items;
const item = items.get(target);
if (!item) return null;
return item.container;
}
/**
* Restores the containers items to the default position,
* returning and giving back elements from other containers.
*/
reset() {
const seen = new Set();
const mono = this.mv.monostate;
const items = mono.items;
// TODO: animate
const homes_new = [];
const children = this.mv.$slot.assignedNodes();
for (let target of children) {
if (seen.has(target)) continue;
seen.add(target);
const item = items.get(target);
// TODO: re-evaluate element for tracking
if (!item) continue; // may not track all elements
const old_container = item.container;
const old_idx = item.idx;
mono.dirty.add(old_container);
const idx = homes_new.length; // next idx
item.idx = idx;
item.container = this; // FIXME: move things in old container
homes_new[idx] = target;
if (old_container !== this) {
//log(`deleting ${old_container.id}.${old_idx}`);
old_container.mv.homes.splice(old_idx, 1);
old_container.reindex();
}
// const desig = target.id || target.innerText || target.nodeName;
//log(`${desig} ${old_container.id}.${old_idx} -> ${this.id}.${idx}`);
}
for (let target of this.mv.homes) {
if (seen.has(target)) continue;
seen.add(target);
const item = items.get(target);
// TODO: re-evaluate element for tracking
if (!item) continue; // may not track all elements
const orig_container = target.parentElement;
const old_container = item.container; // should always be 'this'
const old_idx = item.idx;
if (!orig_container) {
// target could have been recently removed
//log(`${desig} ${old_container.id}.${old_idx} -> removed`);
old_container.mv.homes.splice(old_idx, 1);
// no need to reindex. homes_new set after this
item.idx = null;
continue;
}
mono.dirty.add(orig_container);
const idx = orig_container.mv.homes.length;
item.idx = idx;
item.container = orig_container;
orig_container.mv.homes[idx] = target;
//log(`${desig} ${old_container.id}.${old_idx} -> ${orig_container.id}.${idx}`);
}
this.mv.homes = homes_new;
MvSorter.items_moved();
}
static get template() {
return html`
<style>
:host { display: flex }
main {
display: flex;
flex-basis: 100%;
gap: var(--gap); /* FIXME: add gap in distribution calc */
min-height: 20px;
min-width: 20px;
transition: min-height .5s, min-width .5s;
}
#dropzone {
pointer-events:none;
position: absolute;
/* outline: 3px dashed hsla(0, 0%, 0%, 0.5); */
outline: var(--dropzone-outline, 3px dashed hsla(0, 0%, 0%, 0.5));
outline-offset: var(--dropzone-outline-offset, -6px);
background: var(--dropzone-background);
height: 100px;
width: 100px;
opacity: 0;
transition: opacity .2s;
}
</style>
<main><slot></slot></main>
`;
}
constructor() {
super();
const $root = this.attachShadow({ mode: "open" });
$root.innerHTML = MvSorter.template;
const mv = this.mv = {}; // put our data here
const mono = mv.monostate = MvSorter._monostate;
mono.containers.add(this);
mv.id = mono.containers.size;
mv.homes = [];
this.register_attributes();
// this.constructor.throttled_move =
// throttle(this.constructor.throttled_move_handler, 500, { trailing: false });
// this.constructor.throttled_anim =
// throttle(this.constructor.throttled_anim_handler, 5000, { trailing: false });
/*
100 ms was to short for catching both intersection observer
and resize observer.
*/
this.constructor.debounced_items_moved =
debounce(MvSorter.items_moved, 200);
// Timing based on transition animation time
mv.debounced_container_moved = debounce(()=>{
this.container_moved();
MvSorter.items_moved();
},500);
//# Global listeners will only be added once, since they are identical
//# global touch handlers
window.addEventListener('touchmove', MvSorter.touchmove_handler);
window.addEventListener('touchstart', MvSorter.touchstart_handler, { passive: false });
window.addEventListener('touchend', MvSorter.touchend_handler, { passive: false });
//# global mouse handlers
window.addEventListener('mousemove', MvSorter.mousemove_handler);
window.addEventListener('mouseup', MvSorter.mouseup_handler);
}
connectedCallback() {
// super.connectedCallback();
// log('[+]', MvSorter.desig(this) );
const mv = this.mv; // put our data here
const $main = mv.$main = this.$$('main');
mv.$slot = this.$$('slot');
mv.$dropzone = this.$$("#dropzone");
this.properties_reactions();
mv.mutation_observer = new MutationObserver(this.nodes_changed.bind(this));
mv.mutation_observer.observe(this, { childList: true });
mv.debounced_domchange = debounce(this.domchange_handler.bind(this), 100);
// const io = new IntersectionObserver(mv.debounced_domchange);
// io.observe(this);
mv.resize_observer = new ResizeObserver(this.container_moved.bind(this));
mv.parent_target = null; // update on dom change
mv.gap = parseFloat(getComputedStyle(mv.$main).gap) || 0;
this.dir_changed();
this.lock_dir_changed();
this.add_dropzone();
this.domchange_handler();
// this.container_moved();
this.container_rect_changed();
//mv.$drag_image = document.createElement('div');
// mv.bound_tap_handler = this.tap_handler.bind(this);
mv.bound_dragstart_handler = this.dragstart_handler.bind(this);
this.addEventListener('click', mv.bound_tap_handler);
this.addEventListener('dragstart', mv.bound_dragstart_handler);
}
// Disconnection happens during movement. Keep things around in case they
// will be attached again.
disabled_disconnectedCallback() {
const mono = this.mv.monostate;
const items = mono.items;
//log('[-]', this.id);
// Setting display to none should make offsetParent return null
// which will disable updates during our removal of the children here
// this.style.display = 'none';
if (this.mv.parent_target) {
//log('removing parent', this.mv.parent_target);
this.mv.parent_target = null;
const parent = items.get(this.mv.parent_target);
if (parent) parent.children_containers.delete(this);
}
const children = this.mv.$slot.assignedNodes();
for (let target of children.reverse()) {
this.remove_item(target);
}
// super.disconnectedCallback();
}
// Place all items for this container based on its new dimensions.
// optimized for frequent calls
// FROM: ResizeObserver, domchange_handler, animScaleCrash
container_moved(ev) {
const mono = this.mv.monostate;
const items = mono.items;
// log(`container_moved ${this.id}`);
// Do not move children to animated parents
if (this.mv.parent_target) {
const parent = items.get(this.mv.parent_target);
//log(`container ${this.mv.id} has parent ${parent._id}`);
if (parent.grabbed) return;
const at_home = parent.X.pos == parent.X.pos_home && parent.Y.pos == parent.Y.pos_home;
if (!at_home) return;
}
let hurry = false;
mono.dirty.add(this);
for (const child of this.children ) {
const item = items.get(child);
if (!item) continue; // may not track all elements
for (let child_cont of item.children_containers) {
child_cont.container_moved();
}
if (item.container === this) continue;
mono.dirty.add(item.container);
hurry = true;
}
this.mv.gap = parseFloat(getComputedStyle(this.mv.$main).gap) || 0;
// this.mv.gap = 0;
// log("gap", this.mv.gap);
if (mono.render_jobs.has('items_moved')) { } // on my way
else if (hurry) {
//log('hurry', this.mv.id);
mono.render_jobs.set('items_moved', setTimeout(MvSorter.items_moved));
}
else if (mono.dirty.size) { // soon
MvSorter.debounced_items_moved();
}
}
// FROM: container_moved, nodes_changed
static items_moved() {
const mono = MvSorter._monostate;
// log('items moved');
mono.render_jobs.delete('items_moved');
for (let container of mono.dirty) {
// log(`items_moved in ${container.mv.id} ${container.id}`);
container.container_rect_changed();
}
for (let container of mono.dirty) {
mono.dirty.delete(container);
container.render_items();
}
}
// FROM: items_moved
render_items() {
const container = this;
const mv = container.mv;
const mono = mv.monostate;
const items = mono.items;
// Skip if container not currently visible
if (!this.offsetParent) return;
const ax = this.axisAB();
let oA = mv[ax.A].a + mv[ax.A].p1;
let oB = mv[ax.B].a + mv[ax.B].p1;
// log(`${this.mv.id} render_items at (${oA},${oB})`);
for (let target of mv.homes) {
if (!target.parentElement) continue; // recently removed
const item = items.get(target);
const A = item[ax.A];
const B = item[ax.B];
const X = item.X;
const Y = item.Y;
const at_home = A.pos == A.pos_home && B.pos == B.pos_home;
container.item_rect_changed(target);
oA += A.m1;
A.pos_home = Math.round(oA - item[ax.A].offset);
B.pos_home = Math.round(oB + B.m1 - item[ax.B].offset);
//log(`render ${target.id} ${container.mv.id}.${item.idx} : (${Math.round(X.pos)},${Math.round(Y.pos)}) -> (${X.pos_home},${Y.pos_home})`);
if (item.grabbed) { }
else if (at_home) {
A.pos = A.pos_end = A.pos_home;
B.pos = B.pos_end = B.pos_home;
} else {
item.container.update_dropzone(target);
// anim both since we check pos in animScaleCrash
for (let axis of ['X', 'Y']) {
item[axis].pos_end = item[axis].pos_home;
MvSorter.addAnim('pos' + axis, MvSorter.animPosFall(target, axis), target);
}
}
const translate = `translate(${X.pos}px, ${Y.pos}px) `;
target.style.transform = translate;
//log( translate );
//target.style.visibility = 'visible'; // TEST
oA += A.size + A.m2 + mv.gap;
}
// log("render_items", container.id, oA);
}
// FROM: add_item
position_item_last(target) {
const container = this;
const mv = container.mv;
const mono = mv.monostate;
const items = mono.items;
if (mv.homes.length < 2) return;
const ax = this.axisAB();
const last = mv.homes[mv.homes.length - 2];
const l_item = items.get(last);
const item = items.get(target);
const A = item[ax.A];
const B = item[ax.B];
const gA = l_item[ax.A].offset + l_item[ax.A].pos_home;
const oA = gA + l_item[ax.A].size + l_item[ax.A].m2 + mv.gap + A.m1;
const gB = l_item[ax.B].offset + l_item[ax.B].pos_home;
const oB = gB - l_item[ax.B].m1 + B.m1;
A.pos = A.pos_end = A.pos_home = Math.round(oA - A.offset);
B.pos = B.pos_end = B.pos_home = Math.round(oB - B.offset);
const X = item.X;
const Y = item.Y;
//log(`${container.mv.id} ${container.id} add_item ${target.id} (${X.pos},${Y.pos})`);
const translate = `translate(${X.pos}px, ${Y.pos}px) `;
target.style.transform = translate;
}
dir_changed() {
let dir = 'row';
if (this.column) dir = 'column';
if (this.row) dir = 'row';
this.mv.direction = dir;
//log(`dir_changed to ${dir}`);
this.mv.$main.style['flex-direction'] = dir;
}
lock_dir_changed() {
//log(`lock_dir_changed to ${this.lock} ${this.direction}`);
if (this.lock && this.mv.direction == 'row') {
this.mv.axes = ['X'];
} else if (this.lock && this.mv.direction == 'column') {
this.mv.axes = ['Y'];
} else {
this.mv.axes = ['X', 'Y'];
}
}
static axis_map = {
row: {
A: 'X',
B: 'Y',
inline: 'width',
min_inline: 'min-width',
offset: 'offsetWidth',
},
col: {
A: 'Y',
B: 'X',
inline: 'height',
min_inline: 'min-height',
offset: 'offsetHeight',
},
}
axisAB() {
if (this.mv.direction == 'row') return MvSorter.axis_map.row;
return MvSorter.axis_map.col;
}
static axisB(axis) {
return axis == 'X' ? 'Y' : 'X';
}
// FROM: MutationObserver
nodes_changed(mutations) {
const mono = this.mv.monostate;
// log("Container", this.mv.id, 'nodes_changed', mutations);
// mono.dirty.add(this);
// return MvSorter.items_moved();
const removed = new Set();
for (const mutation of mutations) {
for ( const $el of mutation.removedNodes) {
removed.add($el);
}
}
for (const $el of this.children) {
removed.delete($el);
}
for (const $el of removed) {
const item = mono.items.get($el);
if (!item) continue;
mono.dirty.add(item.container);
// log("removed", item._id, item.container);
}
for (const cont of mono.dirty) {
if (cont === this) continue;
//log("sub-commit", cont.id);
cont.commit();
}
mono.dirty.add(this);
this.domchange_handler();
MvSorter.items_moved();
}
add_item(target) {
// log("add_item...");
if (target.nodeType !== Node.ELEMENT_NODE) return;
if (target.id == 'dropzone') return;
if (!target.offsetParent) return; // element hidden
const mono = this.mv.monostate;
if (mono.items.has(target)) return;
if (target.tagName.startsWith("DOM-") ) return;
//log("add_item for", this.id, ":", MvSorter.desig(target));
const $handle = target.querySelector("MV-DRAGHANDLE") || target;
$handle.draggable = true;
const X = {
scale: 1, // multiplier for transform
pos: 0, // relative its static position
pos_end: 0, // current target of movement
pos_home: 0, // resting place
pos_speed: 0, // pixels per frame
offset: 0, // relative page
offset_handle: 0, // relative target
t_origin: 0, // transform origin
size: 0, // size excluding margins
grab: 0, // half the size of the handle
m_size: 0, // size including margins
m1: 0, // first margin
m2: 0, // second margin
rotate: 0, // in radians
turned: false, // used by animPosFall
crashed: false, // used by animScaleCrash
};
const Y = Object.assign({}, X);
const idx = this.mv.homes.length; // next idx
// Polymer dom-repeat reuses elements. Do not assume that the
// logical identity of the item goes unchanged. No id stuff here.
const item = {
X, Y,
grabbed: false,
throwed: false,
animQueue: new Map(),
children_containers: new Set(),
idx: idx,
container: this,
get _id() { return MvSorter.desig(target) },
};
this.mv.homes[idx] = target;
mono.items.set(target, item);
//log('added to items', target);
this.item_rect_changed(target);
this.position_item_last(target);
this.mv.resize_observer.observe(target);
}
// FROM: render_items, add_item, item_drag_start, container_rect_changed
item_rect_changed(target) {
/* Keep last known value if target is not currently visible. */
if (!target.offsetParent) return;
const item = this.mv.monostate.items.get(target);
const X = item.X;
const Y = item.Y;
const transform = target.style.transform;
target.style.transform = "";
const rect = target.getBoundingClientRect(); // original dimensions
target.style.transform = transform;
//log( item._id, rect );
X.pos_mid = rect.width / 2;
Y.pos_mid = rect.height / 2;
// Excluding margins
X.size = rect.width;
Y.size = rect.height;
const X_prev = X.offset;
const Y_prev = Y.offset;
const X_new = window.scrollX + rect.left;
const Y_new = window.scrollY + rect.top;
const c = getComputedStyle(target);
X.m1 = parseFloat(c.marginLeft);
X.m2 = parseFloat(c.marginRight);
Y.m1 = parseFloat(c.marginTop);
Y.m2 = parseFloat(c.marginBottom);
// Adding the gap to the item with would also have to consider
// item position in container. Will instead add gap in
// find_dropzone()
// m_size is used in find_cropzone()
X.m_size = X.m1 + X.size + X.m2;
Y.m_size = Y.m1 + Y.size + Y.m2;
// rounding values will move some items 1px
X.offset = X_new;
Y.offset = Y_new;
// for DEBUG
if (false) { // X_prev !== X.offset || Y_prev !== Y.offset ){
//log(`*** ${this.id} ${MvSorter.desig(target)} (${Math.round(X_prev)},${Math.round(Y_prev)})-> (${Math.round(X_new)},${Math.round(Y_new)}) `);
log(`${this.id} ${MvSorter.desig(target)} Size ${X.m_size},${Y.m_size}`);
}
this.handle_rect_changed(target);
X.t_origin = X.pos + X.offset_handle + X.grab;
Y.t_origin = Y.pos + Y.offset_handle + Y.grab;
//log( item );
}
// item_rect_changed,
handle_rect_changed(target) {
const mono = this.mv.monostate;
const items = mono.items;
const item = items.get(target);
if (!item) return;
const handle = item.handle;
if (!handle) return;
if (handle == target) {
item.X.offset_handle = 0;
item.Y.offset_handle = 0;
// console.warn("handle_rect_changed");
item.X.grab = mono.grab_x;
item.Y.grab = mono.grab_y;
// item.X.grab = item.X.size / 2;
// item.Y.grab = item.Y.size / 2;
return;
}
if (!handle.offsetParent) return;
//log('offset_handle update');
const rect = handle.getBoundingClientRect();
item.X.offset_handle = rect.x + window.scrollX - item.X.offset - item.X.pos;
item.Y.offset_handle = rect.y + window.scrollY - item.Y.offset - item.Y.pos;
//log('Y', rect.y, window.scrollY, item.Y.offset );
item.X.grab = rect.width / 2;
item.Y.grab = rect.height / 2;
}
remove_item($target) {
const mono = this.mv.monostate;
const items = mono.items;
const item = items.get($target);
if (!item) return;
//log('remove_item disabled'); return;
const container = item.container;
mono.dirty.add(container);
if (!Number.isInteger(item.idx)) {
//log(`${item._id} was removed from ${container.id}`);
}
else {
//log(`Removed ${item._id} ${container.id}.${item.idx}`);
container.mv.homes.splice(item.idx, 1);
container.reindex();
}
for (let child_cont of item.children_containers) {
child_cont.mv.parent_target = null;
}
item.children_containers.clear();
this.mv.resize_observer.unobserve($target);
items.delete($target);
log( 'Removal', this.id, item._id );
MvSorter.debounced_items_moved();
}
dragstart_handler(ev) {
const $target = this.find_target(ev.target);
//# invisible drag image not supported cross-browser. Have to use
//# preventDefault and listen to mouseup or hide the image while
//# its handled in window, by displacing it. But since dragging
//# stops mousemove events, we can't use drag api at all.
//ev.dataTransfer.setDragImage(this.mv.$drag_image, 0, 0);
//ev.dataTransfer.setDragImage($target, -99999, -99999);
ev.preventDefault();
const $handle = $target.querySelector("MV-DRAGHANDLE");
//log('drag start', ev, ev.target, $target, $handle);
this.item_drag_start($target, $handle, {
x: ev.clientX,
y: ev.clientY,
});
}
static touchstart_handler(ev) {
if (ev.touches[1]) return true;
//# Get the original target since we listen on the window
const $path0 = ev.composedPath()[0];
const $target = MvSorter.find_target($path0);
// log('touchstart', $target, ev.target);
if (!$target) return true;
const $handle = $target.querySelector("MV-DRAGHANDLE");
if ($handle) if (!$path0.closest("MV-DRAGHANDLE")) return true;
const touche = ev.touches[0];
ev.stopPropagation();
ev.preventDefault();
const $cont = $target.closest("mv-sorter");
$cont.item_drag_start($target, $handle, {
x: touche.clientX,
y: touche.clientY,
});
}
item_drag_start($target, $handle, grabpoint) {
const mono = this.mv.monostate;
const item = mono.items.get($target);
if (!item) return;
if (item.grabbed) return; // already grabbed?
if (this.disabled) return;
item.handle = $handle ?? $target;
item.grabbed = true;
// log('item_drag_start', item._id, $target);
mono.last_grabbed = $target;
const rect = $target.getBoundingClientRect();
mono.grab_x = grabpoint.x - rect.x;
mono.grab_y = grabpoint.y - rect.y;
// TODO: Only do handle_rect_changed when needed
this.item_rect_changed($target);
item.throwed = false;
// Calculate scale for hovering
const size = Math.max(item.X.size, item.Y.size);
const add = 0.01 * size + 5;
const scale = (size + add) / size;
MvSorter.addAnim('scale', MvSorter.animScale($target, scale, 200), $target);
$target.classList.add('moved');
this.classList.add('moving-child');
document.body.classList.add('mv-moving-child');
this.mv.$main.style['user-select'] = 'none';
if (window.getSelection) window.getSelection().removeAllRanges();
if (this.mv.parent_target) {
//log('z-index', 2, this.mv.parent_target.id);
this.mv.parent_target.style['z-index'] = 2;
}
for (let axis of this.mv.axes) {
const A = item[axis];
A.turned = false;
A.crashed = false;
A.pos_start = A.pos; // Only defined if axis used
MvSorter.addAnim('pos' + axis, MvSorter.animPosDrag($target, axis), $target);
}
}
dragend_handler(ev) {
const $target = this.find_target(ev.target);
this.item_drag_end($target);
}
static touchend_handler(ev) {
const $target = MvSorter.find_target(ev.composedPath()[0]);
// log("touchend", $target, ev.target);
if (!$target) return true;
const $cont = $target.closest("mv-sorter");
if (!$cont.item_drag_end($target)) return;
ev.stopPropagation();
ev.preventDefault();
}
touchcancel_handler(ev) {
// log('cancel');
}
item_drag_end(target) {
const item = this.mv.monostate.items.get(target);
if (!item || !item.grabbed) return;
//log(`item_drag_end ${item._id} (${item.X.pos_end},${item.Y.pos_end}) --> (${item.X.pos_home},${item.Y.pos_home}) `);
item.throwed = true;
item.grabbed = false;
this.mv.monostate.last_grabbed = null;
this.classList.remove('moving-child');
document.body.classList.remove('mv-moving-child');
this.mv.$main.style['user-select'] = '';
if (this.mv.parent_target) {
//log('z-index', 1, this.mv.parent_target.id);
this.mv.parent_target.style['z-index'] = 1;
}
// Boost throw, from 1 to 4 times
const speed2 = (item.X.pos_speed * item.X.pos_speed + item.Y.pos_speed * item.Y.pos_speed) / 20;
let boost = 1;
if (speed2 < 1) boost = 4 ** speed2;
else boost = (3 + speed2) / speed2;
// log("drop speed", item._id, speed2, boost, item.X.pos_speed, item.X.pos_speed);
for (let axis of this.mv.axes) {
const axB = MvSorter.axisB(axis);
item[axis].pos_end = item[axis].pos_home;
// Boost throw
item[axis].pos_speed *= boost;
MvSorter.addAnim('pos' + axis, MvSorter.animPosFall(target, axis), target);
MvSorter.addAnim('rotate' + axB, MvSorter.animRotateRecover(target, axB), target);
}
return true;
}
static item_moved(target) {
const mono = MvSorter._monostate;
const item = mono.items.get(target);
/* Moved away from original home? */
if (!item.X.pos_home && !item.Y.pos_home) return;
const $cont = item.container;
const $orig = $cont.element_origin(target);
//log('item_moved', item._id, $orig.id, "=>", $cont.id );