2597 |
05 Mar 08 |
gregory |
1 |
/************************************************** |
2597 |
05 Mar 08 |
gregory |
* dom-drag.js |
2597 |
05 Mar 08 |
gregory |
* 09.25.2001 |
2597 |
05 Mar 08 |
gregory |
* www.youngpup.net |
2597 |
05 Mar 08 |
gregory |
* Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005 |
2597 |
05 Mar 08 |
gregory |
6 |
************************************************** |
2597 |
05 Mar 08 |
gregory |
* 10.28.2001 - fixed minor bug where events |
2597 |
05 Mar 08 |
gregory |
* sometimes fired off the handle, not the root. |
2597 |
05 Mar 08 |
gregory |
9 |
**************************************************/ |
2597 |
05 Mar 08 |
gregory |
10 |
|
2597 |
05 Mar 08 |
gregory |
var Drag = { |
2597 |
05 Mar 08 |
gregory |
12 |
|
2597 |
05 Mar 08 |
gregory |
obj : null, |
2597 |
05 Mar 08 |
gregory |
14 |
|
2597 |
05 Mar 08 |
gregory |
init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) |
2597 |
05 Mar 08 |
gregory |
16 |
{ |
2597 |
05 Mar 08 |
gregory |
o.onmousedown = Drag.start; |
2597 |
05 Mar 08 |
gregory |
18 |
|
2597 |
05 Mar 08 |
gregory |
o.hmode = bSwapHorzRef ? false : true ; |
2597 |
05 Mar 08 |
gregory |
o.vmode = bSwapVertRef ? false : true ; |
2597 |
05 Mar 08 |
gregory |
21 |
|
2597 |
05 Mar 08 |
gregory |
o.root = oRoot && oRoot != null ? oRoot : o ; |
2597 |
05 Mar 08 |
gregory |
23 |
|
2597 |
05 Mar 08 |
gregory |
if (o.hmode && isNaN(parseInt(o.root.style.left ))) o.root.style.left = "0px"; |
2597 |
05 Mar 08 |
gregory |
if (o.vmode && isNaN(parseInt(o.root.style.top ))) o.root.style.top = "0px"; |
2597 |
05 Mar 08 |
gregory |
if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right = "0px"; |
2597 |
05 Mar 08 |
gregory |
if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px"; |
2597 |
05 Mar 08 |
gregory |
28 |
|
2597 |
05 Mar 08 |
gregory |
o.minX = typeof minX != 'undefined' ? minX : null; |
2597 |
05 Mar 08 |
gregory |
o.minY = typeof minY != 'undefined' ? minY : null; |
2597 |
05 Mar 08 |
gregory |
o.maxX = typeof maxX != 'undefined' ? maxX : null; |
2597 |
05 Mar 08 |
gregory |
o.maxY = typeof maxY != 'undefined' ? maxY : null; |
2597 |
05 Mar 08 |
gregory |
33 |
|
2597 |
05 Mar 08 |
gregory |
o.xMapper = fXMapper ? fXMapper : null; |
2597 |
05 Mar 08 |
gregory |
o.yMapper = fYMapper ? fYMapper : null; |
2597 |
05 Mar 08 |
gregory |
36 |
|
2597 |
05 Mar 08 |
gregory |
o.root.onDragStart = new Function(); |
2597 |
05 Mar 08 |
gregory |
o.root.onDragEnd = new Function(); |
2597 |
05 Mar 08 |
gregory |
o.root.onDrag = new Function(); |
2597 |
05 Mar 08 |
gregory |
40 |
}, |
2597 |
05 Mar 08 |
gregory |
41 |
|
2597 |
05 Mar 08 |
gregory |
start : function(e) |
2597 |
05 Mar 08 |
gregory |
43 |
{ |
2597 |
05 Mar 08 |
gregory |
var o = Drag.obj = this; |
2597 |
05 Mar 08 |
gregory |
e = Drag.fixE(e); |
2597 |
05 Mar 08 |
gregory |
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom); |
2597 |
05 Mar 08 |
gregory |
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right ); |
2597 |
05 Mar 08 |
gregory |
o.root.onDragStart(x, y); |
2597 |
05 Mar 08 |
gregory |
49 |
|
2597 |
05 Mar 08 |
gregory |
o.lastMouseX = e.clientX; |
2597 |
05 Mar 08 |
gregory |
o.lastMouseY = e.clientY; |
2597 |
05 Mar 08 |
gregory |
52 |
|
2597 |
05 Mar 08 |
gregory |
if (o.hmode) { |
2597 |
05 Mar 08 |
gregory |
if (o.minX != null) o.minMouseX = e.clientX - x + o.minX; |
2597 |
05 Mar 08 |
gregory |
if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX; |
2597 |
05 Mar 08 |
gregory |
} else { |
2597 |
05 Mar 08 |
gregory |
if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x; |
2597 |
05 Mar 08 |
gregory |
if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x; |
2597 |
05 Mar 08 |
gregory |
59 |
} |
2597 |
05 Mar 08 |
gregory |
60 |
|
2597 |
05 Mar 08 |
gregory |
if (o.vmode) { |
2597 |
05 Mar 08 |
gregory |
if (o.minY != null) o.minMouseY = e.clientY - y + o.minY; |
2597 |
05 Mar 08 |
gregory |
if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY; |
2597 |
05 Mar 08 |
gregory |
} else { |
2597 |
05 Mar 08 |
gregory |
if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y; |
2597 |
05 Mar 08 |
gregory |
if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y; |
2597 |
05 Mar 08 |
gregory |
67 |
} |
2597 |
05 Mar 08 |
gregory |
68 |
|
2597 |
05 Mar 08 |
gregory |
document.onmousemove = Drag.drag; |
2597 |
05 Mar 08 |
gregory |
document.onmouseup = Drag.end; |
2597 |
05 Mar 08 |
gregory |
71 |
|
2597 |
05 Mar 08 |
gregory |
return false; |
2597 |
05 Mar 08 |
gregory |
73 |
}, |
2597 |
05 Mar 08 |
gregory |
74 |
|
2597 |
05 Mar 08 |
gregory |
drag : function(e) |
2597 |
05 Mar 08 |
gregory |
76 |
{ |
2597 |
05 Mar 08 |
gregory |
e = Drag.fixE(e); |
2597 |
05 Mar 08 |
gregory |
var o = Drag.obj; |
2597 |
05 Mar 08 |
gregory |
79 |
|
2597 |
05 Mar 08 |
gregory |
var ey = e.clientY; |
2597 |
05 Mar 08 |
gregory |
var ex = e.clientX; |
2597 |
05 Mar 08 |
gregory |
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom); |
2597 |
05 Mar 08 |
gregory |
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right ); |
2597 |
05 Mar 08 |
gregory |
var nx, ny; |
2597 |
05 Mar 08 |
gregory |
85 |
|
2597 |
05 Mar 08 |
gregory |
if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX); |
2597 |
05 Mar 08 |
gregory |
if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX); |
2597 |
05 Mar 08 |
gregory |
if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY); |
2597 |
05 Mar 08 |
gregory |
if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY); |
2597 |
05 Mar 08 |
gregory |
90 |
|
2597 |
05 Mar 08 |
gregory |
nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1)); |
2597 |
05 Mar 08 |
gregory |
ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1)); |
2597 |
05 Mar 08 |
gregory |
93 |
|
2597 |
05 Mar 08 |
gregory |
if (o.xMapper) nx = o.xMapper(y) |
2597 |
05 Mar 08 |
gregory |
else if (o.yMapper) ny = o.yMapper(x) |
2597 |
05 Mar 08 |
gregory |
96 |
|
2597 |
05 Mar 08 |
gregory |
Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px"; |
2597 |
05 Mar 08 |
gregory |
Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px"; |
2597 |
05 Mar 08 |
gregory |
Drag.obj.lastMouseX = ex; |
2597 |
05 Mar 08 |
gregory |
Drag.obj.lastMouseY = ey; |
2597 |
05 Mar 08 |
gregory |
101 |
|
2597 |
05 Mar 08 |
gregory |
Drag.obj.root.onDrag(nx, ny); |
2597 |
05 Mar 08 |
gregory |
return false; |
2597 |
05 Mar 08 |
gregory |
104 |
}, |
2597 |
05 Mar 08 |
gregory |
105 |
|
2597 |
05 Mar 08 |
gregory |
end : function() |
2597 |
05 Mar 08 |
gregory |
107 |
{ |
2597 |
05 Mar 08 |
gregory |
document.onmousemove = null; |
2597 |
05 Mar 08 |
gregory |
document.onmouseup = null; |
2597 |
05 Mar 08 |
gregory |
Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), |
2597 |
05 Mar 08 |
gregory |
parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"])); |
2597 |
05 Mar 08 |
gregory |
Drag.obj = null; |
2597 |
05 Mar 08 |
gregory |
113 |
}, |
2597 |
05 Mar 08 |
gregory |
114 |
|
2597 |
05 Mar 08 |
gregory |
fixE : function(e) |
2597 |
05 Mar 08 |
gregory |
116 |
{ |
2597 |
05 Mar 08 |
gregory |
if (typeof e == 'undefined') e = window.event; |
2597 |
05 Mar 08 |
gregory |
if (typeof e.layerX == 'undefined') e.layerX = e.offsetX; |
2597 |
05 Mar 08 |
gregory |
if (typeof e.layerY == 'undefined') e.layerY = e.offsetY; |
2597 |
05 Mar 08 |
gregory |
return e; |
2597 |
05 Mar 08 |
gregory |
121 |
} |
2597 |
05 Mar 08 |
gregory |
122 |
}; |