drop-code.mustache revision aed0a1e5e3f44c4b7d2afd9e1c7dc78203e11251
742N/A<div class="intro">
742N/A<p>This example shows how to use the Drop Target events to code your application.</p>
742N/A</div>
742N/A
742N/A<div class="example">
742N/A {{>drop-code-source}}
742N/A</div>
742N/A
742N/A<h3>Setting up the HTML</h3>
742N/A<p>First we need to create the HTML for the example.</p>
742N/A
742N/A```
742N/A{{>drop-code-source-html}}
742N/A```
742N/A
742N/A<p>Now we give the HTML some CSS to make them visible.</p>
742N/A
742N/A```
742N/A{{>drop-code-source-css}}
742N/A```
742N/A
742N/A<h3>Setting up the YUI Instance</h3>
742N/A<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>
742N/A
742N/A```
742N/AYUI().use('dd-drop', 'dd-constrain');
742N/A```
742N/A
742N/A<h3>Making the Nodes draggable</h3>
742N/A<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>
742N/A<p>In this example we are using the data config option of the drag to associate data with this Drag instance.</p>
742N/A<p>So we have set up an object literal containing information about our drag items.</p>
742N/A
742N/A```
742N/A var data = {
742N/A 'drag1': { color: 'white', size: 'x-small', price: '$5.00' },
742N/A 'drag2': { color: 'blue', size: 'small', price: '$6.00' },
742N/A 'drag3': { color: 'green', size: 'medium', price: '$7.00' },
742N/A 'drag4': { color: 'red', size: 'large', price: '$10.00' },
742N/A 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' }
742N/A };
742N/A```
742N/A
742N/A<p>Now we walk through the nodes and create a drag instance from each of them.</p>
742N/A
742N/A```
742N/AYUI().use('dd-drop', 'dd-constrain', function(Y) {
742N/A //Data to attach to each drag object
742N/A var data = {
742N/A 'drag1': { color: 'white', size: 'x-small', price: '$5.00' },
742N/A 'drag2': { color: 'blue', size: 'small', price: '$6.00' },
742N/A 'drag3': { color: 'green', size: 'medium', price: '$7.00' },
742N/A 'drag4': { color: 'red', size: 'large', price: '$10.00' },
742N/A 'drag5': { color: 'purple', size: 'x-large', price: '$15.00' }
742N/A };
742N/A //Get all the divs with the class drag
742N/A var drags = Y.Node.all('#play div.drag');
742N/A //Walk through each one
742N/A drags.each(function(v, k) {
742N/A //scope a local var for the data
742N/A var thisData = {};
742N/A //Using Y.mix to break this data from the data above
742N/A Y.mix(thisData, data[v.get('id')]);
742N/A
742N/A //Create the new Drag Instance
742N/A var dd = new Y.DD.Drag({
742N/A //Give it the node
742N/A node: v,
742N/A //Set the dragMode to intersect
742N/A dragMode: 'intersect',
742N/A //Attach the data here.
742N/A data: thisData
742N/A }).plug(Y.Plugin.DDConstrained, {
742N/A //Keep it inside the work area
742N/A constrain2node: '#play'
742N/A });
742N/A //Prevent the default end event (this moves the node back to its start position)
742N/A dd.on('drag:end', function(e) {
742N/A e.preventDefault();
742N/A });
742N/A });
742N/A});
742N/A```
742N/A
742N/A<h3>Setting up the Drop Target</h3>
742N/A<p>Here we set up the Drop Target and assign a listener to it.</p>
742N/A
742N/A```
742N/Avar drop = new Y.DD.Drop({
742N/A node: '#drop'
742N/A});
742N/A//Listen for a drop:hit on this target
742N/Adrop.on('drop:hit', function(e) {
742N/A //Now we get the drag instance that triggered the drop hit
742N/A var drag = e.drag;
742N/A //get the data from it
742N/A var data = drag.get('data');
742N/A
742N/A //Do something with the data
742N/A var out = ['id: ' + drag.get('node').get('id')];
742N/A Y.each(data, function(v, k) {
742N/A out[out.length] = k + ': ' + v;
742N/A });
742N/A var str = '<p><strong>Dropped</strong>: ' + out.join(', ') + '</p>';
742N/A this.get('node').set('innerHTML', str);
742N/A});
742N/A```
742N/A
742N/A<h3>Full Example Source</h3>
742N/A
742N/A```
742N/A{{>drop-code-source-js}}
742N/A```
742N/A