annotate src/alternatives/draggabilly.html @ 11:80ef70ca5860

add onStart
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 11 May 2023 14:37:10 -0600
parents eee8862be4c7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 <!doctype html>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 <html>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 <head>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 <meta name="viewport" content="width=device-width, initial-scale=1">
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 <style>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 @import "/site.css";
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 div[drag] {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 border: 1px solid;
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 margin: 8px;
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 padding: 8px;
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 background-color: LightGreen;
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
13 cursor: move;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
14 }
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
15 div[drag].is-dragging {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
16 z-index: 1;
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 </style>
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
19 <script src="https://unpkg.com/draggabilly@3.0.0/dist/draggabilly.pkgd.js"></script>
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 <script>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 'use strict';
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
23 let dragEntered;
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
25 function onDragStart( event, pointer ) {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
26 dragEntered = null;
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 function indexOf(a,el) {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 for( let i=0; i<a.length; i++ ) {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 if( a[i] === el )
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 return i;
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return -1;
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
37 function onDragEnter(dragging,entered) {
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 let divs = document.querySelectorAll('div[drag]');
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 let iEntered = indexOf(divs,entered);
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 let iDragging = indexOf(divs,dragging);
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 console.log(iDragging+' '+iEntered);
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 let parent = entered.parentNode;
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 if( iDragging < iEntered ) {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 let next = divs[iEntered+1];
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 if( next ) {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 parent.insertBefore( dragging, next );
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 } else {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 parent.appendChild( dragging );
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 } else {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 parent.insertBefore( dragging, entered );
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
55 function onDragMove( event, pointer, moveVector ) {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
56 let target = pointer.target;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
57 let x = pointer.clientX;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
58 let y = pointer.clientY;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
59 let divs = document.querySelectorAll('div[drag]');
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
60 for( let i=0; i<divs.length; i++ ) {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
61 let div = divs[i];
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
62 if( div === target )
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
63 continue;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
64 let rect = div.getBoundingClientRect();
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
65 if( rect.x <= x && x <= rect.x+rect.width && rect.y <= y && y <= rect.y+rect.height ) {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
66 if( div !== dragEntered ) {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
67 dragEntered = div;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
68 onDragEnter(target,div);
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
69 }
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
70 break;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
71 }
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
72 }
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
73 }
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
74
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
75 function onDragEnd( event, pointer ) {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
76 //console.log('onDragEnd');
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
77 //console.log(event);
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
78 //console.log(pointer);
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
79 let target = pointer.target;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
80 //target.style.left = null;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
81 //target.style.top = null;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
82 target.draggie.setPosition(0,0);
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
83 //console.log(target.draggie.position);
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
84 }
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
85
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 function init() {
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
87 let divs = document.querySelectorAll('div[drag]');
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
88 for( let i=0; i<divs.length; i++ ) {
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
89 let div = divs[i];
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
90 let draggie = new Draggabilly(div,{});
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
91 draggie.on( 'dragStart', onDragStart );
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
92 draggie.on( 'dragMove', onDragMove );
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
93 draggie.on( 'dragEnd', onDragEnd );
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
94 div.draggie = draggie;
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
95 }
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 </script>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 </head>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 <body>
2
264646feb67b add draggabilly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
100 <h1><a href="https://draggabilly.desandro.com/">Draggabilly</a></h1>
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101
9
eee8862be4c7 update text
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
102 <p>This supports drag but not drop. When I try to implement drop in a list, it gets confused when the DOM changes. The library is in <a href="https://unpkg.com/draggabilly@3.0.0/dist/draggabilly.pkgd.js">draggabilly.pkgd.js</a>.</p>
1
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 <div>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 <script>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 for( let i=1; i<=10; i++ ) {
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 document.write(`
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 <div drag id="${i}">${i} - drag me</div>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 ` );
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 }
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 </script>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 </div>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 </body>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 <script> init(); </script>
9c372fce698a start alternatives
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 </html>