base-test.js added
[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 Drag(mouseup_cb, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)\r
34 {\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
39     {\r
40         this.parentNode.connected[this.parentNode.connected.length] = conn;\r
41     }\r
42     \r
43     this.connected     = new Array();\r
44     /*                  alert("agnulla"+this.style.left); */\r
45     \r
46     this.hmode                  = bSwapHorzRef ? false : true ;\r
47     this.vmode                  = bSwapVertRef ? false : true ;\r
48     \r
49     this.root = this.xynt_dom_el && this.xynt_dom_el != null ? this.xynt_dom_el : this ;\r
50     \r
51     if (this.hmode && isNaN(parseInt(this.root.style.left  ))) {\r
52         var res = parseInt(getStyle(o, "left", "left"));\r
53                     if (isNaN(res)) {\r
54                         this.root.style.left   = "0px";\r
55                     }\r
56         else {\r
57             this.root.style.left   = res;\r
58         }\r
59     }\r
60     if (this.vmode  && isNaN(parseInt(this.root.style.top   ))) {\r
61         var res = parseInt(getStyle(o, "top", "top"));\r
62         if (isNaN(res)) {\r
63             this.root.style.top   = "0px";\r
64         }\r
65         else {\r
66             this.root.style.top   = res;\r
67         }\r
68     }\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
71     \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
76     \r
77     this.xMapper = fXMapper ? fXMapper : null;\r
78     this.yMapper = fYMapper ? fYMapper : null;\r
79     \r
80     this.root.onDragStart       = new Function();\r
81     this.root.onDragEnd = new Function();\r
82     this.root.onDrag            = new Function();\r
83 }\r
84 \r
85 Drag.prototype = {\r
86 \r
87         obj : null,\r
88 \r
89         start : function(e)\r
90         {\r
91             console.log("Drag.start");\r
92                 var o = Drag.obj = this;\r
93                 e = Drag.prototype.fixE(e);\r
94 \r
95                if (!e) \r
96                    var e = window.event\r
97                // handle event\r
98                e.cancelBubble = true;\r
99                if (e.stopPropagation) \r
100                    e.stopPropagation();\r
101 \r
102                 o.oldzidx = o.style.zIndex;\r
103                 o.style.zIndex = 10;\r
104 \r
105                 // alert("start");\r
106 \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
110 \r
111                 o.lastMouseX    = e.clientX;\r
112                 o.lastMouseY    = e.clientY;\r
113 \r
114                 if (o.hmode) {\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
117                 } else {\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
120                 }\r
121 \r
122                 if (o.vmode) {\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
125                 } else {\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
128                 }\r
129 \r
130                 document.onmousemove    = Drag.prototype.drag;\r
131                 document.onmouseup      = Drag.prototype.end;\r
132 \r
133             for(var i = 0 ; i < o.connected.length ; i++) {\r
134                 o.connected[i].update('start', o);\r
135             }\r
136 \r
137                 return false;\r
138         },\r
139 \r
140         drag : function(e)\r
141         {\r
142                 e = Drag.prototype.fixE(e);\r
143                 var o = Drag.obj;\r
144 \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
149                 var nx, ny;\r
150 \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
155 \r
156                 nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));\r
157                 ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));\r
158 \r
159                 if (o.xMapper)          nx = o.xMapper(y)\r
160                 else if (o.yMapper)     ny = o.yMapper(x)\r
161 \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
166 \r
167                 Drag.obj.root.onDrag(nx, ny);\r
168 \r
169             for(var i = 0 ; i < o.connected.length ; i++) {\r
170                 o.connected[i].update('drag', o);\r
171             }\r
172 \r
173                 return false;\r
174         },\r
175 \r
176         end : function(e)\r
177         {\r
178                 e = Drag.prototype.fixE(e);\r
179                 var o = Drag.obj;\r
180 \r
181                 o.style.zIndex = o.oldzidx;\r
182                 // alert("END");\r
183                 if (o.mouseup_cb != null) {\r
184                     if (o.mouseup_cb(o) == 1) {\r
185                         o.onmousedown = null;\r
186                     }\r
187                 }\r
188 \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
195             }\r
196 \r
197                 Drag.obj = null;\r
198         },\r
199 \r
200         fixE : function(e)\r
201         {\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
205                 return e;\r
206         }\r
207 };\r