2008-06-04 02:34:33 +00:00
|
|
|
/*
|
2008-09-04 22:03:30 +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)
|
2010-07-09 13:01:04 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2008-11-20 04:10:34 +00:00
|
|
|
*
|
2011-11-18 00:48:09 +00:00
|
|
|
* http://docs.jquery.com/UI/Droppable
|
2008-06-04 02:34:33 +00:00
|
|
|
*
|
|
|
|
* Depends:
|
2009-09-17 10:39:12 +00:00
|
|
|
* jquery.ui.core.js
|
2010-01-07 03:19:50 +00:00
|
|
|
* jquery.ui.widget.js
|
2010-02-03 23:19:48 +00:00
|
|
|
* jquery.ui.draggable.js
|
2008-06-04 02:34:33 +00:00
|
|
|
*/
|
2010-07-13 13:57:58 +00:00
|
|
|
(function( $, undefined ) {
|
2008-06-04 02:34:33 +00:00
|
|
|
|
2012-01-15 16:52:01 +00:00
|
|
|
var guid = 0,
|
|
|
|
droppables = {};
|
2012-01-09 02:53:47 +00:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
var orig = $.ui.draggable.prototype._trigger;
|
|
|
|
$.ui.draggable.prototype._trigger = function( type, event, ui ) {
|
|
|
|
var droppable,
|
2012-01-15 18:35:23 +00:00
|
|
|
method = "_draggable" + type.substr( 0, 1 ).toUpperCase() + type.substr( 1 ),
|
|
|
|
allowed = orig.apply( this, arguments );
|
|
|
|
|
|
|
|
if ( allowed ) {
|
|
|
|
for ( droppable in droppables ) {
|
|
|
|
droppables[ droppable ][ method ]( event, ui );
|
|
|
|
}
|
2012-01-09 02:53:47 +00:00
|
|
|
}
|
2012-01-15 18:35:23 +00:00
|
|
|
|
|
|
|
return allowed;
|
2012-01-09 02:53:47 +00:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2011-11-18 00:48:09 +00:00
|
|
|
$.widget( "ui.droppable", {
|
2011-05-28 19:39:55 +00:00
|
|
|
version: "@VERSION",
|
2010-02-05 03:03:50 +00:00
|
|
|
widgetEventPrefix: "drop",
|
2012-01-15 17:27:01 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
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"
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
2011-11-13 23:39:04 +00:00
|
|
|
|
2011-11-13 22:28:56 +00:00
|
|
|
// draggableProportions: width and height of currently dragging draggable
|
2011-11-13 22:42:05 +00:00
|
|
|
// 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-13 22:49:37 +00:00
|
|
|
_create: function() {
|
|
|
|
this.refreshPosition();
|
2012-01-15 16:52:01 +00:00
|
|
|
this.guid = guid++;
|
2012-01-09 02:53:47 +00:00
|
|
|
droppables[ this.guid ] = this;
|
2008-06-04 02:34:33 +00:00
|
|
|
},
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2012-01-15 17:27:01 +00:00
|
|
|
/** public **/
|
|
|
|
|
2012-01-08 01:45:10 +00:00
|
|
|
// TODO: rename to refresh()?
|
|
|
|
refreshPosition: function() {
|
|
|
|
// Store current location
|
|
|
|
this.offset = this.element.offset();
|
|
|
|
|
|
|
|
// Store the droppable's proportions
|
|
|
|
// TODO: should this delegate to core?
|
|
|
|
this.proportions = {
|
|
|
|
width: this.element[0].offsetWidth,
|
|
|
|
height: this.element[0].offsetHeight
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-01-15 17:27:01 +00:00
|
|
|
/** draggable integration **/
|
|
|
|
|
2012-01-09 02:53:47 +00:00
|
|
|
_draggableStart: 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
|
|
|
|
// TODO: Should this use the helper if it exists?
|
|
|
|
this.draggableProportions = {
|
|
|
|
width: draggable[0].offsetWidth,
|
|
|
|
height: draggable[0].offsetHeight
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
_draggableDrag: function( event, ui ) {
|
2012-01-08 01:45:10 +00:00
|
|
|
var edges = {
|
2011-11-18 00:48:09 +00:00
|
|
|
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
|
|
|
|
},
|
2012-01-08 01:45:10 +00:00
|
|
|
over = $.ui.droppable.tolerance[ this.options.tolerance ]
|
|
|
|
.call( this, 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
|
|
|
},
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2012-01-09 02:53:47 +00:00
|
|
|
_draggableStop: function( event, ui ) {
|
2012-01-08 01:45:10 +00:00
|
|
|
if ( this.over ) {
|
|
|
|
this._trigger( "drop", event, this._uiHash() );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.over = false;
|
|
|
|
},
|
|
|
|
|
2012-01-15 17:27:01 +00:00
|
|
|
/** internal **/
|
|
|
|
|
2012-01-08 01:45:10 +00:00
|
|
|
// TODO: fill me out
|
|
|
|
_uiHash: function() {
|
|
|
|
return {};
|
2012-01-09 02:53:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_destroy: function() {
|
|
|
|
delete droppables[ this.guid ];
|
2012-01-08 01:45:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.ui.droppable.tolerance = {
|
2012-01-15 17:49:44 +00:00
|
|
|
// Half of the draggable overlaps the droppable, horizontally and vertically
|
2012-01-08 01:45:10 +00:00
|
|
|
intersect: function( event, edges, ui ) {
|
2012-01-15 17:27:01 +00:00
|
|
|
var xHalf = ui.offset.left + this.draggableProportions.width / 2,
|
|
|
|
yHalf = ui.offset.top + this.draggableProportions.height / 2;
|
2011-11-18 00:48:09 +00:00
|
|
|
|
2012-01-15 17:27:01 +00:00
|
|
|
return this.offset.left < xHalf && edges.right > xHalf &&
|
|
|
|
this.offset.top < yHalf && edges.bottom > yHalf;
|
2011-11-13 22:42:05 +00:00
|
|
|
},
|
2011-11-18 00:48:09 +00:00
|
|
|
|
2012-01-15 17:49:44 +00:00
|
|
|
// Draggable overlaps droppable by at least one pixel
|
2012-01-08 01:45:10 +00:00
|
|
|
touch: function( event, edges, ui ) {
|
2012-01-15 17:49:44 +00:00
|
|
|
return this.offset.left < edges.draggableRight &&
|
|
|
|
edges.right > ui.offset.left &&
|
|
|
|
this.offset.top < edges.draggableBottom &&
|
|
|
|
edges.bottom > ui.offset.top;
|
2011-11-13 23:39:04 +00:00
|
|
|
},
|
2012-01-07 16:50:16 +00:00
|
|
|
|
2012-01-15 17:49:44 +00:00
|
|
|
// Pointer overlaps droppable
|
2012-01-08 01:45:10 +00:00
|
|
|
pointer: function( event, edges, ui ) {
|
2012-01-15 18:35:23 +00:00
|
|
|
return ui.pointer.x >= this.offset.left && ui.pointer.x <= edges.right &&
|
|
|
|
ui.pointer.y >= this.offset.top && ui.pointer.y <= edges.bottom;
|
2008-06-04 02:34:33 +00:00
|
|
|
}
|
2012-01-08 01:45:10 +00:00
|
|
|
};
|
2008-11-18 02:55:25 +00:00
|
|
|
|
2011-11-18 00:48:09 +00:00
|
|
|
})( jQuery );
|