4 * Copyright (C) 2006-2011 Matteo Nastasi
5 * mailto: nastasi@alternativeoutput.it
6 * matteo.nastasi@milug.org
7 * web: http://www.alternativeoutput.it
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details. You should have received a
18 * copy of the GNU General Public License along with this program; if
19 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
20 * Suite 330, Boston, MA 02111-1307, USA.
24 /**************************************************
28 **************************************************
29 * 10.28.2001 - fixed minor bug where events
30 * sometimes fired off the handle, not the root.
31 **************************************************/
33 function xynt_dd(mouseup_cb, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
35 xynt_connect.call(this);
37 this.onmousedown = xynt_dd.prototype.start;
38 this.mouseup_cb = mouseup_cb;
40 this.hmode = bSwapHorzRef ? false : true ;
41 this.vmode = bSwapVertRef ? false : true ;
43 this.root = (this.xynt_dom_el && this.xynt_dom_el != null) ? this.xynt_dom_el : this ;
45 if (this.hmode && isNaN(parseInt(this.root.style.left))) {
46 var res = parseInt(getStyle(o, "left", "left"));
48 this.root.style.left = "0px";
51 this.root.style.left = res;
54 if (this.vmode && isNaN(parseInt(this.root.style.top ))) {
55 var res = parseInt(getStyle(o, "top", "top"));
57 this.root.style.top = "0px";
60 this.root.style.top = res;
63 if (!this.hmode && isNaN(parseInt(this.root.style.right ))) this.root.style.right = "0px";
64 if (!this.vmode && isNaN(parseInt(this.root.style.bottom))) this.root.style.bottom = "0px";
66 this.minX = typeof minX != 'undefined' ? minX : null;
67 this.minY = typeof minY != 'undefined' ? minY : null;
68 this.maxX = typeof maxX != 'undefined' ? maxX : null;
69 this.maxY = typeof maxY != 'undefined' ? maxY : null;
71 this.xMapper = fXMapper ? fXMapper : null;
72 this.yMapper = fYMapper ? fYMapper : null;
74 this.root.onDragStart = new Function();
75 this.root.onDragEnd = new Function();
76 this.root.onDrag = new Function();
85 var o = xynt_dd.obj = this;
86 e = xynt_dd.prototype.fixE(e);
91 e.cancelBubble = true;
92 if (e.stopPropagation)
95 o.oldzidx = o.style.zIndex;
98 var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
99 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
100 o.root.onDragStart(x, y);
102 o.lastMouseX = e.clientX;
103 o.lastMouseY = e.clientY;
106 if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
107 if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
109 if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
110 if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
114 if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
115 if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
117 if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
118 if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
121 // console.log("assign");
122 document.onmousemove = xynt_dd.prototype.drag;
123 document.onmouseup = xynt_dd.prototype.end;
125 for(var i = 0 ; i < o.xynt_conn_ected.length ; i++) {
126 o.xynt_conn_ected[i].xynt_conn_update('start', o);
134 e = xynt_dd.prototype.fixE(e);
139 var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
140 var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
143 if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
144 if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
145 if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
146 if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
148 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
149 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
151 if (o.xMapper) nx = o.xMapper(y)
152 else if (o.yMapper) ny = o.yMapper(x)
154 xynt_dd.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
155 xynt_dd.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
156 xynt_dd.obj.lastMouseX = ex;
157 xynt_dd.obj.lastMouseY = ey;
159 xynt_dd.obj.root.onDrag(nx, ny);
161 console.log("pre call it"+o.xynt_conn_ected.length);
162 for(var i = 0 ; i < o.xynt_conn_ected.length ; i++) {
163 console.log("call it");
164 o.xynt_conn_ected[i].xynt_conn_update('drag', o);
172 e = xynt_dd.prototype.fixE(e);
175 o.style.zIndex = o.oldzidx;
176 if (o.mouseup_cb != null) {
177 if (o.mouseup_cb(o) == 1) {
178 o.onmousedown = null;
182 document.onmousemove = null;
183 document.onmouseup = null;
184 xynt_dd.obj.root.onDragEnd( parseInt(xynt_dd.obj.root.style[xynt_dd.obj.hmode ? "left" : "right"]),
185 parseInt(xynt_dd.obj.root.style[xynt_dd.obj.vmode ? "top" : "bottom"]));
186 for(var i = 0 ; i < o.xynt_conn_ected.length ; i++) {
187 o.xynt_conn_ected[i].xynt_conn_update('end', o);
195 if (typeof e == 'undefined') e = window.event;
196 if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
197 if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
202 Extends(xynt_dd, xynt_connect);