-
Notifications
You must be signed in to change notification settings - Fork 1
/
draggable-overlap.js
65 lines (62 loc) · 2.6 KB
/
draggable-overlap.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
$.ui.plugin.add("draggable", "overlap", {
start: function (event, ui)
{
var i = $(this).data("draggable"), o = i.options;
i.overlapTargets = [];
if (typeof (o.overlap.items) === "function")
{
i.overlapTargets = o.overlap.items.apply(this);
}
else if ($.isArray(o.overlap.items))
{
i.overlapTargets = o.overlap.items;
}
else
{
//assume jQuery object
o.overlap.items.each(function () {
var $t = $(this);
var offset = $t.offset();
i.overlapTargets.push({ top: offset.top, right: $t.width() + offset.left, bottom: $t.height() + offset.top, left: offset.left, element: $t });
});
}
//assume that the dragged object isn't going to change it's shape while we are dragging it and cache the size
i.dragElementSize = { width: $(this).outerWidth(), height: $(this).outerHeight() };
},
drag: function (event, ui)
{
//check for overlaps here
var data = $(this).data("draggable");
var currentItem = { top: ui.offset.top, right: data.dragElementSize.width + ui.offset.left, bottom: data.dragElementSize.height + ui.offset.top, left: ui.offset.left };
var overlapItems = [];
for (var i = 0, l = data.overlapTargets.length; i < l; i++)
{
var checkItem = data.overlapTargets[i];
overlapping = currentItem.left < checkItem.right && currentItem.right > checkItem.left && currentItem.top < checkItem.bottom && currentItem.bottom > checkItem.top;
if (overlapping)
{
var overlapRect = {
top: Math.max(currentItem.top, checkItem.top),
left: Math.max(currentItem.left, checkItem.left),
width: Math.min(currentItem.right, checkItem.right) - Math.max(currentItem.left, checkItem.left),
height: Math.min(currentItem.bottom, checkItem.bottom) - Math.max(currentItem.top, checkItem.top)
};
var result = $.extend({}, data.overlapTargets[i]);
result.overlapRect = overlapRect;
overlapItems.push(result);
if (data.options.overlap.fast)
{
break;
}
}
}
if (overlapItems.length > 0)
{
data.options.overlap.overlap.call(this, overlapItems, event.target);
}
else
{
data.options.overlap.overlap.call(this, new Array(), event.target);
}
}
});