Drag & Drop: List reorder w/Bubbling
This example shows how to make a sortable list using Custom Event Bubbling.
- Item #1
- Item #2
- Item #3
- Item #4
- Item #5
- Item #1
- Item #2
- Item #3
- Item #4
- Item #5
Setting up the lists
First we will make some lists that we want to make sortable.
Setting up the YUI Instance
Now we need to create our YUI instance and tell it to load the dd-constrain, dd-proxy and dd-drop, modules.
Making the Nodes Drag Instances and Drop Targets
Now we have our YUI instance ready, we can make the list items draggable. We will do this using Y.Node.all
We will be passing the selector string #play ul li to Y.Node.all to have it return us a NodeList of the li's in our 2 lists.
Using this selector syntax we will be able to add new list markup to the #play div and not have to change our code.
Then we will walk that NodeList and create our draggable Nodes.
Note that we are setting the following configs on the Drag instance: proxy, moveOnEnd, constrain2node, target.
Making the List Drop Target's too
We need to make the UL nodes a Drop Target so we can catch drops on the empty space of the list.
Using this selector syntax we will be able to add new list markup to the #play div and not have to change our code.
Using Event Bubbling
By default, all Drag and Drop instances bubble their event's up to the DragDropMgr. In this example we are assuming that there are no other Drag Operations in this YUI Instance.
Start Drag Event
The first thing we will do is handle the drag:start event. In this event, we will setup some styles to apply to the node and dragNode of the current Drag instance.
We will also be copying the innerHTML of the node and copying that to the innerHTML of the dragNode.
It should be noted, that
doing this will also copy any id's of the nodes inside the node. So if you are using this on something that is id based, you may need to remove the id's
of the nodes inside the node that is being dragged.
End Drag Event
In this event, we will reset some of the styles set in the drag:start event.
Drag Event
In this event, we will track the up/down movement for later use.
Over Drop Event
In this event, know which Target we are over, so we add the Drag node to the list either above or below the current Drop Target.
Drop Hit Event
In this event, we check to see if the target that was dropped on was not an LI node. If it wasn't, then we know it was dropped on the empty space of the UL.
