moded from root dir
[brisk.git] / web / briskin5 / dom-drag.js
diff --git a/web/briskin5/dom-drag.js b/web/briskin5/dom-drag.js
new file mode 100644 (file)
index 0000000..4150f81
--- /dev/null
@@ -0,0 +1,182 @@
+/*\r
+ *  brisk - dom-drag.js\r
+ *\r
+ *  Copyright (C) 2006-2008 Matteo Nastasi\r
+ *                          mailto: nastasi@alternativeoutput.it \r
+ *                                  matteo.nastasi@milug.org\r
+ *                          web: http://www.alternativeoutput.it\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * General Public License for more details. You should have received a\r
+ * copy of the GNU General Public License along with this program; if\r
+ * not, write to the Free Software Foundation, Inc, 59 Temple Place -\r
+ * Suite 330, Boston, MA 02111-1307, USA.\r
+ *\r
+ * $Id$\r
+ *\r
+ */\r
+\r
+/**************************************************\r
+ * dom-drag.js\r
+ * 09.25.2001\r
+ * www.youngpup.net\r
+ **************************************************\r
+ * 10.28.2001 - fixed minor bug where events\r
+ * sometimes fired off the handle, not the root.\r
+ **************************************************/\r
+\r
+var Drag = {\r
+\r
+       obj : null,\r
+\r
+       init : function(o, mouseup_cb, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)\r
+       {\r
+               o.onmousedown   = Drag.start;\r
+               o.mouseup_cb    = mouseup_cb;\r
+\r
+               /*              alert("agnulla"+o.style.left); */\r
+\r
+               o.hmode                 = bSwapHorzRef ? false : true ;\r
+               o.vmode                 = bSwapVertRef ? false : true ;\r
+\r
+               o.root = oRoot && oRoot != null ? oRoot : o ;\r
+\r
+               if (o.hmode && isNaN(parseInt(o.root.style.left  ))) {\r
+                    var res = parseInt(getStyle(o, "left", "left"));\r
+                   if (isNaN(res)) {\r
+                       o.root.style.left   = "0px";\r
+                   }\r
+                   else {\r
+                       o.root.style.left   = res;\r
+                   }\r
+               }\r
+               if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) {\r
+                    var res = parseInt(getStyle(o, "top", "top"));\r
+                   if (isNaN(res)) {\r
+                       o.root.style.top   = "0px";\r
+                   }\r
+                   else {\r
+                       o.root.style.top   = res;\r
+                   }\r
+               }\r
+               if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";\r
+               if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";\r
+\r
+               o.minX  = typeof minX != 'undefined' ? minX : null;\r
+               o.minY  = typeof minY != 'undefined' ? minY : null;\r
+               o.maxX  = typeof maxX != 'undefined' ? maxX : null;\r
+               o.maxY  = typeof maxY != 'undefined' ? maxY : null;\r
+\r
+               o.xMapper = fXMapper ? fXMapper : null;\r
+               o.yMapper = fYMapper ? fYMapper : null;\r
+\r
+               o.root.onDragStart      = new Function();\r
+               o.root.onDragEnd        = new Function();\r
+               o.root.onDrag           = new Function();\r
+       },\r
+\r
+       start : function(e)\r
+       {\r
+               var o = Drag.obj = this;\r
+               e = Drag.fixE(e);\r
+\r
+               o.oldzidx = o.style.zIndex;\r
+               o.style.zIndex = 10;\r
+\r
+               // alert("start");\r
+\r
+               var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
+               var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
+               o.root.onDragStart(x, y);\r
+\r
+               o.lastMouseX    = e.clientX;\r
+               o.lastMouseY    = e.clientY;\r
+\r
+               if (o.hmode) {\r
+                       if (o.minX != null)     o.minMouseX     = e.clientX - x + o.minX;\r
+                       if (o.maxX != null)     o.maxMouseX     = o.minMouseX + o.maxX - o.minX;\r
+               } else {\r
+                       if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;\r
+                       if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;\r
+               }\r
+\r
+               if (o.vmode) {\r
+                       if (o.minY != null)     o.minMouseY     = e.clientY - y + o.minY;\r
+                       if (o.maxY != null)     o.maxMouseY     = o.minMouseY + o.maxY - o.minY;\r
+               } else {\r
+                       if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;\r
+                       if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;\r
+               }\r
+\r
+               document.onmousemove    = Drag.drag;\r
+               document.onmouseup      = Drag.end;\r
+\r
+               return false;\r
+       },\r
+\r
+       drag : function(e)\r
+       {\r
+               e = Drag.fixE(e);\r
+               var o = Drag.obj;\r
+\r
+               var ey  = e.clientY;\r
+               var ex  = e.clientX;\r
+               var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);\r
+               var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );\r
+               var nx, ny;\r
+\r
+               if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);\r
+               if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);\r
+               if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);\r
+               if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);\r
+\r
+               nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));\r
+               ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));\r
+\r
+               if (o.xMapper)          nx = o.xMapper(y)\r
+               else if (o.yMapper)     ny = o.yMapper(x)\r
+\r
+               Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";\r
+               Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";\r
+               Drag.obj.lastMouseX     = ex;\r
+               Drag.obj.lastMouseY     = ey;\r
+\r
+               Drag.obj.root.onDrag(nx, ny);\r
+               return false;\r
+       },\r
+\r
+       end : function(e)\r
+       {\r
+               e = Drag.fixE(e);\r
+               var o = Drag.obj;\r
+\r
+               o.style.zIndex = o.oldzidx;\r
+               // alert("END");\r
+               if (o.mouseup_cb != null) {\r
+                   if (o.mouseup_cb(o) == 1) {\r
+                       o.onmousedown = null;\r
+                   }\r
+               }\r
+\r
+               document.onmousemove = null;\r
+               document.onmouseup   = null;\r
+               Drag.obj.root.onDragEnd(        parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), \r
+                                                                       parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));\r
+               Drag.obj = null;\r
+       },\r
+\r
+       fixE : function(e)\r
+       {\r
+               if (typeof e == 'undefined') e = window.event;\r
+               if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;\r
+               if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;\r
+               return e;\r
+       }\r
+};\r