Skip to content

Commit f656aeb

Browse files
committed
Sortable: Redetermine floating flag when recalculating positions
This addresses a bug where users initialize empty sortable lists are add items dynamically. In this situation refresh() should recognize the position and orientation of the new items. Fixes #7498 Closes jquerygh-1381
1 parent fb4124b commit f656aeb

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

tests/unit/sortable/sortable_methods.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,39 @@ test( "disable", function() {
9090
equal( chainable, element, "disable is chainable" );
9191
});
9292

93+
test( "refresh() should update the positions of initially empty lists (see #7498)", function() {
94+
expect( 1 );
95+
96+
var changeCount = 0,
97+
element = $( "#qunit-fixture" ).html( "<ul></ul>" ).find( "ul" );
98+
99+
element
100+
.css({
101+
"float": "left",
102+
width: "100px"
103+
})
104+
.sortable({
105+
change: function() {
106+
changeCount++;
107+
}
108+
})
109+
.append( "<li>a</li><li>a</li>" )
110+
.find( "li" )
111+
.css({
112+
"float": "left",
113+
width: "50px",
114+
height: "50px"
115+
});
116+
117+
element.sortable( "refresh" );
118+
119+
// Switch the order of the two li elements
120+
element.find( "li" ).eq( 0 ).simulate( "drag", {
121+
dx: 55,
122+
moves: 15
123+
});
124+
125+
equal( changeCount, 1 );
126+
});
127+
93128
})(jQuery);

ui/sortable.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,12 @@ return $.widget("ui.sortable", $.ui.mouse, {
7777
},
7878

7979
_create: function() {
80-
81-
var o = this.options;
8280
this.containerCache = {};
8381
this.element.addClass("ui-sortable");
8482

8583
//Get the items
8684
this.refresh();
8785

88-
//Let's determine if the items are being displayed horizontally
89-
this.floating = this.items.length ? o.axis === "x" || this._isFloating(this.items[0].item) : false;
90-
9186
//Let's determine the parent's offset
9287
this.offset = this.element.offset();
9388

@@ -731,6 +726,11 @@ return $.widget("ui.sortable", $.ui.mouse, {
731726

732727
refreshPositions: function(fast) {
733728

729+
// Determine whether items are being displayed horizontally
730+
this.floating = this.items.length ?
731+
this.options.axis === "x" || this._isFloating( this.items[ 0 ].item ) :
732+
false;
733+
734734
//This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change
735735
if(this.offsetParent && this.helper) {
736736
this.offset.parent = this._getParentOffset();

0 commit comments

Comments
 (0)