// Make the drop_area div droppable
$("#drop_area").droppable({
accept: "#temp_item",
drop: function(event,ui){
// Get the id of the item that was just dropped
var id = ui.draggable.attr('id');
// Clone the item and append it to the drop area
$(this).append($(ui.draggable).clone());
// Remove the ability to drag the current item around
$('#temp_region').removeClass("dragg");
$('#temp_region').removeClass("ui-draggable");
// Create a new name for the dropped item
var $new_name = rnd();
$('#' + id).attr('id', $new_name);
// Remove the dropped item's CSS position directives
$('#' + $new_name).css('top', '');
$('#' + $new_name).css('left', '');
// Make the newly renamed item droppable so that nesting can take place
$('#' + $new_name).droppable({
accept: '*',
drop: function(e,ui){
if ( e.clientY < $(this).closest("div").position().top + $(this).closest("div").height() )
{
// Determine what is being dropped and onto what
var dropped_item = $(ui.draggable).attr('id');
var dropped_on = $(this).attr('id');
// Get the HTML of each item
var dropped_html = $('#' + dropped_item).html();
var dropped_on_html = $('#' + dropped_on).html();
// Remove the dropped item from the DOM
$('#' + dropped_item).remove();
// Append the dropped item into the item it was just dropped on
$('#' + dropped_on).append(ui.draggable);
// Remove the dropped item's CSS position directives
$('#' + dropped_item).css('top', '');
$('#' + dropped_item).css('left', '');
// Let the user move the dropped item
$('#' + dropped_item).draggable();
}
}
});
// Force the dropped item to only move around in the container
$('#' + $new_name).draggable({containment: '#drop_area'});
// All the user to snap items to each other for usability sake
$('#' + $new_name).draggable({
snap: true,
stop: function(event, ui){
}
});
}
});
|