Native HTML5 Drag and Drop - HTML5 Rocks
I wanted to add drag and drop to my ToDo list application, derived from this one.
I found a nice reference here: Native HTML5 Drag and Drop - HTML5 Rocks:
I had to modify the code just a bit to convert it to CoffeeScript. Here's the result.
Since I'm targeting HTML5 in good browsers (i.e. not IE) I removed some unnecessary code.
events:
<snip>
'dragstart .todo-content': 'dragstart' #NEW FROM HERE SOUTH....
'dragenter .todo-content': 'dragenter'
'dragover .todo-content': 'dragover'
'dragleave .todo-content': 'dragleave'
'dragend .todo-content': 'dragend'
'drop .todo-content' : 'drop'
<snip>
dragstart: (e) ->
@$el.addClass 'dragging'
TodoView.dragSrcEl = @
e = e.originalEvent
if e.dataTransfer.effectAllowed = 'move'
e.dataTransfer.setData('text/html', @el.innerHTML);
dragover: (e) ->
e = e.originalEvent
e.preventDefault()
e.dataTransfer.dropEffect = 'move'
dragenter: ->
@$el.addClass 'over'
dragleave: ->
@$el.removeClass 'over'
dragend: ->
@$el.removeClass 'dragging'
drop: (e) ->
e.stopPropagation()
# Don't do anything if dropping the same column we're dragging.
if TodoView.dragSrcEl != @
TodoView.dragSrcEl.el.innerHTML = this.el.innerHTML;
e = e.originalEvent
this.el.innerHTML = e.dataTransfer.getData('text/html');
@$el.removeClass 'over'
TodoView.dragSrcEl.el.removeClass 'dragging'
Copyright (C) Michael Wolf 2011 and beyond