Skip to content

Commit

Permalink
Sortable: Calculating item distance and direction using a more robust…
Browse files Browse the repository at this point in the history
… algorithm to better support sorting among nested sortables. Fixes #8572 - Wrong placeholder positions. Fixes #8573 - Can't drag an item out of an inner sortable. Fixes #8574 - Hard to put an item between two inner sortables.

Use the item which has the least distance between the mouse
pointer and one of its borders to rearrange, with direction being
determined by the nearest border.
Also we use this algorithm to rearrange even when currentContainer
is not changed to override the defective rearrangment in
_mouseDrag
  • Loading branch information
zhizhangchen authored and scottgonzalez committed Oct 10, 2012
1 parent 20e6064 commit bae06d2
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions ui/jquery.ui.sortable.js
Expand Up @@ -734,16 +734,26 @@ $.widget("ui.sortable", $.ui.mouse, {
if(this.containers.length === 1) {
this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
this.containers[innermostIndex].containerCache.over = 1;
} else if(this.currentContainer != this.containers[innermostIndex]) {
} else {

//When entering a new container, we will find the item with the least distance and append our item near it
var dist = 10000; var itemWithLeastDistance = null; var base = this.positionAbs[this.containers[innermostIndex].floating ? 'left' : 'top'];
var dist = 10000; var itemWithLeastDistance = null;
var posProperty = this.containers[innermostIndex].floating ? 'left' : 'top';
var sizeProperty = this.containers[innermostIndex].floating ? 'width' : 'height';
var base = this.positionAbs[posProperty] + this.offset.click[posProperty];
for (var j = this.items.length - 1; j >= 0; j--) {
if(!$.contains(this.containers[innermostIndex].element[0], this.items[j].item[0])) continue;
var cur = this.containers[innermostIndex].floating ? this.items[j].item.offset().left : this.items[j].item.offset().top;
if(this.items[j].item[0] == this.currentItem[0]) continue;
var cur = this.items[j].item.offset()[posProperty];
var nearBottom = false;
if(Math.abs(cur - base) > Math.abs(cur + this.items[j][sizeProperty] - base)){
nearBottom = true;
cur += this.items[j][sizeProperty];
}

if(Math.abs(cur - base) < dist) {
dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j];
this.direction = (cur - base > 0) ? 'down' : 'up';
this.direction = nearBottom ? "up": "down";
}
}

Expand Down

4 comments on commit bae06d2

@arpowers
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this commit screws up the way that sortable was working.
I had an interface working perfectly with the 1.9 version, but now on 1.9.1 sortable won't work at all with nested elements.

Are you sure this didn't mess with nesting?

@arpowers
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified. This commit causes bugs especially when using nested divs.

@mikesherov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arpowers, thanks please provide a test case on the bug tracker at http://bugs.jqueryui.com

@jzaefferer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhizhangchen John, could you sign our CLA? http://jquery.github.com/cla.html Thanks.

Please sign in to comment.