Drag class renamed to xynt_dd and inherit from xynt_connect class
[xynt.git] / xynt-dd.js
1 /*\r
2  *  brisk - dom-drag.js\r
3  *\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
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 function xynt_dd(mouseup_cb, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)\r
34 {\r
35     xynt_connect.call(this);\r
36 \r
37     this.onmousedown   = xynt_dd.prototype.start;\r
38     this.mouseup_cb    = mouseup_cb;\r
39 \r
40     this.hmode = bSwapHorzRef ? false : true ;\r
41     this.vmode = bSwapVertRef ? false : true ;\r
42     \r
43     this.root = (this.xynt_dom_el && this.xynt_dom_el != null) ? this.xynt_dom_el : this ;\r
44     \r
45     if (this.hmode && isNaN(parseInt(this.root.style.left))) {\r
46         var res = parseInt(getStyle(o, "left", "left"));\r
47         if (isNaN(res)) {\r
48             this.root.style.left   = "0px";\r
49         }\r
50         else {\r
51             this.root.style.left   = res;\r
52         }\r
53     }\r
54     if (this.vmode  && isNaN(parseInt(this.root.style.top   ))) {\r
55         var res = parseInt(getStyle(o, "top", "top"));\r
56         if (isNaN(res)) {\r
57             this.root.style.top   = "0px";\r
58         }\r
59         else {\r
60             this.root.style.top   = res;\r
61         }\r
62     }\r
63     if (!this.hmode && isNaN(parseInt(this.root.style.right ))) this.root.style.right  = "0px";\r
64     if (!this.vmode && isNaN(parseInt(this.root.style.bottom))) this.root.style.bottom = "0px";\r
65     \r
66     this.minX   = typeof minX != 'undefined' ? minX : null;\r
67     this.minY   = typeof minY != 'undefined' ? minY : null;\r
68     this.maxX   = typeof maxX != 'undefined' ? maxX : null;\r
69     this.maxY   = typeof maxY != 'undefined' ? maxY : null;\r
70     \r
71     this.xMapper = fXMapper ? fXMapper : null;\r
72     this.yMapper = fYMapper ? fYMapper : null;\r
73     \r
74     this.root.onDragStart = new Function();\r
75     this.root.onDragEnd = new Function();\r
76     this.root.onDrag = new Function();\r
77 }\r
78 \r
79 xynt_dd.prototype = {\r
80 \r
81     obj : null,\r
82 \r
83     start : function(e)\r
84     {\r
85         var o = xynt_dd.obj = this;\r
86         e = xynt_dd.prototype.fixE(e);\r
87 \r
88         if (!e) \r
89             var e = window.event\r
90         // handle event\r
91         e.cancelBubble = true;\r
92         if (e.stopPropagation) \r
93             e.stopPropagation();\r
94 \r
95         o.oldzidx = o.style.zIndex;\r
96         o.style.zIndex = 10;\r
97 \r
98         var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
99         var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
100         o.root.onDragStart(x, y);\r
101 \r
102         o.lastMouseX    = e.clientX;\r
103         o.lastMouseY    = e.clientY;\r
104 \r
105         if (o.hmode) {\r
106             if (o.minX != null) o.minMouseX     = e.clientX - x + o.minX;\r
107             if (o.maxX != null) o.maxMouseX     = o.minMouseX + o.maxX - o.minX;\r
108         } else {\r
109             if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;\r
110             if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;\r
111         }\r
112 \r
113         if (o.vmode) {\r
114             if (o.minY != null) o.minMouseY     = e.clientY - y + o.minY;\r
115             if (o.maxY != null) o.maxMouseY     = o.minMouseY + o.maxY - o.minY;\r
116         } else {\r
117             if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;\r
118             if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;\r
119         }\r
120 \r
121         // console.log("assign");\r
122         document.onmousemove    = xynt_dd.prototype.drag;\r
123         document.onmouseup      = xynt_dd.prototype.end;\r
124 \r
125         for(var i = 0 ; i < o.xynt_conn_ected.length ; i++) {\r
126             o.xynt_conn_ected[i].xynt_conn_update('start', o);\r
127         }\r
128 \r
129         return false;\r
130     },\r
131 \r
132     drag : function(e)\r
133     {\r
134         e = xynt_dd.prototype.fixE(e);\r
135         var o = xynt_dd.obj;\r
136         \r
137         var ey  = e.clientY;\r
138         var ex  = e.clientX;\r
139         var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
140         var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
141         var nx, ny;\r
142 \r
143         if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);\r
144         if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);\r
145         if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);\r
146         if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);\r
147         \r
148         nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));\r
149         ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));\r
150 \r
151         if (o.xMapper)          nx = o.xMapper(y)\r
152         else if (o.yMapper)     ny = o.yMapper(x)\r
153 \r
154         xynt_dd.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";\r
155         xynt_dd.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";\r
156         xynt_dd.obj.lastMouseX  = ex;\r
157         xynt_dd.obj.lastMouseY  = ey;\r
158 \r
159         xynt_dd.obj.root.onDrag(nx, ny);\r
160         \r
161         console.log("pre call it"+o.xynt_conn_ected.length);\r
162         for(var i = 0 ; i < o.xynt_conn_ected.length ; i++) {\r
163             console.log("call it");\r
164             o.xynt_conn_ected[i].xynt_conn_update('drag', o);\r
165         }\r
166         \r
167         return false;\r
168     },\r
169 \r
170     end : function(e)\r
171     {\r
172         e = xynt_dd.prototype.fixE(e);\r
173         var o = xynt_dd.obj;\r
174 \r
175         o.style.zIndex = o.oldzidx;\r
176         if (o.mouseup_cb != null) {\r
177             if (o.mouseup_cb(o) == 1) {\r
178                 o.onmousedown = null;\r
179             }\r
180         }\r
181 \r
182         document.onmousemove = null;\r
183         document.onmouseup   = null;\r
184         xynt_dd.obj.root.onDragEnd(     parseInt(xynt_dd.obj.root.style[xynt_dd.obj.hmode ? "left" : "right"]), \r
185                                         parseInt(xynt_dd.obj.root.style[xynt_dd.obj.vmode ? "top" : "bottom"]));\r
186         for(var i = 0 ; i < o.xynt_conn_ected.length ; i++) {\r
187             o.xynt_conn_ected[i].xynt_conn_update('end', o);\r
188         }\r
189 \r
190         xynt_dd.obj = null;\r
191     },\r
192 \r
193     fixE : function(e)\r
194     {\r
195         if (typeof e == 'undefined') e = window.event;\r
196         if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;\r
197         if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;\r
198         return e;\r
199     }\r
200 };\r
201 \r
202 Extends(xynt_dd, xynt_connect);