Droppable: Intersect tolerance added

This commit is contained in:
Dave Stein 2011-11-13 18:39:04 -05:00
parent 9ee7c90a59
commit 3914cebe66

View File

@ -25,7 +25,7 @@ $.widget("ui.droppable", {
// greedy: false, // greedy: false,
// hoverClass: false, // hoverClass: false,
// scope: 'default', // scope: 'default',
tolerance: 'touch' //'intersect' tolerance: 'intersect'
}, },
// draggableProportions: width and height of currently dragging draggable // draggableProportions: width and height of currently dragging draggable
@ -59,36 +59,48 @@ $.widget("ui.droppable", {
_drag: function( event, ui ) { _drag: function( event, ui ) {
var rightEdge, bottomEdge, draggableRightEdge, draggableBottomEdge, xOverlap, yOverlap; var handleFunc, edges,
tolerance = this.options.tolerance,
over = false;
switch ( this.options.tolerance ) { switch ( tolerance ) {
case 'touch': case "intersect":
rightEdge = ( this.offset.left + this.proportions.width ), case "touch":
bottomEdge = ( this.offset.top + this.proportions.height ),
draggableRightEdge = ( ui.offset.left + this.draggableProportions.width ), edges = {
draggableBottomEdge = ( ui.offset.top + this.draggableProportions.height ), right: ( this.offset.left + this.proportions.width ),
xOverlap = ( draggableRightEdge >= this.offset.left && ui.offset.left <= rightEdge ), bottom: ( this.offset.top + this.proportions.height ),
yOverlap = ( draggableBottomEdge >= this.offset.top && ui.offset.top <= bottomEdge ); draggableRight: ( ui.offset.left + this.draggableProportions.width ),
draggableBottom: ( ui.offset.top + this.draggableProportions.height )
};
if ( xOverlap && yOverlap ) { handleFunc = "_handle" + tolerance.substr(0, 1 ).toUpperCase() + tolerance.substr( 1 );
this._trigger( "over", event, this._uiHash() ); over = this[ handleFunc ]( edges, ui );
this.over = true;
}
else if ( this.over ) {
this.over = false;
this._trigger( "out", event, this._uiHash() );
}
break; break;
default: default:
throw( "Invalid tolerance passed: " + this.options.tolerance + ". Allowed: " + this.allowedTolerance.join( ', ' ) ); throw( "Invalid tolerance passed: " + this.options.tolerance + ". Allowed: " + this.allowedTolerance.join( ", " ) );
break;
} }
// If there is sufficient overlap as deemed by tolerance
if ( over === true ) {
this._trigger( "over", event, this._uiHash() );
this.over = true;
}
// If there isn't enough overlap and droppable was previously flagged as over
else if ( this.over === true ) {
this.over = false;
this._trigger( "out", event, this._uiHash() );
}
}, },
@ -101,6 +113,42 @@ $.widget("ui.droppable", {
},
// Determines if draggable is over droppable based on intersect tolerance
_handleIntersect: function( edges, ui ) {
var xDiff = edges.draggableRight - this.offset.left,
yDiff = edges.draggableBottom - this.offset.top,
xHalfway = Math.round( this.proportions.width / 2 ),
yHalfway = Math.round( this.proportions.height / 2 ),
xHalfOverlap = false;
yHalfOverlap = false;
// If Coming from left or right
xHalfOverlap = ( ui.offset.left < this.offset.left ) ?
( xDiff >= xHalfway ) :
( xDiff <= xHalfway + this.proportions.width );
// If Coming from top or bottom
yHalfOverlap = ( ui.offset.top < this.offset.top ) ?
( yDiff >= yHalfway ) :
( yDiff <= yHalfway + this.proportions.height );
return ( xHalfOverlap && yHalfOverlap );
},
// Determines if draggable is over droppable based on touch tolerance
_handleTouch: function( edges, ui ) {
var xOverlap = ( edges.draggableRight >= this.offset.left && ui.offset.left <= edges.right ),
yOverlap = ( edges.draggableBottom >= this.offset.top && ui.offset.top <= edges.bottom );
return ( xOverlap && yOverlap );
}, },
_mouseUp: function( event ) { _mouseUp: function( event ) {