src/cm/media/js/lib/yui/yui_3.10.3/docs/dd/drop-code.html
author gibus
Tue, 16 Jul 2013 14:29:46 +0200
changeset 525 89ef5ed3c48b
permissions -rw-r--r--
Upgrades to yui 3.10.3

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Using Drop Based Coding</title>
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
    <link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
    <script src="../../build/yui/yui-min.js"></script>
    
</head>
<body>
<!--
<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>
-->
<div id="doc">
    <div id="hd">
        <h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
    </div>
    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

            <h1>Example: Using Drop Based Coding</h1>
    <div class="yui3-g">
        <div class="yui3-u-3-4">
            <div id="main">
                <div class="content"><div class="intro">
<p>This example shows how to use the Drop Target events to code your application.</p>
</div>

<div class="example">
    <style>
    .drag {
        height: 50px;
        width: 50px;
        border: 1px solid black;
        background-color: #004C6D;
        color: white;
        cursor: move;
        float: left;
        margin: 4px;
        z-index: 2;
    }
    #play {
        border: 1px solid black;
        height: 300px;
        position: relative;
    }
    #drop {
        position: absolute;
        bottom: 5px;
        right: 5px;
        border: 1px solid black;
        background-color: #8DD5E7;
        height: 75px;
        width: 65%;
        z-index: 1;
    }
    #drop p {
        margin: 1em;
    }
    #drop p strong {
        font-weight: bold;
    }
    #drop.yui3-dd-drop-over {
        background-color: #FFA928;
    }


</style>

    <div id="play">
        <div id="drag1" class="drag">Drag #1</div>
        <div id="drag2" class="drag">Drag #2</div>
        <div id="drag3" class="drag">Drag #3</div>
        <div id="drag4" class="drag">Drag #4</div>
        <div id="drag5" class="drag">Drag #5</div>
        <div id="drop"></div>
    </div>



<script>
YUI().use('dd-drop', 'dd-constrain', function(Y) {
        var data = {
            'drag1': { color: 'white', size: 'x-small', price: '$5.00' },
            'drag2': { color: 'blue', size: 'small', price: '$6.00' },
            'drag3': { color: 'green', size: 'medium', price: '$7.00' },
            'drag4': { color: 'red', size: 'large', price: '$10.00' },
            'drag5': { color: 'purple', size: 'x-large', price: '$15.00' }
        };
        var drags = Y.Node.all('#play div.drag');
        drags.each(function(v, k) {
            var thisData = {};
            Y.mix(thisData, data[v.get('id')]);
            var dd = new Y.DD.Drag({
                node: v,
                dragMode: 'intersect',
                data: thisData
            }).plug(Y.Plugin.DDConstrained, {
                constrain2node: '#play'
            });
            dd.on('drag:end', function(e) {
                e.preventDefault();
            });
        });

        var drop = new Y.DD.Drop({
            node: '#drop'
        });
        drop.on('drop:hit', function(e) {
            var drag = e.drag;
            var data = drag.get('data');
            var out = ['id: ' + drag.get('node').get('id')];
            Y.each(data, function(v, k) {
                out[out.length] = k + ': ' + v;
            });
            var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>';
            this.get('node').set('innerHTML', str);
        });
});

</script>

</div>

<h3 id="setting-up-the-html">Setting up the HTML</h3>
<p>First we need to create the HTML for the example.</p>

<pre class="code prettyprint">&lt;div id=&quot;play&quot;&gt;
    &lt;div id=&quot;drag1&quot; class=&quot;drag&quot;&gt;Drag #1&lt;&#x2F;div&gt;
    &lt;div id=&quot;drag2&quot; class=&quot;drag&quot;&gt;Drag #2&lt;&#x2F;div&gt;
    &lt;div id=&quot;drag3&quot; class=&quot;drag&quot;&gt;Drag #3&lt;&#x2F;div&gt;
    &lt;div id=&quot;drag4&quot; class=&quot;drag&quot;&gt;Drag #4&lt;&#x2F;div&gt;
    &lt;div id=&quot;drag5&quot; class=&quot;drag&quot;&gt;Drag #5&lt;&#x2F;div&gt;
    &lt;div id=&quot;drop&quot;&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;</pre>


<p>Now we give the HTML some CSS to make them visible.</p>

<pre class="code prettyprint">.drag {
    height: 50px;
    width: 50px;
    border: 1px solid black;
    background-color: #004C6D;
    color: white;
    cursor: move;
    float: left;
    margin: 4px;
    z-index: 2;
}
#play {
    border: 1px solid black;
    height: 300px;
    position: relative;
}
#drop {
    position: absolute;
    bottom: 5px;
    right: 5px;
    border: 1px solid black;
    background-color: #8DD5E7;
    height: 75px;
    width: 65%;
    z-index: 1;
}
#drop p {
    margin: 1em;
}
#drop p strong {
    font-weight: bold;
}
#drop.yui3-dd-drop-over {
    background-color: #FFA928;
}</pre>


<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
<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>

<pre class="code prettyprint">YUI().use(&#x27;dd-drop&#x27;, &#x27;dd-constrain&#x27;);</pre>


<h3 id="making-the-nodes-draggable">Making the Nodes draggable</h3>
<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>
<p>In this example we are using the data config option of the drag to associate data with this Drag instance.</p>
<p>So we have set up an object literal containing information about our drag items.</p>

<pre class="code prettyprint">var data = {
    &#x27;drag1&#x27;: { color: &#x27;white&#x27;, size: &#x27;x-small&#x27;, price: &#x27;$5.00&#x27; },
    &#x27;drag2&#x27;: { color: &#x27;blue&#x27;, size: &#x27;small&#x27;, price: &#x27;$6.00&#x27; },
    &#x27;drag3&#x27;: { color: &#x27;green&#x27;, size: &#x27;medium&#x27;, price: &#x27;$7.00&#x27; },
    &#x27;drag4&#x27;: { color: &#x27;red&#x27;, size: &#x27;large&#x27;, price: &#x27;$10.00&#x27; },
    &#x27;drag5&#x27;: { color: &#x27;purple&#x27;, size: &#x27;x-large&#x27;, price: &#x27;$15.00&#x27; }
};</pre>


<p>Now we walk through the nodes and create a drag instance from each of them.</p>

<pre class="code prettyprint">YUI().use(&#x27;dd-drop&#x27;, &#x27;dd-constrain&#x27;, function(Y) {
    &#x2F;&#x2F;Data to attach to each drag object
    var data = {
        &#x27;drag1&#x27;: { color: &#x27;white&#x27;, size: &#x27;x-small&#x27;, price: &#x27;$5.00&#x27; },
        &#x27;drag2&#x27;: { color: &#x27;blue&#x27;, size: &#x27;small&#x27;, price: &#x27;$6.00&#x27; },
        &#x27;drag3&#x27;: { color: &#x27;green&#x27;, size: &#x27;medium&#x27;, price: &#x27;$7.00&#x27; },
        &#x27;drag4&#x27;: { color: &#x27;red&#x27;, size: &#x27;large&#x27;, price: &#x27;$10.00&#x27; },
        &#x27;drag5&#x27;: { color: &#x27;purple&#x27;, size: &#x27;x-large&#x27;, price: &#x27;$15.00&#x27; }
    };
    &#x2F;&#x2F;Get all the divs with the class drag
    var drags = Y.Node.all(&#x27;#play div.drag&#x27;);
    &#x2F;&#x2F;Walk through each one
    drags.each(function(v, k) {
        &#x2F;&#x2F;scope a local var for the data
        var thisData = {};
        &#x2F;&#x2F;Using Y.mix to break this data from the data above
        Y.mix(thisData, data[v.get(&#x27;id&#x27;)]);

        &#x2F;&#x2F;Create the new Drag Instance
        var dd = new Y.DD.Drag({
            &#x2F;&#x2F;Give it the node
            node: v,
            &#x2F;&#x2F;Set the dragMode to intersect
            dragMode: &#x27;intersect&#x27;,
            &#x2F;&#x2F;Attach the data here.
            data: thisData
        }).plug(Y.Plugin.DDConstrained, {
            &#x2F;&#x2F;Keep it inside the work area
            constrain2node: &#x27;#play&#x27;
        });
        &#x2F;&#x2F;Prevent the default end event (this moves the node back to its start position)
        dd.on(&#x27;drag:end&#x27;, function(e) {
            e.preventDefault();
        });
    });
});</pre>


<h3 id="setting-up-the-drop-target">Setting up the Drop Target</h3>
<p>Here we set up the Drop Target and assign a listener to it.</p>

<pre class="code prettyprint">var drop = new Y.DD.Drop({
    node: &#x27;#drop&#x27;
});
&#x2F;&#x2F;Listen for a drop:hit on this target
drop.on(&#x27;drop:hit&#x27;, function(e) {
    &#x2F;&#x2F;Now we get the drag instance that triggered the drop hit
    var drag = e.drag;
    &#x2F;&#x2F;get the data from it
    var data = drag.get(&#x27;data&#x27;);

    &#x2F;&#x2F;Do something with the data
    var out = [&#x27;id: &#x27; + drag.get(&#x27;node&#x27;).get(&#x27;id&#x27;)];
    Y.each(data, function(v, k) {
        out[out.length] = k + &#x27;: &#x27; + v;
    });
    var str = &#x27;&lt;p&gt;&lt;strong&gt;Dropped&lt;&#x2F;strong&gt;: &#x27; + out.join(&#x27;, &#x27;) + &#x27;&lt;&#x2F;p&gt;&#x27;;
    this.get(&#x27;node&#x27;).set(&#x27;innerHTML&#x27;, str);
});</pre>


<h3 id="full-example-source">Full Example Source</h3>

<pre class="code prettyprint">YUI().use(&#x27;dd-drop&#x27;, &#x27;dd-constrain&#x27;, function(Y) {
        var data = {
            &#x27;drag1&#x27;: { color: &#x27;white&#x27;, size: &#x27;x-small&#x27;, price: &#x27;$5.00&#x27; },
            &#x27;drag2&#x27;: { color: &#x27;blue&#x27;, size: &#x27;small&#x27;, price: &#x27;$6.00&#x27; },
            &#x27;drag3&#x27;: { color: &#x27;green&#x27;, size: &#x27;medium&#x27;, price: &#x27;$7.00&#x27; },
            &#x27;drag4&#x27;: { color: &#x27;red&#x27;, size: &#x27;large&#x27;, price: &#x27;$10.00&#x27; },
            &#x27;drag5&#x27;: { color: &#x27;purple&#x27;, size: &#x27;x-large&#x27;, price: &#x27;$15.00&#x27; }
        };
        var drags = Y.Node.all(&#x27;#play div.drag&#x27;);
        drags.each(function(v, k) {
            var thisData = {};
            Y.mix(thisData, data[v.get(&#x27;id&#x27;)]);
            var dd = new Y.DD.Drag({
                node: v,
                dragMode: &#x27;intersect&#x27;,
                data: thisData
            }).plug(Y.Plugin.DDConstrained, {
                constrain2node: &#x27;#play&#x27;
            });
            dd.on(&#x27;drag:end&#x27;, function(e) {
                e.preventDefault();
            });
        });

        var drop = new Y.DD.Drop({
            node: &#x27;#drop&#x27;
        });
        drop.on(&#x27;drop:hit&#x27;, function(e) {
            var drag = e.drag;
            var data = drag.get(&#x27;data&#x27;);
            var out = [&#x27;id: &#x27; + drag.get(&#x27;node&#x27;).get(&#x27;id&#x27;)];
            Y.each(data, function(v, k) {
                out[out.length] = k + &#x27;: &#x27; + v;
            });
            var str = &#x27;&lt;p&gt;&lt;strong&gt;Dropped&lt;&#x2F;strong&gt;: &#x27; + out.join(&#x27;, &#x27;) + &#x27;&lt;&#x2F;p&gt;&#x27;;
            this.get(&#x27;node&#x27;).set(&#x27;innerHTML&#x27;, str);
        });
});</pre>

</div>
            </div>
        </div>

        <div class="yui3-u-1-4">
            <div class="sidebar">
                
                    <div id="toc" class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Table of Contents</h2>
                        </div>

                        <div class="bd">
                            <ul class="toc">
<li>
<a href="#setting-up-the-html">Setting up the HTML</a>
</li>
<li>
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
</li>
<li>
<a href="#making-the-nodes-draggable">Making the Nodes draggable</a>
</li>
<li>
<a href="#setting-up-the-drop-target">Setting up the Drop Target</a>
</li>
<li>
<a href="#full-example-source">Full Example Source</a>
</li>
</ul>
                        </div>
                    </div>
                

                
                    <div class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Examples</h2>
                        </div>

                        <div class="bd">
                            <ul class="examples">
                                
                                    
                                        <li data-description="A simple drag interaction that doesn&#x27;t require a drop interaction.">
                                            <a href="simple-drag.html">Simple Drag</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="How to apply the Drag Plugin to a node.">
                                            <a href="drag-plugin.html">Drag - Node plugin</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="A simple proxy drag interaction that doesn&#x27;t require a drop interaction.">
                                            <a href="proxy-drag.html">Drag - Proxy</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="How to constrain a draggable Node to another Node&#x27;s region.">
                                            <a href="constrained-drag.html">Drag - Constrained to a Region</a>
                                        </li>
                                    
                                
                                    
                                        <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.">
                                            <a href="groups-drag.html">Drag - Interaction Groups</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
                                            <a href="shim-drag.html">Using the Drag Shim</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="How to use the Drop Target events to code your application.">
                                            <a href="drop-code.html">Using Drop Based Coding</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
                                            <a href="winscroll.html">Window Scrolling</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
                                            <a href="delegate.html">Drag Delegation</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
                                            <a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="How to use Drag plugins with a DD Delegate based solution.">
                                            <a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
                                            <a href="list-drag.html">List Reorder w/Bubbling</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
                                            <a href="scroll-list.html">List Reorder w/Scrolling</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="How to make an animated node a Drop target.">
                                            <a href="anim-drop.html">Animated Drop Targets</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Example Photo Browser application.">
                                            <a href="photo-browser.html">Photo Browser</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Portal style example using Drag &amp; Drop Event Bubbling and Animation.">
                                            <a href="portal-drag.html">Portal Style Example</a>
                                        </li>
                                    
                                
                                    
                                
                                    
                                
                            </ul>
                        </div>
                    </div>
                

                
                    <div class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Examples That Use This Component</h2>
                        </div>

                        <div class="bd">
                            <ul class="examples">
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                        <li data-description="Working with multiple YUI instances.">
                                            <a href="../yui/yui-multi.html">Multiple Instances</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
                                            <a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
                                        </li>
                                    
                                
                            </ul>
                        </div>
                    </div>
                
            </div>
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

<script>
YUI.Env.Tests = {
    examples: [],
    project: '../assets',
    assets: '../assets/dd',
    name: 'drop-code',
    title: 'Using Drop Based Coding',
    newWindow: '',
    auto:  false 
};
YUI.Env.Tests.examples.push('simple-drag');
YUI.Env.Tests.examples.push('drag-plugin');
YUI.Env.Tests.examples.push('proxy-drag');
YUI.Env.Tests.examples.push('constrained-drag');
YUI.Env.Tests.examples.push('groups-drag');
YUI.Env.Tests.examples.push('shim-drag');
YUI.Env.Tests.examples.push('drop-code');
YUI.Env.Tests.examples.push('winscroll');
YUI.Env.Tests.examples.push('delegate');
YUI.Env.Tests.examples.push('delegate-drop');
YUI.Env.Tests.examples.push('delegate-plugins');
YUI.Env.Tests.examples.push('list-drag');
YUI.Env.Tests.examples.push('scroll-list');
YUI.Env.Tests.examples.push('anim-drop');
YUI.Env.Tests.examples.push('photo-browser');
YUI.Env.Tests.examples.push('portal-drag');
YUI.Env.Tests.examples.push('yui-multi');
YUI.Env.Tests.examples.push('stylesheet-theme');

</script>
<script src="../assets/yui/test-runner.js"></script>



</body>
</html>