2 * brisk - dom-drag.js
\r
4 * Copyright (C) 2006 matteo.nastasi@milug.org
\r
6 * This program is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * This program is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
14 * General Public License for more details. You should have received a
\r
15 * copy of the GNU General Public License along with this program; if
\r
16 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
\r
17 * Suite 330, Boston, MA 02111-1307, USA.
\r
23 /**************************************************
\r
27 **************************************************
\r
28 * 10.28.2001 - fixed minor bug where events
\r
29 * sometimes fired off the handle, not the root.
\r
30 **************************************************/
\r
36 init : function(o, mouseup_cb, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
\r
38 o.onmousedown = Drag.start;
\r
39 o.mouseup_cb = mouseup_cb;
\r
41 /* alert("agnulla"+o.style.left); */
\r
43 o.hmode = bSwapHorzRef ? false : true ;
\r
44 o.vmode = bSwapVertRef ? false : true ;
\r
46 o.root = oRoot && oRoot != null ? oRoot : o ;
\r
48 if (o.hmode && isNaN(parseInt(o.root.style.left ))) {
\r
49 o.root.style.left = window.getComputedStyle(o, "").getPropertyValue("left");
\r
50 if (isNaN(parseInt(o.root.style.left)))
\r
51 o.root.style.left = "0px";
\r
53 if (o.vmode && isNaN(parseInt(o.root.style.top ))) {
\r
54 o.root.style.top = window.getComputedStyle(o, "").getPropertyValue("top");
\r
55 if (isNaN(parseInt(o.root.style.top)))
\r
56 o.root.style.top = "0px";
\r
58 if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right = "0px";
\r
59 if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
\r
61 o.minX = typeof minX != 'undefined' ? minX : null;
\r
62 o.minY = typeof minY != 'undefined' ? minY : null;
\r
63 o.maxX = typeof maxX != 'undefined' ? maxX : null;
\r
64 o.maxY = typeof maxY != 'undefined' ? maxY : null;
\r
66 o.xMapper = fXMapper ? fXMapper : null;
\r
67 o.yMapper = fYMapper ? fYMapper : null;
\r
69 o.root.onDragStart = new Function();
\r
70 o.root.onDragEnd = new Function();
\r
71 o.root.onDrag = new Function();
\r
76 var o = Drag.obj = this;
\r
79 o.oldzidx = o.style.zIndex;
\r
80 o.style.zIndex = 10;
\r
84 var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
\r
85 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
\r
86 o.root.onDragStart(x, y);
\r
88 o.lastMouseX = e.clientX;
\r
89 o.lastMouseY = e.clientY;
\r
92 if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
\r
93 if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
\r
95 if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
\r
96 if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
\r
100 if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
\r
101 if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
\r
103 if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
\r
104 if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
\r
107 document.onmousemove = Drag.drag;
\r
108 document.onmouseup = Drag.end;
\r
118 var ey = e.clientY;
\r
119 var ex = e.clientX;
\r
120 var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
\r
121 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
\r
124 if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
\r
125 if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
\r
126 if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
\r
127 if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
\r
129 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
\r
130 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
\r
132 if (o.xMapper) nx = o.xMapper(y)
\r
133 else if (o.yMapper) ny = o.yMapper(x)
\r
135 Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
\r
136 Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
\r
137 Drag.obj.lastMouseX = ex;
\r
138 Drag.obj.lastMouseY = ey;
\r
140 Drag.obj.root.onDrag(nx, ny);
\r
149 o.style.zIndex = o.oldzidx;
\r
151 if (o.mouseup_cb != null) {
\r
152 if (o.mouseup_cb(o) == 1) {
\r
153 o.onmousedown = null;
\r
157 document.onmousemove = null;
\r
158 document.onmouseup = null;
\r
159 Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
\r
160 parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
\r
166 if (typeof e == 'undefined') e = window.event;
\r
167 if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
\r
168 if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
\r