jquery-ui/ui/widgets/droppable.js

460 lines
12 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
*
* Copyright jQuery Foundation and other contributors
2012-08-09 14:13:24 +00:00
* Released under the MIT license.
* http://jquery.org/license
2008-06-04 02:34:33 +00:00
*/
//>>label: Droppable
//>>group: Interactions
//>>description: Enables drop targets for draggable elements.
//>>docs: http://api.jqueryui.com/droppable/
//>>demos: http://jqueryui.com/droppable/
(function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define([
"jquery",
"./draggable",
"./mouse",
"../version",
"../widget"
], factory );
} else {
// Browser globals
factory( jQuery );
}
}(function( $ ) {
2008-06-04 02:34:33 +00:00
2013-05-24 03:54:17 +00:00
$.widget( "ui.droppable", {
version: "@VERSION",
widgetEventPrefix: "drop",
options: {
accept: "*",
addClasses: true,
greedy: false,
scope: "default",
tolerance: "intersect",
// callbacks
activate: null,
deactivate: null,
drop: null,
out: null,
over: null
},
_create: function() {
var proportions,
o = this.options,
accept = o.accept;
this.isover = false;
this.isout = true;
2013-05-24 03:54:17 +00:00
this.accept = $.isFunction( accept ) ? accept : function( d ) {
return d.is( accept );
};
this.proportions = function( /* valueToWrite */ ) {
if ( arguments.length ) {
// Store the droppable's proportions
proportions = arguments[ 0 ];
} else {
// Retrieve or derive the droppable's proportions
return proportions ?
proportions :
proportions = {
width: this.element[ 0 ].offsetWidth,
height: this.element[ 0 ].offsetHeight
};
}
};
this._addToManager( o.scope );
o.addClasses && this._addClass( "ui-droppable" );
2008-06-04 02:34:33 +00:00
},
_addToManager: function( scope ) {
// Add the reference and positions to the manager
$.ui.ddmanager.droppables[ scope ] = $.ui.ddmanager.droppables[ scope ] || [];
$.ui.ddmanager.droppables[ scope ].push( this );
},
_splice: function( drop ) {
var i = 0;
for ( ; i < drop.length; i++ ) {
2013-05-24 03:54:17 +00:00
if ( drop[ i ] === this ) {
drop.splice( i, 1 );
}
}
},
_destroy: function() {
var drop = $.ui.ddmanager.droppables[ this.options.scope ];
this._splice( drop );
2008-06-04 02:34:33 +00:00
},
2013-05-24 03:54:17 +00:00
_setOption: function( key, value ) {
2013-05-24 03:54:17 +00:00
if ( key === "accept" ) {
this.accept = $.isFunction( value ) ? value : function( d ) {
return d.is( value );
};
} else if ( key === "scope" ) {
var drop = $.ui.ddmanager.droppables[ this.options.scope ];
this._splice( drop );
this._addToManager( value );
}
this._super( key, value );
},
2013-05-24 03:54:17 +00:00
_activate: function( event ) {
var draggable = $.ui.ddmanager.current;
this._addActiveClass();
2013-05-24 03:54:17 +00:00
if ( draggable ){
this._trigger( "activate", event, this.ui( draggable ) );
}
},
2013-05-24 03:54:17 +00:00
_deactivate: function( event ) {
var draggable = $.ui.ddmanager.current;
this._removeActiveClass();
2013-05-24 03:54:17 +00:00
if ( draggable ){
this._trigger( "deactivate", event, this.ui( draggable ) );
}
},
2013-05-24 03:54:17 +00:00
_over: function( event ) {
2008-06-04 02:34:33 +00:00
var draggable = $.ui.ddmanager.current;
// Bail if draggable and droppable are same element
2013-05-24 03:54:17 +00:00
if ( !draggable || ( draggable.currentItem || draggable.element )[ 0 ] === this.element[ 0 ] ) {
return;
}
2013-05-24 03:54:17 +00:00
if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
this._addHoverClass();
2013-05-24 03:54:17 +00:00
this._trigger( "over", event, this.ui( draggable ) );
2008-06-04 02:34:33 +00:00
}
2008-06-04 02:34:33 +00:00
},
2013-05-24 03:54:17 +00:00
_out: function( event ) {
2008-06-04 02:34:33 +00:00
var draggable = $.ui.ddmanager.current;
// Bail if draggable and droppable are same element
2013-05-24 03:54:17 +00:00
if ( !draggable || ( draggable.currentItem || draggable.element )[ 0 ] === this.element[ 0 ] ) {
return;
}
2013-05-24 03:54:17 +00:00
if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
this._removeHoverClass();
2013-05-24 03:54:17 +00:00
this._trigger( "out", event, this.ui( draggable ) );
2008-06-04 02:34:33 +00:00
}
2008-06-04 02:34:33 +00:00
},
2013-05-24 03:54:17 +00:00
_drop: function( event, custom ) {
var draggable = custom || $.ui.ddmanager.current,
childrenIntersection = false;
// Bail if draggable and droppable are same element
2013-05-24 03:54:17 +00:00
if ( !draggable || ( draggable.currentItem || draggable.element )[ 0 ] === this.element[ 0 ] ) {
return false;
}
2013-05-24 03:54:17 +00:00
this.element.find( ":data(ui-droppable)" ).not( ".ui-draggable-dragging" ).each(function() {
var inst = $( this ).droppable( "instance" );
2013-05-24 03:54:17 +00:00
if (
inst.options.greedy &&
!inst.options.disabled &&
inst.options.scope === draggable.options.scope &&
2013-05-24 03:54:17 +00:00
inst.accept.call( inst.element[ 0 ], ( draggable.currentItem || draggable.element ) ) &&
intersect( draggable, $.extend( inst, { offset: inst.element.offset() } ), inst.options.tolerance, event )
) { childrenIntersection = true; return false; }
2008-06-04 02:34:33 +00:00
});
2013-05-24 03:54:17 +00:00
if ( childrenIntersection ) {
return false;
}
2013-05-24 03:54:17 +00:00
if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
this._removeActiveClass();
this._removeHoverClass();
2013-05-24 03:54:17 +00:00
this._trigger( "drop", event, this.ui( draggable ) );
return this.element;
2008-06-04 02:34:33 +00:00
}
2008-06-04 02:34:33 +00:00
return false;
2008-06-04 02:34:33 +00:00
},
2013-05-24 03:54:17 +00:00
ui: function( c ) {
return {
2013-05-24 03:54:17 +00:00
draggable: ( c.currentItem || c.element ),
helper: c.helper,
position: c.position,
offset: c.positionAbs
};
},
// Extension points just to make backcompat sane and avoid duplicating logic
// TODO: Remove in 1.13 along with call to it below
_addHoverClass: function() {
this._addClass( "ui-droppable-hover" );
},
_removeHoverClass: function() {
this._removeClass( "ui-droppable-hover" );
},
_addActiveClass: function() {
this._addClass( "ui-droppable-active" );
},
_removeActiveClass: function() {
this._removeClass( "ui-droppable-active" );
}
2008-06-04 02:34:33 +00:00
});
var intersect = (function() {
function isOverAxis( x, reference, size ) {
return ( x >= reference ) && ( x < ( reference + size ) );
}
return function( draggable, droppable, toleranceMode, event ) {
if ( !droppable.offset ) {
return false;
}
var x1 = ( draggable.positionAbs || draggable.position.absolute ).left + draggable.margins.left,
y1 = ( draggable.positionAbs || draggable.position.absolute ).top + draggable.margins.top,
x2 = x1 + draggable.helperProportions.width,
y2 = y1 + draggable.helperProportions.height,
l = droppable.offset.left,
t = droppable.offset.top,
r = l + droppable.proportions().width,
b = t + droppable.proportions().height;
switch ( toleranceMode ) {
case "fit":
2013-05-24 03:54:17 +00:00
return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b );
case "intersect":
2013-05-24 03:54:17 +00:00
return ( l < x1 + ( draggable.helperProportions.width / 2 ) && // Right Half
x2 - ( draggable.helperProportions.width / 2 ) < r && // Left Half
t < y1 + ( draggable.helperProportions.height / 2 ) && // Bottom Half
y2 - ( draggable.helperProportions.height / 2 ) < b ); // Top Half
case "pointer":
return isOverAxis( event.pageY, t, droppable.proportions().height ) && isOverAxis( event.pageX, l, droppable.proportions().width );
case "touch":
2008-06-04 02:34:33 +00:00
return (
2013-05-24 03:54:17 +00:00
( y1 >= t && y1 <= b ) || // Top edge touching
( y2 >= t && y2 <= b ) || // Bottom edge touching
( y1 < t && y2 > b ) // Surrounded vertically
) && (
2013-05-24 03:54:17 +00:00
( x1 >= l && x1 <= r ) || // Left edge touching
( x2 >= l && x2 <= r ) || // Right edge touching
( x1 < l && x2 > r ) // Surrounded horizontally
);
2008-06-04 02:34:33 +00:00
default:
return false;
}
};
})();
2008-06-04 02:34:33 +00:00
/*
This manager tracks offsets of draggables and droppables
*/
$.ui.ddmanager = {
current: null,
droppables: { "default": [] },
2013-05-24 03:54:17 +00:00
prepareOffsets: function( t, event ) {
var i, j,
2013-05-24 03:54:17 +00:00
m = $.ui.ddmanager.droppables[ t.options.scope ] || [],
type = event ? event.type : null, // workaround for #2317
2013-05-24 03:54:17 +00:00
list = ( t.currentItem || t.element ).find( ":data(ui-droppable)" ).addBack();
2013-05-24 03:54:17 +00:00
droppablesLoop: for ( i = 0; i < m.length; i++ ) {
2013-05-24 03:54:17 +00:00
// No disabled and non-accepted
if ( m[ i ].options.disabled || ( t && !m[ i ].accept.call( m[ i ].element[ 0 ], ( t.currentItem || t.element ) ) ) ) {
continue;
}
// Filter out elements in the current dragged item
2013-10-16 18:43:09 +00:00
for ( j = 0; j < list.length; j++ ) {
2013-05-24 03:54:17 +00:00
if ( list[ j ] === m[ i ].element[ 0 ] ) {
m[ i ].proportions().height = 0;
continue droppablesLoop;
}
}
2013-05-24 03:54:17 +00:00
m[ i ].visible = m[ i ].element.css( "display" ) !== "none";
if ( !m[ i ].visible ) {
continue;
}
2013-05-24 03:54:17 +00:00
// Activate the droppable if used directly from draggables
if ( type === "mousedown" ) {
m[ i ]._activate.call( m[ i ], event );
}
m[ i ].offset = m[ i ].element.offset();
m[ i ].proportions({ width: m[ i ].element[ 0 ].offsetWidth, height: m[ i ].element[ 0 ].offsetHeight });
2008-06-04 02:34:33 +00:00
}
2008-06-04 02:34:33 +00:00
},
2013-05-24 03:54:17 +00:00
drop: function( draggable, event ) {
2008-06-04 02:34:33 +00:00
var dropped = false;
// Create a copy of the droppables in case the list changes during the drop (#9116)
2013-05-24 03:54:17 +00:00
$.each( ( $.ui.ddmanager.droppables[ draggable.options.scope ] || [] ).slice(), function() {
2013-05-24 03:54:17 +00:00
if ( !this.options ) {
return;
}
if ( !this.options.disabled && this.visible && intersect( draggable, this, this.options.tolerance, event ) ) {
2013-05-24 03:54:17 +00:00
dropped = this._drop.call( this, event ) || dropped;
}
2013-05-24 03:54:17 +00:00
if ( !this.options.disabled && this.visible && this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
this.isout = true;
this.isover = false;
2013-05-24 03:54:17 +00:00
this._deactivate.call( this, event );
2008-06-04 02:34:33 +00:00
}
2008-06-04 02:34:33 +00:00
});
return dropped;
2008-06-04 02:34:33 +00:00
},
dragStart: function( draggable, event ) {
2013-05-24 03:54:17 +00:00
// Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
draggable.element.parentsUntil( "body" ).on( "scroll.droppable", function() {
2013-05-24 03:54:17 +00:00
if ( !draggable.options.refreshPositions ) {
$.ui.ddmanager.prepareOffsets( draggable, event );
}
});
},
2013-05-24 03:54:17 +00:00
drag: function( draggable, event ) {
2013-05-24 03:54:17 +00:00
// If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
if ( draggable.options.refreshPositions ) {
$.ui.ddmanager.prepareOffsets( draggable, event );
}
2013-05-24 03:54:17 +00:00
// Run through all droppables and check their positions based on specific tolerance options
$.each( $.ui.ddmanager.droppables[ draggable.options.scope ] || [], function() {
2013-05-24 03:54:17 +00:00
if ( this.options.disabled || this.greedyChild || !this.visible ) {
return;
}
var parentInstance, scope, parent,
intersects = intersect( draggable, this, this.options.tolerance, event ),
2013-05-24 03:54:17 +00:00
c = !intersects && this.isover ? "isout" : ( intersects && !this.isover ? "isover" : null );
if ( !c ) {
return;
}
2013-05-24 03:54:17 +00:00
if ( this.options.greedy ) {
// find droppable parents with same scope
scope = this.options.scope;
2013-10-16 18:43:09 +00:00
parent = this.element.parents( ":data(ui-droppable)" ).filter(function() {
2013-05-24 03:54:17 +00:00
return $( this ).droppable( "instance" ).options.scope === scope;
});
2013-05-24 03:54:17 +00:00
if ( parent.length ) {
parentInstance = $( parent[ 0 ] ).droppable( "instance" );
2013-05-24 03:54:17 +00:00
parentInstance.greedyChild = ( c === "isover" );
2008-06-04 02:34:33 +00:00
}
}
2008-06-04 02:34:33 +00:00
// we just moved into a greedy child
2013-05-24 03:54:17 +00:00
if ( parentInstance && c === "isover" ) {
parentInstance.isover = false;
parentInstance.isout = true;
2013-05-24 03:54:17 +00:00
parentInstance._out.call( parentInstance, event );
2008-06-04 02:34:33 +00:00
}
2013-05-24 03:54:17 +00:00
this[ c ] = true;
this[c === "isout" ? "isover" : "isout"] = false;
2013-05-24 03:54:17 +00:00
this[c === "isover" ? "_over" : "_out"].call( this, event );
2008-06-04 02:34:33 +00:00
// we just moved out of a greedy child
2013-05-24 03:54:17 +00:00
if ( parentInstance && c === "isout" ) {
parentInstance.isout = false;
parentInstance.isover = true;
2013-05-24 03:54:17 +00:00
parentInstance._over.call( parentInstance, event );
2008-06-04 02:34:33 +00:00
}
});
},
dragStop: function( draggable, event ) {
draggable.element.parentsUntil( "body" ).off( "scroll.droppable" );
2013-05-24 03:54:17 +00:00
// Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
if ( !draggable.options.refreshPositions ) {
$.ui.ddmanager.prepareOffsets( draggable, event );
}
2008-06-04 02:34:33 +00:00
}
};
// DEPRECATED
// TODO: switch return back to widget declaration at top of file when this is removed
if ( $.uiBackCompat !== false ) {
// Backcompat for activeClass and hoverClass options
$.widget( "ui.droppable", $.ui.droppable, {
options: {
hoverClass: false,
activeClass: false
},
_addActiveClass: function() {
this._super();
if ( this.options.activeClass ) {
this.element.addClass( this.options.activeClass );
}
},
_removeActiveClass: function() {
this._super();
if ( this.options.activeClass ) {
this.element.removeClass( this.options.activeClass );
}
},
_addHoverClass: function() {
this._super();
if ( this.options.hoverClass ) {
this.element.addClass( this.options.hoverClass );
}
},
_removeHoverClass: function() {
this._super();
if ( this.options.hoverClass ) {
this.element.removeClass( this.options.hoverClass );
}
}
});
}
return $.ui.droppable;
}));