droppable.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*!
  2. * jQuery UI Droppable @VERSION
  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. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./core",
  17. "./widget",
  18. "./mouse",
  19. "./draggable"
  20. ], factory );
  21. } else {
  22. // Browser globals
  23. factory( jQuery );
  24. }
  25. }(function( $ ) {
  26. $.widget( "ui.droppable", {
  27. version: "@VERSION",
  28. widgetEventPrefix: "drop",
  29. options: {
  30. accept: "*",
  31. activeClass: false,
  32. addClasses: true,
  33. greedy: false,
  34. hoverClass: false,
  35. scope: "default",
  36. tolerance: "intersect",
  37. // callbacks
  38. activate: null,
  39. deactivate: null,
  40. drop: null,
  41. out: null,
  42. over: null
  43. },
  44. _create: function() {
  45. var proportions,
  46. o = this.options,
  47. accept = o.accept;
  48. this.isover = false;
  49. this.isout = true;
  50. this.accept = $.isFunction( accept ) ? accept : function( d ) {
  51. return d.is( accept );
  52. };
  53. this.proportions = function( /* valueToWrite */ ) {
  54. if ( arguments.length ) {
  55. // Store the droppable's proportions
  56. proportions = arguments[ 0 ];
  57. } else {
  58. // Retrieve or derive the droppable's proportions
  59. return proportions ?
  60. proportions :
  61. proportions = {
  62. width: this.element[ 0 ].offsetWidth,
  63. height: this.element[ 0 ].offsetHeight
  64. };
  65. }
  66. };
  67. this._addToManager( o.scope );
  68. o.addClasses && this.element.addClass( "ui-droppable" );
  69. },
  70. _addToManager: function( scope ) {
  71. // Add the reference and positions to the manager
  72. $.ui.ddmanager.droppables[ scope ] = $.ui.ddmanager.droppables[ scope ] || [];
  73. $.ui.ddmanager.droppables[ scope ].push( this );
  74. },
  75. _splice: function( drop ) {
  76. var i = 0;
  77. for ( ; i < drop.length; i++ ) {
  78. if ( drop[ i ] === this ) {
  79. drop.splice( i, 1 );
  80. }
  81. }
  82. },
  83. _destroy: function() {
  84. var drop = $.ui.ddmanager.droppables[ this.options.scope ];
  85. this._splice( drop );
  86. this.element.removeClass( "ui-droppable ui-droppable-disabled" );
  87. },
  88. _setOption: function( key, value ) {
  89. if ( key === "accept" ) {
  90. this.accept = $.isFunction( value ) ? value : function( d ) {
  91. return d.is( value );
  92. };
  93. } else if ( key === "scope" ) {
  94. var drop = $.ui.ddmanager.droppables[ this.options.scope ];
  95. this._splice( drop );
  96. this._addToManager( value );
  97. }
  98. this._super( key, value );
  99. },
  100. _activate: function( event ) {
  101. var draggable = $.ui.ddmanager.current;
  102. if ( this.options.activeClass ) {
  103. this.element.addClass( this.options.activeClass );
  104. }
  105. if ( draggable ){
  106. this._trigger( "activate", event, this.ui( draggable ) );
  107. }
  108. },
  109. _deactivate: function( event ) {
  110. var draggable = $.ui.ddmanager.current;
  111. if ( this.options.activeClass ) {
  112. this.element.removeClass( this.options.activeClass );
  113. }
  114. if ( draggable ){
  115. this._trigger( "deactivate", event, this.ui( draggable ) );
  116. }
  117. },
  118. _over: function( event ) {
  119. var draggable = $.ui.ddmanager.current;
  120. // Bail if draggable and droppable are same element
  121. if ( !draggable || ( draggable.currentItem || draggable.element )[ 0 ] === this.element[ 0 ] ) {
  122. return;
  123. }
  124. if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
  125. if ( this.options.hoverClass ) {
  126. this.element.addClass( this.options.hoverClass );
  127. }
  128. this._trigger( "over", event, this.ui( draggable ) );
  129. }
  130. },
  131. _out: function( event ) {
  132. var draggable = $.ui.ddmanager.current;
  133. // Bail if draggable and droppable are same element
  134. if ( !draggable || ( draggable.currentItem || draggable.element )[ 0 ] === this.element[ 0 ] ) {
  135. return;
  136. }
  137. if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
  138. if ( this.options.hoverClass ) {
  139. this.element.removeClass( this.options.hoverClass );
  140. }
  141. this._trigger( "out", event, this.ui( draggable ) );
  142. }
  143. },
  144. _drop: function( event, custom ) {
  145. var draggable = custom || $.ui.ddmanager.current,
  146. childrenIntersection = false;
  147. // Bail if draggable and droppable are same element
  148. if ( !draggable || ( draggable.currentItem || draggable.element )[ 0 ] === this.element[ 0 ] ) {
  149. return false;
  150. }
  151. this.element.find( ":data(ui-droppable)" ).not( ".ui-draggable-dragging" ).each(function() {
  152. var inst = $( this ).droppable( "instance" );
  153. if (
  154. inst.options.greedy &&
  155. !inst.options.disabled &&
  156. inst.options.scope === draggable.options.scope &&
  157. inst.accept.call( inst.element[ 0 ], ( draggable.currentItem || draggable.element ) ) &&
  158. $.ui.intersect( draggable, $.extend( inst, { offset: inst.element.offset() } ), inst.options.tolerance )
  159. ) { childrenIntersection = true; return false; }
  160. });
  161. if ( childrenIntersection ) {
  162. return false;
  163. }
  164. if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
  165. if ( this.options.activeClass ) {
  166. this.element.removeClass( this.options.activeClass );
  167. }
  168. if ( this.options.hoverClass ) {
  169. this.element.removeClass( this.options.hoverClass );
  170. }
  171. this._trigger( "drop", event, this.ui( draggable ) );
  172. return this.element;
  173. }
  174. return false;
  175. },
  176. ui: function( c ) {
  177. return {
  178. draggable: ( c.currentItem || c.element ),
  179. helper: c.helper,
  180. position: c.position,
  181. offset: c.positionAbs
  182. };
  183. }
  184. });
  185. $.ui.intersect = (function() {
  186. function isOverAxis( x, reference, size ) {
  187. return ( x >= reference ) && ( x < ( reference + size ) );
  188. }
  189. return function( draggable, droppable, toleranceMode ) {
  190. if ( !droppable.offset ) {
  191. return false;
  192. }
  193. var draggableLeft, draggableTop,
  194. x1 = ( draggable.positionAbs || draggable.position.absolute ).left,
  195. y1 = ( draggable.positionAbs || draggable.position.absolute ).top,
  196. x2 = x1 + draggable.helperProportions.width,
  197. y2 = y1 + draggable.helperProportions.height,
  198. l = droppable.offset.left,
  199. t = droppable.offset.top,
  200. r = l + droppable.proportions().width,
  201. b = t + droppable.proportions().height;
  202. switch ( toleranceMode ) {
  203. case "fit":
  204. return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b );
  205. case "intersect":
  206. return ( l < x1 + ( draggable.helperProportions.width / 2 ) && // Right Half
  207. x2 - ( draggable.helperProportions.width / 2 ) < r && // Left Half
  208. t < y1 + ( draggable.helperProportions.height / 2 ) && // Bottom Half
  209. y2 - ( draggable.helperProportions.height / 2 ) < b ); // Top Half
  210. case "pointer":
  211. draggableLeft = ( ( draggable.positionAbs || draggable.position.absolute ).left + ( draggable.clickOffset || draggable.offset.click ).left );
  212. draggableTop = ( ( draggable.positionAbs || draggable.position.absolute ).top + ( draggable.clickOffset || draggable.offset.click ).top );
  213. return isOverAxis( draggableTop, t, droppable.proportions().height ) && isOverAxis( draggableLeft, l, droppable.proportions().width );
  214. case "touch":
  215. return (
  216. ( y1 >= t && y1 <= b ) || // Top edge touching
  217. ( y2 >= t && y2 <= b ) || // Bottom edge touching
  218. ( y1 < t && y2 > b ) // Surrounded vertically
  219. ) && (
  220. ( x1 >= l && x1 <= r ) || // Left edge touching
  221. ( x2 >= l && x2 <= r ) || // Right edge touching
  222. ( x1 < l && x2 > r ) // Surrounded horizontally
  223. );
  224. default:
  225. return false;
  226. }
  227. };
  228. })();
  229. /*
  230. This manager tracks offsets of draggables and droppables
  231. */
  232. $.ui.ddmanager = {
  233. current: null,
  234. droppables: { "default": [] },
  235. prepareOffsets: function( t, event ) {
  236. var i, j,
  237. m = $.ui.ddmanager.droppables[ t.options.scope ] || [],
  238. type = event ? event.type : null, // workaround for #2317
  239. list = ( t.currentItem || t.element ).find( ":data(ui-droppable)" ).addBack();
  240. droppablesLoop: for ( i = 0; i < m.length; i++ ) {
  241. // No disabled and non-accepted
  242. if ( m[ i ].options.disabled || ( t && !m[ i ].accept.call( m[ i ].element[ 0 ], ( t.currentItem || t.element ) ) ) ) {
  243. continue;
  244. }
  245. // Filter out elements in the current dragged item
  246. for ( j = 0; j < list.length; j++ ) {
  247. if ( list[ j ] === m[ i ].element[ 0 ] ) {
  248. m[ i ].proportions().height = 0;
  249. continue droppablesLoop;
  250. }
  251. }
  252. m[ i ].visible = m[ i ].element.css( "display" ) !== "none";
  253. if ( !m[ i ].visible ) {
  254. continue;
  255. }
  256. // Activate the droppable if used directly from draggables
  257. if ( type === "mousedown" ) {
  258. m[ i ]._activate.call( m[ i ], event );
  259. }
  260. m[ i ].offset = m[ i ].element.offset();
  261. m[ i ].proportions({ width: m[ i ].element[ 0 ].offsetWidth, height: m[ i ].element[ 0 ].offsetHeight });
  262. }
  263. },
  264. drop: function( draggable, event ) {
  265. var dropped = false;
  266. // Create a copy of the droppables in case the list changes during the drop (#9116)
  267. $.each( ( $.ui.ddmanager.droppables[ draggable.options.scope ] || [] ).slice(), function() {
  268. if ( !this.options ) {
  269. return;
  270. }
  271. if ( !this.options.disabled && this.visible && $.ui.intersect( draggable, this, this.options.tolerance ) ) {
  272. dropped = this._drop.call( this, event ) || dropped;
  273. }
  274. if ( !this.options.disabled && this.visible && this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
  275. this.isout = true;
  276. this.isover = false;
  277. this._deactivate.call( this, event );
  278. }
  279. });
  280. return dropped;
  281. },
  282. dragStart: function( draggable, event ) {
  283. // Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
  284. draggable.element.parentsUntil( "body" ).bind( "scroll.droppable", function() {
  285. if ( !draggable.options.refreshPositions ) {
  286. $.ui.ddmanager.prepareOffsets( draggable, event );
  287. }
  288. });
  289. },
  290. drag: function( draggable, event ) {
  291. // If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
  292. if ( draggable.options.refreshPositions ) {
  293. $.ui.ddmanager.prepareOffsets( draggable, event );
  294. }
  295. // Run through all droppables and check their positions based on specific tolerance options
  296. $.each( $.ui.ddmanager.droppables[ draggable.options.scope ] || [], function() {
  297. if ( this.options.disabled || this.greedyChild || !this.visible ) {
  298. return;
  299. }
  300. var parentInstance, scope, parent,
  301. intersects = $.ui.intersect( draggable, this, this.options.tolerance ),
  302. c = !intersects && this.isover ? "isout" : ( intersects && !this.isover ? "isover" : null );
  303. if ( !c ) {
  304. return;
  305. }
  306. if ( this.options.greedy ) {
  307. // find droppable parents with same scope
  308. scope = this.options.scope;
  309. parent = this.element.parents( ":data(ui-droppable)" ).filter(function() {
  310. return $( this ).droppable( "instance" ).options.scope === scope;
  311. });
  312. if ( parent.length ) {
  313. parentInstance = $( parent[ 0 ] ).droppable( "instance" );
  314. parentInstance.greedyChild = ( c === "isover" );
  315. }
  316. }
  317. // we just moved into a greedy child
  318. if ( parentInstance && c === "isover" ) {
  319. parentInstance.isover = false;
  320. parentInstance.isout = true;
  321. parentInstance._out.call( parentInstance, event );
  322. }
  323. this[ c ] = true;
  324. this[c === "isout" ? "isover" : "isout"] = false;
  325. this[c === "isover" ? "_over" : "_out"].call( this, event );
  326. // we just moved out of a greedy child
  327. if ( parentInstance && c === "isout" ) {
  328. parentInstance.isout = false;
  329. parentInstance.isover = true;
  330. parentInstance._over.call( parentInstance, event );
  331. }
  332. });
  333. },
  334. dragStop: function( draggable, event ) {
  335. draggable.element.parentsUntil( "body" ).unbind( "scroll.droppable" );
  336. // Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
  337. if ( !draggable.options.refreshPositions ) {
  338. $.ui.ddmanager.prepareOffsets( draggable, event );
  339. }
  340. }
  341. };
  342. return $.ui.droppable;
  343. }));