1b0c8af448ec91cf1c01a988fd79dec1bd1da3be
[brisk.git] / web / dom-drag.js
1 /*\r
2  *  brisk - dom-drag.js\r
3  *\r
4  *  Copyright (C) 2006 matteo.nastasi@milug.org\r
5  *\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
10  *\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
18  *\r
19  * $Id$\r
20  *\r
21  */\r
22 \r
23 /**************************************************\r
24  * dom-drag.js\r
25  * 09.25.2001\r
26  * www.youngpup.net\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
31 \r
32 var Drag = {\r
33 \r
34         obj : null,\r
35 \r
36         init : function(o, mouseup_cb, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)\r
37         {\r
38                 o.onmousedown   = Drag.start;\r
39                 o.mouseup_cb    = mouseup_cb;\r
40 \r
41                 /*              alert("agnulla"+o.style.left); */\r
42 \r
43                 o.hmode                 = bSwapHorzRef ? false : true ;\r
44                 o.vmode                 = bSwapVertRef ? false : true ;\r
45 \r
46                 o.root = oRoot && oRoot != null ? oRoot : o ;\r
47 \r
48                 if (o.hmode && isNaN(parseInt(o.root.style.left  ))) {\r
49                     var res = parseInt(getStyle(o, "left", "left"));\r
50                     if (isNaN(res)) {\r
51                         o.root.style.left   = "0px";\r
52                     }\r
53                     else {\r
54                         o.root.style.left   = res;\r
55                     }\r
56                 }\r
57                 if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) {\r
58                     var res = parseInt(getStyle(o, "top", "top"));\r
59                     if (isNaN(res)) {\r
60                         o.root.style.top   = "0px";\r
61                     }\r
62                     else {\r
63                         o.root.style.top   = res;\r
64                     }\r
65                 }\r
66                 if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";\r
67                 if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";\r
68 \r
69                 o.minX  = typeof minX != 'undefined' ? minX : null;\r
70                 o.minY  = typeof minY != 'undefined' ? minY : null;\r
71                 o.maxX  = typeof maxX != 'undefined' ? maxX : null;\r
72                 o.maxY  = typeof maxY != 'undefined' ? maxY : null;\r
73 \r
74                 o.xMapper = fXMapper ? fXMapper : null;\r
75                 o.yMapper = fYMapper ? fYMapper : null;\r
76 \r
77                 o.root.onDragStart      = new Function();\r
78                 o.root.onDragEnd        = new Function();\r
79                 o.root.onDrag           = new Function();\r
80         },\r
81 \r
82         start : function(e)\r
83         {\r
84                 var o = Drag.obj = this;\r
85                 e = Drag.fixE(e);\r
86 \r
87                 o.oldzidx = o.style.zIndex;\r
88                 o.style.zIndex = 10;\r
89 \r
90                 // alert("start");\r
91 \r
92                 var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
93                 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
94                 o.root.onDragStart(x, y);\r
95 \r
96                 o.lastMouseX    = e.clientX;\r
97                 o.lastMouseY    = e.clientY;\r
98 \r
99                 if (o.hmode) {\r
100                         if (o.minX != null)     o.minMouseX     = e.clientX - x + o.minX;\r
101                         if (o.maxX != null)     o.maxMouseX     = o.minMouseX + o.maxX - o.minX;\r
102                 } else {\r
103                         if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;\r
104                         if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;\r
105                 }\r
106 \r
107                 if (o.vmode) {\r
108                         if (o.minY != null)     o.minMouseY     = e.clientY - y + o.minY;\r
109                         if (o.maxY != null)     o.maxMouseY     = o.minMouseY + o.maxY - o.minY;\r
110                 } else {\r
111                         if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;\r
112                         if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;\r
113                 }\r
114 \r
115                 document.onmousemove    = Drag.drag;\r
116                 document.onmouseup      = Drag.end;\r
117 \r
118                 return false;\r
119         },\r
120 \r
121         drag : function(e)\r
122         {\r
123                 e = Drag.fixE(e);\r
124                 var o = Drag.obj;\r
125 \r
126                 var ey  = e.clientY;\r
127                 var ex  = e.clientX;\r
128                 var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
129                 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
130                 var nx, ny;\r
131 \r
132                 if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);\r
133                 if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);\r
134                 if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);\r
135                 if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);\r
136 \r
137                 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));\r
138                 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));\r
139 \r
140                 if (o.xMapper)          nx = o.xMapper(y)\r
141                 else if (o.yMapper)     ny = o.yMapper(x)\r
142 \r
143                 Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";\r
144                 Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";\r
145                 Drag.obj.lastMouseX     = ex;\r
146                 Drag.obj.lastMouseY     = ey;\r
147 \r
148                 Drag.obj.root.onDrag(nx, ny);\r
149                 return false;\r
150         },\r
151 \r
152         end : function(e)\r
153         {\r
154                 e = Drag.fixE(e);\r
155                 var o = Drag.obj;\r
156 \r
157                 o.style.zIndex = o.oldzidx;\r
158                 // alert("END");\r
159                 if (o.mouseup_cb != null) {\r
160                     if (o.mouseup_cb(o) == 1) {\r
161                         o.onmousedown = null;\r
162                     }\r
163                 }\r
164 \r
165                 document.onmousemove = null;\r
166                 document.onmouseup   = null;\r
167                 Drag.obj.root.onDragEnd(        parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), \r
168                                                                         parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));\r
169                 Drag.obj = null;\r
170         },\r
171 \r
172         fixE : function(e)\r
173         {\r
174                 if (typeof e == 'undefined') e = window.event;\r
175                 if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;\r
176                 if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;\r
177                 return e;\r
178         }\r
179 };\r