scroll-list.mustache revision 5b139d58acfcae4b1bcab5a895706abafc2a87a0
3909N/A<p>This example shows how to make a sortable list using Custom Event Bubbling and Node scrolling.</p>
0N/A<p>Now we have our YUI instance ready, we can make the list items draggable. We will do this using `Y.Node.all`</p>
0N/A<p>We will be passing the selector string `#play ul li` to `Y.all` to have it return us a `NodeList` of the li's in our 2 lists.
0N/AUsing this selector syntax we will be able to add new list markup to the `#play` div and not have to change our code.</p>
0N/A<p>Also note that we are using the Node's `parentNode` as our node in the DDNodeScroll plugin.</p>
0N/A }).plug(Y.Plugin.DDProxy, {
0N/A }).plug(Y.Plugin.DDConstrained, {
0N/A }).plug(Y.Plugin.DDNodeScroll, {
0N/A<p>We need to make each UL node a Drop Target so we can catch drops on the empty space of the list.
0N/AUsing this selector syntax we will be able to add new list markup to the `#play` div and not have to change our code.</p>
var uls = Y.all('#play ul');
uls.each(function(v, k) {
var tar = new Y.DD.Drop({
<p>By default, all Drag and Drop instances bubble their events up to the DragDropMgr. In this example we are assuming that there are no other Drag operations in this YUI Instance.</p>
<p>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.</p>
<p>We will also be copying the `innerHTML` of the `node` and copying that to the `innerHTML` of the `dragNode`.</p>
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
Y.DD.DDM.on('drag:start', function(e) {
var drag = e.target;
drag.get('node').setStyle('opacity', '.25');
drag.get('dragNode').setStyles({
borderColor: drag.get('node').getStyle('borderColor'),
backgroundColor: drag.get('node').getStyle('backgroundColor')
Y.DD.DDM.on('drag:end', function(e) {
var drag = e.target;
drag.get('node').setStyles({
<p>In this event, we will track the up/down movement for later use.</p>
Y.DD.DDM.on('drag:drag', function(e) {
var y = e.target.lastXY[1];
Y.DD.DDM.syncActiveShims(true);
<p>In this event, we know which target we are over, so we add the Drag node to the list either above or below the current Drop Target.</p>
<p>Here we also change the node which needs to be scrolled. Since we are changing lists, we need to tell the scrolling plugin instance that it needs to check against another node.</p>
Y.DD.DDM.on('drop:over', function(e) {
var drag = e.drag.get('node'),
drop = e.drop.get('node');
if (drop.get('tagName').toLowerCase() === 'li') {
drop = drop.get('nextSibling');
e.drop.get('node').get('parentNode').insertBefore(drag, drop);
<p>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.</p>
Y.DD.DDM.on('drag:drophit', function(e) {
var drop = e.drop.get('node'),
drag = e.drag.get('node');
if (drop.get('tagName').toLowerCase() !== 'li') {
if (!drop.contains(drag)) {
drop.appendChild(drag);