moded from root dir
[brisk.git] / web / briskin5 / dom-drag.js
1 /*\r
2  *  brisk - dom-drag.js\r
3  *\r
4  *  Copyright (C) 2006-2008 Matteo Nastasi\r
5  *                          mailto: nastasi@alternativeoutput.it \r
6  *                                  matteo.nastasi@milug.org\r
7  *                          web: http://www.alternativeoutput.it\r
8  *\r
9  * This program is free software; you can redistribute it and/or modify\r
10  * it under the terms of the GNU General Public License as published by\r
11  * the Free Software Foundation; either version 2 of the License, or\r
12  * (at your option) any later version.\r
13  *\r
14  * This program is distributed in the hope that it will be useful, but\r
15  * WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
17  * General Public License for more details. You should have received a\r
18  * copy of the GNU General Public License along with this program; if\r
19  * not, write to the Free Software Foundation, Inc, 59 Temple Place -\r
20  * Suite 330, Boston, MA 02111-1307, USA.\r
21  *\r
22  * $Id$\r
23  *\r
24  */\r
25 \r
26 /**************************************************\r
27  * dom-drag.js\r
28  * 09.25.2001\r
29  * www.youngpup.net\r
30  **************************************************\r
31  * 10.28.2001 - fixed minor bug where events\r
32  * sometimes fired off the handle, not the root.\r
33  **************************************************/\r
34 \r
35 var Drag = {\r
36 \r
37         obj : null,\r
38 \r
39         init : function(o, mouseup_cb, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)\r
40         {\r
41                 o.onmousedown   = Drag.start;\r
42                 o.mouseup_cb    = mouseup_cb;\r
43 \r
44                 /*              alert("agnulla"+o.style.left); */\r
45 \r
46                 o.hmode                 = bSwapHorzRef ? false : true ;\r
47                 o.vmode                 = bSwapVertRef ? false : true ;\r
48 \r
49                 o.root = oRoot && oRoot != null ? oRoot : o ;\r
50 \r
51                 if (o.hmode && isNaN(parseInt(o.root.style.left  ))) {\r
52                     var res = parseInt(getStyle(o, "left", "left"));\r
53                     if (isNaN(res)) {\r
54                         o.root.style.left   = "0px";\r
55                     }\r
56                     else {\r
57                         o.root.style.left   = res;\r
58                     }\r
59                 }\r
60                 if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) {\r
61                     var res = parseInt(getStyle(o, "top", "top"));\r
62                     if (isNaN(res)) {\r
63                         o.root.style.top   = "0px";\r
64                     }\r
65                     else {\r
66                         o.root.style.top   = res;\r
67                     }\r
68                 }\r
69                 if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";\r
70                 if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";\r
71 \r
72                 o.minX  = typeof minX != 'undefined' ? minX : null;\r
73                 o.minY  = typeof minY != 'undefined' ? minY : null;\r
74                 o.maxX  = typeof maxX != 'undefined' ? maxX : null;\r
75                 o.maxY  = typeof maxY != 'undefined' ? maxY : null;\r
76 \r
77                 o.xMapper = fXMapper ? fXMapper : null;\r
78                 o.yMapper = fYMapper ? fYMapper : null;\r
79 \r
80                 o.root.onDragStart      = new Function();\r
81                 o.root.onDragEnd        = new Function();\r
82                 o.root.onDrag           = new Function();\r
83         },\r
84 \r
85         start : function(e)\r
86         {\r
87                 var o = Drag.obj = this;\r
88                 e = Drag.fixE(e);\r
89 \r
90                 o.oldzidx = o.style.zIndex;\r
91                 o.style.zIndex = 10;\r
92 \r
93                 // alert("start");\r
94 \r
95                 var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
96                 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
97                 o.root.onDragStart(x, y);\r
98 \r
99                 o.lastMouseX    = e.clientX;\r
100                 o.lastMouseY    = e.clientY;\r
101 \r
102                 if (o.hmode) {\r
103                         if (o.minX != null)     o.minMouseX     = e.clientX - x + o.minX;\r
104                         if (o.maxX != null)     o.maxMouseX     = o.minMouseX + o.maxX - o.minX;\r
105                 } else {\r
106                         if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;\r
107                         if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;\r
108                 }\r
109 \r
110                 if (o.vmode) {\r
111                         if (o.minY != null)     o.minMouseY     = e.clientY - y + o.minY;\r
112                         if (o.maxY != null)     o.maxMouseY     = o.minMouseY + o.maxY - o.minY;\r
113                 } else {\r
114                         if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;\r
115                         if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;\r
116                 }\r
117 \r
118                 document.onmousemove    = Drag.drag;\r
119                 document.onmouseup      = Drag.end;\r
120 \r
121                 return false;\r
122         },\r
123 \r
124         drag : function(e)\r
125         {\r
126                 e = Drag.fixE(e);\r
127                 var o = Drag.obj;\r
128 \r
129                 var ey  = e.clientY;\r
130                 var ex  = e.clientX;\r
131                 var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
132                 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
133                 var nx, ny;\r
134 \r
135                 if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);\r
136                 if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);\r
137                 if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);\r
138                 if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);\r
139 \r
140                 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));\r
141                 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));\r
142 \r
143                 if (o.xMapper)          nx = o.xMapper(y)\r
144                 else if (o.yMapper)     ny = o.yMapper(x)\r
145 \r
146                 Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";\r
147                 Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";\r
148                 Drag.obj.lastMouseX     = ex;\r
149                 Drag.obj.lastMouseY     = ey;\r
150 \r
151                 Drag.obj.root.onDrag(nx, ny);\r
152                 return false;\r
153         },\r
154 \r
155         end : function(e)\r
156         {\r
157                 e = Drag.fixE(e);\r
158                 var o = Drag.obj;\r
159 \r
160                 o.style.zIndex = o.oldzidx;\r
161                 // alert("END");\r
162                 if (o.mouseup_cb != null) {\r
163                     if (o.mouseup_cb(o) == 1) {\r
164                         o.onmousedown = null;\r
165                     }\r
166                 }\r
167 \r
168                 document.onmousemove = null;\r
169                 document.onmouseup   = null;\r
170                 Drag.obj.root.onDragEnd(        parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), \r
171                                                                         parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));\r
172                 Drag.obj = null;\r
173         },\r
174 \r
175         fixE : function(e)\r
176         {\r
177                 if (typeof e == 'undefined') e = window.event;\r
178                 if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;\r
179                 if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;\r
180                 return e;\r
181         }\r
182 };\r