From 694da1984b8dbe442e2c4beb0e6599b5b0fc4b5e Mon Sep 17 00:00:00 2001 From: TJ VanToll Date: Fri, 31 Oct 2014 09:46:41 -0400 Subject: [PATCH 1/3] 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 gh-1381 --- tests/unit/sortable/sortable_methods.js | 28 +++++++++++++++++++++++++ ui/sortable.js | 9 ++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/unit/sortable/sortable_methods.js b/tests/unit/sortable/sortable_methods.js index f3fe240e701..2e6520b54df 100644 --- a/tests/unit/sortable/sortable_methods.js +++ b/tests/unit/sortable/sortable_methods.js @@ -90,4 +90,32 @@ test( "disable", function() { equal( chainable, element, "disable is chainable" ); }); +test( "refresh() should update the positions of initially empty lists (see #7498)", function() { + expect( 1 ); + + var changeCount = 0, + element = $( "#qunit-fixture" ).html( "" ).find( "ul" ); + + element + .css({ "float": "left", width: "100px" }) + .sortable({ + change: function() { + changeCount++; + } + }) + .append( "
  • a
  • a
  • " ) + .find( "li" ) + .css({ "float": "left", width: "50px", height: "50px" }); + + element.sortable( "refresh" ); + + // Switch the order of the two li elements + element.find( "li:first" ).simulate( "drag", { + dx: 55, + moves: 15 + }); + + equal( changeCount, 1 ); +}); + })(jQuery); diff --git a/ui/sortable.js b/ui/sortable.js index 0207c86206d..2e1e153ef04 100644 --- a/ui/sortable.js +++ b/ui/sortable.js @@ -77,17 +77,12 @@ return $.widget("ui.sortable", $.ui.mouse, { }, _create: function() { - - var o = this.options; this.containerCache = {}; this.element.addClass("ui-sortable"); //Get the items this.refresh(); - //Let's determine if the items are being displayed horizontally - this.floating = this.items.length ? o.axis === "x" || this._isFloating(this.items[0].item) : false; - //Let's determine the parent's offset this.offset = this.element.offset(); @@ -731,6 +726,10 @@ return $.widget("ui.sortable", $.ui.mouse, { refreshPositions: function(fast) { + // Determine whether items are being displayed horizontally + this.floating = this.items.length ? this.options.axis === "x" || + this._isFloating( this.items[ 0 ].item ) : false; + //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change if(this.offsetParent && this.helper) { this.offset.parent = this._getParentOffset(); From d995c2c149f0ea93bc3b5a6cc107a98bf45e0ca6 Mon Sep 17 00:00:00 2001 From: TJ VanToll Date: Mon, 3 Nov 2014 17:23:06 -0500 Subject: [PATCH 2/3] Fix: review fixes --- tests/unit/sortable/sortable_methods.js | 2 +- ui/sortable.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/unit/sortable/sortable_methods.js b/tests/unit/sortable/sortable_methods.js index 2e6520b54df..4dd5041cc38 100644 --- a/tests/unit/sortable/sortable_methods.js +++ b/tests/unit/sortable/sortable_methods.js @@ -110,7 +110,7 @@ test( "refresh() should update the positions of initially empty lists (see #7498 element.sortable( "refresh" ); // Switch the order of the two li elements - element.find( "li:first" ).simulate( "drag", { + element.find( "li" ).eq( 0 ).simulate( "drag", { dx: 55, moves: 15 }); diff --git a/ui/sortable.js b/ui/sortable.js index 2e1e153ef04..913949c20c6 100644 --- a/ui/sortable.js +++ b/ui/sortable.js @@ -727,8 +727,9 @@ return $.widget("ui.sortable", $.ui.mouse, { refreshPositions: function(fast) { // Determine whether items are being displayed horizontally - this.floating = this.items.length ? this.options.axis === "x" || - this._isFloating( this.items[ 0 ].item ) : false; + this.floating = this.items.length ? + this.options.axis === "x" || this._isFloating( this.items[ 0 ].item ) : + false; //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change if(this.offsetParent && this.helper) { From 2c95d5d68ca1b0dc0937bf9fe2261beaa64347e5 Mon Sep 17 00:00:00 2001 From: TJ VanToll Date: Mon, 3 Nov 2014 17:27:06 -0500 Subject: [PATCH 3/3] Fix: more review fixes --- tests/unit/sortable/sortable_methods.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/unit/sortable/sortable_methods.js b/tests/unit/sortable/sortable_methods.js index 4dd5041cc38..9c0a86a359b 100644 --- a/tests/unit/sortable/sortable_methods.js +++ b/tests/unit/sortable/sortable_methods.js @@ -97,7 +97,10 @@ test( "refresh() should update the positions of initially empty lists (see #7498 element = $( "#qunit-fixture" ).html( "
      " ).find( "ul" ); element - .css({ "float": "left", width: "100px" }) + .css({ + "float": "left", + width: "100px" + }) .sortable({ change: function() { changeCount++; @@ -105,7 +108,11 @@ test( "refresh() should update the positions of initially empty lists (see #7498 }) .append( "
    • a
    • a
    • " ) .find( "li" ) - .css({ "float": "left", width: "50px", height: "50px" }); + .css({ + "float": "left", + width: "50px", + height: "50px" + }); element.sortable( "refresh" );