2 * brisk - dom-drag.js
\r
4 * Copyright (C) 2006-2011 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
33 function Drag(mouseup_cb, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
\r
35 console.log("xx"+this);
\r
36 this.onmousedown = Drag.prototype.start;
\r
37 this.mouseup_cb = mouseup_cb;
\r
38 this.connect = function(conn)
\r
40 this.parentNode.connected[this.parentNode.connected.length] = conn;
\r
43 this.connected = new Array();
\r
44 /* alert("agnulla"+this.style.left); */
\r
46 this.hmode = bSwapHorzRef ? false : true ;
\r
47 this.vmode = bSwapVertRef ? false : true ;
\r
49 this.root = this.xynt_dom_el && this.xynt_dom_el != null ? this.xynt_dom_el : this ;
\r
51 if (this.hmode && isNaN(parseInt(this.root.style.left ))) {
\r
52 var res = parseInt(getStyle(o, "left", "left"));
\r
54 this.root.style.left = "0px";
\r
57 this.root.style.left = res;
\r
60 if (this.vmode && isNaN(parseInt(this.root.style.top ))) {
\r
61 var res = parseInt(getStyle(o, "top", "top"));
\r
63 this.root.style.top = "0px";
\r
66 this.root.style.top = res;
\r
69 if (!this.hmode && isNaN(parseInt(this.root.style.right ))) this.root.style.right = "0px";
\r
70 if (!this.vmode && isNaN(parseInt(this.root.style.bottom))) this.root.style.bottom = "0px";
\r
72 this.minX = typeof minX != 'undefined' ? minX : null;
\r
73 this.minY = typeof minY != 'undefined' ? minY : null;
\r
74 this.maxX = typeof maxX != 'undefined' ? maxX : null;
\r
75 this.maxY = typeof maxY != 'undefined' ? maxY : null;
\r
77 this.xMapper = fXMapper ? fXMapper : null;
\r
78 this.yMapper = fYMapper ? fYMapper : null;
\r
80 this.root.onDragStart = new Function();
\r
81 this.root.onDragEnd = new Function();
\r
82 this.root.onDrag = new Function();
\r
91 console.log("Drag.start");
\r
92 var o = Drag.obj = this;
\r
93 e = Drag.prototype.fixE(e);
\r
96 var e = window.event
\r
98 e.cancelBubble = true;
\r
99 if (e.stopPropagation)
\r
100 e.stopPropagation();
\r
102 o.oldzidx = o.style.zIndex;
\r
103 o.style.zIndex = 10;
\r
107 var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
\r
108 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
\r
109 o.root.onDragStart(x, y);
\r
111 o.lastMouseX = e.clientX;
\r
112 o.lastMouseY = e.clientY;
\r
115 if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
\r
116 if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
\r
118 if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
\r
119 if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
\r
123 if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
\r
124 if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
\r
126 if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
\r
127 if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
\r
130 document.onmousemove = Drag.prototype.drag;
\r
131 document.onmouseup = Drag.prototype.end;
\r
133 for(var i = 0 ; i < o.connected.length ; i++) {
\r
134 o.connected[i].update('start', o);
\r
142 e = Drag.prototype.fixE(e);
\r
145 var ey = e.clientY;
\r
146 var ex = e.clientX;
\r
147 var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
\r
148 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
\r
151 if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
\r
152 if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
\r
153 if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
\r
154 if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
\r
156 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
\r
157 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
\r
159 if (o.xMapper) nx = o.xMapper(y)
\r
160 else if (o.yMapper) ny = o.yMapper(x)
\r
162 Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
\r
163 Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
\r
164 Drag.obj.lastMouseX = ex;
\r
165 Drag.obj.lastMouseY = ey;
\r
167 Drag.obj.root.onDrag(nx, ny);
\r
169 for(var i = 0 ; i < o.connected.length ; i++) {
\r
170 o.connected[i].update('drag', o);
\r
178 e = Drag.prototype.fixE(e);
\r
181 o.style.zIndex = o.oldzidx;
\r
183 if (o.mouseup_cb != null) {
\r
184 if (o.mouseup_cb(o) == 1) {
\r
185 o.onmousedown = null;
\r
189 document.onmousemove = null;
\r
190 document.onmouseup = null;
\r
191 Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
\r
192 parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
\r
193 for(var i = 0 ; i < o.connected.length ; i++) {
\r
194 o.connected[i].update('end', o);
\r
202 if (typeof e == 'undefined') e = window.event;
\r
203 if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
\r
204 if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
\r