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

299 lines
6.4 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Droppable @VERSION
2012-07-04 13:08:08 +00:00
* http://jqueryui.com
2008-06-04 02:34:33 +00:00
*
2013-01-10 13:52:20 +00:00
* Copyright 2013 jQuery Foundation and other contributors
2012-08-09 14:13:24 +00:00
* Released under the MIT license.
* 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
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
var guid = 0,
droppables = {};
(function() {
var orig = $.ui.draggable.prototype._trigger;
$.ui.draggable.prototype._trigger = function( type, event, ui ) {
2012-01-29 17:47:35 +00:00
var method = "_draggable" + type.substr( 0, 1 ).toUpperCase() + type.substr( 1 ),
allowed = orig.apply( this, arguments );
2012-01-29 17:47:35 +00:00
if ( allowed && $.ui.droppable[ method ] ) {
$.ui.droppable[ method ]( event, ui );
}
return allowed;
};
})();
2011-11-18 00:48:09 +00:00
$.widget( "ui.droppable", {
version: "@VERSION",
widgetEventPrefix: "drop",
2012-01-15 17:27:01 +00:00
options: {
2012-01-29 17:47:35 +00:00
accept: null,
2013-02-18 22:42:19 +00:00
greedy: false,
2011-11-18 00:48:09 +00:00
tolerance: "intersect"
},
2011-11-13 23:39:04 +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
_create: function() {
this.refresh();
this.guid = guid++;
droppables[ this.guid ] = this;
2008-06-04 02:34:33 +00:00
},
2012-01-15 17:27:01 +00:00
/** public **/
refresh: function() {
2012-01-08 01:45:10 +00:00
this.offset = this.element.offset();
this.proportions = {
width: this.element.outerWidth(),
height: this.element.outerHeight()
2012-01-08 01:45:10 +00:00
};
},
2012-01-29 17:47:35 +00:00
/** internal **/
2012-01-15 17:27:01 +00:00
2012-12-21 16:33:13 +00:00
_start: function( event ) {
// If draggable is acceptable to this droppable
if ( !this._isAcceptable( event.target ) ) {
return false;
}
this._trigger( "activate", event, this._uiHash() );
},
_isAcceptable: function( draggable ) {
if ( $.isFunction( this.options.accept ) ) {
if ( this.options.accept.call( this.element, draggable ) !== true ) {
return false;
}
}
else if ( this.options.accept && !$( draggable ).is( this.options.accept ) ) {
return false;
}
return true;
2012-01-29 17:47:35 +00:00
},
_drag: function( event, ui ) {
var draggableProportions = $.ui.droppable.draggableProportions,
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 + draggableProportions.width,
draggableBottom: ui.offset.top + draggableProportions.height
2011-11-18 00:48:09 +00:00
},
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
},
2012-12-21 16:33:13 +00:00
_stop: function( event ) {
2013-02-18 22:42:19 +00:00
var greedy_child,
self = this;
2012-01-08 01:45:10 +00:00
if ( this.over ) {
2013-02-18 22:42:19 +00:00
this.element.find(":data('" + this.widgetFullName + "')").each( function() {
2013-02-18 22:42:19 +00:00
var drop = $(this).data( self.widgetFullName );
2013-02-18 22:42:19 +00:00
if ( drop.options.greedy === true && drop.over === true ) {
greedy_child = true;
return false;
}
});
2013-02-18 22:42:19 +00:00
if ( !greedy_child ) {
this._trigger( "drop", event, this._uiHash() );
}
2012-01-08 01:45:10 +00:00
}
this._trigger( "deactivate", event, this._uiHash() );
2012-01-08 01:45:10 +00:00
this.over = false;
},
// TODO: fill me out
_uiHash: function() {
return {};
},
_destroy: function() {
delete droppables[ this.guid ];
2012-01-08 01:45:10 +00:00
}
});
$.extend( $.ui.droppable, {
// draggableProportions: width and height of currently dragging draggable
2012-01-29 17:47:35 +00:00
// active: array of active droppables
2011-11-18 00:48:09 +00:00
tolerance: {
// Half of the draggable overlaps the droppable, horizontally and vertically
intersect: function( event, edges, ui ) {
var draggableProportions = $.ui.droppable.draggableProportions,
xHalf = ui.offset.left + draggableProportions.width / 2,
yHalf = ui.offset.top + draggableProportions.height / 2;
return this.offset.left < xHalf && edges.right > xHalf &&
this.offset.top < yHalf && edges.bottom > yHalf;
},
// Draggable overlaps droppable by at least one pixel
touch: function( event, edges, ui ) {
return this.offset.left < edges.draggableRight &&
edges.right > ui.offset.left &&
this.offset.top < edges.draggableBottom &&
edges.bottom > ui.offset.top;
},
// Pointer overlaps droppable
pointer: function( event, edges, ui ) {
return ui.pointer.x >= this.offset.left && ui.pointer.x <= edges.right &&
ui.pointer.y >= this.offset.top && ui.pointer.y <= edges.bottom;
}
},
2011-11-18 00:48:09 +00:00
_draggableStart: function( event, ui ) {
2012-01-29 17:47:35 +00:00
var droppable,
element = ui.helper || $( event.target );
2012-01-07 16:50:16 +00:00
this.draggableProportions = {
width: element.outerWidth(),
height: element.outerHeight()
};
2012-01-29 17:47:35 +00:00
this.active = [];
for ( droppable in droppables ) {
if ( droppables[ droppable ]._start( event, ui ) !== false ) {
2012-01-29 17:47:35 +00:00
this.active.push( droppables[ droppable ] );
}
}
},
_draggableDrag: function( event, ui ) {
$.each( this.active, function() {
this._drag( event, ui );
});
},
_draggableStop: function( event, ui ) {
$.each( this.active, function() {
this._stop( event, ui );
2012-01-29 17:47:35 +00:00
});
2008-06-04 02:34:33 +00:00
}
});
2011-11-18 00:48:09 +00:00
})( jQuery );
// DEPRECATED
if ( $.uiBackCompat !== false ) {
// activeClass option
$.widget( "ui.droppable", $.ui.droppable, {
options: {
activeClass: false
},
_create: function() {
var self = this,
added = false;
this._super();
// On drag, see if a class should be added to droppable
$(this.document[0].body).on( "drag", ".ui-draggable", function( event ) {
if ( !added && self.options.activeClass && self._isAcceptable( event.target ) ) {
self.element.addClass( self.options.activeClass );
added = true;
}
});
// On dragstop, remove class if one was added
$(this.document[0].body).on( "dragstop", ".ui-draggable", function() {
if ( added ) {
self.element.removeClass( self.options.activeClass );
added = false;
}
});
}
});
// hoverClass option
$.widget( "ui.droppable", $.ui.droppable, {
options: {
hoverClass: false
},
_create: function() {
var self = this,
added = false;
this._super();
// On over, add class if needed
$(this.element).on( "dropover", function() {
if ( !added && self.options.hoverClass ) {
self.element.addClass( self.options.hoverClass );
added = true;
}
});
// On out, remove class if one was added
$(this.element).on( "dropout", function() {
if ( added ) {
self.element.removeClass( self.options.hoverClass );
added = false;
}
});
}
});
}