2 * jQuery UI Droppable 1.10.4
5 * Copyright 2014 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/droppable/
15 * jquery.ui.draggable.js
17 (function( $, undefined ) {
19 function isOverAxis( x, reference, size ) {
20 return ( x > reference ) && ( x < ( reference + size ) );
23 $.widget("ui.droppable", {
25 widgetEventPrefix: "drop",
33 tolerance: "intersect",
51 this.accept = $.isFunction(accept) ? accept : function(d) {
55 this.proportions = function( /* valueToWrite */ ) {
56 if ( arguments.length ) {
57 // Store the droppable's proportions
58 proportions = arguments[ 0 ];
60 // Retrieve or derive the droppable's proportions
64 width: this.element[ 0 ].offsetWidth,
65 height: this.element[ 0 ].offsetHeight
70 // Add the reference and positions to the manager
71 $.ui.ddmanager.droppables[o.scope] = $.ui.ddmanager.droppables[o.scope] || [];
72 $.ui.ddmanager.droppables[o.scope].push(this);
74 (o.addClasses && this.element.addClass("ui-droppable"));
78 _destroy: function() {
80 drop = $.ui.ddmanager.droppables[this.options.scope];
82 for ( ; i < drop.length; i++ ) {
83 if ( drop[i] === this ) {
88 this.element.removeClass("ui-droppable ui-droppable-disabled");
91 _setOption: function(key, value) {
93 if(key === "accept") {
94 this.accept = $.isFunction(value) ? value : function(d) {
98 $.Widget.prototype._setOption.apply(this, arguments);
101 _activate: function(event) {
102 var draggable = $.ui.ddmanager.current;
103 if(this.options.activeClass) {
104 this.element.addClass(this.options.activeClass);
107 this._trigger("activate", event, this.ui(draggable));
111 _deactivate: function(event) {
112 var draggable = $.ui.ddmanager.current;
113 if(this.options.activeClass) {
114 this.element.removeClass(this.options.activeClass);
117 this._trigger("deactivate", event, this.ui(draggable));
121 _over: function(event) {
123 var draggable = $.ui.ddmanager.current;
125 // Bail if draggable and droppable are same element
126 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
130 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
131 if(this.options.hoverClass) {
132 this.element.addClass(this.options.hoverClass);
134 this._trigger("over", event, this.ui(draggable));
139 _out: function(event) {
141 var draggable = $.ui.ddmanager.current;
143 // Bail if draggable and droppable are same element
144 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
148 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
149 if(this.options.hoverClass) {
150 this.element.removeClass(this.options.hoverClass);
152 this._trigger("out", event, this.ui(draggable));
157 _drop: function(event,custom) {
159 var draggable = custom || $.ui.ddmanager.current,
160 childrenIntersection = false;
162 // Bail if draggable and droppable are same element
163 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
167 this.element.find(":data(ui-droppable)").not(".ui-draggable-dragging").each(function() {
168 var inst = $.data(this, "ui-droppable");
170 inst.options.greedy &&
171 !inst.options.disabled &&
172 inst.options.scope === draggable.options.scope &&
173 inst.accept.call(inst.element[0], (draggable.currentItem || draggable.element)) &&
174 $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance)
175 ) { childrenIntersection = true; return false; }
177 if(childrenIntersection) {
181 if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
182 if(this.options.activeClass) {
183 this.element.removeClass(this.options.activeClass);
185 if(this.options.hoverClass) {
186 this.element.removeClass(this.options.hoverClass);
188 this._trigger("drop", event, this.ui(draggable));
198 draggable: (c.currentItem || c.element),
200 position: c.position,
201 offset: c.positionAbs
207 $.ui.intersect = function(draggable, droppable, toleranceMode) {
209 if (!droppable.offset) {
213 var draggableLeft, draggableTop,
214 x1 = (draggable.positionAbs || draggable.position.absolute).left,
215 y1 = (draggable.positionAbs || draggable.position.absolute).top,
216 x2 = x1 + draggable.helperProportions.width,
217 y2 = y1 + draggable.helperProportions.height,
218 l = droppable.offset.left,
219 t = droppable.offset.top,
220 r = l + droppable.proportions().width,
221 b = t + droppable.proportions().height;
223 switch (toleranceMode) {
225 return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
227 return (l < x1 + (draggable.helperProportions.width / 2) && // Right Half
228 x2 - (draggable.helperProportions.width / 2) < r && // Left Half
229 t < y1 + (draggable.helperProportions.height / 2) && // Bottom Half
230 y2 - (draggable.helperProportions.height / 2) < b ); // Top Half
232 draggableLeft = ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left);
233 draggableTop = ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top);
234 return isOverAxis( draggableTop, t, droppable.proportions().height ) && isOverAxis( draggableLeft, l, droppable.proportions().width );
237 (y1 >= t && y1 <= b) || // Top edge touching
238 (y2 >= t && y2 <= b) || // Bottom edge touching
239 (y1 < t && y2 > b) // Surrounded vertically
241 (x1 >= l && x1 <= r) || // Left edge touching
242 (x2 >= l && x2 <= r) || // Right edge touching
243 (x1 < l && x2 > r) // Surrounded horizontally
252 This manager tracks offsets of draggables and droppables
256 droppables: { "default": [] },
257 prepareOffsets: function(t, event) {
260 m = $.ui.ddmanager.droppables[t.options.scope] || [],
261 type = event ? event.type : null, // workaround for #2317
262 list = (t.currentItem || t.element).find(":data(ui-droppable)").addBack();
264 droppablesLoop: for (i = 0; i < m.length; i++) {
266 //No disabled and non-accepted
267 if(m[i].options.disabled || (t && !m[i].accept.call(m[i].element[0],(t.currentItem || t.element)))) {
271 // Filter out elements in the current dragged item
272 for (j=0; j < list.length; j++) {
273 if(list[j] === m[i].element[0]) {
274 m[i].proportions().height = 0;
275 continue droppablesLoop;
279 m[i].visible = m[i].element.css("display") !== "none";
284 //Activate the droppable if used directly from draggables
285 if(type === "mousedown") {
286 m[i]._activate.call(m[i], event);
289 m[ i ].offset = m[ i ].element.offset();
290 m[ i ].proportions({ width: m[ i ].element[ 0 ].offsetWidth, height: m[ i ].element[ 0 ].offsetHeight });
295 drop: function(draggable, event) {
298 // Create a copy of the droppables in case the list changes during the drop (#9116)
299 $.each(($.ui.ddmanager.droppables[draggable.options.scope] || []).slice(), function() {
304 if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance)) {
305 dropped = this._drop.call(this, event) || dropped;
308 if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
311 this._deactivate.call(this, event);
318 dragStart: function( draggable, event ) {
319 //Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
320 draggable.element.parentsUntil( "body" ).bind( "scroll.droppable", function() {
321 if( !draggable.options.refreshPositions ) {
322 $.ui.ddmanager.prepareOffsets( draggable, event );
326 drag: function(draggable, event) {
328 //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
329 if(draggable.options.refreshPositions) {
330 $.ui.ddmanager.prepareOffsets(draggable, event);
333 //Run through all droppables and check their positions based on specific tolerance options
334 $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
336 if(this.options.disabled || this.greedyChild || !this.visible) {
340 var parentInstance, scope, parent,
341 intersects = $.ui.intersect(draggable, this, this.options.tolerance),
342 c = !intersects && this.isover ? "isout" : (intersects && !this.isover ? "isover" : null);
347 if (this.options.greedy) {
348 // find droppable parents with same scope
349 scope = this.options.scope;
350 parent = this.element.parents(":data(ui-droppable)").filter(function () {
351 return $.data(this, "ui-droppable").options.scope === scope;
355 parentInstance = $.data(parent[0], "ui-droppable");
356 parentInstance.greedyChild = (c === "isover");
360 // we just moved into a greedy child
361 if (parentInstance && c === "isover") {
362 parentInstance.isover = false;
363 parentInstance.isout = true;
364 parentInstance._out.call(parentInstance, event);
368 this[c === "isout" ? "isover" : "isout"] = false;
369 this[c === "isover" ? "_over" : "_out"].call(this, event);
371 // we just moved out of a greedy child
372 if (parentInstance && c === "isout") {
373 parentInstance.isout = false;
374 parentInstance.isover = true;
375 parentInstance._over.call(parentInstance, event);
380 dragStop: function( draggable, event ) {
381 draggable.element.parentsUntil( "body" ).unbind( "scroll.droppable" );
382 //Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
383 if( !draggable.options.refreshPositions ) {
384 $.ui.ddmanager.prepareOffsets( draggable, event );