jquery-ui/ui/jquery.ui.droppable.js

150 lines
4.1 KiB
JavaScript
Raw Normal View History

2008-06-04 02:34:33 +00:00
/*
* jQuery UI Droppable @VERSION
2008-06-04 02:34:33 +00:00
*
2011-01-17 14:13:18 +00:00
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
2011-11-18 00:48:09 +00:00
* http://docs.jquery.com/UI/Droppable
2008-06-04 02:34:33 +00:00
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
* jquery.ui.interaction.js
2010-02-03 23:19:48 +00:00
* jquery.ui.draggable.js
2008-06-04 02:34:33 +00:00
*/
(function( $, undefined ) {
2008-06-04 02:34:33 +00:00
2011-11-18 00:48:09 +00:00
$.widget( "ui.droppable", {
version: "@VERSION",
widgetEventPrefix: "drop",
options: {
2011-11-18 00:48:09 +00:00
// accept: null,
2011-11-13 22:28:56 +00:00
// greedy: false,
2011-11-18 00:48:09 +00:00
tolerance: "intersect"
},
2011-11-13 23:39:04 +00:00
2011-11-13 22:28:56 +00:00
// draggableProportions: width and height of currently dragging draggable
// over: whether or not a draggable is currently over droppable
2011-11-13 22:28:56 +00:00
// proportions: width and height of droppable
2011-11-13 23:39:04 +00:00
2011-11-18 00:48:09 +00:00
// TODO: move below _create()
// TODO: rename to refresh()?
refreshPosition: function() {
2011-11-13 22:28:56 +00:00
// Store current location
this.offset = this.element.offset();
2011-11-18 00:48:09 +00:00
// Store the droppable's proportions
// TODO: should this delegate to core?
this.proportions = {
width: this.element[0].offsetWidth,
height: this.element[0].offsetHeight
};
},
2011-11-13 23:39:04 +00:00
_create: function() {
this.refreshPosition();
2011-11-18 00:48:09 +00:00
// TODO: make this much more efficient
// possibly just override draggable's methods
$( "*" ).live( "drag", $.proxy( this._drag, this ) );
$( "*" ).live( "dragstart", $.proxy( this._dragStart, this ) );
2011-11-13 23:39:04 +00:00
this._bind( this.document, {
mouseup: "_mouseUp"
});
2008-06-04 02:34:33 +00:00
},
2011-11-13 22:28:56 +00:00
_drag: function( event, ui ) {
2011-11-18 00:48:09 +00:00
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
},
2011-11-27 21:59:11 +00:00
over = this[ handleFunc ]( event, edges, ui );
2011-11-13 23:39:04 +00:00
// If there is sufficient overlap as deemed by tolerance
2011-11-18 00:48:09 +00:00
if ( over ) {
2011-11-13 23:39:04 +00:00
this._trigger( "over", event, this._uiHash() );
this.over = true;
// If there isn't enough overlap and droppable was previously flagged as over
2011-11-18 00:48:09 +00:00
} else if ( this.over ) {
2011-11-13 23:39:04 +00:00
this.over = false;
this._trigger( "out", event, this._uiHash() );
}
2008-06-04 02:34:33 +00:00
},
2011-11-13 22:28:56 +00:00
_dragStart: function( event, ui ) {
var draggable = $( event.target );
2011-11-13 23:39:04 +00:00
// TODO: Possibly move into draggable hash, so if there are multiple droppables, it's not recalculating all the time
2011-11-18 00:48:09 +00:00
this.draggableProportions = {
width: draggable[0].offsetWidth,
height: draggable[0].offsetHeight
};
2011-11-13 23:39:04 +00:00
},
// Determines if draggable is over droppable based on intersect tolerance
2011-11-18 00:48:09 +00:00
// TODO: move all tolerance methods into a hash
// $.ui.droppable.tolerance.intersect
2011-11-27 21:59:11 +00:00
_handleIntersect: function( event, edges, ui ) {
2011-11-13 23:39:04 +00:00
var xDiff = edges.draggableRight - this.offset.left,
yDiff = edges.draggableBottom - this.offset.top,
2012-01-07 16:50:16 +00:00
xHalfway = this.proportions.width / 2,
yHalfway = this.proportions.height / 2,
2011-11-18 00:48:09 +00:00
xOverlap = false,
yOverlap = false;
// If Coming from left or right
xOverlap = ui.offset.left < this.offset.left ?
xDiff >= xHalfway :
xDiff <= xHalfway + this.proportions.width;
// If Coming from top or bottom
yOverlap = ui.offset.top < this.offset.top ?
yDiff >= yHalfway :
yDiff <= yHalfway + this.proportions.height;
return xOverlap && yOverlap;
},
2011-11-18 00:48:09 +00:00
2011-11-13 23:39:04 +00:00
// Determines if draggable is over droppable based on touch tolerance
2011-11-27 21:59:11 +00:00
_handleTouch: function( event, edges, ui ) {
2011-11-18 00:48:09 +00:00
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;
2011-11-13 23:39:04 +00:00
},
2012-01-07 16:50:16 +00:00
// Determines if draggable is over droppable based on pointer tolerance
2011-11-27 21:59:11 +00:00
_handlePointer: function( event, edges, ui ) {
var xOverlap = event.pageX >= this.offset.left &&
event.pageX <= edges.right,
yOverlap = event.pageY >= this.offset.top &&
event.pageY <= edges.bottom;
return xOverlap && yOverlap;
},
2011-11-13 23:39:04 +00:00
2011-11-18 00:48:09 +00:00
// TODO: shouldn't this be dragStop?
_mouseUp: function( event ) {
if ( this.over ) {
this._trigger( "drop", event, this._uiHash() );
}
2011-11-13 23:39:04 +00:00
this.over = false;
},
2011-11-13 23:39:04 +00:00
// TODO: fill me out
_uiHash: function() {
return {};
2008-06-04 02:34:33 +00:00
}
2011-11-13 22:28:56 +00:00
});
2011-11-18 00:48:09 +00:00
})( jQuery );