1
0

droppable.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*!
  2. * jQuery UI Droppable 1.11.4
  3. * http://jqueryui.com
  4. *
  5. * Copyright 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: "1.11.4",
  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, event )
  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, event ) {
  190. if ( !droppable.offset ) {
  191. return false;
  192. }
  193. var x1 = ( draggable.positionAbs || draggable.position.absolute ).left + draggable.margins.left,
  194. y1 = ( draggable.positionAbs || draggable.position.absolute ).top + draggable.margins.top,
  195. x2 = x1 + draggable.helperProportions.width,
  196. y2 = y1 + draggable.helperProportions.height,
  197. l = droppable.offset.left,
  198. t = droppable.offset.top,
  199. r = l + droppable.proportions().width,
  200. b = t + droppable.proportions().height;
  201. switch ( toleranceMode ) {
  202. case "fit":
  203. return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b );
  204. case "intersect":
  205. return ( l < x1 + ( draggable.helperProportions.width / 2 ) && // Right Half
  206. x2 - ( draggable.helperProportions.width / 2 ) < r && // Left Half
  207. t < y1 + ( draggable.helperProportions.height / 2 ) && // Bottom Half
  208. y2 - ( draggable.helperProportions.height / 2 ) < b ); // Top Half
  209. case "pointer":
  210. return isOverAxis( event.pageY, t, droppable.proportions().height ) && isOverAxis( event.pageX, l, droppable.proportions().width );
  211. case "touch":
  212. return (
  213. ( y1 >= t && y1 <= b ) || // Top edge touching
  214. ( y2 >= t && y2 <= b ) || // Bottom edge touching
  215. ( y1 < t && y2 > b ) // Surrounded vertically
  216. ) && (
  217. ( x1 >= l && x1 <= r ) || // Left edge touching
  218. ( x2 >= l && x2 <= r ) || // Right edge touching
  219. ( x1 < l && x2 > r ) // Surrounded horizontally
  220. );
  221. default:
  222. return false;
  223. }
  224. };
  225. })();
  226. /*
  227. This manager tracks offsets of draggables and droppables
  228. */
  229. $.ui.ddmanager = {
  230. current: null,
  231. droppables: { "default": [] },
  232. prepareOffsets: function( t, event ) {
  233. var i, j,
  234. m = $.ui.ddmanager.droppables[ t.options.scope ] || [],
  235. type = event ? event.type : null, // workaround for #2317
  236. list = ( t.currentItem || t.element ).find( ":data(ui-droppable)" ).addBack();
  237. droppablesLoop: for ( i = 0; i < m.length; i++ ) {
  238. // No disabled and non-accepted
  239. if ( m[ i ].options.disabled || ( t && !m[ i ].accept.call( m[ i ].element[ 0 ], ( t.currentItem || t.element ) ) ) ) {
  240. continue;
  241. }
  242. // Filter out elements in the current dragged item
  243. for ( j = 0; j < list.length; j++ ) {
  244. if ( list[ j ] === m[ i ].element[ 0 ] ) {
  245. m[ i ].proportions().height = 0;
  246. continue droppablesLoop;
  247. }
  248. }
  249. m[ i ].visible = m[ i ].element.css( "display" ) !== "none";
  250. if ( !m[ i ].visible ) {
  251. continue;
  252. }
  253. // Activate the droppable if used directly from draggables
  254. if ( type === "mousedown" ) {
  255. m[ i ]._activate.call( m[ i ], event );
  256. }
  257. m[ i ].offset = m[ i ].element.offset();
  258. m[ i ].proportions({ width: m[ i ].element[ 0 ].offsetWidth, height: m[ i ].element[ 0 ].offsetHeight });
  259. }
  260. },
  261. drop: function( draggable, event ) {
  262. var dropped = false;
  263. // Create a copy of the droppables in case the list changes during the drop (#9116)
  264. $.each( ( $.ui.ddmanager.droppables[ draggable.options.scope ] || [] ).slice(), function() {
  265. if ( !this.options ) {
  266. return;
  267. }
  268. if ( !this.options.disabled && this.visible && $.ui.intersect( draggable, this, this.options.tolerance, event ) ) {
  269. dropped = this._drop.call( this, event ) || dropped;
  270. }
  271. if ( !this.options.disabled && this.visible && this.accept.call( this.element[ 0 ], ( draggable.currentItem || draggable.element ) ) ) {
  272. this.isout = true;
  273. this.isover = false;
  274. this._deactivate.call( this, event );
  275. }
  276. });
  277. return dropped;
  278. },
  279. dragStart: function( draggable, event ) {
  280. // Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
  281. draggable.element.parentsUntil( "body" ).bind( "scroll.droppable", function() {
  282. if ( !draggable.options.refreshPositions ) {
  283. $.ui.ddmanager.prepareOffsets( draggable, event );
  284. }
  285. });
  286. },
  287. drag: function( draggable, event ) {
  288. // If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
  289. if ( draggable.options.refreshPositions ) {
  290. $.ui.ddmanager.prepareOffsets( draggable, event );
  291. }
  292. // Run through all droppables and check their positions based on specific tolerance options
  293. $.each( $.ui.ddmanager.droppables[ draggable.options.scope ] || [], function() {
  294. if ( this.options.disabled || this.greedyChild || !this.visible ) {
  295. return;
  296. }
  297. var parentInstance, scope, parent,
  298. intersects = $.ui.intersect( draggable, this, this.options.tolerance, event ),
  299. c = !intersects && this.isover ? "isout" : ( intersects && !this.isover ? "isover" : null );
  300. if ( !c ) {
  301. return;
  302. }
  303. if ( this.options.greedy ) {
  304. // find droppable parents with same scope
  305. scope = this.options.scope;
  306. parent = this.element.parents( ":data(ui-droppable)" ).filter(function() {
  307. return $( this ).droppable( "instance" ).options.scope === scope;
  308. });
  309. if ( parent.length ) {
  310. parentInstance = $( parent[ 0 ] ).droppable( "instance" );
  311. parentInstance.greedyChild = ( c === "isover" );
  312. }
  313. }
  314. // we just moved into a greedy child
  315. if ( parentInstance && c === "isover" ) {
  316. parentInstance.isover = false;
  317. parentInstance.isout = true;
  318. parentInstance._out.call( parentInstance, event );
  319. }
  320. this[ c ] = true;
  321. this[c === "isout" ? "isover" : "isout"] = false;
  322. this[c === "isover" ? "_over" : "_out"].call( this, event );
  323. // we just moved out of a greedy child
  324. if ( parentInstance && c === "isout" ) {
  325. parentInstance.isout = false;
  326. parentInstance.isover = true;
  327. parentInstance._over.call( parentInstance, event );
  328. }
  329. });
  330. },
  331. dragStop: function( draggable, event ) {
  332. draggable.element.parentsUntil( "body" ).unbind( "scroll.droppable" );
  333. // Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
  334. if ( !draggable.options.refreshPositions ) {
  335. $.ui.ddmanager.prepareOffsets( draggable, event );
  336. }
  337. }
  338. };
  339. return $.ui.droppable;
  340. }));