2 * brisk - dom-drag.js
\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
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
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
24 /**************************************************
\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
37 init : function(o, mouseup_cb, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
\r
39 o.onmousedown = Drag.start;
\r
40 o.ontouchstart = Drag.start;
\r
41 o.mouseup_cb = mouseup_cb;
\r
43 /* alert("agnulla"+o.style.left); */
\r
45 o.hmode = bSwapHorzRef ? false : true ;
\r
46 o.vmode = bSwapVertRef ? false : true ;
\r
48 o.root = oRoot && oRoot != null ? oRoot : o ;
\r
50 if (o.hmode && isNaN(parseInt(o.root.style.left ))) {
\r
51 var res = parseInt(getStyle(o, "left", "left"));
\r
53 o.root.style.left = "0px";
\r
56 o.root.style.left = res;
\r
59 if (o.vmode && isNaN(parseInt(o.root.style.top ))) {
\r
60 var res = parseInt(getStyle(o, "top", "top"));
\r
62 o.root.style.top = "0px";
\r
65 o.root.style.top = res;
\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
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
76 o.xMapper = fXMapper ? fXMapper : null;
\r
77 o.yMapper = fYMapper ? fYMapper : null;
\r
79 o.root.onDragStart = new Function();
\r
80 o.root.onDragEnd = new Function();
\r
81 o.root.onDrag = new Function();
\r
86 var o = Drag.obj = this;
\r
89 o.oldzidx = o.style.zIndex;
\r
90 o.style.zIndex = 10;
\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
100 if (e.type == 'mousedown') {
\r
101 o.root.onDragStart(x, y);
\r
102 clientX = e.clientX;
\r
103 clientY = e.clientY;
\r
106 var touch = event.targetTouches[0];
\r
107 clientX = touch.pageX;
\r
108 clientY = touch.pageY;
\r
110 o.lastMouseX = clientX;
\r
111 o.lastMouseY = clientY;
\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
117 if (o.minX != null) o.maxMouseX = -o.minX + clientX + x;
\r
118 if (o.maxX != null) o.minMouseX = -o.maxX + clientX + x;
\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
125 if (o.minY != null) o.maxMouseY = -o.minY + clientY + y;
\r
126 if (o.maxY != null) o.minMouseY = -o.maxY + clientY + y;
\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
135 console.log('here assign touch move');
\r
136 document.ontouchmove = Drag.drag;
\r
137 document.ontouchend = Drag.end;
\r
140 //if (e.type != 'mousedown') {
\r
141 // e.preventDefault();
\r
148 console.log('drag: begin');
\r
156 if (e.type == 'mousemove') {
\r
161 var touch = event.targetTouches[0];
\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
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
175 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
\r
176 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
\r
178 if (o.xMapper) nx = o.xMapper(y)
\r
179 else if (o.yMapper) ny = o.yMapper(x)
\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
186 Drag.obj.root.onDrag(nx, ny);
\r
195 o.style.zIndex = o.oldzidx;
\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
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
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