jquery.ui.droppable.js 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*!
  2. * jQuery UI Droppable v1.9 stable
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2012 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. $.widget("ui.droppable", {
  19. version: "@VERSION",
  20. widgetEventPrefix: "drop",
  21. options: {
  22. accept: '*',
  23. activeClass: false,
  24. addClasses: true,
  25. greedy: false,
  26. hoverClass: false,
  27. scope: 'default',
  28. tolerance: 'intersect'
  29. },
  30. _create: function() {
  31. var o = this.options, accept = o.accept;
  32. this.isover = 0; this.isout = 1;
  33. this.accept = $.isFunction(accept) ? accept : function(d) {
  34. return d.is(accept);
  35. };
  36. //Store the droppable's proportions
  37. this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
  38. // Add the reference and positions to the manager
  39. $.ui.ddmanager.droppables[o.scope] = $.ui.ddmanager.droppables[o.scope] || [];
  40. $.ui.ddmanager.droppables[o.scope].push(this);
  41. (o.addClasses && this.element.addClass("ui-droppable"));
  42. },
  43. _destroy: function() {
  44. var drop = $.ui.ddmanager.droppables[this.options.scope];
  45. for ( var i = 0; i < drop.length; i++ )
  46. if ( drop[i] == this )
  47. drop.splice(i, 1);
  48. this.element.removeClass("ui-droppable ui-droppable-disabled");
  49. },
  50. _setOption: function(key, value) {
  51. if(key == 'accept') {
  52. this.accept = $.isFunction(value) ? value : function(d) {
  53. return d.is(value);
  54. };
  55. }
  56. $.Widget.prototype._setOption.apply(this, arguments);
  57. },
  58. _activate: function(event) {
  59. var draggable = $.ui.ddmanager.current;
  60. if(this.options.activeClass) this.element.addClass(this.options.activeClass);
  61. (draggable && this._trigger('activate', event, this.ui(draggable)));
  62. },
  63. _deactivate: function(event) {
  64. var draggable = $.ui.ddmanager.current;
  65. if(this.options.activeClass) this.element.removeClass(this.options.activeClass);
  66. (draggable && this._trigger('deactivate', event, this.ui(draggable)));
  67. },
  68. _over: function(event) {
  69. var draggable = $.ui.ddmanager.current;
  70. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
  71. if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  72. if(this.options.hoverClass) this.element.addClass(this.options.hoverClass);
  73. this._trigger('over', event, this.ui(draggable));
  74. }
  75. },
  76. _out: function(event) {
  77. var draggable = $.ui.ddmanager.current;
  78. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
  79. if (this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  80. if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass);
  81. this._trigger('out', event, this.ui(draggable));
  82. }
  83. },
  84. _drop: function(event,custom) {
  85. var draggable = custom || $.ui.ddmanager.current;
  86. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return false; // Bail if draggable and droppable are same element
  87. var childrenIntersection = false;
  88. this.element.find(":data(droppable)").not(".ui-draggable-dragging").each(function() {
  89. var inst = $.data(this, 'droppable');
  90. if(
  91. inst.options.greedy
  92. && !inst.options.disabled
  93. && inst.options.scope == draggable.options.scope
  94. && inst.accept.call(inst.element[0], (draggable.currentItem || draggable.element))
  95. && $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance)
  96. ) { childrenIntersection = true; return false; }
  97. });
  98. if(childrenIntersection) return false;
  99. if(this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  100. if(this.options.activeClass) this.element.removeClass(this.options.activeClass);
  101. if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass);
  102. this._trigger('drop', event, this.ui(draggable));
  103. return this.element;
  104. }
  105. return false;
  106. },
  107. ui: function(c) {
  108. return {
  109. draggable: (c.currentItem || c.element),
  110. helper: c.helper,
  111. position: c.position,
  112. offset: c.positionAbs
  113. };
  114. }
  115. });
  116. $.ui.intersect = function(draggable, droppable, toleranceMode) {
  117. if (!droppable.offset) return false;
  118. var x1 = (draggable.positionAbs || draggable.position.absolute).left, x2 = x1 + draggable.helperProportions.width,
  119. y1 = (draggable.positionAbs || draggable.position.absolute).top, y2 = y1 + draggable.helperProportions.height;
  120. var l = droppable.offset.left, r = l + droppable.proportions.width,
  121. t = droppable.offset.top, b = t + droppable.proportions.height;
  122. switch (toleranceMode) {
  123. case 'fit':
  124. return (l <= x1 && x2 <= r
  125. && t <= y1 && y2 <= b);
  126. break;
  127. case 'intersect':
  128. return (l < x1 + (draggable.helperProportions.width / 2) // Right Half
  129. && x2 - (draggable.helperProportions.width / 2) < r // Left Half
  130. && t < y1 + (draggable.helperProportions.height / 2) // Bottom Half
  131. && y2 - (draggable.helperProportions.height / 2) < b ); // Top Half
  132. break;
  133. case 'pointer':
  134. var draggableLeft = ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left),
  135. draggableTop = ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top),
  136. isOver = $.ui.isOver(draggableTop, draggableLeft, t, l, droppable.proportions.height, droppable.proportions.width);
  137. return isOver;
  138. break;
  139. case 'touch':
  140. return (
  141. (y1 >= t && y1 <= b) || // Top edge touching
  142. (y2 >= t && y2 <= b) || // Bottom edge touching
  143. (y1 < t && y2 > b) // Surrounded vertically
  144. ) && (
  145. (x1 >= l && x1 <= r) || // Left edge touching
  146. (x2 >= l && x2 <= r) || // Right edge touching
  147. (x1 < l && x2 > r) // Surrounded horizontally
  148. );
  149. break;
  150. default:
  151. return false;
  152. break;
  153. }
  154. };
  155. /*
  156. This manager tracks offsets of draggables and droppables
  157. */
  158. $.ui.ddmanager = {
  159. current: null,
  160. droppables: { 'default': [] },
  161. prepareOffsets: function(t, event) {
  162. var m = $.ui.ddmanager.droppables[t.options.scope] || [];
  163. var type = event ? event.type : null; // workaround for #2317
  164. var list = (t.currentItem || t.element).find(":data(droppable)").andSelf();
  165. droppablesLoop: for (var i = 0; i < m.length; i++) {
  166. if(m[i].options.disabled || (t && !m[i].accept.call(m[i].element[0],(t.currentItem || t.element)))) continue; //No disabled and non-accepted
  167. for (var j=0; j < list.length; j++) { if(list[j] == m[i].element[0]) { m[i].proportions.height = 0; continue droppablesLoop; } }; //Filter out elements in the current dragged item
  168. m[i].visible = m[i].element.css("display") != "none"; if(!m[i].visible) continue; //If the element is not visible, continue
  169. if(type == "mousedown") m[i]._activate.call(m[i], event); //Activate the droppable if used directly from draggables
  170. m[i].offset = m[i].element.offset();
  171. m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight };
  172. }
  173. },
  174. drop: function(draggable, event) {
  175. var dropped = false;
  176. $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
  177. if(!this.options) return;
  178. if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance))
  179. dropped = this._drop.call(this, event) || dropped;
  180. if (!this.options.disabled && this.visible && this.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  181. this.isout = 1; this.isover = 0;
  182. this._deactivate.call(this, event);
  183. }
  184. });
  185. return dropped;
  186. },
  187. dragStart: function( draggable, event ) {
  188. //Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
  189. draggable.element.parentsUntil( "body" ).bind( "scroll.droppable", function() {
  190. if( !draggable.options.refreshPositions ) $.ui.ddmanager.prepareOffsets( draggable, event );
  191. });
  192. },
  193. drag: function(draggable, event) {
  194. //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
  195. if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, event);
  196. //Run through all droppables and check their positions based on specific tolerance options
  197. $.each($.ui.ddmanager.droppables[draggable.options.scope] || [], function() {
  198. if(this.options.disabled || this.greedyChild || !this.visible) return;
  199. var intersects = $.ui.intersect(draggable, this, this.options.tolerance);
  200. var c = !intersects && this.isover == 1 ? 'isout' : (intersects && this.isover == 0 ? 'isover' : null);
  201. if(!c) return;
  202. var parentInstance;
  203. if (this.options.greedy) {
  204. // find droppable parents with same scope
  205. var scope = this.options.scope;
  206. var parent = this.element.parents(':data(droppable)').filter(function () {
  207. return $.data(this, 'droppable').options.scope === scope;
  208. });
  209. if (parent.length) {
  210. parentInstance = $.data(parent[0], 'droppable');
  211. parentInstance.greedyChild = (c == 'isover' ? 1 : 0);
  212. }
  213. }
  214. // we just moved into a greedy child
  215. if (parentInstance && c == 'isover') {
  216. parentInstance['isover'] = 0;
  217. parentInstance['isout'] = 1;
  218. parentInstance._out.call(parentInstance, event);
  219. }
  220. this[c] = 1; this[c == 'isout' ? 'isover' : 'isout'] = 0;
  221. this[c == "isover" ? "_over" : "_out"].call(this, event);
  222. // we just moved out of a greedy child
  223. if (parentInstance && c == 'isout') {
  224. parentInstance['isout'] = 0;
  225. parentInstance['isover'] = 1;
  226. parentInstance._over.call(parentInstance, event);
  227. }
  228. });
  229. },
  230. dragStop: function( draggable, event ) {
  231. draggable.element.parentsUntil( "body" ).unbind( "scroll.droppable" );
  232. //Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
  233. if( !draggable.options.refreshPositions ) $.ui.ddmanager.prepareOffsets( draggable, event );
  234. }
  235. };
  236. })(jQuery);