Skip to content

Sortable: Allow 0-height containers to be sortable as in 1.12.1 #2008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tests/unit/sortable/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,57 @@ QUnit.test( "ui-sortable-handle applied to appropriate element", function( asser
assert.equal( el.find( ".ui-sortable-handle" ).length, 0, "class name removed on destroy" );
} );

// gh-1998
QUnit.test( "drag & drop works with a zero-height container", function( assert ) {
var ready = assert.async();
assert.expect( 3 );

var el = $( "<ul class='list-gh-1998'>\n" +
" <style>" +
" .list-gh-1998 {\n" +
" margin: 0;\n" +
" padding: 0;\n" +
" }\n" +
" .list-item-gh-1998 {\n" +
" float: left;\n" +
" display: block;\n" +
" width: 100px;\n" +
" height: 100px;\n" +
" }\n" +
" </style>\n" +
" <li class='list-item-gh-1998'>Item 1</li>\n" +
" <li class='list-item-gh-1998'>Item 2</li>\n" +
" <li class='list-item-gh-1998'>Item 3</li>\n" +
"</ul>" )
.sortable()
.appendTo( "#qunit-fixture" );

function step1() {
el.find( "li" ).eq( 0 ).simulate( "drag", {
dx: 100,
dy: 3,
Copy link
Member Author

Choose a reason for hiding this comment

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

@fnagel I'm not sure why dy matters here since we're dragging horizontally but it had a visible effect on the test... I'm not sure if it's an actual issue or just a jQuery Simulate quirk but in manual testing it seemed to work fine so I gave up on debugging this.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I'm not really helpful here, but I guess you are right: could be a jQuery Simulate quirk too.
Having some tests should be fine...

moves: 3
} );
setTimeout( step2 );
}

function step2() {
el.find( "li" ).eq( 2 ).simulate( "drag", {
dx: -200,
dy: -3,
moves: 3
} );
setTimeout( step3 );
}

function step3() {
assert.equal( el.find( "li" ).eq( 0 ).text(), "Item 3" );
assert.equal( el.find( "li" ).eq( 1 ).text(), "Item 2" );
assert.equal( el.find( "li" ).eq( 2 ).text(), "Item 1" );
ready();
}

step1();
} );

} );
121 changes: 57 additions & 64 deletions ui/widgets/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,79 +421,76 @@ return $.widget( "ui.sortable", $.ui.mouse, {
this.helper[ 0 ].style.top = this.position.top + "px";
}

//Post events to containers
this._contactContainers( event );

if ( this.innermostContainer !== null ) {

//Do scrolling
if ( o.scroll ) {
if ( this._scroll( event ) !== false ) {
//Do scrolling
if ( o.scroll ) {
if ( this._scroll( event ) !== false ) {

//Update item positions used in position checks
this._refreshItemPositions( true );
//Update item positions used in position checks
this._refreshItemPositions( true );

if ( $.ui.ddmanager && !o.dropBehaviour ) {
$.ui.ddmanager.prepareOffsets( this, event );
}
if ( $.ui.ddmanager && !o.dropBehaviour ) {
$.ui.ddmanager.prepareOffsets( this, event );
}
}
}

this.dragDirection = {
vertical: this._getDragVerticalDirection(),
horizontal: this._getDragHorizontalDirection()
};

//Rearrange
for ( i = this.items.length - 1; i >= 0; i-- ) {

//Cache variables and intersection, continue if no intersection
item = this.items[ i ];
itemElement = item.item[ 0 ];
intersection = this._intersectsWithPointer( item );
if ( !intersection ) {
continue;
}

// Only put the placeholder inside the current Container, skip all
// items from other containers. This works because when moving
// an item from one container to another the
// currentContainer is switched before the placeholder is moved.
//
// Without this, moving items in "sub-sortables" can cause
// the placeholder to jitter between the outer and inner container.
if ( item.instance !== this.currentContainer ) {
continue;
}
this.dragDirection = {
vertical: this._getDragVerticalDirection(),
horizontal: this._getDragHorizontalDirection()
};

// Cannot intersect with itself
// no useless actions that have been done before
// no action if the item moved is the parent of the item checked
if ( itemElement !== this.currentItem[ 0 ] &&
this.placeholder[ intersection === 1 ?
"next" : "prev" ]()[ 0 ] !== itemElement &&
!$.contains( this.placeholder[ 0 ], itemElement ) &&
( this.options.type === "semi-dynamic" ?
!$.contains( this.element[ 0 ], itemElement ) :
true
)
) {
//Rearrange
for ( i = this.items.length - 1; i >= 0; i-- ) {

this.direction = intersection === 1 ? "down" : "up";
//Cache variables and intersection, continue if no intersection
item = this.items[ i ];
itemElement = item.item[ 0 ];
intersection = this._intersectsWithPointer( item );
if ( !intersection ) {
continue;
}

if ( this.options.tolerance === "pointer" ||
this._intersectsWithSides( item ) ) {
this._rearrange( event, item );
} else {
break;
}
// Only put the placeholder inside the current Container, skip all
// items from other containers. This works because when moving
// an item from one container to another the
// currentContainer is switched before the placeholder is moved.
//
// Without this, moving items in "sub-sortables" can cause
// the placeholder to jitter between the outer and inner container.
if ( item.instance !== this.currentContainer ) {
continue;
}

this._trigger( "change", event, this._uiHash() );
// Cannot intersect with itself
// no useless actions that have been done before
// no action if the item moved is the parent of the item checked
if ( itemElement !== this.currentItem[ 0 ] &&
this.placeholder[ intersection === 1 ?
"next" : "prev" ]()[ 0 ] !== itemElement &&
!$.contains( this.placeholder[ 0 ], itemElement ) &&
( this.options.type === "semi-dynamic" ?
!$.contains( this.element[ 0 ], itemElement ) :
true
)
) {

this.direction = intersection === 1 ? "down" : "up";

if ( this.options.tolerance === "pointer" ||
this._intersectsWithSides( item ) ) {
this._rearrange( event, item );
} else {
break;
}

this._trigger( "change", event, this._uiHash() );
break;
}
}

//Post events to containers
this._contactContainers( event );

//Interconnect with droppables
if ( $.ui.ddmanager ) {
$.ui.ddmanager.drag( this, event );
Expand Down Expand Up @@ -888,9 +885,7 @@ return $.widget( "ui.sortable", $.ui.mouse, {
this.options.axis === "x" || this._isFloating( this.items[ 0 ].item ) :
false;

if ( this.innermostContainer !== null ) {
this._refreshItemPositions( fast );
}
this._refreshItemPositions( fast );

var i, p;

Expand Down Expand Up @@ -1038,8 +1033,6 @@ return $.widget( "ui.sortable", $.ui.mouse, {

}

this.innermostContainer = innermostContainer;

// If no intersecting containers found, return
if ( !innermostContainer ) {
return;
Expand Down