forked from JoelBesada/pasteboard
/
draganddrop.coffee
55 lines (44 loc) · 1.34 KB
/
draganddrop.coffee
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
###
# Drag and Drop module, handles drag / drop events
# and sends the dropped image to the editor.
###
dragAndDrop = (pasteboard) ->
$body = $ "body"
$dropArea = $("<div>")
.addClass("drop-area")
onDragStart = (e) ->
$body.addClass "dragging"
onDragEnd = (e) ->
$body.removeClass "dragging"
onDragOver = (e) ->
e.stopPropagation();
e.preventDefault();
e.originalEvent.dataTransfer.dropEffect = 'copy';
onDragDrop = (e) ->
e.preventDefault()
e.stopPropagation()
$body.removeClass "dragging"
for file in e.originalEvent.dataTransfer.files
if /image/.test file.type
pasteboard.fileHandler.readFile file
return
$(pasteboard).trigger "noimagefound", drop: true
self =
isSupported: () -> !!(Modernizr.draganddrop and pasteboard.fileHandler.isSupported())
# Initializes the module
init: () ->
unless @isSupported()
$("html").addClass("no-draganddrop-pb") # add -pb to prevent conflict with Modernizr
return
$body.prepend $dropArea
$dropArea.on
"dragenter.dragevent": onDragStart
"dragleave.dragevent": onDragEnd
"dragover.dragevent": onDragOver
"drop.dragevent": onDragDrop
# Hides the elements related to the module
# and stops event listeners
hide: () ->
$dropArea.off(".dragevent")
$dropArea.detach()
window.moduleLoader.addModule "dragAndDrop", dragAndDrop