2 * jQuery UI Droppable 1.12.1
5 * Copyright jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
11 //>>group: Interactions
12 //>>description: Enables drop targets for draggable elements.
13 //>>docs: http://api.jqueryui.com/droppable/
14 //>>demos: http://jqueryui.com/droppable/
16 ( function( factory ) {
17 if ( typeof define === "function" && define.amd ) {
19 // AMD. Register as an anonymous module.
34 $.widget( "ui.droppable", {
36 widgetEventPrefix: "drop",
42 tolerance: "intersect",
60 this.accept = $.isFunction( accept ) ? accept : function( d ) {
61 return d.is( accept );
64 this.proportions = function( /* valueToWrite */ ) {
65 if ( arguments.length ) {
67 // Store the droppable's proportions
68 proportions = arguments[ 0 ];
71 // Retrieve or derive the droppable's proportions
75 width: this.element[ 0 ].offsetWidth,
76 height: this.element[ 0 ].offsetHeight
81 this._addToManager( o.scope );
83 o.addClasses && this._addClass( "ui-droppable" );
87 _addToManager: function( scope ) {
89 // Add the reference and positions to the manager
90 $.ui.ddmanager.droppables[ scope ] = $.ui.ddmanager.droppables[ scope ] || [];
91 $.ui.ddmanager.droppables[ scope ].push( this );
94 _splice: function( drop ) {
96 for ( ; i < drop.length; i++ ) {
97 if ( drop[ i ] === this ) {
103 _destroy: function() {
104 var drop = $.ui.ddmanager.droppables[ this.options.scope ];
106 this._splice( drop );
109 _setOption: function( key, value ) {
111 if ( key === "accept" ) {
112 this.accept = $.isFunction( value ) ? value : function( d ) {
113 return d.is( value );
115 } else if ( key === "scope" ) {
116 var drop = $.ui.ddmanager.droppables[ this.options.scope ];
118 this._splice( drop );
119 this._addToManager( value );
122 this._super( key, value );
125 _activate: function( event ) {
126 var draggable = $.ui.ddmanager.current;
128 this._addActiveClass();
130 this._trigger( "activate", event, this.ui( draggable ) );
134 _deactivate: function( event ) {
135 var draggable = $.ui.ddmanager.current;
137 this._removeActiveClass();
139 this._trigger( "deactivate", event, this.ui( draggable ) );
143 _over: function( event ) {
145 var draggable = $.ui.ddmanager.current;
147 // Bail if draggable and droppable are same element
148 if ( !draggable || ( draggable.currentItem ||
149 draggable.element )[ 0 ] === this.element[ 0 ] ) {
153 if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem ||
154 draggable.element ) ) ) {
155 this._addHoverClass();
156 this._trigger( "over", event, this.ui( draggable ) );
161 _out: function( event ) {
163 var draggable = $.ui.ddmanager.current;
165 // Bail if draggable and droppable are same element
166 if ( !draggable || ( draggable.currentItem ||
167 draggable.element )[ 0 ] === this.element[ 0 ] ) {
171 if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem ||
172 draggable.element ) ) ) {
173 this._removeHoverClass();
174 this._trigger( "out", event, this.ui( draggable ) );
179 _drop: function( event, custom ) {
181 var draggable = custom || $.ui.ddmanager.current,
182 childrenIntersection = false;
184 // Bail if draggable and droppable are same element
185 if ( !draggable || ( draggable.currentItem ||
186 draggable.element )[ 0 ] === this.element[ 0 ] ) {
191 .find( ":data(ui-droppable)" )
192 .not( ".ui-draggable-dragging" )
194 var inst = $( this ).droppable( "instance" );
196 inst.options.greedy &&
197 !inst.options.disabled &&
198 inst.options.scope === draggable.options.scope &&
200 inst.element[ 0 ], ( draggable.currentItem || draggable.element )
204 $.extend( inst, { offset: inst.element.offset() } ),
205 inst.options.tolerance, event
208 childrenIntersection = true;
211 if ( childrenIntersection ) {
215 if ( this.accept.call( this.element[ 0 ],
216 ( draggable.currentItem || draggable.element ) ) ) {
217 this._removeActiveClass();
218 this._removeHoverClass();
220 this._trigger( "drop", event, this.ui( draggable ) );
230 draggable: ( c.currentItem || c.element ),
232 position: c.position,
233 offset: c.positionAbs
237 // Extension points just to make backcompat sane and avoid duplicating logic
238 // TODO: Remove in 1.13 along with call to it below
239 _addHoverClass: function() {
240 this._addClass( "ui-droppable-hover" );
243 _removeHoverClass: function() {
244 this._removeClass( "ui-droppable-hover" );
247 _addActiveClass: function() {
248 this._addClass( "ui-droppable-active" );
251 _removeActiveClass: function() {
252 this._removeClass( "ui-droppable-active" );
256 var intersect = $.ui.intersect = ( function() {
257 function isOverAxis( x, reference, size ) {
258 return ( x >= reference ) && ( x < ( reference + size ) );
261 return function( draggable, droppable, toleranceMode, event ) {
263 if ( !droppable.offset ) {
267 var x1 = ( draggable.positionAbs ||
268 draggable.position.absolute ).left + draggable.margins.left,
269 y1 = ( draggable.positionAbs ||
270 draggable.position.absolute ).top + draggable.margins.top,
271 x2 = x1 + draggable.helperProportions.width,
272 y2 = y1 + draggable.helperProportions.height,
273 l = droppable.offset.left,
274 t = droppable.offset.top,
275 r = l + droppable.proportions().width,
276 b = t + droppable.proportions().height;
278 switch ( toleranceMode ) {
280 return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b );
282 return ( l < x1 + ( draggable.helperProportions.width / 2 ) && // Right Half
283 x2 - ( draggable.helperProportions.width / 2 ) < r && // Left Half
284 t < y1 + ( draggable.helperProportions.height / 2 ) && // Bottom Half
285 y2 - ( draggable.helperProportions.height / 2 ) < b ); // Top Half
287 return isOverAxis( event.pageY, t, droppable.proportions().height ) &&
288 isOverAxis( event.pageX, l, droppable.proportions().width );
291 ( y1 >= t && y1 <= b ) || // Top edge touching
292 ( y2 >= t && y2 <= b ) || // Bottom edge touching
293 ( y1 < t && y2 > b ) // Surrounded vertically
295 ( x1 >= l && x1 <= r ) || // Left edge touching
296 ( x2 >= l && x2 <= r ) || // Right edge touching
297 ( x1 < l && x2 > r ) // Surrounded horizontally
306 This manager tracks offsets of draggables and droppables
310 droppables: { "default": [] },
311 prepareOffsets: function( t, event ) {
314 m = $.ui.ddmanager.droppables[ t.options.scope ] || [],
315 type = event ? event.type : null, // workaround for #2317
316 list = ( t.currentItem || t.element ).find( ":data(ui-droppable)" ).addBack();
318 droppablesLoop: for ( i = 0; i < m.length; i++ ) {
320 // No disabled and non-accepted
321 if ( m[ i ].options.disabled || ( t && !m[ i ].accept.call( m[ i ].element[ 0 ],
322 ( t.currentItem || t.element ) ) ) ) {
326 // Filter out elements in the current dragged item
327 for ( j = 0; j < list.length; j++ ) {
328 if ( list[ j ] === m[ i ].element[ 0 ] ) {
329 m[ i ].proportions().height = 0;
330 continue droppablesLoop;
334 m[ i ].visible = m[ i ].element.css( "display" ) !== "none";
335 if ( !m[ i ].visible ) {
339 // Activate the droppable if used directly from draggables
340 if ( type === "mousedown" ) {
341 m[ i ]._activate.call( m[ i ], event );
344 m[ i ].offset = m[ i ].element.offset();
345 m[ i ].proportions( {
346 width: m[ i ].element[ 0 ].offsetWidth,
347 height: m[ i ].element[ 0 ].offsetHeight
353 drop: function( draggable, event ) {
357 // Create a copy of the droppables in case the list changes during the drop (#9116)
358 $.each( ( $.ui.ddmanager.droppables[ draggable.options.scope ] || [] ).slice(), function() {
360 if ( !this.options ) {
363 if ( !this.options.disabled && this.visible &&
364 intersect( draggable, this, this.options.tolerance, event ) ) {
365 dropped = this._drop.call( this, event ) || dropped;
368 if ( !this.options.disabled && this.visible && this.accept.call( this.element[ 0 ],
369 ( draggable.currentItem || draggable.element ) ) ) {
372 this._deactivate.call( this, event );
379 dragStart: function( draggable, event ) {
381 // Listen for scrolling so that if the dragging causes scrolling the position of the
382 // droppables can be recalculated (see #5003)
383 draggable.element.parentsUntil( "body" ).on( "scroll.droppable", function() {
384 if ( !draggable.options.refreshPositions ) {
385 $.ui.ddmanager.prepareOffsets( draggable, event );
389 drag: function( draggable, event ) {
391 // If you have a highly dynamic page, you might try this option. It renders positions
392 // every time you move the mouse.
393 if ( draggable.options.refreshPositions ) {
394 $.ui.ddmanager.prepareOffsets( draggable, event );
397 // Run through all droppables and check their positions based on specific tolerance options
398 $.each( $.ui.ddmanager.droppables[ draggable.options.scope ] || [], function() {
400 if ( this.options.disabled || this.greedyChild || !this.visible ) {
404 var parentInstance, scope, parent,
405 intersects = intersect( draggable, this, this.options.tolerance, event ),
406 c = !intersects && this.isover ?
408 ( intersects && !this.isover ? "isover" : null );
413 if ( this.options.greedy ) {
415 // find droppable parents with same scope
416 scope = this.options.scope;
417 parent = this.element.parents( ":data(ui-droppable)" ).filter( function() {
418 return $( this ).droppable( "instance" ).options.scope === scope;
421 if ( parent.length ) {
422 parentInstance = $( parent[ 0 ] ).droppable( "instance" );
423 parentInstance.greedyChild = ( c === "isover" );
427 // We just moved into a greedy child
428 if ( parentInstance && c === "isover" ) {
429 parentInstance.isover = false;
430 parentInstance.isout = true;
431 parentInstance._out.call( parentInstance, event );
435 this[ c === "isout" ? "isover" : "isout" ] = false;
436 this[ c === "isover" ? "_over" : "_out" ].call( this, event );
438 // We just moved out of a greedy child
439 if ( parentInstance && c === "isout" ) {
440 parentInstance.isout = false;
441 parentInstance.isover = true;
442 parentInstance._over.call( parentInstance, event );
447 dragStop: function( draggable, event ) {
448 draggable.element.parentsUntil( "body" ).off( "scroll.droppable" );
450 // Call prepareOffsets one final time since IE does not fire return scroll events when
451 // overflow was caused by drag (see #5003)
452 if ( !draggable.options.refreshPositions ) {
453 $.ui.ddmanager.prepareOffsets( draggable, event );
459 // TODO: switch return back to widget declaration at top of file when this is removed
460 if ( $.uiBackCompat !== false ) {
462 // Backcompat for activeClass and hoverClass options
463 $.widget( "ui.droppable", $.ui.droppable, {
468 _addActiveClass: function() {
470 if ( this.options.activeClass ) {
471 this.element.addClass( this.options.activeClass );
474 _removeActiveClass: function() {
476 if ( this.options.activeClass ) {
477 this.element.removeClass( this.options.activeClass );
480 _addHoverClass: function() {
482 if ( this.options.hoverClass ) {
483 this.element.addClass( this.options.hoverClass );
486 _removeHoverClass: function() {
488 if ( this.options.hoverClass ) {
489 this.element.removeClass( this.options.hoverClass );
495 return $.ui.droppable;