|
1 |
|
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
3 <html> |
|
4 <head> |
|
5 <title>YUI Library Examples: Drag & Drop: Drop Based Coding</title> |
|
6 <meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
7 <link rel="stylesheet" type="text/css" href="../../assets/yui.css" > |
|
8 |
|
9 <style> |
|
10 /*Supplemental CSS for the YUI distribution*/ |
|
11 #custom-doc { width: 95%; min-width: 950px; } |
|
12 #pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
13 /* #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}*/ |
|
14 </style> |
|
15 |
|
16 <link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css"> |
|
17 |
|
18 <!--there is no custom header content for this example--> |
|
19 |
|
20 <link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
21 <script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
22 |
|
23 </head> |
|
24 <body id="yahoo-com" class=" yui-skin-sam"> |
|
25 <div id="custom-doc" class="yui-t2"> |
|
26 <div id="hd"> |
|
27 <div id="ygunav"> |
|
28 <p> |
|
29 <em> |
|
30 <a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
31 </em> |
|
32 </p> |
|
33 <form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
34 <input name="vs" type="hidden" value="developer.yahoo.com"> |
|
35 <input name="vs" type="hidden" value="yuiblog.com"> |
|
36 <div id="sitesearch"> |
|
37 <label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
38 <input type="text" id="searchinput" name="p"> |
|
39 <input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
40 </div> |
|
41 </form> |
|
42 </div> |
|
43 <div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
44 <div id="pagetitle"><h1>YUI Library Examples: Drag & Drop: Drop Based Coding</h1></div> |
|
45 </div> |
|
46 <div id="bd"> |
|
47 |
|
48 <div id="bar-note"><p><strong>Note:</strong> This is YUI 3.x. Looking for <a href="http://developer.yahoo.com/yui/">YUI 2.x</a>?</p></div> |
|
49 |
|
50 <div id="yui-main"> |
|
51 <div class="yui-b"> |
|
52 <div class="yui-ge"> |
|
53 <div class="yui-u first example" id="main"> |
|
54 |
|
55 <h2>Drag & Drop: Drop Based Coding</h2> |
|
56 |
|
57 <div id="example" class="promo"> |
|
58 <p> |
|
59 <p>This example shows how to use the Drop Target events to code your application.</p> |
|
60 </p> |
|
61 |
|
62 <div class="module example-container "> |
|
63 <div class="hd exampleHd"> |
|
64 <p class="newWindowButton yui-skin-sam"> |
|
65 <a href="drop-code_clean.html" target="_blank">View example in new window.</a> |
|
66 </p> |
|
67 </div> <div id="example-canvas" class="bd"> |
|
68 |
|
69 |
|
70 <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
71 |
|
72 <style> |
|
73 .drag { |
|
74 height: 50px; |
|
75 width: 50px; |
|
76 border: 1px solid black; |
|
77 background-color: #004C6D; |
|
78 color: white; |
|
79 cursor: move; |
|
80 float: left; |
|
81 margin: 4px; |
|
82 z-index: 2; |
|
83 } |
|
84 #play { |
|
85 border: 1px solid black; |
|
86 height: 300px; |
|
87 position: relative; |
|
88 } |
|
89 #drop { |
|
90 position: absolute; |
|
91 bottom: 5px; |
|
92 right: 5px; |
|
93 border: 1px solid black; |
|
94 background-color: #8DD5E7; |
|
95 height: 75px; |
|
96 width: 65%; |
|
97 z-index: 1; |
|
98 } |
|
99 #drop p { |
|
100 margin: 1em; |
|
101 } |
|
102 #drop p strong { |
|
103 font-weight: bold; |
|
104 } |
|
105 #drop.yui-dd-drop-over { |
|
106 background-color: #FFA928; |
|
107 } |
|
108 </style> |
|
109 |
|
110 <div id="play"> |
|
111 <div id="drag1" class="drag">Drag #1</div> |
|
112 <div id="drag2" class="drag">Drag #2</div> |
|
113 <div id="drag3" class="drag">Drag #3</div> |
|
114 <div id="drag4" class="drag">Drag #4</div> |
|
115 <div id="drag5" class="drag">Drag #5</div> |
|
116 <div id="drop"></div> |
|
117 </div> |
|
118 |
|
119 <script> |
|
120 |
|
121 YUI({base:"../../build/", timeout: 10000}).use('dd-drop', 'dd-constrain', function(Y) { |
|
122 var data = { |
|
123 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
124 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
125 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
126 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
127 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
128 }; |
|
129 var drags = Y.Node.all('#play div.drag'); |
|
130 drags.each(function(v, k) { |
|
131 var thisData = {}; |
|
132 Y.mix(thisData, data[v.get('id')]); |
|
133 var dd = new Y.DD.Drag({ |
|
134 node: v, |
|
135 constrain2node: '#play', |
|
136 dragMode: 'intersect', |
|
137 data: thisData |
|
138 }); |
|
139 dd.on('drag:end', function(e) { |
|
140 e.preventDefault(); |
|
141 }); |
|
142 }); |
|
143 |
|
144 var drop = new Y.DD.Drop({ |
|
145 node: '#drop' |
|
146 }); |
|
147 drop.on('drop:hit', function(e) { |
|
148 var drag = e.drag; |
|
149 var data = drag.get('data'); |
|
150 var out = ['id: ' + drag.get('node').get('id')]; |
|
151 Y.each(data, function(v, k) { |
|
152 out[out.length] = k + ': ' + v; |
|
153 }); |
|
154 var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>'; |
|
155 this.get('node').set('innerHTML', str); |
|
156 }); |
|
157 }); |
|
158 </script> |
|
159 |
|
160 <!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
161 |
|
162 |
|
163 </div> |
|
164 </div> |
|
165 </div> |
|
166 |
|
167 <h3>Setting up the HTML</h3> |
|
168 <p>First we need to create the HTML for the example.</p> |
|
169 <textarea name="code" class="HTML"> |
|
170 <div id="play"> |
|
171 <div id="drag1" class="drag">Drag #1</div> |
|
172 <div id="drag2" class="drag">Drag #2</div> |
|
173 <div id="drag3" class="drag">Drag #3</div> |
|
174 <div id="drag4" class="drag">Drag #4</div> |
|
175 <div id="drag5" class="drag">Drag #5</div> |
|
176 <div id="drop"></div> |
|
177 </div> |
|
178 </textarea> |
|
179 <p>Now we give the HTML some CSS to make them visible.</p> |
|
180 <textarea name="code" class="CSS"> |
|
181 .drag { |
|
182 height: 50px; |
|
183 width: 50px; |
|
184 border: 1px solid black; |
|
185 background-color: #004C6D; |
|
186 color: white; |
|
187 cursor: move; |
|
188 float: left; |
|
189 margin: 4px; |
|
190 z-index: 2; |
|
191 } |
|
192 #play { |
|
193 border: 1px solid black; |
|
194 height: 300px; |
|
195 position: relative; |
|
196 } |
|
197 #drop { |
|
198 position: absolute; |
|
199 bottom: 5px; |
|
200 right: 5px; |
|
201 border: 1px solid black; |
|
202 background-color: #8DD5E7; |
|
203 height: 75px; |
|
204 width: 65%; |
|
205 z-index: 1; |
|
206 } |
|
207 #drop p { |
|
208 margin: 1em; |
|
209 } |
|
210 #drop p strong { |
|
211 font-weight: bold; |
|
212 } |
|
213 #drop.yui-dd-drop-over { |
|
214 background-color: #FFA928; |
|
215 } |
|
216 </textarea> |
|
217 |
|
218 <h3>Setting up the YUI Instance</h3> |
|
219 <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> |
|
220 <textarea name="code" class="JScript"> |
|
221 YUI().use('dd-drop', 'dd-constrain'); |
|
222 </textarea> |
|
223 |
|
224 <h3>Making the Nodes draggable</h3> |
|
225 <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> |
|
226 <p>In this example we are using the data config option of the drag to associate data with this Drag instance.</p> |
|
227 <p>So we have set up an object literal containing information about our drag items.</p> |
|
228 <textarea name="code" class="JScript"> |
|
229 var data = { |
|
230 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
231 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
232 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
233 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
234 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
235 }; |
|
236 </textarea> |
|
237 <p>Now we walk through the nodes and create a drag instance from each of them.</p> |
|
238 <textarea name="code" class="JScript"> |
|
239 YUI().use('dd-drop', 'dd-constrain', function(Y) { |
|
240 //Data to attach to each drag object |
|
241 var data = { |
|
242 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
243 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
244 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
245 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
246 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
247 }; |
|
248 //Get all the divs with the class drag |
|
249 var drags = Y.Node.all('#play div.drag'); |
|
250 //Walk through each one |
|
251 drags.each(function(v, k) { |
|
252 //scope a local var for the data |
|
253 var thisData = {}; |
|
254 //Using Y.mix to break this data from the data above |
|
255 Y.mix(thisData, data[v.get('id')]); |
|
256 |
|
257 //Create the new Drag Instance |
|
258 var dd = new Y.DD.Drag({ |
|
259 //Give it the node |
|
260 node: v, |
|
261 //Keep it inside the work area |
|
262 constrain2node: '#play', |
|
263 //Set the dragMode to intersect |
|
264 dragMode: 'intersect', |
|
265 //Attach the data here.. |
|
266 data: thisData |
|
267 }); |
|
268 //Prevent the default end event (this moves the node back to it's start position) |
|
269 dd.on('drag:end', function(e) { |
|
270 e.preventDefault(); |
|
271 }); |
|
272 }); |
|
273 }); |
|
274 </textarea> |
|
275 |
|
276 |
|
277 <h3>Setting up the Drop Target</h3> |
|
278 <p>Here we set up the Drop Target and assign a listener to it.</p> |
|
279 <textarea name="code" class="JScript"> |
|
280 var drop = new Y.DD.Drop({ |
|
281 node: '#drop' |
|
282 }); |
|
283 //Listen for a drop:hit on this target |
|
284 drop.on('drop:hit', function(e) { |
|
285 //Now we get the drag instance that triggered the drop hit |
|
286 var drag = e.drag; |
|
287 //get the data from it |
|
288 var data = drag.get('data'); |
|
289 |
|
290 //Do something with the data |
|
291 var out = ['id: ' + drag.get('node').get('id')]; |
|
292 Y.each(data, function(v, k) { |
|
293 out[out.length] = k + ': ' + v; |
|
294 }); |
|
295 var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>'; |
|
296 this.get('node').set('innerHTML', str); |
|
297 }); |
|
298 </textarea> |
|
299 |
|
300 <h3>Full Example Source</h3> |
|
301 <textarea name="code" class="JScript"> |
|
302 YUI().use('dd-drop', 'dd-constrain', function(Y) { |
|
303 var data = { |
|
304 'drag1': { color: 'white', size: 'x-small', price: '$5.00' }, |
|
305 'drag2': { color: 'blue', size: 'small', price: '$6.00' }, |
|
306 'drag3': { color: 'green', size: 'medium', price: '$7.00' }, |
|
307 'drag4': { color: 'red', size: 'large', price: '$10.00' }, |
|
308 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' } |
|
309 }; |
|
310 var drags = Y.Node.all('#play div.drag'); |
|
311 drags.each(function(v, k, items) { |
|
312 var thisData = {}; |
|
313 Y.mix(thisData, data[v.get('id')]); |
|
314 var dd = new Y.DD.Drag({ |
|
315 node: items.item(k), |
|
316 constrain2node: '#play', |
|
317 dragMode: 'intersect', |
|
318 data: thisData |
|
319 }); |
|
320 //Prevent the default end event (this moves the node back to it's start position) |
|
321 dd.on('drag:end', function(e) { |
|
322 e.preventDefault(); |
|
323 }); |
|
324 }); |
|
325 |
|
326 var drop = new Y.DD.Drop({ |
|
327 node: '#drop' |
|
328 }); |
|
329 drop.on('drop:hit', function(e) { |
|
330 var drag = e.drag; |
|
331 var data = drag.get('data'); |
|
332 var out = ['id: ' + drag.get('node').get('id')]; |
|
333 Y.each(data, function(v, k) { |
|
334 out[out.length] = k + ': ' + v; |
|
335 }); |
|
336 var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>'; |
|
337 this.get('node').set('innerHTML', str); |
|
338 }); |
|
339 }); |
|
340 </textarea> |
|
341 </div> |
|
342 <div class="yui-u sidebar"> |
|
343 |
|
344 |
|
345 <div id="examples" class="mod box4"> |
|
346 <div class="hd"> |
|
347 <h4> |
|
348 Drag & Drop Examples:</h4> |
|
349 </div> |
|
350 <div class="bd"> |
|
351 <ul> |
|
352 <li><a href='../dd/simple-drag.html'>Simple Drag</a></li><li><a href='../dd/drag-plugin.html'>Drag Node Plugin</a></li><li><a href='../dd/proxy-drag.html'>Proxy Drag</a></li><li><a href='../dd/constrained-drag.html'>Drag Constrained to a Region</a></li><li><a href='../dd/groups-drag.html'>Interaction Groups</a></li><li><a href='../dd/shim-drag.html'>Using the Drag Shim</a></li><li><a href='../dd/anim-drop.html'>Animated Drop Targets</a></li><li class='selected'><a href='../dd/drop-code.html'>Drop Based Coding</a></li><li><a href='../dd/winscroll.html'>Window Scrolling</a></li><li><a href='../dd/list-drag.html'>List reorder w/Bubbling</a></li><li><a href='../dd/scroll-list.html'>List reorder w/Scrolling</a></li><li><a href='../dd/portal-drag.html'>Portal Style Example</a></li><li><a href='../dd/photo-browser.html'>Photo Browser</a></li> </ul> |
|
353 </div> |
|
354 </div> |
|
355 |
|
356 <div class="mod box4"> |
|
357 <div class="hd"> |
|
358 <h4>More Drag & Drop Resources:</h4> |
|
359 </div> |
|
360 <div class="bd"> |
|
361 <ul> |
|
362 <!-- <li><a href="http://developer.yahoo.com/yui/dd/">User's Guide</a> (external)</li> --> |
|
363 <li><a href="../../api/module_dd.html">API Documentation</a></li> |
|
364 </ul> |
|
365 </div> |
|
366 </div> |
|
367 </div> |
|
368 </div> |
|
369 |
|
370 </div> |
|
371 </div> |
|
372 |
|
373 |
|
374 <div class="yui-b toc3" id="tocWrapper"> |
|
375 <!-- TABLE OF CONTENTS --> |
|
376 <div id="toc"> |
|
377 |
|
378 <ul> |
|
379 <li class="sect first">YUI 3.x Project</li><li class="item"><a title="The Yahoo! User Interface (YUI) Library, 3.x Branch, " href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site (external)</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="The YUI Library can be downloaded from SourceForge" href="http://sourceforge.net/projects/yui/">YUI 3 on Sourceforge (external)</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/3/license.html">YUI License (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI (Global Prerequisite) - Functional Examples" href="../../examples/yui/index.html">YUI (Global Prerequisite)</a></li><li class="item"><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="item"><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Plugin - Functional Examples" href="../../examples/plugin/index.html">Plugin</a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget</a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="Cache - Functional Examples" href="../../examples/cache/index.html">Cache</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="DataSchema - Functional Examples" href="../../examples/dataschema/index.html">DataSchema</a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource</a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType</a></li><li class="selected "><a title="Drag & Drop - Functional Examples" href="../../examples/dd/index.html">Drag & Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="item"><a title="History - Functional Examples" href="../../examples/history/index.html">History</a></li><li class="item"><a title="ImageLoader - Functional Examples" href="../../examples/imageloader/index.html">ImageLoader</a></li><li class="item"><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Queue - Functional Examples" href="../../examples/queue/index.html">Queue</a></li><li class="item"><a title="Stylesheet - Functional Examples" href="../../examples/stylesheet/index.html">Stylesheet</a></li><li class="sect">YUI 3 Widgets - Examples</li><li class="item"><a title="Overlay - Functional Examples" href="../../examples/overlay/index.html">Overlay</a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider</a></li><li class="sect">YUI 3 Node Plugins - Examples</li><li class="item"><a title="FocusManager Node Plugin - Functional Examples" href="../../examples/node-focusmanager/index.html">FocusManager Node Plugin</a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin</a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">YUI 3 Developer Tools - Examples</li><li class="item"><a title="Console - Functional Examples" href="../../examples/console/index.html">Console</a></li><li class="item"><a title="Profiler - Functional Examples" href="../../examples/profiler/index.html">Profiler</a></li><li class="item"><a title="Test - Functional Examples" href="../../examples/test/index.html">Test</a></li><li class="sect">The YUI Community</li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="item"><a title="The Yahoo! Group YDN-JavaScript hosts the YUI community forum" href="http://tech.groups.yahoo.com/group/ydn-javascript/">YUI Forum (external)</a></li><li class="item"><a title="The Yahoo! Group yui3 is dedicated to the 3.x branch of the Yahoo! User Interface (YUI) Library." href="http://tech.groups.yahoo.com/group/yui3/">YUI 3 Forum (external)</a></li><li class="item"><a title="YUI is used by Yahoo! and by hundreds of other sites, including many you know and love." href="/yui/poweredby/">YUI Sightings (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li><li class="sect">YUI Articles on the YUI Website</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Reporting Bugs and Making Feature Requests for YUI Components" href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests (external)</a></li><li class="item"><a title="Serve YUI source files from Yahoo! -- free, fast, and simple" href="http://developer.yahoo.com/yui/3/articles/hosting/">Serving YUI Files from Yahoo! (external)</a></li></ul> |
|
380 </div> |
|
381 </div> |
|
382 </div><!--closes bd--> |
|
383 |
|
384 <div id="ft"> |
|
385 <p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
386 <p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
387 <a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
388 <a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
389 <a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
390 </div> |
|
391 </div> |
|
392 <script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
393 <script language="javascript"> |
|
394 dp.SyntaxHighlighter.HighlightAll('code'); |
|
395 </script> |
|
396 </body> |
|
397 </html> |