Draggable: modified snapping algorithm to use edges and corners. Fixed #8165 - Draggable: Snapping doesn't take top/left into account properly(cherry picked from commit bd126a9c1c)

This commit is contained in:
Zbigniew Motyka 2012-10-29 09:55:54 +01:00 committed by Scott González
parent 149e6eb0bc
commit f74a908056
2 changed files with 38 additions and 2 deletions

View File

@ -1257,6 +1257,43 @@ test( "snap, snapMode, and snapTolerance", function() {
deepEqual( element.offset(), { top: newY, left: newX }, "doesn't snap on the inner snapTolerance area when snapMode is outer" ); deepEqual( element.offset(), { top: newY, left: newX }, "doesn't snap on the inner snapTolerance area when snapMode is outer" );
}); });
test( "#8165: Snapping large rectangles to small rectangles doesn't snap properly", function() {
expect( 1 );
var snapTolerance = 20,
y = 1,
element = $( "#draggable1" )
.css({
width: "50px",
height: "200px"
}).offset({
top: y,
left: 1
}),
element2 = $( "#draggable2" )
.css({
width: "50px",
height: "50px"
}).offset({
top: y + snapTolerance + 1,
left: 200
}),
newX = element2.offset().left - element.outerWidth() - snapTolerance + 1;
$( "#draggable1, #draggable2" ).draggable({
snap: true,
snapTolerance: snapTolerance
});
element.simulate( "drag", {
handle: "corner",
x: newX,
moves: 1
});
notDeepEqual( element.offset(), { top: y, left: newX }, "snaps even if only a side (not a corner) is inside the snapTolerance" );
});
test( "stack", function() { test( "stack", function() {
expect( 2 ); expect( 2 );

View File

@ -850,8 +850,7 @@ $.ui.plugin.add("draggable", "snap", {
t = inst.snapElements[i].top; t = inst.snapElements[i].top;
b = t + inst.snapElements[i].height; b = t + inst.snapElements[i].height;
//Yes, I know, this is insane ;) if(x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d) {
if(!((l-d < x1 && x1 < r+d && t-d < y1 && y1 < b+d) || (l-d < x1 && x1 < r+d && t-d < y2 && y2 < b+d) || (l-d < x2 && x2 < r+d && t-d < y1 && y1 < b+d) || (l-d < x2 && x2 < r+d && t-d < y2 && y2 < b+d))) {
if(inst.snapElements[i].snapping) { if(inst.snapElements[i].snapping) {
(inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item }))); (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
} }