Project

General

Profile

Download (6.61 KB) Statistics
| Branch: | Tag: | Revision:
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
 * 05.30.2005 - added a workaround for firefox
10
 * activating links when finished dragging.
11
 * mmosier@astrolabe.com
12
 **************************************************/
13
<?php
14
/*
15
    The DragList drag and drop ordered lists implementation is available under the terms of the new BSD license.
16
   
17
   Copyright (c) 2005, DTLink, LLC
18
   All rights reserved.
19
                                                                                  
20
   Redistribution  and  use  in  source  and binary forms, with or
21
   without modification, are permitted provided that the following
22
   conditions are met:
23
                                                                                  
24
   *   Redistributions  of  source  code  must  retain  the  above
25
   copyright  notice,  this  list  of conditions and the following
26
   disclaimer.
27
                                                                                  
28
   *  Redistributions  in  binary  form  must  reproduce the above
29
   copyright  notice,  this  list  of conditions and the following
30
   disclaimer in the documentation and/or other materials provided
31
   with the distribution.
32
    
33
   *  Neither  the  name  of  DTLink,  LLC  nor  the  names of its
34
   contributors may be used to endorse or promote products derived
35
   from this software without specific prior written permission.
36
    
37
   THIS   SOFTWARE  IS  PROVIDED  BY  THE  COPYRIGHT  HOLDERS  AND
38
   CONTRIBUTORS  "AS  IS"  AND  ANY EXPRESS OR IMPLIED WARRANTIES,
39
   INCLUDING,  BUT  NOT  LIMITED  TO,  THE  IMPLIED  WARRANTIES OF
40
   MERCHANTABILITY  AND  FITNESS  FOR  A  PARTICULAR  PURPOSE  ARE
41
   DISCLAIMED.   IN   NO   EVENT  SHALL  THE  COPYRIGHT  OWNER  OR
42
   CONTRIBUTORS  BE  LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43
   SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44
   NOT  LIMITED  TO,  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45
   LOSS  OF  USE,  DATA,  OR  PROFITS;  OR  BUSINESS INTERRUPTION)
46
   HOWEVER  CAUSED  AND  ON  ANY  THEORY  OF LIABILITY, WHETHER IN
47
   CONTRACT,  STRICT  LIABILITY,  OR TORT (INCLUDING NEGLIGENCE OR
48
   OTHERWISE)  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49
   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
*/
51
?>
52

    
53
var Drag = {
54

    
55
	obj : null,
56

    
57
	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
58
	{
59
		o.onmousedown	= Drag.start;
60

    
61
		o.hmode			= bSwapHorzRef ? false : true ;
62
		o.vmode			= bSwapVertRef ? false : true ;
63

    
64
		o.root = oRoot && oRoot != null ? oRoot : o ;
65

    
66
		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
67
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
68
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
69
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
70

    
71
		o.minX	= typeof minX != 'undefined' ? minX : null;
72
		o.minY	= typeof minY != 'undefined' ? minY : null;
73
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
74
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;
75

    
76
		o.xMapper = fXMapper ? fXMapper : null;
77
		o.yMapper = fYMapper ? fYMapper : null;
78

    
79
		o.root.onDragStart	= new Function();
80
		o.root.onDragEnd	= new Function();
81
		o.root.onDrag		= new Function();
82
	},
83

    
84
	start : function(e)
85
	{
86
		var o = Drag.obj = this;
87
		e = Drag.fixE(e);
88
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
89
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
90
		o.root.onDragStart(x, y);
91

    
92
		o.startX		= x;
93
		o.startY		= y;
94
		o.lastMouseX	= e.clientX;
95
		o.lastMouseY	= e.clientY;
96

    
97
		if (o.hmode) {
98
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
99
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
100
		} else {
101
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
102
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
103
		}
104

    
105
		if (o.vmode) {
106
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
107
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
108
		} else {
109
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
110
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
111
		}
112

    
113
		document.onmousemove	= Drag.drag;
114
		document.onmouseup		= Drag.end;
115

    
116
		if (o.linkDisabled) {
117
			var hrefs = o.root.getElementsByTagName("a");
118
			for (var i = 0; i < hrefs.length; i++) {
119
				hrefs[i].onclick = hrefs[i].prevOnclick;
120
				hrefs[i].prevOnclick = null;
121
			}
122
			o.linkDisabled = false;
123
		}
124

    
125
		return false;
126
	},
127

    
128
	drag : function(e)
129
	{
130
		e = Drag.fixE(e);
131
		var o = Drag.obj;
132

    
133
		var ey	= e.clientY;
134
		var ex	= e.clientX;
135
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
136
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
137
		var nx, ny;
138

    
139
		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
140
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
141
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
142
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
143

    
144
		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
145
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
146

    
147
		if (o.xMapper)		nx = o.xMapper(y)
148
		else if (o.yMapper)	ny = o.yMapper(x)
149

    
150
		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
151
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
152
		Drag.obj.lastMouseX	= ex;
153
		Drag.obj.lastMouseY	= ey;
154

    
155
		var threshold = 4;
156
		if (!o.linkDisabled) {
157
			if (Math.abs(nx - o.startX) > threshold || Math.abs(ny - o.startY) > threshold) {
158
				var hrefs = o.root.getElementsByTagName("a");
159
				for (var i = 0; i < hrefs.length; i++) {
160
					hrefs[i].prevOnclick = hrefs[i].onclick;
161
					hrefs[i].onclick = function() { return false; };
162
				}
163
				o.linkDisabled = true;
164
			}
165
		}
166

    
167
		Drag.obj.root.onDrag(nx, ny, Drag.obj.root);
168
		return false;
169
	},
170

    
171
	end : function()
172
	{
173
		document.onmousemove = null;
174
		document.onmouseup   = null;
175
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
176
									parseInt(Drag.obj.root.style[Drag.obj.vmode
177
                                    ? "top" : "bottom"]), Drag.obj.root);
178
		Drag.obj = null;
179
	},
180

    
181
	fixE : function(e)
182
	{
183
		if (typeof e == 'undefined') e = window.event;
184
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
185
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
186
		return e;
187
	}
188
};
(24-24/146)