dir tree refactored
[xynt.git] / web / xynt / xynt-dd.js
1 /*
2  *  brisk - dom-drag.js
3  *
4  *  Copyright (C) 2006-2011 Matteo Nastasi
5  *                          mailto: nastasi@alternativeoutput.it 
6  *                                  matteo.nastasi@milug.org
7  *                          web: http://www.alternativeoutput.it
8  *
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.
13  *
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.
21  *
22  */
23
24 /**************************************************
25  * dom-drag.js
26  * 09.25.2001
27  * www.youngpup.net
28  **************************************************
29  * 10.28.2001 - fixed minor bug where events
30  * sometimes fired off the handle, not the root.
31  **************************************************/
32
33 function xynt_dd(mouseup_cb, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
34 {
35     xynt_connect.call(this);
36
37     this.onmousedown   = xynt_dd.prototype.start;
38     this.mouseup_cb    = mouseup_cb;
39
40     this.hmode = bSwapHorzRef ? false : true ;
41     this.vmode = bSwapVertRef ? false : true ;
42     
43     this.root = (this.xynt_dom_el && this.xynt_dom_el != null) ? this.xynt_dom_el : this ;
44     
45     if (this.hmode && isNaN(parseInt(this.root.style.left))) {
46         var res = parseInt(getStyle(o, "left", "left"));
47         if (isNaN(res)) {
48             this.root.style.left   = "0px";
49         }
50         else {
51             this.root.style.left   = res;
52         }
53     }
54     if (this.vmode  && isNaN(parseInt(this.root.style.top   ))) {
55         var res = parseInt(getStyle(o, "top", "top"));
56         if (isNaN(res)) {
57             this.root.style.top   = "0px";
58         }
59         else {
60             this.root.style.top   = res;
61         }
62     }
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";
65     
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;
70     
71     this.xMapper = fXMapper ? fXMapper : null;
72     this.yMapper = fYMapper ? fYMapper : null;
73     
74     this.root.onDragStart = new Function();
75     this.root.onDragEnd = new Function();
76     this.root.onDrag = new Function();
77 }
78
79 xynt_dd.prototype = {
80
81     obj : null,
82
83     start : function(e)
84     {
85         var o = xynt_dd.obj = this;
86         e = xynt_dd.prototype.fixE(e);
87
88         if (!e) 
89             var e = window.event
90         // handle event
91         e.cancelBubble = true;
92         if (e.stopPropagation) 
93             e.stopPropagation();
94
95         o.oldzidx = o.style.zIndex;
96         o.style.zIndex = 10;
97
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);
101
102         o.lastMouseX    = e.clientX;
103         o.lastMouseY    = e.clientY;
104
105         if (o.hmode) {
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;
108         } else {
109             if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
110             if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
111         }
112
113         if (o.vmode) {
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;
116         } else {
117             if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
118             if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
119         }
120
121         // console.log("assign");
122         document.onmousemove    = xynt_dd.prototype.drag;
123         document.onmouseup      = xynt_dd.prototype.end;
124
125         for(var i = 0 ; i < o.xynt_conn_ected.length ; i++) {
126             o.xynt_conn_ected[i].xynt_conn_update('start', o);
127         }
128
129         return false;
130     },
131
132     drag : function(e)
133     {
134         e = xynt_dd.prototype.fixE(e);
135         var o = xynt_dd.obj;
136         
137         var ey  = e.clientY;
138         var ex  = e.clientX;
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 );
141         var nx, ny;
142
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);
147         
148         nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
149         ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
150
151         if (o.xMapper)          nx = o.xMapper(y)
152         else if (o.yMapper)     ny = o.yMapper(x)
153
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;
158
159         xynt_dd.obj.root.onDrag(nx, ny);
160         
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);
165         }
166         
167         return false;
168     },
169
170     end : function(e)
171     {
172         e = xynt_dd.prototype.fixE(e);
173         var o = xynt_dd.obj;
174
175         o.style.zIndex = o.oldzidx;
176         if (o.mouseup_cb != null) {
177             if (o.mouseup_cb(o) == 1) {
178                 o.onmousedown = null;
179             }
180         }
181
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);
188         }
189
190         xynt_dd.obj = null;
191     },
192
193     fixE : function(e)
194     {
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;
198         return e;
199     }
200 };
201
202 Extends(xynt_dd, xynt_connect);