imported the whole jquery-ui package, refreshed with 1.10.2
[myslice.git] / third-party / jquery-ui-1.10.2 / ui / jquery.ui.droppable.js
1 /*!
2  * jQuery UI Droppable 1.10.2
3  * http://jqueryui.com
4  *
5  * Copyright 2013 jQuery Foundation and other contributors
6  * Released under the MIT license.
7  * http://jquery.org/license
8  *
9  * http://api.jqueryui.com/droppable/
10  *
11  * Depends:
12  *      jquery.ui.core.js
13  *      jquery.ui.widget.js
14  *      jquery.ui.mouse.js
15  *      jquery.ui.draggable.js
16  */
17 (function( $, undefined ) {
18
19 function isOverAxis( x, reference, size ) {
20         return ( x > reference ) && ( x < ( reference + size ) );
21 }
22
23 $.widget("ui.droppable", {
24         version: "1.10.2",
25         widgetEventPrefix: "drop",
26         options: {
27                 accept: "*",
28                 activeClass: false,
29                 addClasses: true,
30                 greedy: false,
31                 hoverClass: false,
32                 scope: "default",
33                 tolerance: "intersect",
34
35                 // callbacks
36                 activate: null,
37                 deactivate: null,
38                 drop: null,
39                 out: null,
40                 over: null
41         },
42         _create: function() {
43
44                 var o = this.options,
45                         accept = o.accept;
46
47                 this.isover = false;
48                 this.isout = true;
49
50                 this.accept = $.isFunction(accept) ? accept : function(d) {
51                         return d.is(accept);
52                 };
53
54                 //Store the droppable's proportions
55                 this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
56
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);
60
61                 (o.addClasses && this.element.addClass("ui-droppable"));
62
63         },
64
65         _destroy: function() {
66                 var i = 0,
67                         drop = $.ui.ddmanager.droppables[this.options.scope];
68
69                 for ( ; i < drop.length; i++ ) {
70                         if ( drop[i] === this ) {
71                                 drop.splice(i, 1);
72                         }
73                 }
74
75                 this.element.removeClass("ui-droppable ui-droppable-disabled");
76         },
77
78         _setOption: function(key, value) {
79
80                 if(key === "accept") {
81                         this.accept = $.isFunction(value) ? value : function(d) {
82                                 return d.is(value);
83                         };
84                 }
85                 $.Widget.prototype._setOption.apply(this, arguments);
86         },
87
88         _activate: function(event) {
89                 var draggable = $.ui.ddmanager.current;
90                 if(this.options.activeClass) {
91                         this.element.addClass(this.options.activeClass);
92                 }
93                 if(draggable){
94                         this._trigger("activate", event, this.ui(draggable));
95                 }
96         },
97
98         _deactivate: function(event) {
99                 var draggable = $.ui.ddmanager.current;
100                 if(this.options.activeClass) {
101                         this.element.removeClass(this.options.activeClass);
102                 }
103                 if(draggable){
104                         this._trigger("deactivate", event, this.ui(draggable));
105                 }
106         },
107
108         _over: function(event) {
109
110                 var draggable = $.ui.ddmanager.current;
111
112                 // Bail if draggable and droppable are same element
113                 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
114                         return;
115                 }
116
117                 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
118                         if(this.options.hoverClass) {
119                                 this.element.addClass(this.options.hoverClass);
120                         }
121                         this._trigger("over", event, this.ui(draggable));
122                 }
123
124         },
125
126         _out: function(event) {
127
128                 var draggable = $.ui.ddmanager.current;
129
130                 // Bail if draggable and droppable are same element
131                 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
132                         return;
133                 }
134
135                 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
136                         if(this.options.hoverClass) {
137                                 this.element.removeClass(this.options.hoverClass);
138                         }
139                         this._trigger("out", event, this.ui(draggable));
140                 }
141
142         },
143
144         _drop: function(event,custom) {
145
146                 var draggable = custom || $.ui.ddmanager.current,
147                         childrenIntersection = false;
148
149                 // Bail if draggable and droppable are same element
150                 if (!draggable || (draggable.currentItem || draggable.element)[0] === this.element[0]) {
151                         return false;
152                 }
153
154                 this.element.find(":data(ui-droppable)").not(".ui-draggable-dragging").each(function() {
155                         var inst = $.data(this, "ui-droppable");
156                         if(
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; }
163                 });
164                 if(childrenIntersection) {
165                         return false;
166                 }
167
168                 if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
169                         if(this.options.activeClass) {
170                                 this.element.removeClass(this.options.activeClass);
171                         }
172                         if(this.options.hoverClass) {
173                                 this.element.removeClass(this.options.hoverClass);
174                         }
175                         this._trigger("drop", event, this.ui(draggable));
176                         return this.element;
177                 }
178
179                 return false;
180
181         },
182
183         ui: function(c) {
184                 return {
185                         draggable: (c.currentItem || c.element),
186                         helper: c.helper,
187                         position: c.position,
188                         offset: c.positionAbs
189                 };
190         }
191
192 });
193
194 $.ui.intersect = function(draggable, droppable, toleranceMode) {
195
196         if (!droppable.offset) {
197                 return false;
198         }
199
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;
205
206         switch (toleranceMode) {
207                 case "fit":
208                         return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
209                 case "intersect":
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
214                 case "pointer":
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 );
218                 case "touch":
219                         return (
220                                 (y1 >= t && y1 <= b) || // Top edge touching
221                                 (y2 >= t && y2 <= b) || // Bottom edge touching
222                                 (y1 < t && y2 > b)              // Surrounded vertically
223                         ) && (
224                                 (x1 >= l && x1 <= r) || // Left edge touching
225                                 (x2 >= l && x2 <= r) || // Right edge touching
226                                 (x1 < l && x2 > r)              // Surrounded horizontally
227                         );
228                 default:
229                         return false;
230                 }
231
232 };
233
234 /*
235         This manager tracks offsets of draggables and droppables
236 */
237 $.ui.ddmanager = {
238         current: null,
239         droppables: { "default": [] },
240         prepareOffsets: function(t, event) {
241
242                 var i, j,
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();
246
247                 droppablesLoop: for (i = 0; i < m.length; i++) {
248
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)))) {
251                                 continue;
252                         }
253
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;
259                                 }
260                         }
261
262                         m[i].visible = m[i].element.css("display") !== "none";
263                         if(!m[i].visible) {
264                                 continue;
265                         }
266
267                         //Activate the droppable if used directly from draggables
268                         if(type === "mousedown") {
269                                 m[i]._activate.call(m[i], event);
270                         }
271
272                         m[i].offset = m[i].element.offset();
273                         m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight };
274
275                 }
276
277         },
278         drop: function(draggable, event) {
279
280                 var dropped = false;
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() {
283
284                         if(!this.options) {
285                                 return;
286                         }
287                         if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance)) {
288                                 dropped = this._drop.call(this, event) || dropped;
289                         }
290
291                         if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
292                                 this.isout = true;
293                                 this.isover = false;
294                                 this._deactivate.call(this, event);
295                         }
296
297                 });
298                 return dropped;
299
300         },
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 );
306                         }
307                 });
308         },
309         drag: function(draggable, event) {
310
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);
314                 }
315
316                 //Run through all droppables and check their positions based on specific tolerance options
317                 $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
318
319                         if(this.options.disabled || this.greedyChild || !this.visible) {
320                                 return;
321                         }
322
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);
326                         if(!c) {
327                                 return;
328                         }
329
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;
335                                 });
336
337                                 if (parent.length) {
338                                         parentInstance = $.data(parent[0], "ui-droppable");
339                                         parentInstance.greedyChild = (c === "isover");
340                                 }
341                         }
342
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);
348                         }
349
350                         this[c] = true;
351                         this[c === "isout" ? "isover" : "isout"] = false;
352                         this[c === "isover" ? "_over" : "_out"].call(this, event);
353
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);
359                         }
360                 });
361
362         },
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 );
368                 }
369         }
370 };
371
372 })(jQuery);