FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
code-guild/code-guild/javascript/HTML5_drag.js at master · movemountains/code-guild · GitHub
movemountains
code-guild
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
code-guild
/
code-guild
/
javascript
/
HTML5_drag.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
77 lines (63 loc) · 2.12 KB
Breadcrumbs
code-guild
/
code-guild
/
javascript
/
HTML5_drag.js
Copy path
File metadata and controls
77 lines (63 loc) · 2.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//create a nodelist
var
boxes
=
document
.
querySelectorAll
(
'#drag-these .drag-and-drop'
)
;
//create an array to push the nodes into
var
boxesArray
=
[
]
;
//move nodes into an array
for
(
var
i
=
0
;
i
<
boxes
.
length
;
i
++
)
{
boxesArray
.
push
(
boxes
[
i
]
)
;
}
//used to store the element that drag initiates from
var
dragSourceElement
=
null
;
//event handler for the beginning of the drag
function
handleDragStart
(
e
)
{
//change opacity for feedback
this
.
style
.
opacity
=
'0.75'
dragSourceElement
=
this
;
//capture the inner HTML data from the element initiating the drag
e
.
dataTransfer
.
effectAllowed
=
'move'
;
e
.
dataTransfer
.
setData
(
'text/html'
,
this
.
innerHTML
)
;
}
//this handler allows us to drop an element when it is over a droppable area
function
handleDragOver
(
e
)
{
if
(
e
.
preventDefault
)
{
e
.
preventDefault
(
)
;
}
e
.
dataTransfer
.
dropEffect
=
'move'
;
return
false
;
}
//these handlers will toggle the over class to indicate where something can be
//dropped to
function
handleDragEnter
(
e
)
{
this
.
classList
.
add
(
'over'
)
;
}
function
handleDragLeave
(
e
)
{
this
.
classList
.
remove
(
'over'
)
;
}
//allow the drop and prevent bubbling
function
handleDrop
(
e
)
{
if
(
e
.
stopPropagation
)
{
e
.
stopPropagation
(
)
;
}
//check if the element being dropped onto is the source element of hte drag
if
(
dragSourceElement
!=
this
)
{
//set the element being dropped to the data stored from the initiator
dragSourceElement
.
innerHTML
=
this
.
innerHTML
;
this
.
innerHTML
=
e
.
dataTransfer
.
getData
(
'text/html'
)
;
}
return
false
;
}
//remove the over class after the drop
function
handleDragEnd
(
e
)
{
boxesArray
.
forEach
(
function
(
node
)
{
node
.
classList
.
remove
(
'over'
)
;
}
)
;
}
//add event listeners to each node to handle dragging events
boxesArray
.
forEach
(
function
(
node
)
{
node
.
addEventListener
(
'dragstart'
,
handleDragStart
,
false
)
;
node
.
addEventListener
(
'dragenter'
,
handleDragEnter
,
false
)
;
node
.
addEventListener
(
'dragover'
,
handleDragOver
,
false
)
;
node
.
addEventListener
(
'dragleave'
,
handleDragLeave
,
false
)
;
node
.
addEventListener
(
'drop'
,
handleDrop
,
false
)
;
node
.
addEventListener
(
'dragend'
,
handleDragEnd
,
false
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL