Brought in following assets that Kevin's EDI project will be using:
[openemr.git] / public / assets / jquery-ui-1-10-4 / ui / jquery.ui.droppable.js
blob762d5498cf446638a523ab37a5be6b4658f10ec9
1 /*!
2  * jQuery UI Droppable 1.10.4
3  * http://jqueryui.com
4  *
5  * Copyright 2014 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 ) {
19 function isOverAxis( x, reference, size ) {
20         return ( x > reference ) && ( x < ( reference + size ) );
23 $.widget("ui.droppable", {
24         version: "1.10.4",
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",
35                 // callbacks
36                 activate: null,
37                 deactivate: null,
38                 drop: null,
39                 out: null,
40                 over: null
41         },
42         _create: function() {
44                 var proportions,
45                         o = this.options,
46                         accept = o.accept;
48                 this.isover = false;
49                 this.isout = true;
51                 this.accept = $.isFunction(accept) ? accept : function(d) {
52                         return d.is(accept);
53                 };
55                 this.proportions = function( /* valueToWrite */ ) {
56                         if ( arguments.length ) {
57                                 // Store the droppable's proportions
58                                 proportions = arguments[ 0 ];
59                         } else {
60                                 // Retrieve or derive the droppable's proportions
61                                 return proportions ?
62                                         proportions :
63                                         proportions = {
64                                                 width: this.element[ 0 ].offsetWidth,
65                                                 height: this.element[ 0 ].offsetHeight
66                                         };
67                         }
68                 };
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"));
76         },
78         _destroy: function() {
79                 var i = 0,
80                         drop = $.ui.ddmanager.droppables[this.options.scope];
82                 for ( ; i < drop.length; i++ ) {
83                         if ( drop[i] === this ) {
84                                 drop.splice(i, 1);
85                         }
86                 }
88                 this.element.removeClass("ui-droppable ui-droppable-disabled");
89         },
91         _setOption: function(key, value) {
93                 if(key === "accept") {
94                         this.accept = $.isFunction(value) ? value : function(d) {
95                                 return d.is(value);
96                         };
97                 }
98                 $.Widget.prototype._setOption.apply(this, arguments);
99         },
101         _activate: function(event) {
102                 var draggable = $.ui.ddmanager.current;
103                 if(this.options.activeClass) {
104                         this.element.addClass(this.options.activeClass);
105                 }
106                 if(draggable){
107                         this._trigger("activate", event, this.ui(draggable));
108                 }
109         },
111         _deactivate: function(event) {
112                 var draggable = $.ui.ddmanager.current;
113                 if(this.options.activeClass) {
114                         this.element.removeClass(this.options.activeClass);
115                 }
116                 if(draggable){
117                         this._trigger("deactivate", event, this.ui(draggable));
118                 }
119         },
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]) {
127                         return;
128                 }
130                 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
131                         if(this.options.hoverClass) {
132                                 this.element.addClass(this.options.hoverClass);
133                         }
134                         this._trigger("over", event, this.ui(draggable));
135                 }
137         },
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]) {
145                         return;
146                 }
148                 if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
149                         if(this.options.hoverClass) {
150                                 this.element.removeClass(this.options.hoverClass);
151                         }
152                         this._trigger("out", event, this.ui(draggable));
153                 }
155         },
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]) {
164                         return false;
165                 }
167                 this.element.find(":data(ui-droppable)").not(".ui-draggable-dragging").each(function() {
168                         var inst = $.data(this, "ui-droppable");
169                         if(
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; }
176                 });
177                 if(childrenIntersection) {
178                         return false;
179                 }
181                 if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
182                         if(this.options.activeClass) {
183                                 this.element.removeClass(this.options.activeClass);
184                         }
185                         if(this.options.hoverClass) {
186                                 this.element.removeClass(this.options.hoverClass);
187                         }
188                         this._trigger("drop", event, this.ui(draggable));
189                         return this.element;
190                 }
192                 return false;
194         },
196         ui: function(c) {
197                 return {
198                         draggable: (c.currentItem || c.element),
199                         helper: c.helper,
200                         position: c.position,
201                         offset: c.positionAbs
202                 };
203         }
207 $.ui.intersect = function(draggable, droppable, toleranceMode) {
209         if (!droppable.offset) {
210                 return false;
211         }
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) {
224                 case "fit":
225                         return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
226                 case "intersect":
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
231                 case "pointer":
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 );
235                 case "touch":
236                         return (
237                                 (y1 >= t && y1 <= b) || // Top edge touching
238                                 (y2 >= t && y2 <= b) || // Bottom edge touching
239                                 (y1 < t && y2 > b)              // Surrounded vertically
240                         ) && (
241                                 (x1 >= l && x1 <= r) || // Left edge touching
242                                 (x2 >= l && x2 <= r) || // Right edge touching
243                                 (x1 < l && x2 > r)              // Surrounded horizontally
244                         );
245                 default:
246                         return false;
247                 }
252         This manager tracks offsets of draggables and droppables
254 $.ui.ddmanager = {
255         current: null,
256         droppables: { "default": [] },
257         prepareOffsets: function(t, event) {
259                 var i, j,
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)))) {
268                                 continue;
269                         }
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;
276                                 }
277                         }
279                         m[i].visible = m[i].element.css("display") !== "none";
280                         if(!m[i].visible) {
281                                 continue;
282                         }
284                         //Activate the droppable if used directly from draggables
285                         if(type === "mousedown") {
286                                 m[i]._activate.call(m[i], event);
287                         }
289                         m[ i ].offset = m[ i ].element.offset();
290                         m[ i ].proportions({ width: m[ i ].element[ 0 ].offsetWidth, height: m[ i ].element[ 0 ].offsetHeight });
292                 }
294         },
295         drop: function(draggable, event) {
297                 var dropped = false;
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() {
301                         if(!this.options) {
302                                 return;
303                         }
304                         if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance)) {
305                                 dropped = this._drop.call(this, event) || dropped;
306                         }
308                         if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
309                                 this.isout = true;
310                                 this.isover = false;
311                                 this._deactivate.call(this, event);
312                         }
314                 });
315                 return dropped;
317         },
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 );
323                         }
324                 });
325         },
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);
331                 }
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) {
337                                 return;
338                         }
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);
343                         if(!c) {
344                                 return;
345                         }
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;
352                                 });
354                                 if (parent.length) {
355                                         parentInstance = $.data(parent[0], "ui-droppable");
356                                         parentInstance.greedyChild = (c === "isover");
357                                 }
358                         }
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);
365                         }
367                         this[c] = true;
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);
376                         }
377                 });
379         },
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 );
385                 }
386         }
389 })(jQuery);