5
<title>Example: List Reorder w/Bubbling</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: List Reorder w/Bubbling</h1>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
23
<p>This example shows how to make a sortable list using Custom Event Bubbling.</p>
27
<style type="text/css" media="screen">
32
border: 1px solid black;
37
#play:after { display: block; clear: both; visibility: hidden; content: '.'; height: 0;}
39
border: 1px solid black;
49
background-image: none;
50
list-style-type: none;
59
background-color: #8DD5E7;
60
border:1px solid #004C6D;
63
background-color: #EDFF9F;
64
border:1px solid #CDCDCD;
70
<li class="list1">Item #1</li>
71
<li class="list1">Item #2</li>
72
<li class="list1">Item #3</li>
73
<li class="list1">Item #4</li>
74
<li class="list1">Item #5</li>
77
<li class="list2">Item #1</li>
78
<li class="list2">Item #2</li>
79
<li class="list2">Item #3</li>
80
<li class="list2">Item #4</li>
81
<li class="list2">Item #5</li>
85
<script type="text/javascript">
86
YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', function(Y) {
87
//Listen for all drop:over events
88
Y.DD.DDM.on('drop:over', function(e) {
89
//Get a reference to our drag and drop nodes
90
var drag = e.drag.get('node'),
91
drop = e.drop.get('node');
93
//Are we dropping on a li node?
94
if (drop.get('tagName').toLowerCase() === 'li') {
95
//Are we not going up?
97
drop = drop.get('nextSibling');
99
//Add the node to this list
100
e.drop.get('node').get('parentNode').insertBefore(drag, drop);
101
//Resize this nodes shim, so we can drop on it later.
105
//Listen for all drag:drag events
106
Y.DD.DDM.on('drag:drag', function(e) {
107
//Get the last y point
108
var y = e.target.lastXY[1];
109
//is it greater than the lastY var?
117
//Cache for next check
120
//Listen for all drag:start events
121
Y.DD.DDM.on('drag:start', function(e) {
122
//Get our drag object
124
//Set some styles here
125
drag.get('node').setStyle('opacity', '.25');
126
drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
127
drag.get('dragNode').setStyles({
129
borderColor: drag.get('node').getStyle('borderColor'),
130
backgroundColor: drag.get('node').getStyle('backgroundColor')
133
//Listen for a drag:end events
134
Y.DD.DDM.on('drag:end', function(e) {
136
//Put our styles back
137
drag.get('node').setStyles({
142
//Listen for all drag:drophit events
143
Y.DD.DDM.on('drag:drophit', function(e) {
144
var drop = e.drop.get('node'),
145
drag = e.drag.get('node');
147
//if we are not on an li, we must have been dropped on a ul
148
if (drop.get('tagName').toLowerCase() !== 'li') {
149
if (!drop.contains(drag)) {
150
drop.appendChild(drag);
156
var goingUp = false, lastY = 0;
158
//Get the list of li's in the lists and make them draggable
159
var lis = Y.Node.all('#play ul li');
160
lis.each(function(v, k) {
161
var dd = new Y.DD.Drag({
166
}).plug(Y.Plugin.DDProxy, {
168
}).plug(Y.Plugin.DDConstrained, {
169
constrain2node: '#play'
173
//Create simple targets for the 2 lists.
174
var uls = Y.Node.all('#play ul');
175
uls.each(function(v, k) {
176
var tar = new Y.DD.Drop({
190
<h3 id="setting-up-the-lists">Setting up the lists</h3>
191
<p>First we will make some lists that we want to make sortable.</p>
193
<pre class="code prettyprint"><div id="play">
194
<ul id="list1">
195
<li class="list1">Item #1</li>
196
<li class="list1">Item #2</li>
197
<li class="list1">Item #3</li>
198
<li class="list1">Item #4</li>
199
<li class="list1">Item #5</li>
201
<ul id="list2">
202
<li class="list2">Item #1</li>
203
<li class="list2">Item #2</li>
204
<li class="list2">Item #3</li>
205
<li class="list2">Item #4</li>
206
<li class="list2">Item #5</li>
208
</div></pre>
211
<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
212
<p>Now we need to create our YUI instance and tell it to load the <code>dd-constrain</code>, <code>dd-proxy</code> and <code>dd-drop</code>, modules.</p>
214
<pre class="code prettyprint">YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', function(Y) {</pre>
217
<h3 id="making-the-nodes-drag-instances-and-drop-targets">Making the Nodes Drag Instances and Drop Targets</h3>
218
<p>Now we have our YUI instance ready, we can make the list items draggable. We will do this using <code>Y.Node.all</code></p>
219
<p>We will be passing the selector string <code>#play ul li</code> to <code>Y.Node.all</code> to have it return a <code>NodeList</code> of the li's in our two lists.
220
Using this selector syntax we will be able to add new list markup to the <code>#play</code> div and not have to change our code.</p>
221
<p>Then we will walk that <code>NodeList</code> and create our draggable Nodes.</p>
222
<p>Note that we are adding the <code>DDProxy</code> and <code>DDConstrained</code> plugins to each Drag instance, and setting the following configuration options: <code>moveOnEnd, constrain2node, target</code>.</p>
224
<pre class="code prettyprint">//Get the list of li's in the lists and make them draggable
225
var lis = Y.Node.all('#play ul li');
226
lis.each(function(v, k) {
227
var dd = new Y.DD.Drag({
229
//Make it Drop target and pass this config to the Drop constructor
231
padding: '0 0 0 20'
233
}).plug(Y.Plugin.DDProxy, {
234
//Don't move the node at the end of the drag
236
}).plug(Y.Plugin.DDConstrained, {
237
//Keep it inside the #play node
238
constrain2node: '#play'
243
<h3 id="making-the-list-drop-targets-too">Making the List Drop Targets too</h3>
244
<p>We need to make the UL nodes a Drop Target so we can catch drops on the empty space of the list.
245
Using this selector syntax we will be able to add new list markup to the <code>#play</code> div and not have to change our code.</p>
247
<pre class="code prettyprint">//Create simple targets for the 2 lists.
248
var uls = Y.Node.all('#play ul');
249
uls.each(function(v, k) {
250
var tar = new Y.DD.Drop({
256
<h3 id="using-event-bubbling">Using Event Bubbling</h3>
257
<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>
259
<h3 id="start-drag-event">Start Drag Event</h3>
260
<p>The first thing we will do is handle the drag:start event. In this event, we will set up some styles to apply to the <code>node</code> and <code>dragNode</code> of the current Drag instance.</p>
261
<p>We will also be copying the <code>innerHTML</code> of the <code>node</code> and copying that to the <code>innerHTML</code> of the <code>dragNode</code>. </p>
262
<p><em>It should be noted, that
263
doing this will also copy any <code>id</code>'s of the nodes inside the <code>node</code>. So if you are using this on something that is <code>id</code> based, you may need to remove the <code>id</code>'s
264
of the nodes inside the <code>node</code> that is being dragged.
267
<pre class="code prettyprint">Y.DD.DDM.on('drag:start', function(e) {
268
//Get our drag object
270
//Set some styles here
271
drag.get('node').setStyle('opacity', '.25');
272
drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
273
drag.get('dragNode').setStyles({
274
opacity: '.5',
275
borderColor: drag.get('node').getStyle('borderColor'),
276
backgroundColor: drag.get('node').getStyle('backgroundColor')
282
<h3 id="end-drag-event">End Drag Event</h3>
283
<p>In this event, we will reset some of the styles set in the drag:start event.</p>
285
<pre class="code prettyprint">Y.DD.DDM.on('drag:end', function(e) {
287
//Put our styles back
288
drag.get('node').setStyles({
289
visibility: '',
290
opacity: '1'
295
<h3 id="drag-event">Drag Event</h3>
296
<p>In this event, we will track the up/down movement for later use.</p>
298
<pre class="code prettyprint">Y.DD.DDM.on('drag:drag', function(e) {
299
//Get the last y point
300
var y = e.target.lastXY[1];
301
//is it greater than the lastY var?
302
if (y &lt; lastY) {
303
//We are going up
306
//We are going down.
309
//Cache for next check
314
<h3 id="over-drop-event">Over Drop Event</h3>
315
<p>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.</p>
317
<pre class="code prettyprint">Y.DD.DDM.on('drop:over', function(e) {
318
//Get a reference to our drag and drop nodes
319
var drag = e.drag.get('node'),
320
drop = e.drop.get('node');
322
//Are we dropping on a li node?
323
if (drop.get('tagName').toLowerCase() === 'li') {
324
//Are we not going up?
326
drop = drop.get('nextSibling');
328
//Add the node to this list
329
e.drop.get('node').get('parentNode').insertBefore(drag, drop);
330
//Resize this nodes shim, so we can drop on it later.
336
<h3 id="drop-hit-event">Drop Hit Event</h3>
337
<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>
339
<pre class="code prettyprint">Y.DD.DDM.on('drag:drophit', function(e) {
340
var drop = e.drop.get('node'),
341
drag = e.drag.get('node');
343
//if we are not on an li, we must have been dropped on a ul
344
if (drop.get('tagName').toLowerCase() !== 'li') {
345
if (!drop.contains(drag)) {
346
drop.appendChild(drag);
352
<h3 id="full-javascript-source">Full Javascript Source</h3>
353
<pre class="code prettyprint">YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', function(Y) {
354
//Listen for all drop:over events
355
Y.DD.DDM.on('drop:over', function(e) {
356
//Get a reference to our drag and drop nodes
357
var drag = e.drag.get('node'),
358
drop = e.drop.get('node');
360
//Are we dropping on a li node?
361
if (drop.get('tagName').toLowerCase() === 'li') {
362
//Are we not going up?
364
drop = drop.get('nextSibling');
366
//Add the node to this list
367
e.drop.get('node').get('parentNode').insertBefore(drag, drop);
368
//Resize this nodes shim, so we can drop on it later.
372
//Listen for all drag:drag events
373
Y.DD.DDM.on('drag:drag', function(e) {
374
//Get the last y point
375
var y = e.target.lastXY[1];
376
//is it greater than the lastY var?
378
//We are going up
381
//We are going down.
384
//Cache for next check
387
//Listen for all drag:start events
388
Y.DD.DDM.on('drag:start', function(e) {
389
//Get our drag object
391
//Set some styles here
392
drag.get('node').setStyle('opacity', '.25');
393
drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
394
drag.get('dragNode').setStyles({
395
opacity: '.5',
396
borderColor: drag.get('node').getStyle('borderColor'),
397
backgroundColor: drag.get('node').getStyle('backgroundColor')
400
//Listen for a drag:end events
401
Y.DD.DDM.on('drag:end', function(e) {
403
//Put our styles back
404
drag.get('node').setStyles({
405
visibility: '',
406
opacity: '1'
409
//Listen for all drag:drophit events
410
Y.DD.DDM.on('drag:drophit', function(e) {
411
var drop = e.drop.get('node'),
412
drag = e.drag.get('node');
414
//if we are not on an li, we must have been dropped on a ul
415
if (drop.get('tagName').toLowerCase() !== 'li') {
416
if (!drop.contains(drag)) {
417
drop.appendChild(drag);
422
//Static Vars
423
var goingUp = false, lastY = 0;
425
//Get the list of li's in the lists and make them draggable
426
var lis = Y.Node.all('#play ul li');
427
lis.each(function(v, k) {
428
var dd = new Y.DD.Drag({
431
padding: '0 0 0 20'
433
}).plug(Y.Plugin.DDProxy, {
435
}).plug(Y.Plugin.DDConstrained, {
436
constrain2node: '#play'
440
//Create simple targets for the 2 lists.
441
var uls = Y.Node.all('#play ul');
442
uls.each(function(v, k) {
443
var tar = new Y.DD.Drop({
453
<div id="sidebar" class="yui3-u">
455
<div id="toc" class="sidebox">
457
<h2 class="no-toc">Table of Contents</h2>
463
<a href="#setting-up-the-lists">Setting up the lists</a>
466
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
469
<a href="#making-the-nodes-drag-instances-and-drop-targets">Making the Nodes Drag Instances and Drop Targets</a>
472
<a href="#making-the-list-drop-targets-too">Making the List Drop Targets too</a>
475
<a href="#using-event-bubbling">Using Event Bubbling</a>
478
<a href="#start-drag-event">Start Drag Event</a>
481
<a href="#end-drag-event">End Drag Event</a>
484
<a href="#drag-event">Drag Event</a>
487
<a href="#over-drop-event">Over Drop Event</a>
490
<a href="#drop-hit-event">Drop Hit Event</a>
493
<a href="#full-javascript-source">Full Javascript Source</a>
501
<div class="sidebox">
503
<h2 class="no-toc">Examples</h2>
507
<ul class="examples">
510
<li data-description="A simple drag interaction that doesn't require a drop interaction.">
511
<a href="simple-drag.html">Simple Drag</a>
516
<li data-description="How to apply the Drag Plugin to a node.">
517
<a href="drag-plugin.html">Drag - Node plugin</a>
522
<li data-description="A simple proxy drag interaction that doesn't require a drop interaction.">
523
<a href="proxy-drag.html">Drag - Proxy</a>
528
<li data-description="How to constrain a draggable Node to another Node's region.">
529
<a href="constrained-drag.html">Drag - Constrained to a Region</a>
534
<li data-description="Using interaction groups, this example demonstrates how to tie into the Drag & Drop Utility's interesting moments to provide visual affordances for the current drag operation.">
535
<a href="groups-drag.html">Drag - Interaction Groups</a>
540
<li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
541
<a href="shim-drag.html">Using the Drag Shim</a>
546
<li data-description="How to use the Drop Target events to code your application.">
547
<a href="drop-code.html">Using Drop Based Coding</a>
552
<li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
553
<a href="winscroll.html">Window Scrolling</a>
558
<li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
559
<a href="delegate.html">Drag Delegation</a>
564
<li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
565
<a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
570
<li data-description="How to use Drag plugins with a DD Delegate based solution.">
571
<a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
576
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
577
<a href="list-drag.html">List Reorder w/Bubbling</a>
582
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
583
<a href="scroll-list.html">List Reorder w/Scrolling</a>
588
<li data-description="How to make an animated node a Drop target.">
589
<a href="anim-drop.html">Animated Drop Targets</a>
594
<li data-description="Example Photo Browser application.">
595
<a href="photo-browser.html">Photo Browser</a>
600
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
601
<a href="portal-drag.html">Portal Style Example</a>
615
<div class="sidebox">
617
<h2 class="no-toc">Examples That Use This Component</h2>
621
<ul class="examples">
656
<li data-description="Working with multiple YUI instances.">
657
<a href="../yui/yui-multi.html">Multiple Instances</a>
662
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
663
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
675
<script src="../assets/vendor/prettify/prettify-min.js"></script>
676
<script>prettyPrint();</script>