drag.js
6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
define(["jquery"], function () {
var _void = void 0;
function drag(o) {
var dragula = document.createElement("div");
var $dragula = $(dragula); // 便于使用jquery的事件处理机制
var _dragging = false;
var _moveX;
var _moveY;
var _item;
var _over; // 在拖动组件下方的容器
var _custom; // 自定义的元素
var _mirror; // 被拖动元素的镜像
var _grabbed; // 存放mousedown的context
if(o.moves === _void) { o.moves = always; }
if(o.custom === _void) { o.custom = never; }
if(o.mirrorContainer === _void) { o.mirrorContainer = $("body")[0]; }
o.containers = o.containers.map(function(container) {
return $(container)[0];
});
function always() { return true; }
function never() { return false; }
var events = {
"mousedown": function(e) {
_moveX = e.clientX;
_moveY = e.clientY;
var ignore = whichMouseButton(e) !== 1 || e.metaKey || e.ctrlKey;
if(ignore) return;
var item = e.target;
var context = canStart(item);
if(!context) return;
_grabbed = context;
_dragging = true;
nouserselect(true);
},
"mousemove": function(e) {
if(!_grabbed || !_dragging) return;
var point = {
x: e.clientX,
y: e.clientY
};
if(_moveX == point.x && _moveY == point.y) {
_dragging = false;
}
var grabbed = start(_grabbed);
renderMirror(grabbed);
dragmove(e);
var overContainer;
if(o.overContainer) {
overContainer = o.overContainer(_item, grabbed.source, e);
} else {
overContainer = isOverContainers(grabbed, e);
}
if(overContainer) {
$dragula.trigger("over", [_item, overContainer, point]);
_over = overContainer;
} else if(_over) {
$dragula.trigger("leave", [_item, _over, point]);
_over = null;
}
},
"mouseup": function(e) {
if(!_dragging) return;
var source = _over;
removeMirror();
cleanup();
nouserselect(false);
if(!source) return $dragula.trigger("end", [_item, source]);;
$dragula.trigger("dropping", [_item, source]);
if(o.animation) {
$dragula.trigger("animate", [_item, source, function() {
$(source).append(_item);
$dragula.trigger("dropped", [_item, source]);
}]);
} else {
$(source).append(_item);
$dragula.trigger("dropped", [_item, source]);
}
}
};
var $doc = $(document);
var namespace = ".drag", evtname;
for(evtname in events) {
$doc.off(evtname + namespace).on(evtname + namespace, events[evtname]);
}
function nouserselect(flag) {
$("body").toggleClass("no-userselect", flag);
}
function start(context) {
_custom = o.custom();
_item = context.item;
_dragging = true;
return {
item: _custom || context.item,
source: context.source
};
}
function cleanup() {
_dragging = false;
_grabbed = null;
_over = null;
}
function renderMirror(context) {
if(_mirror) return;
_mirror = context.item;
$(o.mirrorContainer).append(_mirror);
}
function removeMirror() {
if(_custom) {
$(o.mirrorContainer).find(_mirror).remove();
_mirror = null;
}
}
function dragmove(e) {
if(!_mirror) return;
e.preventDefault();
var $mirror = $(_mirror);
var clientX = e.clientX;
var clientY = e.clientY;
if(_custom) {
clientX -= $mirror.width() / 2;
clientY -= $mirror.height() / 2;
}
$(_mirror).css({
left: clientX,
top: clientY
});
}
function isOverContainers(context, e) {
var isOver = false, $container, offset;
o.containers.every(function(container) {
if(context.source == container) return true;
$container = $(container);
offset = $container.offset();
if( offset.left <= e.clientX &&
e.clientX <= $container.width() + offset.left &&
offset.top <= e.clientY &&
e.clientY <= $container.height() + offset.top
) {
isOver = true;
}
return false;
});
return isOver && $container[0];
}
function canStart(item) {
if(_dragging) return;
if(isContainer(item)) return;
var movable = o.moves(item);
if(!movable) return;
var context = getContext(item);
if(!context.source) return;
return {
item: context.item,
source: context.source
};
}
function isContainer(item) {
return ~o.containers.indexOf(item);
}
function getContext(item) {
var $item = $(item),
container = $item.parent()[0];
while(container && !~o.containers.indexOf(container)) {
$item = $item.parent();
container = $item.parent()[0];
container = container.tagName == 'BODY' ? null : container;
}
return {
item: $item[0],
source: container
};
}
function whichMouseButton(e) {
if (e.which !== _void && e.which !== 0) { return e.which; }
if (e.buttons !== _void) { return e.buttons; }
var button = e.button;
if (button !== _void) {
return button & 1 ? 1 : button & 2 ? 3 : (button & 4 ? 2 : 0);
}
}
return $dragula;
}
return drag;
});