droppable.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*!
  2. * jQuery UI Droppable 1.14.1
  3. * https://jqueryui.com
  4. *
  5. * Copyright OpenJS Foundation and other contributors
  6. * Released under the MIT license.
  7. * https://jquery.org/license
  8. */
  9. //>>label: Droppable
  10. //>>group: Interactions
  11. //>>description: Enables drop targets for draggable elements.
  12. //>>docs: https://api.jqueryui.com/droppable/
  13. //>>demos: https://jqueryui.com/droppable/
  14. ( function( factory ) {
  15. "use strict";
  16. if ( typeof define === "function" && define.amd ) {
  17. // AMD. Register as an anonymous module.
  18. define( [
  19. "jquery",
  20. "./draggable",
  21. "./mouse",
  22. "../version",
  23. "../widget"
  24. ], factory );
  25. } else {
  26. // Browser globals
  27. factory( jQuery );
  28. }
  29. } )( function( $ ) {
  30. "use strict";
  31. $.widget( "ui.droppable", {
  32. version: "1.14.1",
  33. widgetEventPrefix: "drop",
  34. options: {
  35. accept: "*",
  36. addClasses: true,
  37. greedy: false,
  38. scope: "default",
  39. tolerance: "intersect",
  40. // Callbacks
  41. activate: null,
  42. deactivate: null,
  43. drop: null,
  44. out: null,
  45. over: null
  46. },
  47. _create: function() {
  48. var proportions,
  49. o = this.options,
  50. accept = o.accept;
  51. this.isover = false;
  52. this.isout = true;
  53. this.accept = typeof accept === "function" ? accept : function( d ) {
  54. return d.is( accept );
  55. };
  56. this.proportions = function( /* valueToWrite */ ) {
  57. if ( arguments.length ) {
  58. // Store the droppable's proportions
  59. proportions = arguments[ 0 ];
  60. } else {
  61. // Retrieve or derive the droppable's proportions
  62. return proportions ?
  63. proportions :
  64. proportions = {
  65. width: this.element[ 0 ].offsetWidth,
  66. height: this.element[ 0 ].offsetHeight
  67. };
  68. }
  69. };
  70. this._addToManager( o.scope );
  71. if ( o.addClasses ) {
  72. this._addClass( "ui-droppable" );
  73. }
  74. },
  75. _addToManager: function( scope ) {
  76. // Add the reference and positions to the manager
  77. $.ui.ddmanager.droppables[ scope ] = $.ui.ddmanager.droppables[ scope ] || [];
  78. $.ui.ddmanager.droppables[ scope ].push( this );
  79. },
  80. _splice: function( drop ) {
  81. var i = 0;
  82. for ( ; i < drop.length; i++ ) {
  83. if ( drop[ i ] === this ) {
  84. drop.splice( i, 1 );
  85. }
  86. }
  87. },
  88. _destroy: function() {
  89. var drop = $.ui.ddmanager.droppables[ this.options.scope ];
  90. this._splice( drop );
  91. },
  92. _setOption: function( key, value ) {
  93. if ( key === "accept" ) {
  94. this.accept = typeof value === "function" ? value : function( d ) {
  95. return d.is( value );
  96. };
  97. } else if ( key === "scope" ) {
  98. var drop = $.ui.ddmanager.droppables[ this.options.scope ];
  99. this._splice( drop );
  100. this._addToManager( value );
  101. }
  102. this._super( key, value );
  103. },
  104. _activate: function( event ) {
  105. var draggable = $.ui.ddmanager.current;
  106. this._addActiveClass();
  107. if ( draggable ) {
  108. this._trigger( "activate", event, this.ui( draggable ) );
  109. }
  110. },
  111. _deactivate: function( event ) {
  112. var draggable = $.ui.ddmanager.current;
  113. this._removeActiveClass();
  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 ||
  122. draggable.element )[ 0 ] === this.element[ 0 ] ) {
  123. return;
  124. }
  125. if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem ||
  126. draggable.element ) ) ) {
  127. this._addHoverClass();
  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 ||
  135. draggable.element )[ 0 ] === this.element[ 0 ] ) {
  136. return;
  137. }
  138. if ( this.accept.call( this.element[ 0 ], ( draggable.currentItem ||
  139. draggable.element ) ) ) {
  140. this._removeHoverClass();
  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 ||
  149. draggable.element )[ 0 ] === this.element[ 0 ] ) {
  150. return false;
  151. }
  152. this.element
  153. .find( ":data(ui-droppable)" )
  154. .not( ".ui-draggable-dragging" )
  155. .each( function() {
  156. var inst = $( this ).droppable( "instance" );
  157. if (
  158. inst.options.greedy &&
  159. !inst.options.disabled &&
  160. inst.options.scope === draggable.options.scope &&
  161. inst.accept.call(
  162. inst.element[ 0 ], ( draggable.currentItem || draggable.element )
  163. ) &&
  164. $.ui.intersect(
  165. draggable,
  166. $.extend( inst, { offset: inst.element.offset() } ),
  167. inst.options.tolerance, event
  168. )
  169. ) {
  170. childrenIntersection = true;
  171. return false;
  172. }
  173. } );
  174. if ( childrenIntersection ) {
  175. return false;
  176. }
  177. if ( this.accept.call( this.element[ 0 ],
  178. ( draggable.currentItem || draggable.element ) ) ) {
  179. this._removeActiveClass();
  180. this._removeHoverClass();
  181. this._trigger( "drop", event, this.ui( draggable ) );
  182. return this.element;
  183. }
  184. return false;
  185. },
  186. ui: function( c ) {
  187. return {
  188. draggable: ( c.currentItem || c.element ),
  189. helper: c.helper,
  190. position: c.position,
  191. offset: c.positionAbs
  192. };
  193. },
  194. // Extension points just to make backcompat sane and avoid duplicating logic
  195. // TODO: Remove in 1.14 along with call to it below
  196. _addHoverClass: function() {
  197. this._addClass( "ui-droppable-hover" );
  198. },
  199. _removeHoverClass: function() {
  200. this._removeClass( "ui-droppable-hover" );
  201. },
  202. _addActiveClass: function() {
  203. this._addClass( "ui-droppable-active" );
  204. },
  205. _removeActiveClass: function() {
  206. this._removeClass( "ui-droppable-active" );
  207. }
  208. } );
  209. $.ui.intersect = ( function() {
  210. function isOverAxis( x, reference, size ) {
  211. return ( x >= reference ) && ( x < ( reference + size ) );
  212. }
  213. return function( draggable, droppable, toleranceMode, event ) {
  214. if ( !droppable.offset ) {
  215. return false;
  216. }
  217. var x1 = ( draggable.positionAbs ||
  218. draggable.position.absolute ).left + draggable.margins.left,
  219. y1 = ( draggable.positionAbs ||
  220. draggable.position.absolute ).top + draggable.margins.top,
  221. x2 = x1 + draggable.helperProportions.width,
  222. y2 = y1 + draggable.helperProportions.height,
  223. l = droppable.offset.left,
  224. t = droppable.offset.top,
  225. r = l + droppable.proportions().width,
  226. b = t + droppable.proportions().height;
  227. switch ( toleranceMode ) {
  228. case "fit":
  229. return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b );
  230. case "intersect":
  231. return ( l < x1 + ( draggable.helperProportions.width / 2 ) && // Right Half
  232. x2 - ( draggable.helperProportions.width / 2 ) < r && // Left Half
  233. t < y1 + ( draggable.helperProportions.height / 2 ) && // Bottom Half
  234. y2 - ( draggable.helperProportions.height / 2 ) < b ); // Top Half
  235. case "pointer":
  236. return isOverAxis( event.pageY, t, droppable.proportions().height ) &&
  237. isOverAxis( event.pageX, l, droppable.proportions().width );
  238. case "touch":
  239. return (
  240. ( y1 >= t && y1 <= b ) || // Top edge touching
  241. ( y2 >= t && y2 <= b ) || // Bottom edge touching
  242. ( y1 < t && y2 > b ) // Surrounded vertically
  243. ) && (
  244. ( x1 >= l && x1 <= r ) || // Left edge touching
  245. ( x2 >= l && x2 <= r ) || // Right edge touching
  246. ( x1 < l && x2 > r ) // Surrounded horizontally
  247. );
  248. default:
  249. return false;
  250. }
  251. };
  252. } )();
  253. /*
  254. This manager tracks offsets of draggables and droppables
  255. */
  256. $.ui.ddmanager = {
  257. current: null,
  258. droppables: { "default": [] },
  259. prepareOffsets: function( t, event ) {
  260. var i, j,
  261. m = $.ui.ddmanager.droppables[ t.options.scope ] || [],
  262. type = event ? event.type : null, // workaround for #2317
  263. list = ( t.currentItem || t.element ).find( ":data(ui-droppable)" ).addBack();
  264. droppablesLoop: for ( i = 0; i < m.length; i++ ) {
  265. // No disabled and non-accepted
  266. if ( m[ i ].options.disabled || ( t && !m[ i ].accept.call( m[ i ].element[ 0 ],
  267. ( t.currentItem || t.element ) ) ) ) {
  268. continue;
  269. }
  270. // Filter out elements in the current dragged item
  271. for ( j = 0; j < list.length; j++ ) {
  272. if ( list[ j ] === m[ i ].element[ 0 ] ) {
  273. m[ i ].proportions().height = 0;
  274. continue droppablesLoop;
  275. }
  276. }
  277. m[ i ].visible = m[ i ].element.css( "display" ) !== "none";
  278. if ( !m[ i ].visible ) {
  279. continue;
  280. }
  281. // Activate the droppable if used directly from draggables
  282. if ( type === "mousedown" ) {
  283. m[ i ]._activate.call( m[ i ], event );
  284. }
  285. m[ i ].offset = m[ i ].element.offset();
  286. m[ i ].proportions( {
  287. width: m[ i ].element[ 0 ].offsetWidth,
  288. height: m[ i ].element[ 0 ].offsetHeight
  289. } );
  290. }
  291. },
  292. drop: function( draggable, event ) {
  293. var dropped = false;
  294. // Create a copy of the droppables in case the list changes during the drop (#9116)
  295. $.each( ( $.ui.ddmanager.droppables[ draggable.options.scope ] || [] ).slice(), function() {
  296. if ( !this.options ) {
  297. return;
  298. }
  299. if ( !this.options.disabled && this.visible &&
  300. $.ui.intersect( draggable, this, this.options.tolerance, event ) ) {
  301. dropped = this._drop.call( this, event ) || dropped;
  302. }
  303. if ( !this.options.disabled && this.visible && this.accept.call( this.element[ 0 ],
  304. ( draggable.currentItem || draggable.element ) ) ) {
  305. this.isout = true;
  306. this.isover = false;
  307. this._deactivate.call( this, event );
  308. }
  309. } );
  310. return dropped;
  311. },
  312. dragStart: function( draggable, event ) {
  313. // Listen for scrolling so that if the dragging causes scrolling the position of the
  314. // droppables can be recalculated (see #5003)
  315. draggable.element.parentsUntil( "body" ).on( "scroll.droppable", function() {
  316. if ( !draggable.options.refreshPositions ) {
  317. $.ui.ddmanager.prepareOffsets( draggable, event );
  318. }
  319. } );
  320. },
  321. drag: function( draggable, event ) {
  322. // If you have a highly dynamic page, you might try this option. It renders positions
  323. // every time you move the mouse.
  324. if ( draggable.options.refreshPositions ) {
  325. $.ui.ddmanager.prepareOffsets( draggable, event );
  326. }
  327. // Run through all droppables and check their positions based on specific tolerance options
  328. $.each( $.ui.ddmanager.droppables[ draggable.options.scope ] || [], function() {
  329. if ( this.options.disabled || this.greedyChild || !this.visible ) {
  330. return;
  331. }
  332. var parentInstance, scope, parent,
  333. intersects = $.ui.intersect( draggable, this, this.options.tolerance, event ),
  334. c = !intersects && this.isover ?
  335. "isout" :
  336. ( intersects && !this.isover ? "isover" : null );
  337. if ( !c ) {
  338. return;
  339. }
  340. if ( this.options.greedy ) {
  341. // find droppable parents with same scope
  342. scope = this.options.scope;
  343. parent = this.element.parents( ":data(ui-droppable)" ).filter( function() {
  344. return $( this ).droppable( "instance" ).options.scope === scope;
  345. } );
  346. if ( parent.length ) {
  347. parentInstance = $( parent[ 0 ] ).droppable( "instance" );
  348. parentInstance.greedyChild = ( c === "isover" );
  349. }
  350. }
  351. // We just moved into a greedy child
  352. if ( parentInstance && c === "isover" ) {
  353. parentInstance.isover = false;
  354. parentInstance.isout = true;
  355. parentInstance._out.call( parentInstance, event );
  356. }
  357. this[ c ] = true;
  358. this[ c === "isout" ? "isover" : "isout" ] = false;
  359. this[ c === "isover" ? "_over" : "_out" ].call( this, event );
  360. // We just moved out of a greedy child
  361. if ( parentInstance && c === "isout" ) {
  362. parentInstance.isout = false;
  363. parentInstance.isover = true;
  364. parentInstance._over.call( parentInstance, event );
  365. }
  366. } );
  367. },
  368. dragStop: function( draggable, event ) {
  369. draggable.element.parentsUntil( "body" ).off( "scroll.droppable" );
  370. // Call prepareOffsets one final time since IE does not fire return scroll events when
  371. // overflow was caused by drag (see #5003)
  372. if ( !draggable.options.refreshPositions ) {
  373. $.ui.ddmanager.prepareOffsets( draggable, event );
  374. }
  375. }
  376. };
  377. // DEPRECATED
  378. // TODO: switch return back to widget declaration at top of file when this is removed
  379. if ( $.uiBackCompat === true ) {
  380. // Backcompat for activeClass and hoverClass options
  381. $.widget( "ui.droppable", $.ui.droppable, {
  382. options: {
  383. hoverClass: false,
  384. activeClass: false
  385. },
  386. _addActiveClass: function() {
  387. this._super();
  388. if ( this.options.activeClass ) {
  389. this.element.addClass( this.options.activeClass );
  390. }
  391. },
  392. _removeActiveClass: function() {
  393. this._super();
  394. if ( this.options.activeClass ) {
  395. this.element.removeClass( this.options.activeClass );
  396. }
  397. },
  398. _addHoverClass: function() {
  399. this._super();
  400. if ( this.options.hoverClass ) {
  401. this.element.addClass( this.options.hoverClass );
  402. }
  403. },
  404. _removeHoverClass: function() {
  405. this._super();
  406. if ( this.options.hoverClass ) {
  407. this.element.removeClass( this.options.hoverClass );
  408. }
  409. }
  410. } );
  411. }
  412. return $.ui.droppable;
  413. } );