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