<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Totti&#039;s Blog &#187; Drag n&#8217; Drop</title>
	<atom:link href="http://iamtotti.com/blog/tag/drag-n-drop/feed/" rel="self" type="application/rss+xml" />
	<link>http://iamtotti.com/blog</link>
	<description>Javascript, and front-end stuff.</description>
	<lastBuildDate>Tue, 24 Aug 2010 17:21:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Ext JS Examples: Drag n&#8217; Drop buttons on a panel</title>
		<link>http://iamtotti.com/blog/2009/12/ext-js-drag-n-drop-buttons-on-a-panel/</link>
		<comments>http://iamtotti.com/blog/2009/12/ext-js-drag-n-drop-buttons-on-a-panel/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 20:17:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ext JS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Drag n' Drop]]></category>
		<category><![CDATA[examples]]></category>

		<guid isPermaLink="false">http://iamtotti.com/blog/?p=42</guid>
		<description><![CDATA[This blog entry was written following a question of an user in ExtJS forum: How to drag n&#8217; drop a button on a panel ? ExtJs people tend to just focus on the grid-to-grid, grid-to-listview.. sort of drag and drop, and forgot to include in their documentation a typical example of &#8216;drag and drop&#8217; of [...]]]></description>
			<content:encoded><![CDATA[<p>This blog entry was written following a question of an user in ExtJS forum: <em>How to drag n&#8217; drop a button on a panel ?</em></p>
<p>ExtJs people tend to just focus on the grid-to-grid, grid-to-listview.. sort of drag and drop, and forgot to include in their documentation a typical example of &#8216;drag and drop&#8217; of a simple element like <em>a button on a panel</em>. Below is my implementation:</p>
<p>ExtJS: 3.x</p>
<p>Demo:<a href="http://iamtotti.com/playground/js/ext-3.1.0/playground/dd_button.html" target="_blank"> Drag and Drop a button</a></p>
<p><span id="more-42"></span>index.html</p>
<pre class="brush:html">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;ExtJS - Drag n' Drop Demo&lt;/title&gt;
	&lt;link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" /&gt;
	&lt;script src="js/ext-base.js" type="text/javascript"&gt;&lt;/script&gt;
	&lt;script src="js/ext-all.js" type="text/javascript"&gt;&lt;/script&gt;

	&lt;script src="js/dd_button.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
	&lt;div id="canvas"&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>dd_button.js</p>
<pre class="brush:js">Ext.onReady(function(){

	// Make the button Draggable inside  tag
	var initDragZone = function(v) {

		v.dragZone = new Ext.dd.DragZone(Ext.getBody(), {
			getDragData: function(e) {
				// .button-draggable == class of the button you want to drag around
				if(sourceEl = e.getTarget('.button-draggable')) {
					d = sourceEl.cloneNode(true);
					d.id = Ext.id();
					return v.dragData = {
						sourceEl: sourceEl,
						repairXY: Ext.fly(sourceEl).getXY(),
						ddel: d
					}
				}
			},

			onDrag: function(e) {
				// !Important: manually fix the default position of Ext-generated proxy element
				// Uncomment these line to see the Ext issue
				var proxy = Ext.DomQuery.select('*', this.getDragEl());
				proxy[2].style.position = '';
			},

			getRepairXY: function() {
				return this.dragData.repairXY;
			}
		});
	};		

        // Make the panel droppable to the button
	var initDropZone = function(g) {
		g.dropZone = new Ext.dd.DropZone(g.body, {

			getTargetFromEvent: function(e) {
				return e.getTarget('#canvas');
			},

			onNodeOver : function(target, dd, e, data){
				return Ext.dd.DropZone.prototype.dropAllowed;
			},

			onNodeDrop : function(target, dd, e, data) {
				// !Important: We assign the dragged element to be set to new drop position
				if(dragEl = Ext.get(data.sourceEl)) {
					dragEl.setXY([e.getPageX(),e.getPageY()]);
				}

				return true;
			}	

		});
	};

        // Make the Panel Droppable
	var myPanel = new Ext.Panel({
		width: 500,
		height: 300,
		renderTo: 'canvas',
		bodyStyle: { background: 'yellow' },
		html: 'Drop your button here',
		listeners: {
			render: initDropZone
		}
	});		

	var myButton = new Ext.Button({
		width: 30,
		height: 20,
		cls: 'button-draggable',
		text: "Drag me",
		renderTo: Ext.getBody(),
		listeners: {
			render: initDragZone
		}
	});
});</pre>
<p>The important point of this implementation is the onDrag method</p>
<pre class="brush:js">onDrag: function(e) {
				// !Important: manually fix the default position of Ext-generated proxy element
				// Uncomment these line to see the Ext issue
				var proxy = Ext.DomQuery.select('*', this.getDragEl());
				proxy[2].style.position = '';
			},</pre>
<p>Try removing the function onDrag above, you&#8217;ll see this positioning issue:</p>
<p><img src="http://iamtotti.com/blog/wp-content/uploads/2009/12/dd_button.jpg" alt="Positioning Issue" width="100%" /></p>
<p>Have fun,</p>
<p>Totti</p>
]]></content:encoded>
			<wfw:commentRss>http://iamtotti.com/blog/2009/12/ext-js-drag-n-drop-buttons-on-a-panel/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
