Skip to content

Commit bd126a9

Browse files
zbigniewmotykamikesherov
authored andcommitted
Draggable: modified snapping algorithm to use edges and corners. Fixed #8165 - Draggable: Snapping doesn't take top/left into account properly
1 parent ebd5f13 commit bd126a9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

tests/unit/draggable/draggable_options.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,43 @@ test( "snap, snapMode, and snapTolerance", function() {
12571257
deepEqual( element.offset(), { top: newY, left: newX }, "doesn't snap on the inner snapTolerance area when snapMode is outer" );
12581258
});
12591259

1260+
test( "#8165: Snapping large rectangles to small rectangles doesn't snap properly", function() {
1261+
expect( 1 );
1262+
1263+
var snapTolerance = 20,
1264+
y = 1,
1265+
element = $( "#draggable1" )
1266+
.css({
1267+
width: "50px",
1268+
height: "200px"
1269+
}).offset({
1270+
top: y,
1271+
left: 1
1272+
}),
1273+
element2 = $( "#draggable2" )
1274+
.css({
1275+
width: "50px",
1276+
height: "50px"
1277+
}).offset({
1278+
top: y + snapTolerance + 1,
1279+
left: 200
1280+
}),
1281+
newX = element2.offset().left - element.outerWidth() - snapTolerance + 1;
1282+
1283+
$( "#draggable1, #draggable2" ).draggable({
1284+
snap: true,
1285+
snapTolerance: snapTolerance
1286+
});
1287+
1288+
element.simulate( "drag", {
1289+
handle: "corner",
1290+
x: newX,
1291+
moves: 1
1292+
});
1293+
1294+
notDeepEqual( element.offset(), { top: y, left: newX }, "snaps even if only a side (not a corner) is inside the snapTolerance" );
1295+
});
1296+
12601297
test( "stack", function() {
12611298
expect( 2 );
12621299

ui/jquery.ui.draggable.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,7 @@ $.ui.plugin.add("draggable", "snap", {
850850
t = inst.snapElements[i].top;
851851
b = t + inst.snapElements[i].height;
852852

853-
//Yes, I know, this is insane ;)
854-
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))) {
853+
if(x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d) {
855854
if(inst.snapElements[i].snapping) {
856855
(inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
857856
}

0 commit comments

Comments
 (0)