removed bug in dragndrop where nodes with html were pushing null/partial content

master
Wes 2011-11-19 14:19:41 +00:00
parent 7d49fcd736
commit c9111adf23
2 changed files with 56 additions and 20 deletions

View File

@ -32,7 +32,7 @@
// add drag and drop if enabled // add drag and drop if enabled
if(opts.dragAndDrop){ if(opts.dragAndDrop){
$('.node').draggable({ $('div.node').draggable({
cursor : 'move', cursor : 'move',
distance : 40, distance : 40,
helper : 'clone', helper : 'clone',
@ -46,7 +46,7 @@
stop : handleDragStop stop : handleDragStop
}); });
$('.node').droppable({ $('div.node').droppable({
accept : '.node', accept : '.node',
activeClass : 'drag-active', activeClass : 'drag-active',
drop : handleDropEvent, drop : handleDropEvent,
@ -72,15 +72,33 @@
} }
// Drop event handler for nodes // Drop event handler for nodes
function handleDropEvent( event, ui ) { function handleDropEvent( event, ui ) {
console.log('handleDropEvent');
var sourceNode = ui.draggable; var sourceNode = ui.draggable;
var targetNode = $(this); var targetNode = $(this);
var targetLi = $('li:contains("' + targetNode.html() +'")').last(); // finding nodes based on plaintext and html
// content is hard!
var targetLi = $('li').filter(function(){
li = $(this).clone()
.children("ul,li")
.remove()
.end();
return li.html() == targetNode.html();
});
var sourceliHtml = $('li:contains("' + sourceNode.html() +'")').last().clone(); var sourceLi = $('li').filter(function(){
var sourceLi = $('li:contains("' + sourceNode.html() +'")').last();
li = $(this).clone()
.children("ul,li")
.remove()
.end();
return li.html() == sourceNode.html();
});
var sourceliClone = sourceLi.clone();
var sourceUl = sourceLi.parent('ul'); var sourceUl = sourceLi.parent('ul');
if(sourceUl.children('li').size() > 1){ if(sourceUl.children('li').size() > 1){
@ -90,9 +108,9 @@
} }
if(targetLi.children('ul').size() >0){ if(targetLi.children('ul').size() >0){
targetLi.children('ul').append('<li>'+sourceliHtml.html()+'</li>'); targetLi.children('ul').append('<li>'+sourceliClone.html()+'</li>');
}else{ }else{
targetLi.append('<ul><li>'+sourceliHtml.html()+'</li></ul>'); targetLi.append('<ul><li>'+sourceliClone.html()+'</li></ul>');
} }
} // handleDropEvent } // handleDropEvent

View File

@ -32,7 +32,7 @@
// add drag and drop if enabled // add drag and drop if enabled
if(opts.dragAndDrop){ if(opts.dragAndDrop){
$('.node').draggable({ $('div.node').draggable({
cursor : 'move', cursor : 'move',
distance : 40, distance : 40,
helper : 'clone', helper : 'clone',
@ -46,7 +46,7 @@
stop : handleDragStop stop : handleDragStop
}); });
$('.node').droppable({ $('div.node').droppable({
accept : '.node', accept : '.node',
activeClass : 'drag-active', activeClass : 'drag-active',
drop : handleDropEvent, drop : handleDropEvent,
@ -72,15 +72,33 @@
} }
// Drop event handler for nodes // Drop event handler for nodes
function handleDropEvent( event, ui ) { function handleDropEvent( event, ui ) {
console.log('handleDropEvent');
var sourceNode = ui.draggable; var sourceNode = ui.draggable;
var targetNode = $(this); var targetNode = $(this);
var targetLi = $('li:contains("' + targetNode.html() +'")').last(); // finding nodes based on plaintext and html
// content is hard!
var targetLi = $('li').filter(function(){
li = $(this).clone()
.children("ul,li")
.remove()
.end();
return li.html() == targetNode.html();
});
var sourceliHtml = $('li:contains("' + sourceNode.html() +'")').last().clone(); var sourceLi = $('li').filter(function(){
var sourceLi = $('li:contains("' + sourceNode.html() +'")').last();
li = $(this).clone()
.children("ul,li")
.remove()
.end();
return li.html() == sourceNode.html();
});
var sourceliClone = sourceLi.clone();
var sourceUl = sourceLi.parent('ul'); var sourceUl = sourceLi.parent('ul');
if(sourceUl.children('li').size() > 1){ if(sourceUl.children('li').size() > 1){
@ -90,14 +108,14 @@
} }
if(targetLi.children('ul').size() >0){ if(targetLi.children('ul').size() >0){
targetLi.children('ul').append('<li>'+sourceliHtml.html()+'</li>'); targetLi.children('ul').append('<li>'+sourceliClone.html()+'</li>');
}else{ }else{
targetLi.append('<ul><li>'+sourceliHtml.html()+'</li></ul>'); targetLi.append('<ul><li>'+sourceliClone.html()+'</li></ul>');
} }
} } // handleDropEvent
} } // Drag and drop
}; };
// Option defaults // Option defaults