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
|
/*
|
14
|
The DragList drag and drop ordered lists implementation is available under the terms of the new BSD license.
|
15
|
|
16
|
Copyright (c) 2005, DTLink, LLC
|
17
|
All rights reserved.
|
18
|
|
19
|
Redistribution and use in source and binary forms, with or
|
20
|
without modification, are permitted provided that the following
|
21
|
conditions are met:
|
22
|
|
23
|
* Redistributions of source code must retain the above
|
24
|
copyright notice, this list of conditions and the following
|
25
|
disclaimer.
|
26
|
|
27
|
* Redistributions in binary form must reproduce the above
|
28
|
copyright notice, this list of conditions and the following
|
29
|
disclaimer in the documentation and/or other materials provided
|
30
|
with the distribution.
|
31
|
|
32
|
* Neither the name of DTLink, LLC nor the names of its
|
33
|
contributors may be used to endorse or promote products derived
|
34
|
from this software without specific prior written permission.
|
35
|
|
36
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
37
|
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
38
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
39
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
40
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
41
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
42
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
43
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
44
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
45
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
46
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
47
|
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
48
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
49
|
*/
|
50
|
|
51
|
var Drag = {
|
52
|
|
53
|
obj : null,
|
54
|
|
55
|
init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
|
56
|
{
|
57
|
o.onmousedown = Drag.start;
|
58
|
|
59
|
o.hmode = bSwapHorzRef ? false : true ;
|
60
|
o.vmode = bSwapVertRef ? false : true ;
|
61
|
|
62
|
o.root = oRoot && oRoot != null ? oRoot : o ;
|
63
|
|
64
|
if (o.hmode && isNaN(parseInt(o.root.style.left ))) o.root.style.left = "0px";
|
65
|
if (o.vmode && isNaN(parseInt(o.root.style.top ))) o.root.style.top = "0px";
|
66
|
if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right = "0px";
|
67
|
if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
|
68
|
|
69
|
o.minX = typeof minX != 'undefined' ? minX : null;
|
70
|
o.minY = typeof minY != 'undefined' ? minY : null;
|
71
|
o.maxX = typeof maxX != 'undefined' ? maxX : null;
|
72
|
o.maxY = typeof maxY != 'undefined' ? maxY : null;
|
73
|
|
74
|
o.xMapper = fXMapper ? fXMapper : null;
|
75
|
o.yMapper = fYMapper ? fYMapper : null;
|
76
|
|
77
|
o.root.onDragStart = new Function();
|
78
|
o.root.onDragEnd = new Function();
|
79
|
o.root.onDrag = new Function();
|
80
|
},
|
81
|
|
82
|
start : function(e)
|
83
|
{
|
84
|
var o = Drag.obj = this;
|
85
|
e = Drag.fixE(e);
|
86
|
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
|
87
|
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
|
88
|
o.root.onDragStart(x, y);
|
89
|
|
90
|
o.startX = x;
|
91
|
o.startY = y;
|
92
|
o.lastMouseX = e.clientX;
|
93
|
o.lastMouseY = e.clientY;
|
94
|
|
95
|
if (o.hmode) {
|
96
|
if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
|
97
|
if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
|
98
|
} else {
|
99
|
if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
|
100
|
if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
|
101
|
}
|
102
|
|
103
|
if (o.vmode) {
|
104
|
if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
|
105
|
if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
|
106
|
} else {
|
107
|
if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
|
108
|
if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
|
109
|
}
|
110
|
|
111
|
document.onmousemove = Drag.drag;
|
112
|
document.onmouseup = Drag.end;
|
113
|
|
114
|
if (o.linkDisabled) {
|
115
|
var hrefs = o.root.getElementsByTagName("a");
|
116
|
for (var i = 0; i < hrefs.length; i++) {
|
117
|
hrefs[i].onclick = hrefs[i].prevOnclick;
|
118
|
hrefs[i].prevOnclick = null;
|
119
|
}
|
120
|
o.linkDisabled = false;
|
121
|
}
|
122
|
|
123
|
return false;
|
124
|
},
|
125
|
|
126
|
drag : function(e)
|
127
|
{
|
128
|
e = Drag.fixE(e);
|
129
|
var o = Drag.obj;
|
130
|
|
131
|
var ey = e.clientY;
|
132
|
var ex = e.clientX;
|
133
|
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
|
134
|
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
|
135
|
var nx, ny;
|
136
|
|
137
|
if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
|
138
|
if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
|
139
|
if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
|
140
|
if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
|
141
|
|
142
|
nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
|
143
|
ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
|
144
|
|
145
|
if (o.xMapper) nx = o.xMapper(y);
|
146
|
else if (o.yMapper) ny = o.yMapper(x);
|
147
|
|
148
|
Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
|
149
|
Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
|
150
|
Drag.obj.lastMouseX = ex;
|
151
|
Drag.obj.lastMouseY = ey;
|
152
|
|
153
|
var threshold = 4;
|
154
|
if (!o.linkDisabled) {
|
155
|
if (Math.abs(nx - o.startX) > threshold || Math.abs(ny - o.startY) > threshold) {
|
156
|
var hrefs = o.root.getElementsByTagName("a");
|
157
|
for (var i = 0; i < hrefs.length; i++) {
|
158
|
hrefs[i].prevOnclick = hrefs[i].onclick;
|
159
|
hrefs[i].onclick = function() { return false; };
|
160
|
}
|
161
|
o.linkDisabled = true;
|
162
|
}
|
163
|
}
|
164
|
|
165
|
Drag.obj.root.onDrag(nx, ny, Drag.obj.root);
|
166
|
return false;
|
167
|
},
|
168
|
|
169
|
end : function()
|
170
|
{
|
171
|
document.onmousemove = null;
|
172
|
document.onmouseup = null;
|
173
|
Drag.obj.root.onDragEnd(
|
174
|
parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
|
175
|
parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]),
|
176
|
Drag.obj.root);
|
177
|
Drag.obj = null;
|
178
|
},
|
179
|
|
180
|
fixE : function(e)
|
181
|
{
|
182
|
if (typeof e == 'undefined') e = window.event;
|
183
|
if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
|
184
|
if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
|
185
|
return e;
|
186
|
}
|
187
|
};
|