mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-11-21 11:04:24 +00:00
Droppable: Cleanup.
This commit is contained in:
parent
4792d5b7af
commit
e9f5c1857f
128
ui/jquery.ui.droppable.js
vendored
128
ui/jquery.ui.droppable.js
vendored
@ -5,7 +5,7 @@
|
||||
* Dual licensed under the MIT or GPL Version 2 licenses.
|
||||
* http://jquery.org/license
|
||||
*
|
||||
* http://docs.jquery.com/UI/Droppables
|
||||
* http://docs.jquery.com/UI/Droppable
|
||||
*
|
||||
* Depends:
|
||||
* jquery.ui.core.js
|
||||
@ -17,161 +17,123 @@
|
||||
$.widget( "ui.droppable", {
|
||||
version: "@VERSION",
|
||||
widgetEventPrefix: "drop",
|
||||
allowedTolerance: ["touch","intersect"],
|
||||
options: {
|
||||
// accept: '*',
|
||||
// activeClass: false,
|
||||
// addClasses: true,
|
||||
// accept: null,
|
||||
// greedy: false,
|
||||
// hoverClass: false,
|
||||
// scope: 'default',
|
||||
tolerance: 'intersect'
|
||||
tolerance: "intersect"
|
||||
},
|
||||
|
||||
// draggableProportions: width and height of currently dragging draggable
|
||||
// over: whether or not a draggable is currently over droppable
|
||||
// proportions: width and height of droppable
|
||||
|
||||
|
||||
// TODO: move below _create()
|
||||
// TODO: rename to refresh()?
|
||||
refreshPosition: function() {
|
||||
|
||||
// Store current location
|
||||
this.offset = this.element.offset();
|
||||
|
||||
// Store the droppable's proportions
|
||||
this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
|
||||
|
||||
// TODO: should this delegate to core?
|
||||
this.proportions = {
|
||||
width: this.element[0].offsetWidth,
|
||||
height: this.element[0].offsetHeight
|
||||
};
|
||||
},
|
||||
|
||||
_create: function() {
|
||||
|
||||
this.refreshPosition();
|
||||
|
||||
// TODO: Use $.Callbacks or .on from 1.7
|
||||
$('*').live( "drag", $.proxy( this._drag, this ) );
|
||||
$('*').live( "dragstart", $.proxy( this._dragStart, this ) );
|
||||
// TODO: make this much more efficient
|
||||
// possibly just override draggable's methods
|
||||
$( "*" ).live( "drag", $.proxy( this._drag, this ) );
|
||||
$( "*" ).live( "dragstart", $.proxy( this._dragStart, this ) );
|
||||
|
||||
this._bind( this.document, {
|
||||
mouseup: "_mouseUp"
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
_drag: function( event, ui ) {
|
||||
|
||||
var handleFunc, edges,
|
||||
tolerance = this.options.tolerance,
|
||||
over = false;
|
||||
|
||||
switch ( tolerance ) {
|
||||
|
||||
case "intersect":
|
||||
case "touch":
|
||||
|
||||
var tolerance = this.options.tolerance,
|
||||
handleFunc = "_handle" + tolerance.substr( 0, 1 ).toUpperCase() + tolerance.substr( 1 ),
|
||||
edges = {
|
||||
right: ( this.offset.left + this.proportions.width ),
|
||||
bottom: ( this.offset.top + this.proportions.height ),
|
||||
draggableRight: ( ui.offset.left + this.draggableProportions.width ),
|
||||
draggableBottom: ( ui.offset.top + this.draggableProportions.height )
|
||||
};
|
||||
|
||||
|
||||
handleFunc = "_handle" + tolerance.substr(0, 1 ).toUpperCase() + tolerance.substr( 1 );
|
||||
right: this.offset.left + this.proportions.width,
|
||||
bottom: this.offset.top + this.proportions.height,
|
||||
draggableRight: ui.offset.left + this.draggableProportions.width,
|
||||
draggableBottom: ui.offset.top + this.draggableProportions.height
|
||||
},
|
||||
over = this[ handleFunc ]( edges, ui );
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw( "Invalid tolerance passed: " + this.options.tolerance + ". Allowed: " + this.allowedTolerance.join( ", " ) );
|
||||
|
||||
}
|
||||
|
||||
// If there is sufficient overlap as deemed by tolerance
|
||||
if ( over === true ) {
|
||||
|
||||
if ( over ) {
|
||||
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 ) {
|
||||
|
||||
} else if ( this.over ) {
|
||||
this.over = false;
|
||||
this._trigger( "out", event, this._uiHash() );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
_dragStart: function( event, ui ) {
|
||||
|
||||
var draggable = $( event.target );
|
||||
|
||||
// TODO: Possibly move into draggable hash, so if there are multiple droppables, it's not recalculating all the time
|
||||
this.draggableProportions = { width: draggable[0].offsetWidth, height: draggable[0].offsetHeight };
|
||||
|
||||
|
||||
|
||||
this.draggableProportions = {
|
||||
width: draggable[0].offsetWidth,
|
||||
height: draggable[0].offsetHeight
|
||||
};
|
||||
},
|
||||
|
||||
// Determines if draggable is over droppable based on intersect tolerance
|
||||
// TODO: move all tolerance methods into a hash
|
||||
// $.ui.droppable.tolerance.intersect
|
||||
_handleIntersect: function( edges, ui ) {
|
||||
|
||||
var xDiff = edges.draggableRight - this.offset.left,
|
||||
yDiff = edges.draggableBottom - this.offset.top,
|
||||
// TODO: is there really any need to round here?
|
||||
xHalfway = Math.round( this.proportions.width / 2 ),
|
||||
yHalfway = Math.round( this.proportions.height / 2 ),
|
||||
xHalfOverlap = false;
|
||||
yHalfOverlap = false;
|
||||
|
||||
xOverlap = false,
|
||||
yOverlap = false;
|
||||
|
||||
// If Coming from left or right
|
||||
xHalfOverlap = ( ui.offset.left < this.offset.left ) ?
|
||||
( xDiff >= xHalfway ) :
|
||||
( xDiff <= xHalfway + this.proportions.width );
|
||||
xOverlap = 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 );
|
||||
|
||||
yOverlap = ui.offset.top < this.offset.top ?
|
||||
yDiff >= yHalfway :
|
||||
yDiff <= yHalfway + this.proportions.height;
|
||||
|
||||
return xOverlap && yOverlap;
|
||||
},
|
||||
|
||||
// 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;
|
||||
|
||||
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 );
|
||||
|
||||
return xOverlap && yOverlap;
|
||||
},
|
||||
|
||||
// TODO: shouldn't this be dragStop?
|
||||
_mouseUp: function( event ) {
|
||||
|
||||
if ( this.over ) {
|
||||
this._trigger( "drop", event, this._uiHash() );
|
||||
}
|
||||
|
||||
this.over = false;
|
||||
|
||||
},
|
||||
|
||||
// TODO: fill me out
|
||||
_uiHash: function() {
|
||||
|
||||
return {};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
})( jQuery );
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user