forked from mikeplate/jquery-drag-drop-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.drag-drop.plugin.js
238 lines (210 loc) · 8.96 KB
/
jquery.drag-drop.plugin.js
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
(function($) {
var defaultOptions = {
makeClone: false, // Drag a clone of the source, and not the actual source element
sourceClass: null, // Class to apply to source element when dragging a clone of the source element
sourceHide: false, // Specify with true that the source element should hade visibility:hidden while dragging a clone
dragClass: null, // Class to apply to the element that is dragged
canDropClass: null, // Class to apply to the dragged element when dropping is possible
dropClass: null,
isActive: true,
container: null, // if set, dragging is limited to this container
// Default is to allow all elements to be dragged
canDrag: function($src, event) {
return $src;
},
// Default is to allow dropping inside elements with css stylesheet "drop"
canDrop: function($dst) {
return $dst.hasClass("drop") || $dst.parents(".drop").size()>0;
},
// Default is to move the element in the DOM and insert it into the element where it is dropped
didDrop: function($src, $dst) {
$src.appendTo($dst);
}
};
// Status during a drag-and-drop operation. Only one such operation can be in progress at any given time.
var $sourceElement = null; // Element that user wanted to drag
var $activeElement = null; // Element that is shown moving around during drag operation
var $destElement = null; // Element currently highlighted as possible drop destination
var dragOffsetX, dragOffsetY; // Position difference from drag-point to active elements left top corner
var limits;
// Private helper methods
function cancelDestElement(options) {
if ($destElement!=null) {
if (options.dropClass)
$destElement.removeClass(options.dropClass);
$destElement = null;
}
if ($activeElement!=null) {
if (options.canDropClass) {
$activeElement.removeClass(options.canDropClass);
}
}
}
// Public methods
var methods = {
init: function(options) {
options = $.extend({}, defaultOptions, options);
this.data("options", options);
this.bind("mousedown.dragdrop touchstart.dragdrop", methods.onStart);
return this;
},
destroy: function() {
this.unbind("mousedown.dragdrop touchstart.dragdrop");
return this;
},
on: function() {
this.data("options").isActive = true;
},
off: function() {
this.data("options").isActive = false;
},
onStart: function(event) {
var $me = $(this);
var options = $me.data("options");
if (!options.isActive)
return;
var $element = options.canDrag($me, event);
if ($element) {
$sourceElement = $element;
var offset = $sourceElement.offset();
var width = $sourceElement.width();
var height = $sourceElement.height();
if (event.type=="touchstart") {
dragOffsetX = event.originalEvent.touches[0].clientX - offset.left;
dragOffsetY = event.originalEvent.touches[0].clientY - offset.top;
}
else {
dragOffsetX = event.pageX - offset.left;
dragOffsetY = event.pageY - offset.top;
}
if (options.makeClone) {
$activeElement = $sourceElement.clone(false);
// Elements that are cloned and dragged around are added to the parent in order
// to get any cascading styles applied.
$activeElement.appendTo($element.parent());
if (options.sourceClass)
$sourceElement.addClass(options.sourceClass);
else if (options.sourceHide)
$sourceElement.css("visibility", "hidden");
}
else {
$activeElement = $sourceElement;
}
$activeElement.css({
position: "absolute",
left: offset.left,
top: offset.top,
width: width,
height: height
});
if (options.dragClass)
$activeElement.addClass(options.dragClass);
var $c = options.container;
if ($c) {
var offset = $c.offset();
limits = {
minX: offset.left,
minY: offset.top,
maxX: offset.left + $c.outerWidth() - $element.outerWidth(),
maxY: offset.top + $c.outerHeight() - $element.outerHeight()
};
}
$(window)
.bind("mousemove.dragdrop touchmove.dragdrop", { source: $me }, methods.onMove)
.bind("mouseup.dragdrop touchend.dragdrop", { source: $me }, methods.onEnd);
event.stopPropagation();
return false;
}
},
onMove: function(event) {
if (!$activeElement)
return;
var $me = event.data.source;
var options = $me.data("options");
var posX, posY;
if (event.type=="touchmove") {
posX = event.originalEvent.touches[0].clientX;
posY = event.originalEvent.touches[0].clientY;
}
else {
posX = event.pageX;
posY = event.pageY;
}
$activeElement.css("display", "none");
var destElement = document.elementFromPoint(
posX - document.documentElement.scrollLeft - document.body.scrollLeft,
posY - document.documentElement.scrollTop - document.body.scrollTop
);
$activeElement.css("display", "");
posX -= dragOffsetX;
posY -= dragOffsetY;
if (limits) {
posX = Math.min(Math.max(posX, limits.minX), limits.maxX);
posY = Math.min(Math.max(posY, limits.minY), limits.maxY);
}
$activeElement.css({ left: posX, top: posY });
if (destElement) {
if ($destElement==null || $destElement.get(0)!=destElement) {
var $possibleDestElement = $(destElement);
if (options.canDrop($possibleDestElement)) {
if (options.dropClass) {
if ($destElement!=null)
$destElement.removeClass(options.dropClass);
$possibleDestElement.addClass(options.dropClass);
}
if (options.canDropClass) {
$activeElement.addClass(options.canDropClass);
}
$destElement = $possibleDestElement;
}
else if ($destElement!=null) {
cancelDestElement(options);
}
}
}
else if ($destElement!=null) {
cancelDestElement(options);
}
event.stopPropagation();
return false;
},
onEnd: function(event) {
if (!$activeElement)
return;
var $me = event.data.source;
var options = $me.data("options");
if ($destElement) {
options.didDrop($sourceElement, $destElement);
}
cancelDestElement(options);
if (options.makeClone) {
$activeElement.remove();
if (options.sourceClass)
$sourceElement.removeClass(options.sourceClass);
else if (options.sourceHide)
$sourceElement.css("visibility", "visible");
}
else {
$activeElement.css("position", "static");
$activeElement.css("width", "");
$activeElement.css("height", "");
if (options.dragClass)
$activeElement.removeClass(options.dragClass);
}
$(window).unbind("mousemove.dragdrop touchmove.dragdrop");
$(window).unbind("mouseup.dragdrop touchend.dragdrop");
$sourceElement = $activeElement = limits = null;
}
};
$.fn.dragdrop = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
else {
$.error('Method '+method+' does not exist on jQuery.dragdrop');
}
};
})(jQuery);