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