ui.droppable.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * jQuery UI Droppable 1.7.2
  3. *
  4. * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
  5. * Dual licensed under the MIT (MIT-LICENSE.txt)
  6. * and GPL (GPL-LICENSE.txt) licenses.
  7. *
  8. * http://docs.jquery.com/UI/Droppables
  9. *
  10. * Depends:
  11. * ui.core.js
  12. * ui.draggable.js
  13. */
  14. (function($) {
  15. $.widget("ui.droppable", {
  16. _init: function() {
  17. var o = this.options, accept = o.accept;
  18. this.isover = 0; this.isout = 1;
  19. this.options.accept = this.options.accept && $.isFunction(this.options.accept) ? this.options.accept : function(d) {
  20. return d.is(accept);
  21. };
  22. //Store the droppable's proportions
  23. this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
  24. // Add the reference and positions to the manager
  25. $.ui.ddmanager.droppables[this.options.scope] = $.ui.ddmanager.droppables[this.options.scope] || [];
  26. $.ui.ddmanager.droppables[this.options.scope].push(this);
  27. (this.options.addClasses && this.element.addClass("ui-droppable"));
  28. },
  29. destroy: function() {
  30. var drop = $.ui.ddmanager.droppables[this.options.scope];
  31. for ( var i = 0; i < drop.length; i++ )
  32. if ( drop[i] == this )
  33. drop.splice(i, 1);
  34. this.element
  35. .removeClass("ui-droppable ui-droppable-disabled")
  36. .removeData("droppable")
  37. .unbind(".droppable");
  38. },
  39. _setData: function(key, value) {
  40. if(key == 'accept') {
  41. this.options.accept = value && $.isFunction(value) ? value : function(d) {
  42. return d.is(value);
  43. };
  44. } else {
  45. $.widget.prototype._setData.apply(this, arguments);
  46. }
  47. },
  48. _activate: function(event) {
  49. var draggable = $.ui.ddmanager.current;
  50. if(this.options.activeClass) this.element.addClass(this.options.activeClass);
  51. (draggable && this._trigger('activate', event, this.ui(draggable)));
  52. },
  53. _deactivate: function(event) {
  54. var draggable = $.ui.ddmanager.current;
  55. if(this.options.activeClass) this.element.removeClass(this.options.activeClass);
  56. (draggable && this._trigger('deactivate', event, this.ui(draggable)));
  57. },
  58. _over: function(event) {
  59. var draggable = $.ui.ddmanager.current;
  60. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
  61. if (this.options.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  62. if(this.options.hoverClass) this.element.addClass(this.options.hoverClass);
  63. this._trigger('over', event, this.ui(draggable));
  64. }
  65. },
  66. _out: function(event) {
  67. var draggable = $.ui.ddmanager.current;
  68. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
  69. if (this.options.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  70. if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass);
  71. this._trigger('out', event, this.ui(draggable));
  72. }
  73. },
  74. _drop: function(event,custom) {
  75. var draggable = custom || $.ui.ddmanager.current;
  76. if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return false; // Bail if draggable and droppable are same element
  77. var childrenIntersection = false;
  78. this.element.find(":data(droppable)").not(".ui-draggable-dragging").each(function() {
  79. var inst = $.data(this, 'droppable');
  80. if(inst.options.greedy && $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance)) {
  81. childrenIntersection = true; return false;
  82. }
  83. });
  84. if(childrenIntersection) return false;
  85. if(this.options.accept.call(this.element[0],(draggable.currentItem || draggable.element))) {
  86. if(this.options.activeClass) this.element.removeClass(this.options.activeClass);
  87. if(this.options.hoverClass) this.element.removeClass(this.options.hoverClass);
  88. this._trigger('drop', event, this.ui(draggable));
  89. return this.element;
  90. }
  91. return false;
  92. },
  93. ui: function(c) {
  94. return {
  95. draggable: (c.currentItem || c.element),
  96. helper: c.helper,
  97. position: c.position,
  98. absolutePosition: c.positionAbs, //deprecated
  99. offset: c.positionAbs
  100. };
  101. }
  102. });
  103. $.extend($.ui.droppable, {
  104. version: "1.7.2",
  105. eventPrefix: 'drop',
  106. defaults: {
  107. accept: '*',
  108. activeClass: false,
  109. addClasses: true,
  110. greedy: false,
  111. hoverClass: false,
  112. scope: 'default',
  113. tolerance: 'intersect'
  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].options.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. m[i].offset = m[i].element.offset();
  170. m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight };
  171. if(type == "mousedown") m[i]._activate.call(m[i], event); //Activate the droppable if used directly from draggables
  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);
  180. if (!this.options.disabled && this.visible && this.options.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. drag: function(draggable, event) {
  188. //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
  189. if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, event);
  190. //Run through all droppables and check their positions based on specific tolerance options
  191. $.each($.ui.ddmanager.droppables[draggable.options.scope], function() {
  192. if(this.options.disabled || this.greedyChild || !this.visible) return;
  193. var intersects = $.ui.intersect(draggable, this, this.options.tolerance);
  194. var c = !intersects && this.isover == 1 ? 'isout' : (intersects && this.isover == 0 ? 'isover' : null);
  195. if(!c) return;
  196. var parentInstance;
  197. if (this.options.greedy) {
  198. var parent = this.element.parents(':data(droppable):eq(0)');
  199. if (parent.length) {
  200. parentInstance = $.data(parent[0], 'droppable');
  201. parentInstance.greedyChild = (c == 'isover' ? 1 : 0);
  202. }
  203. }
  204. // we just moved into a greedy child
  205. if (parentInstance && c == 'isover') {
  206. parentInstance['isover'] = 0;
  207. parentInstance['isout'] = 1;
  208. parentInstance._out.call(parentInstance, event);
  209. }
  210. this[c] = 1; this[c == 'isout' ? 'isover' : 'isout'] = 0;
  211. this[c == "isover" ? "_over" : "_out"].call(this, event);
  212. // we just moved out of a greedy child
  213. if (parentInstance && c == 'isout') {
  214. parentInstance['isout'] = 0;
  215. parentInstance['isover'] = 1;
  216. parentInstance._over.call(parentInstance, event);
  217. }
  218. });
  219. }
  220. };
  221. })(jQuery);