Skip to content
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

Droppable event ordering issue #968

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 39 additions & 2 deletions tests/unit/droppable/droppable_events.js
Expand Up @@ -35,8 +35,6 @@ test( "droppable destruction/recreation on drop event", function() {
ok( !droppable2.hasClass( "active" ), "subsequent droppable no longer active" );
});



// todo: comment the following in when ready to actually test
/*
test("activate", function() {
Expand All @@ -60,4 +58,43 @@ test("drop", function() {
});
*/

test( "drop event callback order", function() {
expect( 4 );

$( "<div id='droppable3'></div>" ).appendTo( "body" );
$( "<div id='droppable4'></div>" ).appendTo( $( "#droppable3" ) );
$( "<div id='draggable2'></div>" ).appendTo( "body" );
var droppable3 = $( "#droppable3" ),
droppable4 = $( "#droppable4" ),
draggable2 = $( "#draggable2" );
Copy link
Member

Choose a reason for hiding this comment

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

Why not just store the references as we create the elements? They don't even need IDs then.


droppable3.css({ position: 'absolute', width: 100, height: 100, left: 0, top: 0 });
droppable4.css({ position: 'absolute', width: 50, height: 50, left: 0, top: 0 });
Copy link
Member

Choose a reason for hiding this comment

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

Always use double quotes for strings. Might as well just chain these into the creation.


droppable3.droppable({ drop: function() { droppable3.addClass( "dropped" ) } });
droppable4.droppable({ drop: function() { droppable4.addClass( "dropped" ) } });
Copy link
Member

Choose a reason for hiding this comment

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

Can chain these as well.


draggable2.draggable();
draggable2.css({ position: 'absolute', width: 1, height: 1, left: 0, top: 0 });

draggable2.simulate( "drag", { dx: 1, dy: 1 } );
Copy link
Member

Choose a reason for hiding this comment

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

And all of these.


ok( droppable3.hasClass( "dropped" ), "parent droppable receives event" );
ok( droppable4.hasClass( "dropped" ), "child droppable receives event" );

droppable3.removeClass( "dropped" );
droppable4.removeClass( "dropped" );

droppable4.droppable({ greedy: true });

draggable2.simulate( "drag", { dx: 1, dy: 1 } );

ok( !droppable3.hasClass( "dropped" ), "parent droppable does not receive event" );
ok( droppable4.hasClass( "dropped" ), "child droppable receives event" );

droppable3.remove();
droppable4.remove();
draggable2.remove();
});

})( jQuery );
23 changes: 20 additions & 3 deletions ui/jquery.ui.droppable.js
Expand Up @@ -42,7 +42,8 @@ $.widget("ui.droppable", {
_create: function() {

var o = this.options,
accept = o.accept;
accept = o.accept,
tgt = this.element;
Copy link
Member

Choose a reason for hiding this comment

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

Use target.


this.isover = false;
this.isout = true;
Expand All @@ -54,9 +55,15 @@ $.widget("ui.droppable", {
//Store the droppable's proportions
this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };

//Store the droppable's depth in the DOM tree
Copy link
Member

Choose a reason for hiding this comment

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

Space after //.

this.depth = 0;
while ( tgt.parent().size() >= 1 ) {
++this.depth;
tgt = tgt.parent();
Copy link
Member

Choose a reason for hiding this comment

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

Why not just use the length for the depth and grab the last element? This loop isn't necessary.

}

// Add the reference and positions to the manager
$.ui.ddmanager.droppables[o.scope] = $.ui.ddmanager.droppables[o.scope] || [];
$.ui.ddmanager.droppables[o.scope].push(this);
$.ui.ddmanager.addDroppable( o.scope, this );

(o.addClasses && this.element.addClass("ui-droppable"));

Expand Down Expand Up @@ -275,6 +282,16 @@ $.ui.ddmanager = {
}

},
addDroppable: function ( scope, droppable ) {
$.ui.ddmanager.droppables[ scope ] = $.ui.ddmanager.droppables[ scope ] || [];
// TODO: Binary search would be faster
Copy link
Member

Choose a reason for hiding this comment

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

Blank line above comments.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, just drop this. If it ever becomes a problem, someone will file a bug and we can deal with it then. Otherwise, this todo will just sit forever.

for ( var idx = 0; idx < $.ui.ddmanager.droppables[ scope ].length; ++idx ) {
if ( $.ui.ddmanager.droppables[ scope ][ idx ].depth <= droppable.depth ) {
break;
}
}
$.ui.ddmanager.droppables[ scope ].splice( idx, 0, droppable );
},
drop: function(draggable, event) {

var dropped = false;
Expand Down