~bcsaller/juju-gui/charmFind

« back to all changes in this revision

Viewing changes to lib/yui/docs/dd/anim-drop.html

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html>
2
 
<html lang="en">
3
 
<head>
4
 
    <meta charset="utf-8">
5
 
    <title>Example: Animated Drop Targets</title>
6
 
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
 
    <link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
 
    <link rel="stylesheet" href="../assets/css/main.css">
9
 
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
 
    <script src="../../build/yui/yui-min.js"></script>
11
 
</head>
12
 
<body>
13
 
 
14
 
<div id="doc">
15
 
    <h1>Example: Animated Drop Targets</h1>
16
 
 
17
 
    
18
 
        <a href="#toc" class="jump">Jump to Table of Contents</a>
19
 
    
20
 
 
21
 
    <div class="yui3-g">
22
 
        <div class="yui3-u-3-4">
23
 
            <div id="main">
24
 
                <div class="content"><div class="intro">
25
 
<p>This example will show you how to make an animated node a Drop target.</p>
26
 
</div>
27
 
 
28
 
<div class="example newwindow">
29
 
    <a href="anim-drop-example.html" target="_blank" class="button">
30
 
        View Example in New Window
31
 
    </a>
32
 
</div>
33
 
 
34
 
 
35
 
 
36
 
<h3 id="setting-up-the-html">Setting up the HTML</h3>
37
 
<p>First we will create our HTML.</p>
38
 
 
39
 
<pre class="code prettyprint">&lt;div id=&quot;dock&quot;&gt;&lt;&#x2F;div&gt;
40
 
 
41
 
&lt;div id=&quot;drag&quot;&gt;Drag #1&lt;&#x2F;div&gt;
42
 
 
43
 
&lt;div id=&quot;anim1&quot; class=&quot;anim&quot;&gt;Anim #1&lt;&#x2F;div&gt;
44
 
&lt;div id=&quot;anim2&quot; class=&quot;anim&quot;&gt;Anim #2&lt;&#x2F;div&gt;
45
 
&lt;div id=&quot;anim3&quot; class=&quot;anim&quot;&gt;Anim #3&lt;&#x2F;div&gt;
46
 
&lt;div id=&quot;anim4&quot; class=&quot;anim&quot;&gt;Anim #4&lt;&#x2F;div&gt;
47
 
&lt;div id=&quot;anim5&quot; class=&quot;anim&quot;&gt;Anim #5&lt;&#x2F;div&gt;</pre>
48
 
 
49
 
 
50
 
<p>Now we give that HTML some CSS to make it visible.</p>
51
 
 
52
 
<pre class="code prettyprint">.anim {
53
 
    position: relative;
54
 
    height: 50px;
55
 
    width: 100px;
56
 
    border: 1px solid black;
57
 
    background-color: #00B8BF;
58
 
    top: 100px;
59
 
}
60
 
#drag {
61
 
    height: 50px;
62
 
    width: 50px;
63
 
    border: 1px solid black;
64
 
    background-color: #004C6D;
65
 
    color: white;
66
 
    cursor: move;
67
 
    z-index: 5;
68
 
}
69
 
#dock {
70
 
    height: 600px;
71
 
    width: 75px;
72
 
    background-color: #D00050;
73
 
    border: 1px solid black;
74
 
    position: absolute;
75
 
    top: 5px;
76
 
    right: 0px;
77
 
}
78
 
.anim.yui3-dd-drop-over {
79
 
    background-color: #EDFF9F;
80
 
}
81
 
.anim.done {
82
 
    background-color: white;
83
 
}
84
 
#drag1.yui3-dd-drag-over {
85
 
    opacity: .5;
86
 
    filter: alpha(opacity=50);
87
 
}</pre>
88
 
 
89
 
 
90
 
<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
91
 
<p>Now we need to create our YUI instance and tell it to load the <code>dd-drop</code>, <code>dd-plugin</code>, <code>dd-drop-plugin</code> and <code>anim</code> modules.</p>
92
 
 
93
 
<pre class="code prettyprint">YUI().use(&#x27;dd-drop&#x27;, &#x27;anim&#x27;, &#x27;dd-plugin&#x27;, &#x27;dd-drop-plugin&#x27;);</pre>
94
 
 
95
 
 
96
 
<h3 id="making-the-node-draggable">Making the Node draggable</h3>
97
 
<p>Now that we have a YUI instance with the modules loaded, we need to instantiate the <code>Drag</code> instance on this Node.</p>
98
 
<p>In this example we will be using Node plugins to accomplish our tasks.</p>
99
 
 
100
 
<pre class="code prettyprint">YUI().use(&#x27;dd-drop&#x27;, &#x27;anim&#x27;, &#x27;dd-plugin&#x27;, &#x27;dd-drop-plugin&#x27;, function(Y) {
101
 
    &#x2F;&#x2F;Get the node #drag
102
 
    var d = Y.one(&#x27;#drag&#x27;);
103
 
    d.plug(Y.Plugin.Drag, { dragMode: &#x27;intersect&#x27; });
104
 
});</pre>
105
 
 
106
 
 
107
 
<h3 id="animating-the-nodes">Animating the Nodes</h3>
108
 
<p>Now we will set up the Animation sequence that we want to run.</p>
109
 
 
110
 
<pre class="code prettyprint">&#x2F;&#x2F;Get all the div&#x27;s with the class anim
111
 
var anims = Y.Node.all(&#x27;div.anim&#x27;);
112
 
var counter = 0;
113
 
anims.each(function(v, k, items) {
114
 
    &#x2F;&#x2F;Get a reference to the Node instance
115
 
    var a = v; 
116
 
    counter++;
117
 
    &#x2F;&#x2F;Add the FX plugin
118
 
    a.plug(Y.Plugin.NodeFX);
119
 
    &#x2F;&#x2F;Add the Drop plugin
120
 
    a.plug(Y.Plugin.Drop);
121
 
 
122
 
    &#x2F;&#x2F;Set the attributes on the animation
123
 
    a.fx.setAttrs({
124
 
        from: {
125
 
            left: 0
126
 
        },
127
 
        to: {
128
 
            curve: function() {
129
 
                var points = [],
130
 
                    n = 10;
131
 
 
132
 
                for (var i = 0; i &lt; n; ++i) {
133
 
                    points.push([
134
 
                        Math.floor(Math.random()*Y.DOM.winWidth() - 60 - a.get(&#x27;offsetWidth&#x27;)),
135
 
                        Math.floor(Math.random()*Y.DOM.winHeight() - a.get(&#x27;offsetHeight&#x27;))
136
 
                    ]);
137
 
                }
138
 
                return points;
139
 
            }
140
 
        },
141
 
        &#x2F;&#x2F;Do the animation 20 times
142
 
        iterations: 20,
143
 
        &#x2F;&#x2F;Alternate it so it &quot;bounces&quot; across the screen
144
 
        direction: &#x27;alternate&#x27;,
145
 
        &#x2F;&#x2F;Give all of them a different duration so we get different speeds.
146
 
        duration: ((counter * 1.75) + 1)
147
 
    });
148
 
});</pre>
149
 
 
150
 
 
151
 
<h3 id="making-the-node-a-target">Making the Node a Target</h3>
152
 
<p>Using the <code>dd-drop-plugin</code>, we now need to make this animated Node a Drop Target.</p>
153
 
<p>To do that, we need to add the plugin after the fx plugin.</p>
154
 
 
155
 
<pre class="code prettyprint">&#x2F;&#x2F;Add the FX plugin
156
 
a.plug(Y.Plugin.NodeFX);
157
 
&#x2F;&#x2F;Add the Drop plugin
158
 
a.plug(Y.Plugin.Drop);</pre>
159
 
 
160
 
 
161
 
<h3 id="syncing-the-target-with-the-animation">Syncing the Target with the Animation</h3>
162
 
<p>The Drop Target needs to be in sync with the animation, since we are changing the height, width, top and left style.</p>
163
 
<p>We do this by adding a listener to the <code>tween</code> event on the animation instance.</p>
164
 
 
165
 
<pre class="code prettyprint">&#x2F;&#x2F;on tween of the original anim, we need to sync the drop&#x27;s shim.
166
 
a.fx.on(&#x27;tween&#x27;, function() {
167
 
    &#x2F;&#x2F;Do we have an active Drag?
168
 
    if (Y.DD.DDM.activeDrag) {
169
 
        &#x2F;&#x2F;Size this shim
170
 
        this.drop.sizeShim();
171
 
        &#x2F;&#x2F;Force an over target check since we might not be moving the mouse.
172
 
        Y.Lang.later(0, a, function() {
173
 
            this.drop._handleTargetOver();
174
 
        });
175
 
    }
176
 
}, a);</pre>
177
 
 
178
 
 
179
 
<h3 id="full-example-source">Full example source</h3>
180
 
 
181
 
<pre class="code prettyprint">YUI().use(&#x27;dd&#x27;, &#x27;dd-plugin&#x27;, &#x27;dd-drop-plugin&#x27;, &#x27;anim&#x27;, function(Y) {
182
 
    &#x2F;&#x2F;Get the node #drag
183
 
    var d = Y.one(&#x27;#drag&#x27;);
184
 
    d.plug(Y.Plugin.Drag, { dragMode: &#x27;intersect&#x27; });
185
 
    
186
 
    &#x2F;&#x2F;Get all the divs with the class anim
187
 
    var anims = Y.Node.all(&#x27;div.anim&#x27;);
188
 
    var counter = 0;
189
 
    anims.each(function(v, k) {
190
 
        &#x2F;&#x2F;Get a reference to the Node instance
191
 
        var a = v;
192
 
        counter++;
193
 
        &#x2F;&#x2F;Add the FX plugin
194
 
        a.plug(Y.Plugin.NodeFX);
195
 
        &#x2F;&#x2F;Add the Drop plugin
196
 
        a.plug(Y.Plugin.Drop);
197
 
 
198
 
        &#x2F;&#x2F;Set the attributes on the animation
199
 
        a.fx.setAttrs({
200
 
            from: {
201
 
                left: 0
202
 
            },
203
 
            to: {
204
 
                curve: function() {
205
 
                    var points = [],
206
 
                        n = 10;
207
 
 
208
 
                    for (var i = 0; i &lt; n; ++i) {
209
 
                        points.push([
210
 
                            Math.floor(Math.random()*Y.DOM.winWidth() - 60 - a.get(&#x27;offsetWidth&#x27;)),
211
 
                            Math.floor(Math.random()*Y.DOM.winHeight() - a.get(&#x27;offsetHeight&#x27;))
212
 
                        ]);
213
 
                    }
214
 
                    return points;
215
 
                }
216
 
            },
217
 
            &#x2F;&#x2F;Do the animation 20 times
218
 
            iterations: 20,
219
 
            &#x2F;&#x2F;Alternate it so it &quot;bounces&quot; across the screen
220
 
            direction: &#x27;alternate&#x27;,
221
 
            &#x2F;&#x2F;Give all of them a different duration so we get different speeds.
222
 
            duration: ((counter * 1.75) + 1)
223
 
        });
224
 
 
225
 
        &#x2F;&#x2F;When this drop is entered, pause the fx
226
 
        a.drop.on(&#x27;drop:enter&#x27;, function() {
227
 
            this.fx.pause();
228
 
        }, a);
229
 
        &#x2F;&#x2F;When the drop is exited, run the fx again
230
 
        a.drop.on(&#x27;drop:exit&#x27;, function() {
231
 
            this.fx.run();
232
 
        }, a);
233
 
        &#x2F;&#x2F;When a drop is on the node, do something special
234
 
        a.drop.on(&#x27;drop:hit&#x27;, function(e) {
235
 
            &#x2F;&#x2F;Stop the animation
236
 
            this.fx.stop();
237
 
            &#x2F;&#x2F;remove the tween listener
238
 
            this.fx.unsubscribeAll(&#x27;tween&#x27;);
239
 
            &#x2F;&#x2F;move it to the dock
240
 
            this.fx.setAttrs({
241
 
                from: {
242
 
                    opacity: 1
243
 
                },
244
 
                to: {
245
 
                    height: 50,
246
 
                    width: 50,
247
 
                    left: function() {
248
 
                        var dW = Y.one(&#x27;body&#x27;).get(&#x27;viewportRegion&#x27;).right;
249
 
                        return ((dW - 60)); &#x2F;&#x2F;Minus 60 for the dock
250
 
                    },
251
 
                    top: 15,
252
 
                    opacity: .5
253
 
                },
254
 
                direction: &#x27;normal&#x27;,
255
 
                iterations: 1,
256
 
                duration: .5,
257
 
                &#x2F;&#x2F;We are using reverse above for the &quot;bouncing&quot;, reset it here.
258
 
                reverse: false
259
 
            });
260
 
 
261
 
            &#x2F;&#x2F;On end, add a class and destroy the target
262
 
            this.fx.on(&#x27;end&#x27;, function() {
263
 
                this.drop.get(&#x27;node&#x27;).addClass(&#x27;done&#x27;);
264
 
                this.drop.destroy();
265
 
            }, this);
266
 
            &#x2F;&#x2F;Run this animation
267
 
            this.fx.run();
268
 
            
269
 
            &#x2F;&#x2F;others that were dropped on.
270
 
            Y.each(e.others, function(v, k) {
271
 
                var node = v.get(&#x27;node&#x27;);
272
 
                node.fx.run();
273
 
            });
274
 
 
275
 
        }, a);
276
 
        
277
 
        &#x2F;&#x2F;on tween of the original anim, we need to sync the drop&#x27;s shim.
278
 
        a.fx.on(&#x27;tween&#x27;, function() {
279
 
            &#x2F;&#x2F;Do we have an active Drag?
280
 
            if (Y.DD.DDM.activeDrag) {
281
 
                &#x2F;&#x2F;Size this shim
282
 
                this.drop.sizeShim();
283
 
                &#x2F;&#x2F;Force an over target check since we might not be moving the mouse.
284
 
                Y.Lang.later(0, a, function() {
285
 
                    this.drop._handleTargetOver();
286
 
                });
287
 
            }
288
 
        }, a);
289
 
 
290
 
        a.fx.run();
291
 
    });
292
 
});</pre>
293
 
 
294
 
</div>
295
 
            </div>
296
 
        </div>
297
 
 
298
 
        <div class="yui3-u-1-4">
299
 
            <div class="sidebar">
300
 
                
301
 
                    <div id="toc" class="sidebox">
302
 
                        <div class="hd">
303
 
                            <h2 class="no-toc">Table of Contents</h2>
304
 
                        </div>
305
 
 
306
 
                        <div class="bd">
307
 
                            <ul class="toc">
308
 
<li>
309
 
<a href="#setting-up-the-html">Setting up the HTML</a>
310
 
</li>
311
 
<li>
312
 
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
313
 
</li>
314
 
<li>
315
 
<a href="#making-the-node-draggable">Making the Node draggable</a>
316
 
</li>
317
 
<li>
318
 
<a href="#animating-the-nodes">Animating the Nodes</a>
319
 
</li>
320
 
<li>
321
 
<a href="#making-the-node-a-target">Making the Node a Target</a>
322
 
</li>
323
 
<li>
324
 
<a href="#syncing-the-target-with-the-animation">Syncing the Target with the Animation</a>
325
 
</li>
326
 
<li>
327
 
<a href="#full-example-source">Full example source</a>
328
 
</li>
329
 
</ul>
330
 
                        </div>
331
 
                    </div>
332
 
                
333
 
 
334
 
                
335
 
                    <div class="sidebox">
336
 
                        <div class="hd">
337
 
                            <h2 class="no-toc">Examples</h2>
338
 
                        </div>
339
 
 
340
 
                        <div class="bd">
341
 
                            <ul class="examples">
342
 
                                
343
 
                                    
344
 
                                        <li data-description="A simple drag interaction that doesn&#x27;t require a drop interaction.">
345
 
                                            <a href="simple-drag.html">Simple Drag</a>
346
 
                                        </li>
347
 
                                    
348
 
                                
349
 
                                    
350
 
                                        <li data-description="How to apply the Drag Plugin to a node.">
351
 
                                            <a href="drag-plugin.html">Drag - Node plugin</a>
352
 
                                        </li>
353
 
                                    
354
 
                                
355
 
                                    
356
 
                                        <li data-description="A simple proxy drag interaction that doesn&#x27;t require a drop interaction.">
357
 
                                            <a href="proxy-drag.html">Drag - Proxy</a>
358
 
                                        </li>
359
 
                                    
360
 
                                
361
 
                                    
362
 
                                        <li data-description="How to constrain a draggable Node to another Node&#x27;s region.">
363
 
                                            <a href="constrained-drag.html">Drag - Constrained to a Region</a>
364
 
                                        </li>
365
 
                                    
366
 
                                
367
 
                                    
368
 
                                        <li data-description="Using interaction groups, this example demonstrates how to tie into the Drag &amp; Drop Utility&#x27;s interesting moments to provide visual affordances for the current drag operation.">
369
 
                                            <a href="groups-drag.html">Drag - Interaction Groups</a>
370
 
                                        </li>
371
 
                                    
372
 
                                
373
 
                                    
374
 
                                        <li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
375
 
                                            <a href="shim-drag.html">Using the Drag Shim</a>
376
 
                                        </li>
377
 
                                    
378
 
                                
379
 
                                    
380
 
                                        <li data-description="How to use the Drop Target events to code your application.">
381
 
                                            <a href="drop-code.html">Using Drop Based Coding</a>
382
 
                                        </li>
383
 
                                    
384
 
                                
385
 
                                    
386
 
                                        <li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
387
 
                                            <a href="winscroll.html">Window Scrolling</a>
388
 
                                        </li>
389
 
                                    
390
 
                                
391
 
                                    
392
 
                                        <li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
393
 
                                            <a href="delegate.html">Drag Delegation</a>
394
 
                                        </li>
395
 
                                    
396
 
                                
397
 
                                    
398
 
                                        <li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
399
 
                                            <a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
400
 
                                        </li>
401
 
                                    
402
 
                                
403
 
                                    
404
 
                                        <li data-description="How to use Drag plugins with a DD Delegate based solution.">
405
 
                                            <a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
406
 
                                        </li>
407
 
                                    
408
 
                                
409
 
                                    
410
 
                                        <li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
411
 
                                            <a href="list-drag.html">List Reorder w/Bubbling</a>
412
 
                                        </li>
413
 
                                    
414
 
                                
415
 
                                    
416
 
                                        <li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
417
 
                                            <a href="scroll-list.html">List Reorder w/Scrolling</a>
418
 
                                        </li>
419
 
                                    
420
 
                                
421
 
                                    
422
 
                                        <li data-description="How to make an animated node a Drop target.">
423
 
                                            <a href="anim-drop.html">Animated Drop Targets</a>
424
 
                                        </li>
425
 
                                    
426
 
                                
427
 
                                    
428
 
                                        <li data-description="Example Photo Browser application.">
429
 
                                            <a href="photo-browser.html">Photo Browser</a>
430
 
                                        </li>
431
 
                                    
432
 
                                
433
 
                                    
434
 
                                        <li data-description="Portal style example using Drag &amp; Drop Event Bubbling and Animation.">
435
 
                                            <a href="portal-drag.html">Portal Style Example</a>
436
 
                                        </li>
437
 
                                    
438
 
                                
439
 
                                    
440
 
                                
441
 
                                    
442
 
                                
443
 
                            </ul>
444
 
                        </div>
445
 
                    </div>
446
 
                
447
 
 
448
 
                
449
 
                    <div class="sidebox">
450
 
                        <div class="hd">
451
 
                            <h2 class="no-toc">Examples That Use This Component</h2>
452
 
                        </div>
453
 
 
454
 
                        <div class="bd">
455
 
                            <ul class="examples">
456
 
                                
457
 
                                    
458
 
                                
459
 
                                    
460
 
                                
461
 
                                    
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
 
                                        <li data-description="Working with multiple YUI instances.">
491
 
                                            <a href="../yui/yui-multi.html">Multiple Instances</a>
492
 
                                        </li>
493
 
                                    
494
 
                                
495
 
                                    
496
 
                                        <li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
497
 
                                            <a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
498
 
                                        </li>
499
 
                                    
500
 
                                
501
 
                            </ul>
502
 
                        </div>
503
 
                    </div>
504
 
                
505
 
            </div>
506
 
        </div>
507
 
    </div>
508
 
</div>
509
 
 
510
 
<script src="../assets/vendor/prettify/prettify-min.js"></script>
511
 
<script>prettyPrint();</script>
512
 
 
513
 
</body>
514
 
</html>