Skip to content

Commit 601ad96

Browse files
committed
Sortable: Adjust itemWithLeastDistance algorithm in _contactContainers to properly handle dragging items to the beginning and ends of lists. Fixes #9314 - Sortable: Items cannot be dragged directly into bottom position. Fixes #9381 - Sortable: Connected list placeholders have an inaccurate initial position
1 parent 61d16ec commit 601ad96

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

tests/unit/sortable/sortable.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
margin: 1px;
3939
border-width: 0;
4040
}
41-
#sortable li{
41+
#sortable li, #sortable2 li{
4242
padding: 0;
4343
margin: 0;
4444
border-width: 0;
@@ -58,7 +58,15 @@ <h2 id="qunit-userAgent"></h2>
5858
<ol id="qunit-tests"></ol>
5959
<div id="qunit-fixture">
6060

61-
<ul id="sortable">
61+
<ul id="sortable" class="connectWith">
62+
<li>Item 1</li>
63+
<li>Item 2</li>
64+
<li>Item 3</li>
65+
<li>Item 4</li>
66+
<li>Item 5</li>
67+
</ul>
68+
69+
<ul id="sortable2" class="connectWith">
6270
<li>Item 1</li>
6371
<li>Item 2</li>
6472
<li>Item 3</li>

tests/unit/sortable/sortable_core.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
/*
22
* sortable_core.js
3-
*/
3+
*/
4+
5+
(function( $ ) {
6+
7+
module( "sortable: core" );
8+
9+
test( "#9314: Sortable: Items cannot be dragged directly into bottom position", function() {
10+
expect( 1 );
11+
12+
var el = $( ".connectWith" ).sortable({
13+
connectWith: ".connectWith"
14+
});
15+
16+
TestHelpers.sortable.sort( $( "li", el[ 1 ] )[ 0 ], 0, -12, 5, "Dragging the sortable into connected sortable" );
17+
});
18+
19+
})( jQuery );

ui/jquery.ui.sortable.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ $.widget("ui.sortable", $.ui.mouse, {
799799
},
800800

801801
_contactContainers: function(event) {
802-
var i, j, dist, itemWithLeastDistance, posProperty, sizeProperty, base, cur, nearBottom, floating,
802+
var i, j, dist, itemWithLeastDistance, posProperty, sizeProperty, cur, nearBottom, floating, axis,
803803
innermostContainer = null,
804804
innermostIndex = null;
805805

@@ -850,26 +850,25 @@ $.widget("ui.sortable", $.ui.mouse, {
850850
floating = innermostContainer.floating || this._isFloating(this.currentItem);
851851
posProperty = floating ? "left" : "top";
852852
sizeProperty = floating ? "width" : "height";
853-
base = this.positionAbs[posProperty] + this.offset.click[posProperty];
853+
axis = floating ? "clientX" : "clientY";
854+
854855
for (j = this.items.length - 1; j >= 0; j--) {
855856
if(!$.contains(this.containers[innermostIndex].element[0], this.items[j].item[0])) {
856857
continue;
857858
}
858859
if(this.items[j].item[0] === this.currentItem[0]) {
859860
continue;
860861
}
861-
if (floating && !this._isOverAxis(this.positionAbs.top + this.offset.click.top, this.items[j].top, this.items[j].height)) {
862-
continue;
863-
}
862+
864863
cur = this.items[j].item.offset()[posProperty];
865864
nearBottom = false;
866-
if(Math.abs(cur - base) > Math.abs(cur + this.items[j][sizeProperty] - base)){
865+
if ( event[ axis ] - cur > this.items[ j ][ sizeProperty ] / 2 ) {
867866
nearBottom = true;
868-
cur += this.items[j][sizeProperty];
869867
}
870868

871-
if(Math.abs(cur - base) < dist) {
872-
dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j];
869+
if ( Math.abs( event[ axis ] - cur ) < dist ) {
870+
dist = Math.abs( event[ axis ] - cur );
871+
itemWithLeastDistance = this.items[ j ];
873872
this.direction = nearBottom ? "up": "down";
874873
}
875874
}

0 commit comments

Comments
 (0)