schmidetzki
02-23-2007, 06:44 PM
I experimented with the new Ext 1.0 drag&drop classes and analized how they work.
Perhaps this will help others to get started using Ext.dd.
There are two base classes Ext.dd.DragSource and Ext.dd.DropTarget.
Here is a simple example of how to use it:
<style>
.box{
width: 200;
border: solid black 1px;
padding:20px;
}
</style>
<div id="dragsource" class="box">Drag me</div>
<div id="droptarget" class="box">drop me</div>
<div class="box">but not here</div>
<script>
new Ext.dd.DragSource("dragsource", {dragData:{name: "Wolfgang"}});
var droptarget=new Ext.dd.DropTarget("droptarget");
droptarget.notifyDrop=function(dd, e, data){
console.log("dropped " + dd.id + " on dragtarget", data);
return true;
}
</script>
The advantage over basic YAHOO.dd is that you can implement a function notifyDrop() on the target what is more practical then to implement onDragDrop() on the dragsource.
The next step ist to user Ext.dd.DragZone and Ext.dd.DropZone.
These two classes makes it possible to use only one YAHOO.util.DDProxy but enabling D&D for a set of elements in one container element. This saves resources because a YAHOO.util.DDProxy object takes much memory in your client.
Here again is a simple example:
<div id="dragzone">
DragZone
<div id="dd1" class="box">Drag me</div>
<div id="dd2" class="box">Drag me too</div>
<div id="dd3" class="box">Drop only</div>
</div>
<div id="dropzone">
DropZone
<div id="drop" class="box">drop me</div>
<div id="nodrop" class="box">but not here</div>
</div>
<script>
new Ext.dd.DragZone("dragzone")
var dragdropzone=new Ext.dd.DropZone("dragzone");
Ext.dd.Registry.register("dd1", {name:"Wolfgang"});
Ext.dd.Registry.register("dd2", {name:"Schmidetzki"});
dragdropzone.onNodeDrop=function(target, dd, e, data){
console.log("dropped " + data.ddel.id + " on " + target.ddel.id, data);
return true;
}
var droponly=new Ext.dd.DropTarget("dd3");
droponly.notifyDrop=function(dd, e, data){
console.log("dd3: dropped " + data.ddel.id + " on dd3", data);
return true;
}
var dropzone=new Ext.dd.DropZone("dropzone", {});
dropzone.onNodeDrop=function(n, dd, e, data){
console.log(n, dd, e, data);
return false;
}
Ext.dd.Registry.register("drop");
</script>
Instead of defining mehtods for all DDSource- and DDTarget-Objects you define only one onNodeDrop() method for each target container.
The example uses two target container. The first is a DragZone and a DropZone, the second is a DropZone only.
To use DragZone and DropZone you have to "register" each drag- (and drop-) element using Ext.dd.Registry.register(). You may assign as data object during registration. This data is automaticaly passed to your onNodeDrop()-method.
What I don't understand however is, why the onNodeDrop() methode is called twice on a drop-target-only.
FireBug logges:
dd3: dropped dd1 on dd3 Object name=Wolfgang ddel=div#dd1.box
dd3: dropped dd1 on dd3 Object name=Wolfgang ddel=div#dd1.box
Perhaps this will help others to get started using Ext.dd.
There are two base classes Ext.dd.DragSource and Ext.dd.DropTarget.
Here is a simple example of how to use it:
<style>
.box{
width: 200;
border: solid black 1px;
padding:20px;
}
</style>
<div id="dragsource" class="box">Drag me</div>
<div id="droptarget" class="box">drop me</div>
<div class="box">but not here</div>
<script>
new Ext.dd.DragSource("dragsource", {dragData:{name: "Wolfgang"}});
var droptarget=new Ext.dd.DropTarget("droptarget");
droptarget.notifyDrop=function(dd, e, data){
console.log("dropped " + dd.id + " on dragtarget", data);
return true;
}
</script>
The advantage over basic YAHOO.dd is that you can implement a function notifyDrop() on the target what is more practical then to implement onDragDrop() on the dragsource.
The next step ist to user Ext.dd.DragZone and Ext.dd.DropZone.
These two classes makes it possible to use only one YAHOO.util.DDProxy but enabling D&D for a set of elements in one container element. This saves resources because a YAHOO.util.DDProxy object takes much memory in your client.
Here again is a simple example:
<div id="dragzone">
DragZone
<div id="dd1" class="box">Drag me</div>
<div id="dd2" class="box">Drag me too</div>
<div id="dd3" class="box">Drop only</div>
</div>
<div id="dropzone">
DropZone
<div id="drop" class="box">drop me</div>
<div id="nodrop" class="box">but not here</div>
</div>
<script>
new Ext.dd.DragZone("dragzone")
var dragdropzone=new Ext.dd.DropZone("dragzone");
Ext.dd.Registry.register("dd1", {name:"Wolfgang"});
Ext.dd.Registry.register("dd2", {name:"Schmidetzki"});
dragdropzone.onNodeDrop=function(target, dd, e, data){
console.log("dropped " + data.ddel.id + " on " + target.ddel.id, data);
return true;
}
var droponly=new Ext.dd.DropTarget("dd3");
droponly.notifyDrop=function(dd, e, data){
console.log("dd3: dropped " + data.ddel.id + " on dd3", data);
return true;
}
var dropzone=new Ext.dd.DropZone("dropzone", {});
dropzone.onNodeDrop=function(n, dd, e, data){
console.log(n, dd, e, data);
return false;
}
Ext.dd.Registry.register("drop");
</script>
Instead of defining mehtods for all DDSource- and DDTarget-Objects you define only one onNodeDrop() method for each target container.
The example uses two target container. The first is a DragZone and a DropZone, the second is a DropZone only.
To use DragZone and DropZone you have to "register" each drag- (and drop-) element using Ext.dd.Registry.register(). You may assign as data object during registration. This data is automaticaly passed to your onNodeDrop()-method.
What I don't understand however is, why the onNodeDrop() methode is called twice on a drop-target-only.
FireBug logges:
dd3: dropped dd1 on dd3 Object name=Wolfgang ddel=div#dd1.box
dd3: dropped dd1 on dd3 Object name=Wolfgang ddel=div#dd1.box