Translated using Weblate.
[phpmyadmin.git] / js / dom-drag.js
blob51fef4bdcca552bcc84203c7c3655672fa9dd37f
1 /**************************************************
2  * dom-drag.js
3  * 09.25.2001
4  * www.youngpup.net
5  **************************************************
6  * Copyright 2001, Aaron Boodman
7  * This code is public domain. Please use it for good, not evil.
8  **************************************************
9  * 10.28.2001 - fixed minor bug where events
10  * sometimes fired off the handle, not the root.
11  **************************************************/
13 var Drag = {
15     obj : null,
17     init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
18     {
19         o.onmousedown    = Drag.start;
21         o.hmode            = bSwapHorzRef ? false : true ;
22         o.vmode            = bSwapVertRef ? false : true ;
24         o.root = oRoot && oRoot != null ? oRoot : o ;
26         if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
27         if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
28         if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
29         if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
31         o.minX    = typeof minX != 'undefined' ? minX : null;
32         o.minY    = typeof minY != 'undefined' ? minY : null;
33         o.maxX    = typeof maxX != 'undefined' ? maxX : null;
34         o.maxY    = typeof maxY != 'undefined' ? maxY : null;
36         o.xMapper = fXMapper ? fXMapper : null;
37         o.yMapper = fYMapper ? fYMapper : null;
39         o.root.onDragStart    = new Function();
40         o.root.onDragEnd    = new Function();
41         o.root.onDrag        = new Function();
42     },
44     start : function(e)
45     {
46         var o = Drag.obj = this;
47         e = Drag.fixE(e);
48         var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
49         var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
50         o.root.onDragStart(x, y);
52         o.lastMouseX    = e.clientX;
53         o.lastMouseY    = e.clientY;
55         if (o.hmode) {
56             if (o.minX != null)    o.minMouseX    = e.clientX - x + o.minX;
57             if (o.maxX != null)    o.maxMouseX    = o.minMouseX + o.maxX - o.minX;
58         } else {
59             if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
60             if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
61         }
63         if (o.vmode) {
64             if (o.minY != null)    o.minMouseY    = e.clientY - y + o.minY;
65             if (o.maxY != null)    o.maxMouseY    = o.minMouseY + o.maxY - o.minY;
66         } else {
67             if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
68             if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
69         }
71         document.onmousemove    = Drag.drag;
72         document.onmouseup        = Drag.end;
74         return false;
75     },
77     drag : function(e)
78     {
79         e = Drag.fixE(e);
80         var o = Drag.obj;
82         var ey    = e.clientY;
83         var ex    = e.clientX;
84         var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
85         var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
86         var nx, ny;
88         if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
89         if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
90         if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
91         if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
93         nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
94         ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
96         if (o.xMapper)        nx = o.xMapper(y)
97         else if (o.yMapper)    ny = o.yMapper(x)
99         Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
100         Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
101         Drag.obj.lastMouseX    = ex;
102         Drag.obj.lastMouseY    = ey;
104         Drag.obj.root.onDrag(nx, ny);
105         return false;
106     },
108     end : function()
109     {
110         document.onmousemove = null;
111         document.onmouseup   = null;
112         Drag.obj.root.onDragEnd(    parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
113                                     parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
114         Drag.obj = null;
115     },
117     fixE : function(e)
118     {
119         if (typeof e == 'undefined') e = window.event;
120         if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
121         if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
122         return e;
123     }