|
1 <!DOCTYPE html> |
|
2 <html lang="en"> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Example: Using Drop Based Coding</title> |
|
6 <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
7 <link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
8 <link rel="stylesheet" href="../assets/css/main.css"> |
|
9 <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
10 <link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
11 <script src="../../build/yui/yui-min.js"></script> |
|
12 |
|
13 </head> |
|
14 <body> |
|
15 <!-- |
|
16 <a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
17 --> |
|
18 <div id="doc"> |
|
19 <div id="hd"> |
|
20 <h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
21 </div> |
|
22 |
|
23 <a href="#toc" class="jump">Jump to Table of Contents</a> |
|
24 |
|
25 |
|
26 <h1>Example: Using Drop Based Coding</h1> |
|
27 <div class="yui3-g"> |
|
28 <div class="yui3-u-3-4"> |
|
29 <div id="main"> |
|
30 <div class="content"><div class="intro"> |
|
31 <p>This example shows how to use the Drop Target events to code your application.</p> |
|
32 </div> |
|
33 |
|
34 <div class="example"> |
|
35 <style> |
|
36 .drag { |
|
37 height: 50px; |
|
38 width: 50px; |
|
39 border: 1px solid black; |
|
40 background-color: #004C6D; |
|
41 color: white; |
|
42 cursor: move; |
|
43 float: left; |
|
44 margin: 4px; |
|
45 z-index: 2; |
|
46 } |
|
47 #play { |
|
48 border: 1px solid black; |
|
49 height: 300px; |
|
50 position: relative; |
|
51 } |
|
52 #drop { |
|
53 position: absolute; |
|
54 bottom: 5px; |
|
55 right: 5px; |
|
56 border: 1px solid black; |
|
57 background-color: #8DD5E7; |
|
58 height: 75px; |
|
59 width: 65%; |
|
60 z-index: 1; |
|
61 } |
|
62 #drop p { |
|
63 margin: 1em; |
|
64 } |
|
65 #drop p strong { |
|
66 font-weight: bold; |
|
67 } |
|
68 #drop.yui3-dd-drop-over { |
|
69 background-color: #FFA928; |
|
70 } |
|
71 |
|
72 |
|
73 </style> |
|
74 |
|
75 <div id="play"> |
|
76 <div id="drag1" class="drag">Drag #1</div> |
|
77 <div id="drag2" class="drag">Drag #2</div> |
|
78 <div id="drag3" class="drag">Drag #3</div> |
|
79 <div id="drag4" class="drag">Drag #4</div> |
|
80 <div id="drag5" class="drag">Drag #5</div> |
|
81 <div id="drop"></div> |
|
82 </div> |
|
83 |
|
84 |
|
85 |
|
86 <script> |
|
87 YUI().use('dd-drop', 'dd-constrain', function(Y) { |
|
88 var data = { |
|
89 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
90 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
91 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
92 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
93 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
94 }; |
|
95 var drags = Y.Node.all('#play div.drag'); |
|
96 drags.each(function(v, k) { |
|
97 var thisData = {}; |
|
98 Y.mix(thisData, data[v.get('id')]); |
|
99 var dd = new Y.DD.Drag({ |
|
100 node: v, |
|
101 dragMode: 'intersect', |
|
102 data: thisData |
|
103 }).plug(Y.Plugin.DDConstrained, { |
|
104 constrain2node: '#play' |
|
105 }); |
|
106 dd.on('drag:end', function(e) { |
|
107 e.preventDefault(); |
|
108 }); |
|
109 }); |
|
110 |
|
111 var drop = new Y.DD.Drop({ |
|
112 node: '#drop' |
|
113 }); |
|
114 drop.on('drop:hit', function(e) { |
|
115 var drag = e.drag; |
|
116 var data = drag.get('data'); |
|
117 var out = ['id: ' + drag.get('node').get('id')]; |
|
118 Y.each(data, function(v, k) { |
|
119 out[out.length] = k + ': ' + v; |
|
120 }); |
|
121 var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>'; |
|
122 this.get('node').set('innerHTML', str); |
|
123 }); |
|
124 }); |
|
125 |
|
126 </script> |
|
127 |
|
128 </div> |
|
129 |
|
130 <h3 id="setting-up-the-html">Setting up the HTML</h3> |
|
131 <p>First we need to create the HTML for the example.</p> |
|
132 |
|
133 <pre class="code prettyprint"><div id="play"> |
|
134 <div id="drag1" class="drag">Drag #1</div> |
|
135 <div id="drag2" class="drag">Drag #2</div> |
|
136 <div id="drag3" class="drag">Drag #3</div> |
|
137 <div id="drag4" class="drag">Drag #4</div> |
|
138 <div id="drag5" class="drag">Drag #5</div> |
|
139 <div id="drop"></div> |
|
140 </div></pre> |
|
141 |
|
142 |
|
143 <p>Now we give the HTML some CSS to make them visible.</p> |
|
144 |
|
145 <pre class="code prettyprint">.drag { |
|
146 height: 50px; |
|
147 width: 50px; |
|
148 border: 1px solid black; |
|
149 background-color: #004C6D; |
|
150 color: white; |
|
151 cursor: move; |
|
152 float: left; |
|
153 margin: 4px; |
|
154 z-index: 2; |
|
155 } |
|
156 #play { |
|
157 border: 1px solid black; |
|
158 height: 300px; |
|
159 position: relative; |
|
160 } |
|
161 #drop { |
|
162 position: absolute; |
|
163 bottom: 5px; |
|
164 right: 5px; |
|
165 border: 1px solid black; |
|
166 background-color: #8DD5E7; |
|
167 height: 75px; |
|
168 width: 65%; |
|
169 z-index: 1; |
|
170 } |
|
171 #drop p { |
|
172 margin: 1em; |
|
173 } |
|
174 #drop p strong { |
|
175 font-weight: bold; |
|
176 } |
|
177 #drop.yui3-dd-drop-over { |
|
178 background-color: #FFA928; |
|
179 }</pre> |
|
180 |
|
181 |
|
182 <h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3> |
|
183 <p>Now we need to create our YUI instance and tell it to load the <code>dd-drop</code> and <code>dd-constrain</code> modules.</p> |
|
184 |
|
185 <pre class="code prettyprint">YUI().use('dd-drop', 'dd-constrain');</pre> |
|
186 |
|
187 |
|
188 <h3 id="making-the-nodes-draggable">Making the Nodes draggable</h3> |
|
189 <p>Now that we have a YUI instance with the <code>dd-drop</code> module, we need to instantiate the <code>Drag</code> instance on each Drag Node.</p> |
|
190 <p>In this example we are using the data config option of the drag to associate data with this Drag instance.</p> |
|
191 <p>So we have set up an object literal containing information about our drag items.</p> |
|
192 |
|
193 <pre class="code prettyprint">var data = { |
|
194 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
195 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
196 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
197 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
198 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
199 };</pre> |
|
200 |
|
201 |
|
202 <p>Now we walk through the nodes and create a drag instance from each of them.</p> |
|
203 |
|
204 <pre class="code prettyprint">YUI().use('dd-drop', 'dd-constrain', function(Y) { |
|
205 //Data to attach to each drag object |
|
206 var data = { |
|
207 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
208 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
209 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
210 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
211 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
212 }; |
|
213 //Get all the divs with the class drag |
|
214 var drags = Y.Node.all('#play div.drag'); |
|
215 //Walk through each one |
|
216 drags.each(function(v, k) { |
|
217 //scope a local var for the data |
|
218 var thisData = {}; |
|
219 //Using Y.mix to break this data from the data above |
|
220 Y.mix(thisData, data[v.get('id')]); |
|
221 |
|
222 //Create the new Drag Instance |
|
223 var dd = new Y.DD.Drag({ |
|
224 //Give it the node |
|
225 node: v, |
|
226 //Set the dragMode to intersect |
|
227 dragMode: 'intersect', |
|
228 //Attach the data here. |
|
229 data: thisData |
|
230 }).plug(Y.Plugin.DDConstrained, { |
|
231 //Keep it inside the work area |
|
232 constrain2node: '#play' |
|
233 }); |
|
234 //Prevent the default end event (this moves the node back to its start position) |
|
235 dd.on('drag:end', function(e) { |
|
236 e.preventDefault(); |
|
237 }); |
|
238 }); |
|
239 });</pre> |
|
240 |
|
241 |
|
242 <h3 id="setting-up-the-drop-target">Setting up the Drop Target</h3> |
|
243 <p>Here we set up the Drop Target and assign a listener to it.</p> |
|
244 |
|
245 <pre class="code prettyprint">var drop = new Y.DD.Drop({ |
|
246 node: '#drop' |
|
247 }); |
|
248 //Listen for a drop:hit on this target |
|
249 drop.on('drop:hit', function(e) { |
|
250 //Now we get the drag instance that triggered the drop hit |
|
251 var drag = e.drag; |
|
252 //get the data from it |
|
253 var data = drag.get('data'); |
|
254 |
|
255 //Do something with the data |
|
256 var out = ['id: ' + drag.get('node').get('id')]; |
|
257 Y.each(data, function(v, k) { |
|
258 out[out.length] = k + ': ' + v; |
|
259 }); |
|
260 var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>'; |
|
261 this.get('node').set('innerHTML', str); |
|
262 });</pre> |
|
263 |
|
264 |
|
265 <h3 id="full-example-source">Full Example Source</h3> |
|
266 |
|
267 <pre class="code prettyprint">YUI().use('dd-drop', 'dd-constrain', function(Y) { |
|
268 var data = { |
|
269 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
270 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
271 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
272 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
273 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
274 }; |
|
275 var drags = Y.Node.all('#play div.drag'); |
|
276 drags.each(function(v, k) { |
|
277 var thisData = {}; |
|
278 Y.mix(thisData, data[v.get('id')]); |
|
279 var dd = new Y.DD.Drag({ |
|
280 node: v, |
|
281 dragMode: 'intersect', |
|
282 data: thisData |
|
283 }).plug(Y.Plugin.DDConstrained, { |
|
284 constrain2node: '#play' |
|
285 }); |
|
286 dd.on('drag:end', function(e) { |
|
287 e.preventDefault(); |
|
288 }); |
|
289 }); |
|
290 |
|
291 var drop = new Y.DD.Drop({ |
|
292 node: '#drop' |
|
293 }); |
|
294 drop.on('drop:hit', function(e) { |
|
295 var drag = e.drag; |
|
296 var data = drag.get('data'); |
|
297 var out = ['id: ' + drag.get('node').get('id')]; |
|
298 Y.each(data, function(v, k) { |
|
299 out[out.length] = k + ': ' + v; |
|
300 }); |
|
301 var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>'; |
|
302 this.get('node').set('innerHTML', str); |
|
303 }); |
|
304 });</pre> |
|
305 |
|
306 </div> |
|
307 </div> |
|
308 </div> |
|
309 |
|
310 <div class="yui3-u-1-4"> |
|
311 <div class="sidebar"> |
|
312 |
|
313 <div id="toc" class="sidebox"> |
|
314 <div class="hd"> |
|
315 <h2 class="no-toc">Table of Contents</h2> |
|
316 </div> |
|
317 |
|
318 <div class="bd"> |
|
319 <ul class="toc"> |
|
320 <li> |
|
321 <a href="#setting-up-the-html">Setting up the HTML</a> |
|
322 </li> |
|
323 <li> |
|
324 <a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a> |
|
325 </li> |
|
326 <li> |
|
327 <a href="#making-the-nodes-draggable">Making the Nodes draggable</a> |
|
328 </li> |
|
329 <li> |
|
330 <a href="#setting-up-the-drop-target">Setting up the Drop Target</a> |
|
331 </li> |
|
332 <li> |
|
333 <a href="#full-example-source">Full Example Source</a> |
|
334 </li> |
|
335 </ul> |
|
336 </div> |
|
337 </div> |
|
338 |
|
339 |
|
340 |
|
341 <div class="sidebox"> |
|
342 <div class="hd"> |
|
343 <h2 class="no-toc">Examples</h2> |
|
344 </div> |
|
345 |
|
346 <div class="bd"> |
|
347 <ul class="examples"> |
|
348 |
|
349 |
|
350 <li data-description="A simple drag interaction that doesn't require a drop interaction."> |
|
351 <a href="simple-drag.html">Simple Drag</a> |
|
352 </li> |
|
353 |
|
354 |
|
355 |
|
356 <li data-description="How to apply the Drag Plugin to a node."> |
|
357 <a href="drag-plugin.html">Drag - Node plugin</a> |
|
358 </li> |
|
359 |
|
360 |
|
361 |
|
362 <li data-description="A simple proxy drag interaction that doesn't require a drop interaction."> |
|
363 <a href="proxy-drag.html">Drag - Proxy</a> |
|
364 </li> |
|
365 |
|
366 |
|
367 |
|
368 <li data-description="How to constrain a draggable Node to another Node's region."> |
|
369 <a href="constrained-drag.html">Drag - Constrained to a Region</a> |
|
370 </li> |
|
371 |
|
372 |
|
373 |
|
374 <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."> |
|
375 <a href="groups-drag.html">Drag - Interaction Groups</a> |
|
376 </li> |
|
377 |
|
378 |
|
379 |
|
380 <li data-description="The use of the drag shim when dragging nodes over other troublesome nodes."> |
|
381 <a href="shim-drag.html">Using the Drag Shim</a> |
|
382 </li> |
|
383 |
|
384 |
|
385 |
|
386 <li data-description="How to use the Drop Target events to code your application."> |
|
387 <a href="drop-code.html">Using Drop Based Coding</a> |
|
388 </li> |
|
389 |
|
390 |
|
391 |
|
392 <li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag."> |
|
393 <a href="winscroll.html">Window Scrolling</a> |
|
394 </li> |
|
395 |
|
396 |
|
397 |
|
398 <li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items."> |
|
399 <a href="delegate.html">Drag Delegation</a> |
|
400 </li> |
|
401 |
|
402 |
|
403 |
|
404 <li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target."> |
|
405 <a href="delegate-drop.html">Drag Delegation with a Drop Target</a> |
|
406 </li> |
|
407 |
|
408 |
|
409 |
|
410 <li data-description="How to use Drag plugins with a DD Delegate based solution."> |
|
411 <a href="delegate-plugins.html">Using Drag Plugins with Delegate</a> |
|
412 </li> |
|
413 |
|
414 |
|
415 |
|
416 <li data-description="This example shows how to make a sortable list using Custom Event Bubbling."> |
|
417 <a href="list-drag.html">List Reorder w/Bubbling</a> |
|
418 </li> |
|
419 |
|
420 |
|
421 |
|
422 <li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling."> |
|
423 <a href="scroll-list.html">List Reorder w/Scrolling</a> |
|
424 </li> |
|
425 |
|
426 |
|
427 |
|
428 <li data-description="How to make an animated node a Drop target."> |
|
429 <a href="anim-drop.html">Animated Drop Targets</a> |
|
430 </li> |
|
431 |
|
432 |
|
433 |
|
434 <li data-description="Example Photo Browser application."> |
|
435 <a href="photo-browser.html">Photo Browser</a> |
|
436 </li> |
|
437 |
|
438 |
|
439 |
|
440 <li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
441 <a href="portal-drag.html">Portal Style Example</a> |
|
442 </li> |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 </ul> |
|
450 </div> |
|
451 </div> |
|
452 |
|
453 |
|
454 |
|
455 <div class="sidebox"> |
|
456 <div class="hd"> |
|
457 <h2 class="no-toc">Examples That Use This Component</h2> |
|
458 </div> |
|
459 |
|
460 <div class="bd"> |
|
461 <ul class="examples"> |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 <li data-description="Working with multiple YUI instances."> |
|
497 <a href="../yui/yui-multi.html">Multiple Instances</a> |
|
498 </li> |
|
499 |
|
500 |
|
501 |
|
502 <li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input"> |
|
503 <a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a> |
|
504 </li> |
|
505 |
|
506 |
|
507 </ul> |
|
508 </div> |
|
509 </div> |
|
510 |
|
511 </div> |
|
512 </div> |
|
513 </div> |
|
514 </div> |
|
515 |
|
516 <script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
517 <script>prettyPrint();</script> |
|
518 |
|
519 <script> |
|
520 YUI.Env.Tests = { |
|
521 examples: [], |
|
522 project: '../assets', |
|
523 assets: '../assets/dd', |
|
524 name: 'drop-code', |
|
525 title: 'Using Drop Based Coding', |
|
526 newWindow: '', |
|
527 auto: false |
|
528 }; |
|
529 YUI.Env.Tests.examples.push('simple-drag'); |
|
530 YUI.Env.Tests.examples.push('drag-plugin'); |
|
531 YUI.Env.Tests.examples.push('proxy-drag'); |
|
532 YUI.Env.Tests.examples.push('constrained-drag'); |
|
533 YUI.Env.Tests.examples.push('groups-drag'); |
|
534 YUI.Env.Tests.examples.push('shim-drag'); |
|
535 YUI.Env.Tests.examples.push('drop-code'); |
|
536 YUI.Env.Tests.examples.push('winscroll'); |
|
537 YUI.Env.Tests.examples.push('delegate'); |
|
538 YUI.Env.Tests.examples.push('delegate-drop'); |
|
539 YUI.Env.Tests.examples.push('delegate-plugins'); |
|
540 YUI.Env.Tests.examples.push('list-drag'); |
|
541 YUI.Env.Tests.examples.push('scroll-list'); |
|
542 YUI.Env.Tests.examples.push('anim-drop'); |
|
543 YUI.Env.Tests.examples.push('photo-browser'); |
|
544 YUI.Env.Tests.examples.push('portal-drag'); |
|
545 YUI.Env.Tests.examples.push('yui-multi'); |
|
546 YUI.Env.Tests.examples.push('stylesheet-theme'); |
|
547 |
|
548 </script> |
|
549 <script src="../assets/yui/test-runner.js"></script> |
|
550 |
|
551 |
|
552 |
|
553 </body> |
|
554 </html> |