View Full Version : Drag and Drop script
Hi!
so I've try to write a drag and drop script based on the recomandation of ScriptGuru. Because i'm beginer in dhtml only the basic work (in ie)
can sombody help me with tath script ??
As you'll see is part of an dhtml api.
Here's a page where you can see the script in action (or in inaction in NS):
http://pageajc.citeglobe.com/dhtml_api/examples/Drag.html
and here's the code:
<pre id=code><font face=courier size=2 id=code>
//state = "start"
newX = 0
newY = 0
Drag = {
enabled : false,
grabbedObj : null,
enable : function()
{
document.onmousemove = Drag.trackMouse;
document.onmouseup=Drag.ungrab
Drag.enabled = true
},
disable : function()
{
document.onmousemove = null;
document.onmouseup= null;
Drag.enabled = false
},
trackMouse : function(e)
{
if(ie){e=event}
//alert("move")
Drag.mouseX = e.clientX
Drag.mouseY = e.clientY
if (Drag.grabbed && Drag.enabled)
{
//state = "draging"
obj = Drag.grabbedObj
newX=(Drag.mouseX + Drag.tempX - Drag.selectX)
newY=(Drag.mouseY + Drag.tempY - Drag.selectY)
minX = obj.DragMinX
maxX = obj.DragMaxX
minY = obj.DragMinY
maxY = obj.DragMaxY
W = obj.getW()
H = obj.getH()
//if (x<l) x=l
//else if (x+w>r) x=r-w
//if (y<t) y=t
//else if (y+h>b) y=b-h
if(minX !=null){if(newX<minX){newX = minX}}
if(minY !=null){if(newY<minY){newY = minY}}
if(maxX !=null){if((newX+W) > maxX){newX = (maxX-W)}}
if(maxY !=null){if((newY+H) > maxY){newY = (maxY-H)}}
obj.setX(newX)
obj.setY(newY)
}
},
grab : function(e)
{
//alert("grab")
//state = "click"
if(ie){e=event}
src = (ie)? e.srcElement : e.target
dragobj = div(src.id)
if(!dragobj) return false
//state ="exist"
if (!dragobj.drag) return false
//state = "grab"
Drag.selectX = e.clientX
Drag.selectY = e.clientY
Drag.tempX = src.style.pixelLeft
Drag.tempY = src.style.pixelTop
Drag.grabbed = true
if (dragobj.isProxy){Drag.grabbedObj=dragobj.proxyFor }
else {Drag.grabbedObj=dragobj}
},
ungrab : function(){
Drag.grabbed=false
Drag.grabbedObj=null
}
}
DynDiv.prototype.enableDrag = function(obj){
//state = "enabled"
//this.setDraglimit(0,0,null,null)
if(obj){
this.haveDragProxy = true
this.DragPoxy = obj
obj.isProxy = true
obj.proxyFor = this
}
obj = obj||this
obj.drag = true
obj.addEvent("mousedown",Drag.grab)
}
DynDiv.prototype.disableDrag = function(){
this.drag = false
this.removeEvent("mousedown",Drag.grab)
}
DynDiv.prototype.setDraglimit=function(minX,minY,m axX,maxY)
{
a=arguments
if(a.length == 1){
if(typeof(a[0])=="object")
{
if(a[0].style){
minX = 0
minY = 0
maxX = a[0].style.offsetWidth
maxY = a[0].style.offsetHeight
}
}
}
this.DragMinX = minX
this.DragMinY = minY
this.DragMaxX = maxX
this.DragMaxY = maxY
}
</font id=code></pre id=code>
Edited by - jc on 06/10/2001 17:48:42
I've realy no idea where can be the errors but mabee tht list of but could help:
1- nothing apen in NS
2- the grabbed layer only respect the left and the top drag limit
3- setting child div to drag only in his parent dosent work at all
4- setting a div to drag inside the area of an other one dosent work at all
5- proxy draged div always return to is default position when you drag it for a second time
ps. I'm working on a DHTML lib for only dom-compatible browser so no need to have a code for NS4 and ie4
Hi JC, :) I don't have alot of time right now and later I'll download you files and see if I can spot anything else... One thing that jumped out at me right off was these lines near the bottom..
maxX = a[0].style.offsetWidth
maxY = a[0].style.offsetHeight
You don't access offset values through "style" so it should be..
maxX = a[0].offsetWidth
maxY = a[0].offsetHeight
You should check some of your other files for this too just incase..
Another thing I noticed was...
Drag.tempX = src.style.pixelLeft
Drag.tempY = src.style.pixelTop
Unless that's in an IE only statement it should be..
Drag.tempX = src.style.left
Drag.tempY = src.style.top
for two reasons.. The biggest reason being the NS6 does not support any pixelValue properties and it's also not part of the "standard" as far as I know..
BTW..
I'd check out www.dynamic-core.net This guy is also making a DOMLib (I think..)
<font color=teal>xanth = createElement("person");
xanth.setAttribute("name", "Mike");
xanth.setAttribute("mail", "[url:4jsuqmf5hr]belgedin@earthlink.net[/url:4jsuqmf5hr]");
xanth.setAttribute("age", 16)
document.forum.appendChild(xanth);
</font id=teal>
tank Mike but actualy, that dosent correct any bug <img src=icon_smile_question.gif border=0 align=middle>.
I had already test to remplace pixelLef by left but ie make an "argument non valide" error I have no idea how to translate it .
I didn't expect it to really...
Anyways if you change them to.
Drag.tempX = parseInt(src.style.left)
Drag.tempY = parseInt(src.style.top)
That should fix that error.. anyways like I said later tonight I'll take a look at you other files :)
<font color=teal>xanth = createElement("person");
xanth.setAttribute("name", "Mike");
xanth.setAttribute("mail", "[url:ub9pkk82td]belgedin@earthlink.net[/url:ub9pkk82td]");
xanth.setAttribute("age", 16)
document.forum.appendChild(xanth);
</font id=teal>
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
I'd check out www.dynamic-core.net This guy is also making a DOMLib (I think..)
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
I already know about Pascal Bestebroer DOMLIb. It's this guy that give me the idea to develop my domlib. but I dont like his Dragmanager i dont know why ...
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
That should fix that error.. anyways like I said later tonight I'll take a look at you other files
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
yeah tanks you make it work in NS
if you check other files specialy animation files ignore the "allo" thing think as if was "hello" (not to hard i hope:))
Edited by - jc on 06/10/2001 20:05:22
Edited by - jc on 06/10/2001 20:10:12
Hehe I would have interpereted "allo" as "hello" hehehe. Ok so the DragManager works in NS6 now?
Cool, =D So then your other problem was about constraining the drag to a certain area right?
Anyways gotta run again wanted to check in really quick though hopefully I can offer some more help when I come back :)
<font color=teal>xanth = createElement("person");
xanth.setAttribute("name", "Mike");
xanth.setAttribute("mail", "[url:796eyqy6rr]belgedin@earthlink.net[/url:796eyqy6rr]");
xanth.setAttribute("age", 16)
document.forum.appendChild(xanth);
</font id=teal>
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Cool, =D So then your other problem was about constraining the drag to a certain area right?
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
exacly we can control the left anbd the top limit but cant the right and the bottom limit
i've just realised that because the script done "undefined" for both size so ...
no correct caculation of limit
I think the error is in the main lib.js file i will see ...
found the error, upload new file
(I put the ofset in the style like before)
so ... as well ...
1- nothing apen in NS ok
2- the grabbed layer only respect the left and the top drag limit ok
3- setting child div to drag only in his parent dosent work at all
4- setting a div to drag inside the area of an other one dosent work at all
5- proxy draged div always return to is default position when you drag it for a second time
3 and 4 is the same error and i think we can see the problem in the setDragLimit() method
5 i'd very no idea probably a math problem
Edited by - jc on 06/10/2001 21:19:22
Hi JC,
This seemed to work for me in NS6/IE6
I changed the setDragLimit function to...
DynDiv.prototype.setDraglimit=function(minX,minY,m axX,maxY)
{
a=arguments
if(a.length <= 0){
minX =0
minY =0
maxX = this.elm.parentNode.offsetWidth
maxY = this.elm.parentNode.offsetHeight
}
this.DragMinX = minX
this.DragMinY = minY
this.DragMaxX = maxX
this.DragMaxY = maxY
}
and you'd call it using...
div("child").enableDrag()
div("child").setDraglimit()
I'm Not sure if it's quite what you're looking for but aside from working it also takes advantage of the standards driven DOM node stuff <img src=icon_smile_big.gif border=0 align=middle>
another thing that seemed to work was hardcoding it liek this.
maxX = div("aa").elm.offsetWidth
maxY = div("aa").elm.offsetHeight
So it's kinda got me stumped on why it won't pass as an argument.. I'll try to figure it out when I've got some more time.
<font color=teal>xanth = createElement("person");
xanth.setAttribute("name", "Mike");
xanth.setAttribute("mail", "[url:ahrne3f26i]belgedin@earthlink.net[/url:ahrne3f26i]");
xanth.setAttribute("age", 16)
document.forum.appendChild(xanth);
</font id=teal>
Edited by - Xanth on 06/11/2001 00:49:16
tanks Mike
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
I'm Not sure if it's quite what you're looking for but aside from working it also takes advantage of the standards driven DOM node stuff
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
<pre id=code><font face=courier size=2 id=code> if(a.length == 1){ if(typeof(a[0])=="object")</font id=code></pre id=code>
the idea of the object thing was to restrict the drag area in an other div no matter if it was his parent or not
ps does someone know what rong with the proxy thing i just cant find anything
i'll upload your corrections...
anyway i think i will move the routine that get the size of the div to the trackmouse function to prevent a bug when the div is resized after i set the drag limit
later i'll upload that correction
thanx for your help Mike
Edited by - jc on 06/11/2001 08:44:58
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>the idea of the object thing was to restrict the drag area in an other div no matter if it was his parent or not<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
Yeah I see what you mean.. For that to work you'd have to get the elements x/y position as well as it's width because the "child" elements 0,0 position will be whatever element it's in... ya know? That make any sense at all? LoL
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>ps does someone know what rong with the proxy thing i just cant find anything<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
What's wrong with it?
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>thanx for your help Mike<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
:) I'm glad I could help out.
<font color=teal>xanth = createElement("person");
xanth.setAttribute("name", "Mike");
xanth.setAttribute("mail", "[url:02z1pd46f0]belgedin@earthlink.net[/url:02z1pd46f0]");
xanth.setAttribute("age", 16)
document.forum.appendChild(xanth);
</font id=teal>
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>ps does someone know what rong with the proxy thing i just cant find anything<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
What's wrong with it?
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>
try to drag it a second then a third time you'll see ...
Edited by - jc on 06/11/2001 12:31:27
i changed the code to fit with wath you just say
i'm actualy at school so i cant upload it
(writen very fast may have an error)
the new code is:
(mike the changes are now in teal)
<pre id=code><font face=courier size=2 id=code>
//state = "start"
newX = 0
newY = 0
Drag = {
enabled : false,
grabbedObj : null,
enable : function()
{
document.onmousemove = Drag.trackMouse;
document.onmouseup=Drag.ungrab
Drag.enabled = true
},
disable : function()
{
document.onmousemove = null;
document.onmouseup= null;
Drag.enabled = false
},
trackMouse : function(e)
{
if(ie){e=event}
//alert("move")
Drag.mouseX = e.clientX
Drag.mouseY = e.clientY
if (Drag.grabbed && Drag.enabled)
{
//state = "draging"
obj = Drag.grabbedObj
newX=(Drag.mouseX + Drag.tempX - Drag.selectX)
newY=(Drag.mouseY + Drag.tempY - Drag.selectY)
<font color=teal>
if(obj.draglimit.useNodeDraglimit)
{
startX = (obj.draglimit.node.isNodeParent)?0:parseInt(obj.d raglimit.node.left)
startY = (obj.draglimit.node.isNodeParent)?0:parseInt(obj.d raglimit.node.top)
minX = startX
maxX = startX+obj.draglimit.node.offsetWidth
minY = startY
maxY = startY+obj.draglimit.node.offsetHeight
}
else{
minX = obj.draglimit.minX
maxX = obj.draglimit.minY
minY = obj.draglimit.maxX
maxY = obj.draglimit.maxY
}
</font id=teal>
W = obj.getW()
H = obj.getH()
if(minX !=null){if(newX<minX){newX = minX}}
if(minY !=null){if(newY<minY){newY = minY}}
if(maxX !=null){if((newX+W) > maxX){newX = (maxX-W)}}
if(maxY !=null){if((newY+H) > maxY){newY = (maxY-H)}}
obj.setX(newX)
obj.setY(newY)
}
},
grab : function(e)
{
//alert("grab")
//state = "click"
if(ie){e=event}
src = (ie)? e.srcElement : e.target
dragobj = div(src.id)
if(!dragobj) return false
//state ="exist"
if (!dragobj.drag) return false
//state = "grab"
Drag.selectX = e.clientX
Drag.selectY = e.clientY
Drag.tempX = parseInt(src.style.left)
Drag.tempY = parseInt(src.style.top)
Drag.grabbed = true
if (dragobj.isProxy){Drag.grabbedObj=dragobj.proxyFor }
else {Drag.grabbedObj=dragobj}
},
ungrab : function(){
Drag.grabbed=false
Drag.grabbedObj=null
}
}
DynDiv.prototype.enableDrag = function(obj){
//state = "enabled"
//this.setDraglimit(0,0,null,null)
if(obj){
this.haveDragProxy = true
this.DragPoxy = obj
obj.isProxy = true
obj.proxyFor = this
}
obj = obj||this
obj.drag = true
obj.addEvent("mousedown",Drag.grab)
}
DynDiv.prototype.disableDrag = function(){
this.drag = false
this.removeEvent("mousedown",Drag.grab)
}
<font color=teal>
DynDiv.prototype.setDraglimit=function(minX,minY,m axX,maxY)
{
this.draglimit={}
a=arguments
if(a.length == 1){
if(a[0]=="parent")
{
this.draglimit.useNodeDraglimit = true
this.draglimit.node = this.elm.parentNode
this.draglimit.node.isNodeParent = true
}
else if( typeOf(a[0])=="object" )
{
this.draglimit.useNodeDraglimit = true
this.draglimit.node = a[0]
}
}
else
{
this.draglimit.minX = minX
this.draglimit.minY = minY
this.draglimit.maxX = maxX
this.draglimit.maxY = maxY
}
}
</font id=teal>
</font id=code></pre id=code>
Edited by - jc on 06/11/2001 16:13:11
i've just test it ...
oh my god, this script is verry buggy
notice that the syntax is now
div(...).setDraglimit(obj)
div(...).setDraglimit("parent")
or
div(...).setDraglimit(100,100,200,200)
Ahhh.. I see, I thought you wanted it to do that.. LoL. So basically then you want so when you click/drag a child element it's parent moves?
<font color=teal>xanth = createElement("person");
xanth.setAttribute("name", "Mike");
xanth.setAttribute("mail", "[url:o341hyjdf3]belgedin@earthlink.net[/url:o341hyjdf3]");
xanth.setAttribute("age", 16)
document.forum.appendChild(xanth);
</font id=teal>
Hmm.. you must have changed alot... Nothing works anymore?
<font color=teal>xanth = createElement("person");
xanth.setAttribute("name", "Mike");
xanth.setAttribute("mail", "[url:aw8obxm30n]belgedin@earthlink.net[/url:aw8obxm30n]");
xanth.setAttribute("age", 16)
document.forum.appendChild(xanth);
</font id=teal>
that what surprise me... i've just move some routine of there original place.
also seem that only the sophisticed thing are working no more the basic <img src=icon_smile_question.gif border=0 align=middle>
only the setDraglimit("parent") work
it so mess up that i think i will upload it as Drag2.js
also need an ajustement in the example; have to include the ("parent") but i think i will not modify the example to work with Drag2.. to buggy (ive just modified the code reply, the changes are now in teal, hope this may help you :))
also noticed: this topic is going to be very big should we delete some of the first replies ??
Edited by - jc on 06/11/2001 15:50:33
Edited by - jc on 06/11/2001 15:54:48
Edited by - jc on 06/11/2001 16:19:36
OOps i found my error
i write too fast in the recess at the shcoll
minX = obj.draglimit.minX
<font color=red>maxX</font id=red> = obj.draglimit.<font color=red>minY</font id=red>
<font color=red>minY</font id=red> = obj.draglimit.<font color=red>maxX</font id=red>
maxY = obj.draglimit.maxY
so now there just the proxy thing to deal with
i'll upload the corection over the drag.js file
Edited by - jc on 06/11/2001 19:18:48
the proxy problem resid somwhere in the math caculation because that the the position of the div is the same that the one indicated by the newX and newY var. (I think)
the DragManager of Pascal Bestebroer's
DOMLib has another position calculation
you can see it [url="http://www.dynamic-core.net/tools/domlib/domlib/examples/domlib.examples.dragging.htm"]here[/url:zw17kc57ed]
the proxy thing has for firt goal something like the menubar of Bratta's windows
if I could have a litle hint of wat's rong and how can I correct it i'll be very happy and I will no more borring you with that Drag and Drop SCRIPT
no, i was rong, i've just remplaced the actual calculation of newX / newY by the one used with in Bratta' window and that dont corect the problem at all.
Xhant or anybody else I REALY NEED your help
this topic is very long and I think I will sart a new topic with only the proxy problem
this topic can now be locked if you want to ...
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.