scripts/dom-drag.js
changeset 10 d3059e20b0fa
equal deleted inserted replaced
9:63a257541313 10:d3059e20b0fa
       
     1 /**************************************************
       
     2  * dom-drag.js
       
     3  * 09.25.2001
       
     4  * www.youngpup.net
       
     5  **************************************************
       
     6  * 10.28.2001 - fixed minor bug where events
       
     7  * sometimes fired off the handle, not the root.
       
     8  **************************************************/
       
     9 
       
    10 var Drag = {
       
    11 
       
    12 	obj : null,
       
    13 
       
    14 	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
       
    15 	{
       
    16 		o.onmousedown	= Drag.start;
       
    17 
       
    18 		o.hmode			= bSwapHorzRef ? false : true ;
       
    19 		o.vmode			= bSwapVertRef ? false : true ;
       
    20 
       
    21 		o.root = oRoot && oRoot != null ? oRoot : o ;
       
    22 
       
    23 		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
       
    24 		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
       
    25 		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
       
    26 		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
       
    27 
       
    28 		o.minX	= typeof minX != 'undefined' ? minX : null;
       
    29 		o.minY	= typeof minY != 'undefined' ? minY : null;
       
    30 		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
       
    31 		o.maxY	= typeof maxY != 'undefined' ? maxY : null;
       
    32 
       
    33 		o.xMapper = fXMapper ? fXMapper : null;
       
    34 		o.yMapper = fYMapper ? fYMapper : null;
       
    35 
       
    36 		o.root.onDragStart	= new Function();
       
    37 		o.root.onDragEnd	= new Function();
       
    38 		o.root.onDrag		= new Function();
       
    39 	},
       
    40 
       
    41 	start : function(e)
       
    42 	{
       
    43 		var o = Drag.obj = this;
       
    44 		e = Drag.fixE(e);
       
    45 		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
       
    46 		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
       
    47 		o.root.onDragStart(x, y);
       
    48 
       
    49 		o.lastMouseX	= e.clientX;
       
    50 		o.lastMouseY	= e.clientY;
       
    51 
       
    52 		if (o.hmode) {
       
    53 			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
       
    54 			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
       
    55 		} else {
       
    56 			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
       
    57 			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
       
    58 		}
       
    59 
       
    60 		if (o.vmode) {
       
    61 			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
       
    62 			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
       
    63 		} else {
       
    64 			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
       
    65 			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
       
    66 		}
       
    67 
       
    68 		document.onmousemove	= Drag.drag;
       
    69 		document.onmouseup		= Drag.end;
       
    70 
       
    71 		return false;
       
    72 	},
       
    73 
       
    74 	drag : function(e)
       
    75 	{
       
    76 		e = Drag.fixE(e);
       
    77 		var o = Drag.obj;
       
    78 
       
    79 		var ey	= e.clientY;
       
    80 		var ex	= e.clientX;
       
    81 		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
       
    82 		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
       
    83 		var nx, ny;
       
    84 
       
    85 		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
       
    86 		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
       
    87 		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
       
    88 		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
       
    89 
       
    90 		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
       
    91 		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
       
    92 
       
    93 		if (o.xMapper)		nx = o.xMapper(y)
       
    94 		else if (o.yMapper)	ny = o.yMapper(x)
       
    95 
       
    96 		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
       
    97 		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
       
    98 		Drag.obj.lastMouseX	= ex;
       
    99 		Drag.obj.lastMouseY	= ey;
       
   100 
       
   101 		Drag.obj.root.onDrag(nx, ny);
       
   102 		return false;
       
   103 	},
       
   104 
       
   105 	end : function()
       
   106 	{
       
   107 		document.onmousemove = null;
       
   108 		document.onmouseup   = null;
       
   109 		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
       
   110 									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
       
   111 		Drag.obj = null;
       
   112 	},
       
   113 
       
   114 	fixE : function(e)
       
   115 	{
       
   116 		if (typeof e == 'undefined') e = window.event;
       
   117 		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
       
   118 		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
       
   119 		return e;
       
   120 	}
       
   121 };