add touch devices management for drag and drop
[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.ontouchstart  = Drag.start;\r
41                 o.mouseup_cb    = mouseup_cb;\r
42 \r
43                 /*              alert("agnulla"+o.style.left); */\r
44 \r
45                 o.hmode                 = bSwapHorzRef ? false : true ;\r
46                 o.vmode                 = bSwapVertRef ? false : true ;\r
47 \r
48                 o.root = oRoot && oRoot != null ? oRoot : o ;\r
49 \r
50                 if (o.hmode && isNaN(parseInt(o.root.style.left  ))) {\r
51                     var res = parseInt(getStyle(o, "left", "left"));\r
52                     if (isNaN(res)) {\r
53                         o.root.style.left   = "0px";\r
54                     }\r
55                     else {\r
56                         o.root.style.left   = res;\r
57                     }\r
58                 }\r
59                 if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) {\r
60                     var res = parseInt(getStyle(o, "top", "top"));\r
61                     if (isNaN(res)) {\r
62                         o.root.style.top   = "0px";\r
63                     }\r
64                     else {\r
65                         o.root.style.top   = res;\r
66                     }\r
67                 }\r
68                 if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";\r
69                 if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";\r
70 \r
71                 o.minX  = typeof minX != 'undefined' ? minX : null;\r
72                 o.minY  = typeof minY != 'undefined' ? minY : null;\r
73                 o.maxX  = typeof maxX != 'undefined' ? maxX : null;\r
74                 o.maxY  = typeof maxY != 'undefined' ? maxY : null;\r
75 \r
76                 o.xMapper = fXMapper ? fXMapper : null;\r
77                 o.yMapper = fYMapper ? fYMapper : null;\r
78 \r
79                 o.root.onDragStart      = new Function();\r
80                 o.root.onDragEnd        = new Function();\r
81                 o.root.onDrag           = new Function();\r
82         },\r
83 \r
84         start : function(e)\r
85         {\r
86                 var o = Drag.obj = this;\r
87                 e = Drag.fixE(e);\r
88 \r
89                 o.oldzidx = o.style.zIndex;\r
90                 o.style.zIndex = 10;\r
91 \r
92                 // alert("start");\r
93 \r
94                 var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
95                 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
96 \r
97                 var clientX = 0;\r
98                 var clientY = 0;\r
99 \r
100                 if (e.type == 'mousedown') {\r
101                     o.root.onDragStart(x, y);\r
102                     clientX = e.clientX;\r
103                     clientY = e.clientY;\r
104                 }\r
105                 else {\r
106                     var touch = event.targetTouches[0];\r
107                     clientX = touch.pageX;\r
108                     clientY = touch.pageY;\r
109                 }\r
110                 o.lastMouseX    = clientX;\r
111                 o.lastMouseY    = clientY;\r
112 \r
113                 if (o.hmode) {\r
114                         if (o.minX != null)     o.minMouseX     = clientX - x + o.minX;\r
115                         if (o.maxX != null)     o.maxMouseX     = o.minMouseX + o.maxX - o.minX;\r
116                 } else {\r
117                         if (o.minX != null) o.maxMouseX = -o.minX + clientX + x;\r
118                         if (o.maxX != null) o.minMouseX = -o.maxX + clientX + x;\r
119                 }\r
120 \r
121                 if (o.vmode) {\r
122                         if (o.minY != null)     o.minMouseY     = clientY - y + o.minY;\r
123                         if (o.maxY != null)     o.maxMouseY     = o.minMouseY + o.maxY - o.minY;\r
124                 } else {\r
125                         if (o.minY != null) o.maxMouseY = -o.minY + clientY + y;\r
126                         if (o.maxY != null) o.minMouseY = -o.maxY + clientY + y;\r
127                 }\r
128 \r
129           if (e.type == 'mousedown') {\r
130             console.log('here assign mouse move');\r
131             document.onmousemove = Drag.drag;\r
132             document.onmouseup  = Drag.end;\r
133           }\r
134           else {\r
135             console.log('here assign touch move');\r
136                    document.ontouchmove = Drag.drag;\r
137                    document.ontouchend = Drag.end;\r
138                 }\r
139 \r
140           //if (e.type != 'mousedown') {\r
141           //          e.preventDefault();\r
142           //      }\r
143           return false;\r
144         },\r
145 \r
146         drag : function(e)\r
147   {\r
148     console.log('drag: begin');\r
149     \r
150                 e = Drag.fixE(e);\r
151                 var o = Drag.obj;\r
152 \r
153                 var ex = 0;\r
154                 var ey = 0;\r
155     \r
156     if (e.type == 'mousemove') {\r
157       ex = e.clientX;\r
158       ey = e.clientY;\r
159     }\r
160     else {\r
161       var touch = event.targetTouches[0];\r
162       ex = touch.pageX;\r
163       ey = touch.pageY;\r
164     }\r
165 \r
166                 var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
167                 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
168                 var nx, ny;\r
169 \r
170                 if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);\r
171                 if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);\r
172                 if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);\r
173                 if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);\r
174 \r
175                 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));\r
176                 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));\r
177 \r
178                 if (o.xMapper)          nx = o.xMapper(y)\r
179                 else if (o.yMapper)     ny = o.yMapper(x)\r
180 \r
181                 Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";\r
182                 Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";\r
183                 Drag.obj.lastMouseX     = ex;\r
184                 Drag.obj.lastMouseY     = ey;\r
185 \r
186                 Drag.obj.root.onDrag(nx, ny);\r
187                 return false;\r
188         },\r
189 \r
190         end : function(e)\r
191         {\r
192                 e = Drag.fixE(e);\r
193                 var o = Drag.obj;\r
194 \r
195                 o.style.zIndex = o.oldzidx;\r
196                 // alert("END");\r
197                 if (o.mouseup_cb != null) {\r
198                     if (o.mouseup_cb(o) == 1) {\r
199                       o.onmousedown = null;\r
200                       o.ontouchstart = null;\r
201                     }\r
202                 }\r
203 \r
204                 document.onmousemove = null;\r
205                 document.onmouseup   = null;\r
206                 document.ontouchmove = null;\r
207                 document.ontouchend  = null;\r
208                 Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), \r
209                                         parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));\r
210                 Drag.obj = null;\r
211         },\r
212 \r
213         fixE : function(e)\r
214         {\r
215                 if (typeof e == 'undefined') e = window.event;\r
216                 if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;\r
217                 if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;\r
218                 return e;\r
219         }\r
220 };\r