Skip to content

Instantly share code, notes, and snippets.

@yosukehasumi
Created May 3, 2019 18:26
Show Gist options
  • Save yosukehasumi/dbe0f45c9742a5d2350b351a6b03290f to your computer and use it in GitHub Desktop.
Save yosukehasumi/dbe0f45c9742a5d2350b351a6b03290f to your computer and use it in GitHub Desktop.
Drag Event handler class
window.DragEventHandler = (containers) ->
focused = false
this.windowDragoverOn = ->
unless focused
focused = true
for container in containers
container.classList.add('window-dragover')
this.windowDragoverOff = ->
if focused
focused = false
for container in containers
container.classList.remove('window-dragover')
addEvent = (obj, evt, fn) ->
if obj.addEventListener
obj.addEventListener(evt, fn, false)
else if obj.attachEvent
obj.attachEvent("on" + evt, fn)
this.init = ->
addEvent window, 'dragover', (event) =>
event = event ? event : window.event
event.preventDefault()
this.windowDragoverOn()
addEvent window, 'drop', (event) =>
event = event ? event : window.event
event.preventDefault()
this.windowDragoverOff()
addEvent document, 'mouseout', (event) =>
event = event ? event : window.event
from = event.relatedTarget || event.toElement
if (!from || from.nodeName == "HTML")
this.windowDragoverOff()
this.init()
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment