YUI 3.x Home -

YUI Library Examples: Drag & Drop: List reorder w/Bubbling

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.

  1. <div id="play">
  2. <ul id="list1">
  3. <li class="list1">Item #1</li>
  4. <li class="list1">Item #2</li>
  5. <li class="list1">Item #3</li>
  6. <li class="list1">Item #4</li>
  7. <li class="list1">Item #5</li>
  8. </ul>
  9. <ul id="list2">
  10. <li class="list2">Item #1</li>
  11. <li class="list2">Item #2</li>
  12. <li class="list2">Item #3</li>
  13. <li class="list2">Item #4</li>
  14. <li class="list2">Item #5</li>
  15. </ul>
  16. </div>
<div id="play">
    <ul id="list1">
        <li class="list1">Item #1</li>
        <li class="list1">Item #2</li>
        <li class="list1">Item #3</li>
        <li class="list1">Item #4</li>
        <li class="list1">Item #5</li>
    </ul>
    <ul id="list2">
        <li class="list2">Item #1</li>
        <li class="list2">Item #2</li>
        <li class="list2">Item #3</li>
        <li class="list2">Item #4</li>
        <li class="list2">Item #5</li>
    </ul>
</div>

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.

  1. YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', function(Y) {
YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', function(Y) {

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.

  1. //Get the list of li's in the lists and make them draggable
  2. var lis = Y.Node.all('#play ul li');
  3. lis.each(function(v, k) {
  4. var dd = new Y.DD.Drag({
  5. node: v,
  6. //Make it Drop target and pass this config to the Drop constructor
  7. target: {
  8. padding: '0 0 0 20'
  9. }
  10. }).plug(Y.Plugin.DDProxy, {
  11. //Don't move the node at the end of the drag
  12. moveOnEnd: false
  13. }).plug(Y.Plugin.DDConstrained, {
  14. //Keep it inside the #play node
  15. constrain2node: '#play'
  16. });
  17. });
//Get the list of li's in the lists and make them draggable
var lis = Y.Node.all('#play ul li');
lis.each(function(v, k) {
    var dd = new Y.DD.Drag({
        node: v,
        //Make it Drop target and pass this config to the Drop constructor
        target: {
            padding: '0 0 0 20'
        }
    }).plug(Y.Plugin.DDProxy, {
        //Don't move the node at the end of the drag
        moveOnEnd: false
    }).plug(Y.Plugin.DDConstrained, {
        //Keep it inside the #play node
        constrain2node: '#play'
    });
});

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.

  1. //Create simple targets for the 2 lists..
  2. var uls = Y.Node.all('#play ul');
  3. uls.each(function(v, k) {
  4. var tar = new Y.DD.Drop({
  5. node: v
  6. });
  7. });
//Create simple targets for the 2 lists..
var uls = Y.Node.all('#play ul');
uls.each(function(v, k) {
    var tar = new Y.DD.Drop({
        node: v
    });
});

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.

  1. Y.DD.DDM.on('drag:start', function(e) {
  2. //Get our drag object
  3. var drag = e.target;
  4. //Set some styles here
  5. drag.get('node').setStyle('opacity', '.25');
  6. drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
  7. drag.get('dragNode').setStyles({
  8. opacity: '.5',
  9. borderColor: drag.get('node').getStyle('borderColor'),
  10. backgroundColor: drag.get('node').getStyle('backgroundColor')
  11. });
  12. });
Y.DD.DDM.on('drag:start', function(e) {
    //Get our drag object
    var drag = e.target;
    //Set some styles here
    drag.get('node').setStyle('opacity', '.25');
    drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
    drag.get('dragNode').setStyles({
        opacity: '.5',
        borderColor: drag.get('node').getStyle('borderColor'),
        backgroundColor: drag.get('node').getStyle('backgroundColor')
    });
});

End Drag Event

In this event, we will reset some of the styles set in the drag:start event.

  1. Y.DD.DDM.on('drag:end', function(e) {
  2. var drag = e.target;
  3. //Put out styles back
  4. drag.get('node').setStyles({
  5. visibility: '',
  6. opacity: '1'
  7. });
  8. });
Y.DD.DDM.on('drag:end', function(e) {
    var drag = e.target;
    //Put out styles back
    drag.get('node').setStyles({
        visibility: '',
        opacity: '1'
    });
});

Drag Event

In this event, we will track the up/down movement for later use.

  1. Y.DD.DDM.on('drag:drag', function(e) {
  2. //Get the last y point
  3. var y = e.target.lastXY[1];
  4. //is it greater than the lastY var?
  5. if (y &lt; lastY) {
  6. //We are going up
  7. goingUp = true;
  8. } else {
  9. //We are going down..
  10. goingUp = false;
  11. }
  12. //Cache for next check
  13. lastY = y;
  14. });
Y.DD.DDM.on('drag:drag', function(e) {
    //Get the last y point
    var y = e.target.lastXY[1];
    //is it greater than the lastY var?
    if (y &lt; lastY) {
        //We are going up
        goingUp = true;
    } else {
        //We are going down..
        goingUp = false;
    }
    //Cache for next check
    lastY = y;
});

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.

  1. Y.DD.DDM.on('drop:over', function(e) {
  2. //Get a reference to out drag and drop nodes
  3. var drag = e.drag.get('node'),
  4. drop = e.drop.get('node');
  5.  
  6. //Are we dropping on a li node?
  7. if (drop.get('tagName').toLowerCase() === 'li') {
  8. //Are we not going up?
  9. if (!goingUp) {
  10. drop = drop.get('nextSibling');
  11. }
  12. //Add the node to this list
  13. e.drop.get('node').get('parentNode').insertBefore(drag, drop);
  14. //Resize this nodes shim, so we can drop on it later.
  15. e.drop.sizeShim();
  16. }
  17. });
Y.DD.DDM.on('drop:over', function(e) {
    //Get a reference to out drag and drop nodes
    var drag = e.drag.get('node'),
        drop = e.drop.get('node');
 
    //Are we dropping on a li node?
    if (drop.get('tagName').toLowerCase() === 'li') {
        //Are we not going up?
        if (!goingUp) {
            drop = drop.get('nextSibling');
        }
        //Add the node to this list
        e.drop.get('node').get('parentNode').insertBefore(drag, drop);
        //Resize this nodes shim, so we can drop on it later.
        e.drop.sizeShim();
    }
});

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.

  1. Y.DD.DDM.on('drag:drophit', function(e) {
  2. var drop = e.drop.get('node'),
  3. drag = e.drag.get('node');
  4.  
  5. //if we are not on an li, we must have been dropped on a ul
  6. if (drop.get('tagName').toLowerCase() !== 'li') {
  7. if (!drop.contains(drag)) {
  8. drop.appendChild(drag);
  9. }
  10. }
  11. });
Y.DD.DDM.on('drag:drophit', function(e) {
    var drop = e.drop.get('node'),
        drag = e.drag.get('node');
 
    //if we are not on an li, we must have been dropped on a ul
    if (drop.get('tagName').toLowerCase() !== 'li') {
        if (!drop.contains(drag)) {
            drop.appendChild(drag);
        }
    }
});

Full Javascript Source

  1. YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', function(Y) {
  2. //Listen for all drop:over events
  3. Y.DD.DDM.on('drop:over', function(e) {
  4. //Get a reference to out drag and drop nodes
  5. var drag = e.drag.get('node'),
  6. drop = e.drop.get('node');
  7.  
  8. //Are we dropping on a li node?
  9. if (drop.get('tagName').toLowerCase() === 'li') {
  10. //Are we not going up?
  11. if (!goingUp) {
  12. drop = drop.get('nextSibling');
  13. }
  14. //Add the node to this list
  15. e.drop.get('node').get('parentNode').insertBefore(drag, drop);
  16. //Resize this nodes shim, so we can drop on it later.
  17. e.drop.sizeShim();
  18. }
  19. });
  20. //Listen for all drag:drag events
  21. Y.DD.DDM.on('drag:drag', function(e) {
  22. //Get the last y point
  23. var y = e.target.lastXY[1];
  24. //is it greater than the lastY var?
  25. if (y < lastY) {
  26. //We are going up
  27. goingUp = true;
  28. } else {
  29. //We are going down..
  30. goingUp = false;
  31. }
  32. //Cache for next check
  33. lastY = y;
  34. });
  35. //Listen for all drag:start events
  36. Y.DD.DDM.on('drag:start', function(e) {
  37. //Get our drag object
  38. var drag = e.target;
  39. //Set some styles here
  40. drag.get('node').setStyle('opacity', '.25');
  41. drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
  42. drag.get('dragNode').setStyles({
  43. opacity: '.5',
  44. borderColor: drag.get('node').getStyle('borderColor'),
  45. backgroundColor: drag.get('node').getStyle('backgroundColor')
  46. });
  47. });
  48. //Listen for a drag:end events
  49. Y.DD.DDM.on('drag:end', function(e) {
  50. var drag = e.target;
  51. //Put out styles back
  52. drag.get('node').setStyles({
  53. visibility: '',
  54. opacity: '1'
  55. });
  56. });
  57. //Listen for all drag:drophit events
  58. Y.DD.DDM.on('drag:drophit', function(e) {
  59. var drop = e.drop.get('node'),
  60. drag = e.drag.get('node');
  61.  
  62. //if we are not on an li, we must have been dropped on a ul
  63. if (drop.get('tagName').toLowerCase() !== 'li') {
  64. if (!drop.contains(drag)) {
  65. drop.appendChild(drag);
  66. }
  67. }
  68. });
  69.  
  70. //Static Vars
  71. var goingUp = false, lastY = 0;
  72.  
  73. //Get the list of li's in the lists and make them draggable
  74. var lis = Y.Node.all('#play ul li');
  75. lis.each(function(v, k) {
  76. var dd = new Y.DD.Drag({
  77. node: v,
  78. target: {
  79. padding: '0 0 0 20'
  80. }
  81. }).plug(Y.Plugin.DDProxy, {
  82. moveOnEnd: false
  83. }).plug(Y.Plugin.DDConstrained, {
  84. constrain2node: '#play'
  85. });
  86. });
  87.  
  88. //Create simple targets for the 2 lists..
  89. var uls = Y.Node.all('#play ul');
  90. uls.each(function(v, k) {
  91. var tar = new Y.DD.Drop({
  92. node: v
  93. });
  94. });
  95.  
  96. });
YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', function(Y) {
    //Listen for all drop:over events
    Y.DD.DDM.on('drop:over', function(e) {
        //Get a reference to out drag and drop nodes
        var drag = e.drag.get('node'),
            drop = e.drop.get('node');
 
        //Are we dropping on a li node?
        if (drop.get('tagName').toLowerCase() === 'li') {
            //Are we not going up?
            if (!goingUp) {
                drop = drop.get('nextSibling');
            }
            //Add the node to this list
            e.drop.get('node').get('parentNode').insertBefore(drag, drop);
            //Resize this nodes shim, so we can drop on it later.
            e.drop.sizeShim();
        }
    });
    //Listen for all drag:drag events
    Y.DD.DDM.on('drag:drag', function(e) {
        //Get the last y point
        var y = e.target.lastXY[1];
        //is it greater than the lastY var?
        if (y < lastY) {
            //We are going up
            goingUp = true;
        } else {
            //We are going down..
            goingUp = false;
        }
        //Cache for next check
        lastY = y;
    });
    //Listen for all drag:start events
    Y.DD.DDM.on('drag:start', function(e) {
        //Get our drag object
        var drag = e.target;
        //Set some styles here
        drag.get('node').setStyle('opacity', '.25');
        drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
        drag.get('dragNode').setStyles({
            opacity: '.5',
            borderColor: drag.get('node').getStyle('borderColor'),
            backgroundColor: drag.get('node').getStyle('backgroundColor')
        });
    });
    //Listen for a drag:end events
    Y.DD.DDM.on('drag:end', function(e) {
        var drag = e.target;
        //Put out styles back
        drag.get('node').setStyles({
            visibility: '',
            opacity: '1'
        });
    });
    //Listen for all drag:drophit events
    Y.DD.DDM.on('drag:drophit', function(e) {
        var drop = e.drop.get('node'),
            drag = e.drag.get('node');
 
        //if we are not on an li, we must have been dropped on a ul
        if (drop.get('tagName').toLowerCase() !== 'li') {
            if (!drop.contains(drag)) {
                drop.appendChild(drag);
            }
        }
    });
 
    //Static Vars
    var goingUp = false, lastY = 0;
 
    //Get the list of li's in the lists and make them draggable
    var lis = Y.Node.all('#play ul li');
    lis.each(function(v, k) {
        var dd = new Y.DD.Drag({
            node: v,
            target: {
                padding: '0 0 0 20'
            }
        }).plug(Y.Plugin.DDProxy, {
            moveOnEnd: false
        }).plug(Y.Plugin.DDConstrained, {
            constrain2node: '#play'
        });
    });
 
    //Create simple targets for the 2 lists..
    var uls = Y.Node.all('#play ul');
    uls.each(function(v, k) {
        var tar = new Y.DD.Drop({
            node: v
        });
    });
 
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings