2 * jQuery UI Droppable 1.10.2
5 * Copyright 2013 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",
50 this.accept = $.isFunction(accept) ? accept : function(d) {
54 //Store the droppable's proportions
55 this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
57 // Add the reference and positions to the manager
58 $.ui.ddmanager.droppables[o.scope] = $.ui.ddmanager.droppables[o.scope] || [];
59 $.ui.ddmanager.droppables[o.scope].push(this);
61 (o.addClasses && this.element.addClass("ui-droppable"));
65 _destroy: function() {
67 drop = $.ui.ddmanager.droppables[this.options.scope];
69 for ( ; i < drop.length; i++ ) {
70 if ( drop[i] === this ) {
75 this.element.removeClass("ui-droppable ui-droppable-disabled");
78 _setOption: function(key, value) {
80 if(key === "accept") {
81 this.accept = $.isFunction(value) ? value : function(d) {
85 $.Widget.prototype._setOption.apply(this, arguments);
88 _activate: function(event) {
89 var draggable = $.ui.ddmanager.current;
90 if(this.options.activeClass) {
91 this.element.addClass(this.options.activeClass);
94 this._trigger("activate", event, this.ui(draggable));
98 _deactivate: function(event) {
99 var draggable = $.ui.ddmanager.current;
100 if(this.options.activeClass) {
101 this.element.removeClass(this.options.activeClass);
104 this._trigger("deactivate", event, this.ui(draggable));
108 _over: function(event) {
110 var draggable = $.ui.ddmanager.current;
112 // Bail if draggable and droppable are same element
113 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
117 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
118 if(this.options.hoverClass) {
119 this.element.addClass(this.options.hoverClass);
121 this._trigger("over", event, this.ui(draggable));
126 _out: function(event) {
128 var draggable = $.ui.ddmanager.current;
130 // Bail if draggable and droppable are same element
131 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
135 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
136 if(this.options.hoverClass) {
137 this.element.removeClass(this.options.hoverClass);
139 this._trigger("out", event, this.ui(draggable));
144 _drop: function(event,custom) {
146 var draggable = custom || $.ui.ddmanager.current,
147 childrenIntersection = false;
149 // Bail if draggable and droppable are same element
150 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
154 this.element.find(":data(ui-droppable)").not(".ui-draggable-dragging").each(function() {
155 var inst = $.data(this, "ui-droppable");
157 inst.options.greedy &&
158 !inst.options.disabled &&
159 inst.options.scope === draggable.options.scope &&
160 inst.accept.call(inst.element[0], (draggable.currentItem || draggable.element)) &&
161 $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance)
162 ) { childrenIntersection = true; return false; }
164 if(childrenIntersection) {
168 if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
169 if(this.options.activeClass) {
170 this.element.removeClass(this.options.activeClass);
172 if(this.options.hoverClass) {
173 this.element.removeClass(this.options.hoverClass);
175 this._trigger("drop", event, this.ui(draggable));
185 draggable: (c.currentItem || c.element),
187 position: c.position,
188 offset: c.positionAbs
194 $.ui.intersect = function(draggable, droppable, toleranceMode) {
196 if (!droppable.offset) {
200 var draggableLeft, draggableTop,
201 x1 = (draggable.positionAbs || draggable.position.absolute).left, x2 = x1 + draggable.helperProportions.width,
202 y1 = (draggable.positionAbs || draggable.position.absolute).top, y2 = y1 + draggable.helperProportions.height,
203 l = droppable.offset.left, r = l + droppable.proportions.width,
204 t = droppable.offset.top, b = t + droppable.proportions.height;
206 switch (toleranceMode) {
208 return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
210 return (l < x1 + (draggable.helperProportions.width / 2) && // Right Half
211 x2 - (draggable.helperProportions.width / 2) < r && // Left Half
212 t < y1 + (draggable.helperProportions.height / 2) && // Bottom Half
213 y2 - (draggable.helperProportions.height / 2) < b ); // Top Half
215 draggableLeft = ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left);
216 draggableTop = ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top);
217 return isOverAxis( draggableTop, t, droppable.proportions.height ) && isOverAxis( draggableLeft, l, droppable.proportions.width );
220 (y1 >= t && y1 <= b) || // Top edge touching
221 (y2 >= t && y2 <= b) || // Bottom edge touching
222 (y1 < t && y2 > b) // Surrounded vertically
224 (x1 >= l && x1 <= r) || // Left edge touching
225 (x2 >= l && x2 <= r) || // Right edge touching
226 (x1 < l && x2 > r) // Surrounded horizontally
235 This manager tracks offsets of draggables and droppables
239 droppables: { "default": [] },
240 prepareOffsets: function(t, event) {
243 m = $.ui.ddmanager.droppables[t.options.scope] || [],
244 type = event ? event.type : null, // workaround for #2317
245 list = (t.currentItem || t.element).find(":data(ui-droppable)").addBack();
247 droppablesLoop: for (i = 0; i < m.length; i++) {
249 //No disabled and non-accepted
250 if(m[i].options.disabled || (t && !m[i].accept.call(m[i].element[0],(t.currentItem || t.element)))) {
254 // Filter out elements in the current dragged item
255 for (j=0; j < list.length; j++) {
256 if(list[j] === m[i].element[0]) {
257 m[i].proportions.height = 0;
258 continue droppablesLoop;
262 m[i].visible = m[i].element.css("display") !== "none";
267 //Activate the droppable if used directly from draggables
268 if(type === "mousedown") {
269 m[i]._activate.call(m[i], event);
272 m[i].offset = m[i].element.offset();
273 m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight };
278 drop: function(draggable, event) {
281 // Create a copy of the droppables in case the list changes during the drop (#9116)
282 $.each(($.ui.ddmanager.droppables[draggable.options.scope] || []).slice(), function() {
287 if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance)) {
288 dropped = this._drop.call(this, event) || dropped;
291 if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
294 this._deactivate.call(this, event);
301 dragStart: function( draggable, event ) {
302 //Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
303 draggable.element.parentsUntil( "body" ).bind( "scroll.droppable", function() {
304 if( !draggable.options.refreshPositions ) {
305 $.ui.ddmanager.prepareOffsets( draggable, event );
309 drag: function(draggable, event) {
311 //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
312 if(draggable.options.refreshPositions) {
313 $.ui.ddmanager.prepareOffsets(draggable, event);
316 //Run through all droppables and check their positions based on specific tolerance options
317 $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
319 if(this.options.disabled || this.greedyChild || !this.visible) {
323 var parentInstance, scope, parent,
324 intersects = $.ui.intersect(draggable, this, this.options.tolerance),
325 c = !intersects && this.isover ? "isout" : (intersects && !this.isover ? "isover" : null);
330 if (this.options.greedy) {
331 // find droppable parents with same scope
332 scope = this.options.scope;
333 parent = this.element.parents(":data(ui-droppable)").filter(function () {
334 return $.data(this, "ui-droppable").options.scope === scope;
338 parentInstance = $.data(parent[0], "ui-droppable");
339 parentInstance.greedyChild = (c === "isover");
343 // we just moved into a greedy child
344 if (parentInstance && c === "isover") {
345 parentInstance.isover = false;
346 parentInstance.isout = true;
347 parentInstance._out.call(parentInstance, event);
351 this[c === "isout" ? "isover" : "isout"] = false;
352 this[c === "isover" ? "_over" : "_out"].call(this, event);
354 // we just moved out of a greedy child
355 if (parentInstance && c === "isout") {
356 parentInstance.isout = false;
357 parentInstance.isover = true;
358 parentInstance._over.call(parentInstance, event);
363 dragStop: function( draggable, event ) {
364 draggable.element.parentsUntil( "body" ).unbind( "scroll.droppable" );
365 //Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
366 if( !draggable.options.refreshPositions ) {
367 $.ui.ddmanager.prepareOffsets( draggable, event );