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.


  1.  events:
  2. <snip>
  3.     'dragstart .todo-content': 'dragstart' #NEW FROM HERE SOUTH....
  4.     'dragenter .todo-content': 'dragenter'
  5.     'dragover .todo-content': 'dragover'
  6.     'dragleave .todo-content': 'dragleave'
  7.     'dragend .todo-content': 'dragend'
  8.     'drop .todo-content' : 'drop'

  9. <snip> 
  10.   dragstart: (e) ->
  11.     @$el.addClass 'dragging'
  12.     TodoView.dragSrcEl = @
  13.     e = e.originalEvent
  14.     if e.dataTransfer.effectAllowed = 'move'
  15.       e.dataTransfer.setData('text/html', @el.innerHTML);
  16.  
  17.   dragover: (e) ->
  18.     e = e.originalEvent
  19.     e.preventDefault()
  20.     e.dataTransfer.dropEffect = 'move'
  21.  
  22.   dragenter: ->
  23.     @$el.addClass 'over'
  24.  
  25.   dragleave: ->
  26.     @$el.removeClass 'over'
  27.  
  28.   dragend: ->
  29.     @$el.removeClass 'dragging'
  30.  
  31.   drop: (e) ->
  32.    e.stopPropagation()
  33.       # Don't do anything if dropping the same column we're dragging.
  34.    if TodoView.dragSrcEl !@
  35.       TodoView.dragSrcEl.el.innerHTML = this.el.innerHTML;
  36.       e = e.originalEvent
  37.       this.el.innerHTML = e.dataTransfer.getData('text/html');
  38.     @$el.removeClass 'over'
  39.     TodoView.dragSrcEl.el.removeClass 'dragging'



Comments

Popular Posts