5
<title>Example: Drag Delegation with a Drop Target</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: Drag Delegation with a Drop Target</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 create multiple draggable items efficiently, still allowing for Drop Targets.</p>
32
border: 1px solid black;
33
background-color: #8DD5E7;
36
list-style-type: none;
45
border: 1px solid black;
46
background-color: #eee;
57
#drop.yui3-dd-drop-over {
58
background-color: #ccc;
81
<div id="drop">Drop on me</div>
87
YUI().use('dd-delegate', 'dd-drop-plugin', function(Y) {
88
var del = new Y.DD.Delegate({
93
del.on('drag:end', function(e) {
94
del.get('currentNode').setStyles({
100
var drop = Y.one('#drop').plug(Y.Plugin.Drop);
101
drop.drop.on('drop:hit', function(e) {
102
drop.set('innerHTML', 'You dropped: <strong>' + e.drag.get('node').get('innerHTML') + '</strong>');
113
<h3 id="setting-up-the-delegate-container-and-items">Setting up the Delegate container and items</h3>
114
<p>First we need to create an HTML Node to act as the delegate container and give it some nodes to make draggable.</p>
116
<pre class="code prettyprint"><div id="play">
118
<div id="demo">
120
<li>Item #1</li>
121
<li>Item #2</li>
122
<li>Item #3</li>
123
<li>Item #4</li>
124
<li>Item #5</li>
125
<li>Item #6</li>
126
<li>Item #7</li>
127
<li>Item #8</li>
128
<li>Item #9</li>
129
<li>Item #10</li>
133
<div id="drop">Drop on me</div>
134
</div></pre>
137
<p>Now we give them some CSS to make them visible.</p>
139
<pre class="code prettyprint">#demo {
143
border: 1px solid black;
144
background-color: #8DD5E7;
147
list-style-type: none;
156
border: 1px solid black;
157
background-color: #eee;
168
#drop.yui3-dd-drop-over {
169
background-color: #ccc;
173
<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
174
<p>Now we need to create our YUI instance and tell it to load the <code>dd-delegate</code> module.</p>
176
<pre class="code prettyprint">YUI().use('dd-delegate', 'dd-drop-plugin');</pre>
179
<h3 id="making-the-nodes-draggable">Making the Nodes draggable</h3>
180
<p>Now that we have a YUI instance with the <code>dd-delegate</code> module, we need to instantiate the <code>Delegate</code> instance on this container and nodes.</p>
182
<pre class="code prettyprint">YUI().use('dd-delegate', 'dd-drop-plugin', function(Y) {
183
var del = new Y.DD.Delegate({
184
container: '#demo',
185
nodes: 'li'
190
<h3 id="listening-to-some-events">Listening to some events</h3>
191
<p>The <code>Delegate</code> object is a bubble target for the <code>DD.Drag</code> instances. So you can listen on it for all drag related events.</p>
193
<pre class="code prettyprint">YUI().use('dd-delegate', 'dd-drop-plugin', function(Y) {
194
var del = new Y.DD.Delegate({
195
container: '#demo',
196
nodes: 'li'
199
del.on('drag:end', function(e) {
200
del.get('currentNode').setStyles({
209
<h3 id="adding-a-drop-target">Adding a Drop Target</h3>
210
<p>Now we can add a normal Drop Target to the page.</p>
212
<pre class="code prettyprint">var drop = Y.one('#drop').plug(Y.Plugin.Drop);</pre>
215
<p>Once that is created, we can add a <code>drop:hit</code> listener and manipulate the drag instance as we normally would.</p>
217
<pre class="code prettyprint">var drop = Y.one('#drop').plug(Y.Plugin.Drop);
218
drop.drop.on('drop:hit', function(e) {
219
drop.set('innerHTML', 'You dropped: <strong>' + e.drag.get('node').get('innerHTML') + '</strong>');
223
<h3 id="full-javascript-source">Full Javascript Source</h3>
225
<pre class="code prettyprint">YUI().use('dd-delegate', 'dd-drop-plugin', function(Y) {
226
var del = new Y.DD.Delegate({
227
container: '#demo',
228
nodes: 'li'
231
del.on('drag:end', function(e) {
232
del.get('currentNode').setStyles({
238
var drop = Y.one('#drop').plug(Y.Plugin.Drop);
239
drop.drop.on('drop:hit', function(e) {
240
drop.set('innerHTML', 'You dropped: <strong>' + e.drag.get('node').get('innerHTML') + '</strong>');
248
<div id="sidebar" class="yui3-u">
250
<div id="toc" class="sidebox">
252
<h2 class="no-toc">Table of Contents</h2>
258
<a href="#setting-up-the-delegate-container-and-items">Setting up the Delegate container and items</a>
261
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
264
<a href="#making-the-nodes-draggable">Making the Nodes draggable</a>
267
<a href="#listening-to-some-events">Listening to some events</a>
270
<a href="#adding-a-drop-target">Adding a Drop Target</a>
273
<a href="#full-javascript-source">Full Javascript Source</a>
281
<div class="sidebox">
283
<h2 class="no-toc">Examples</h2>
287
<ul class="examples">
290
<li data-description="A simple drag interaction that doesn't require a drop interaction.">
291
<a href="simple-drag.html">Simple Drag</a>
296
<li data-description="How to apply the Drag Plugin to a node.">
297
<a href="drag-plugin.html">Drag - Node plugin</a>
302
<li data-description="A simple proxy drag interaction that doesn't require a drop interaction.">
303
<a href="proxy-drag.html">Drag - Proxy</a>
308
<li data-description="How to constrain a draggable Node to another Node's region.">
309
<a href="constrained-drag.html">Drag - Constrained to a Region</a>
314
<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.">
315
<a href="groups-drag.html">Drag - Interaction Groups</a>
320
<li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
321
<a href="shim-drag.html">Using the Drag Shim</a>
326
<li data-description="How to use the Drop Target events to code your application.">
327
<a href="drop-code.html">Using Drop Based Coding</a>
332
<li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
333
<a href="winscroll.html">Window Scrolling</a>
338
<li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
339
<a href="delegate.html">Drag Delegation</a>
344
<li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
345
<a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
350
<li data-description="How to use Drag plugins with a DD Delegate based solution.">
351
<a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
356
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
357
<a href="list-drag.html">List Reorder w/Bubbling</a>
362
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
363
<a href="scroll-list.html">List Reorder w/Scrolling</a>
368
<li data-description="How to make an animated node a Drop target.">
369
<a href="anim-drop.html">Animated Drop Targets</a>
374
<li data-description="Example Photo Browser application.">
375
<a href="photo-browser.html">Photo Browser</a>
380
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
381
<a href="portal-drag.html">Portal Style Example</a>
395
<div class="sidebox">
397
<h2 class="no-toc">Examples That Use This Component</h2>
401
<ul class="examples">
436
<li data-description="Working with multiple YUI instances.">
437
<a href="../yui/yui-multi.html">Multiple Instances</a>
442
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
443
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
455
<script src="../assets/vendor/prettify/prettify-min.js"></script>
456
<script>prettyPrint();</script>