Няма описание

droppable.js 13KB

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