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