Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1260 строки
35 KiB

  1. /*!
  2. * jQuery UI Draggable 1.13.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: Draggable
  10. //>>group: Interactions
  11. //>>description: Enables dragging functionality for any element.
  12. //>>docs: http://api.jqueryui.com/draggable/
  13. //>>demos: http://jqueryui.com/draggable/
  14. //>>css.structure: ../../themes/base/draggable.css
  15. ( function( factory ) {
  16. "use strict";
  17. if ( typeof define === "function" && define.amd ) {
  18. // AMD. Register as an anonymous module.
  19. define( [
  20. "jquery",
  21. "./mouse",
  22. "../data",
  23. "../plugin",
  24. "../safe-active-element",
  25. "../safe-blur",
  26. "../scroll-parent",
  27. "../version",
  28. "../widget"
  29. ], factory );
  30. } else {
  31. // Browser globals
  32. factory( jQuery );
  33. }
  34. } )( function( $ ) {
  35. "use strict";
  36. $.widget( "ui.draggable", $.ui.mouse, {
  37. version: "1.13.1",
  38. widgetEventPrefix: "drag",
  39. options: {
  40. addClasses: true,
  41. appendTo: "parent",
  42. axis: false,
  43. connectToSortable: false,
  44. containment: false,
  45. cursor: "auto",
  46. cursorAt: false,
  47. grid: false,
  48. handle: false,
  49. helper: "original",
  50. iframeFix: false,
  51. opacity: false,
  52. refreshPositions: false,
  53. revert: false,
  54. revertDuration: 500,
  55. scope: "default",
  56. scroll: true,
  57. scrollSensitivity: 20,
  58. scrollSpeed: 20,
  59. snap: false,
  60. snapMode: "both",
  61. snapTolerance: 20,
  62. stack: false,
  63. zIndex: false,
  64. // Callbacks
  65. drag: null,
  66. start: null,
  67. stop: null
  68. },
  69. _create: function() {
  70. if ( this.options.helper === "original" ) {
  71. this._setPositionRelative();
  72. }
  73. if ( this.options.addClasses ) {
  74. this._addClass( "ui-draggable" );
  75. }
  76. this._setHandleClassName();
  77. this._mouseInit();
  78. },
  79. _setOption: function( key, value ) {
  80. this._super( key, value );
  81. if ( key === "handle" ) {
  82. this._removeHandleClassName();
  83. this._setHandleClassName();
  84. }
  85. },
  86. _destroy: function() {
  87. if ( ( this.helper || this.element ).is( ".ui-draggable-dragging" ) ) {
  88. this.destroyOnClear = true;
  89. return;
  90. }
  91. this._removeHandleClassName();
  92. this._mouseDestroy();
  93. },
  94. _mouseCapture: function( event ) {
  95. var o = this.options;
  96. // Among others, prevent a drag on a resizable-handle
  97. if ( this.helper || o.disabled ||
  98. $( event.target ).closest( ".ui-resizable-handle" ).length > 0 ) {
  99. return false;
  100. }
  101. //Quit if we're not on a valid handle
  102. this.handle = this._getHandle( event );
  103. if ( !this.handle ) {
  104. return false;
  105. }
  106. this._blurActiveElement( event );
  107. this._blockFrames( o.iframeFix === true ? "iframe" : o.iframeFix );
  108. return true;
  109. },
  110. _blockFrames: function( selector ) {
  111. this.iframeBlocks = this.document.find( selector ).map( function() {
  112. var iframe = $( this );
  113. return $( "<div>" )
  114. .css( "position", "absolute" )
  115. .appendTo( iframe.parent() )
  116. .outerWidth( iframe.outerWidth() )
  117. .outerHeight( iframe.outerHeight() )
  118. .offset( iframe.offset() )[ 0 ];
  119. } );
  120. },
  121. _unblockFrames: function() {
  122. if ( this.iframeBlocks ) {
  123. this.iframeBlocks.remove();
  124. delete this.iframeBlocks;
  125. }
  126. },
  127. _blurActiveElement: function( event ) {
  128. var activeElement = $.ui.safeActiveElement( this.document[ 0 ] ),
  129. target = $( event.target );
  130. // Don't blur if the event occurred on an element that is within
  131. // the currently focused element
  132. // See #10527, #12472
  133. if ( target.closest( activeElement ).length ) {
  134. return;
  135. }
  136. // Blur any element that currently has focus, see #4261
  137. $.ui.safeBlur( activeElement );
  138. },
  139. _mouseStart: function( event ) {
  140. var o = this.options;
  141. //Create and append the visible helper
  142. this.helper = this._createHelper( event );
  143. this._addClass( this.helper, "ui-draggable-dragging" );
  144. //Cache the helper size
  145. this._cacheHelperProportions();
  146. //If ddmanager is used for droppables, set the global draggable
  147. if ( $.ui.ddmanager ) {
  148. $.ui.ddmanager.current = this;
  149. }
  150. /*
  151. * - Position generation -
  152. * This block generates everything position related - it's the core of draggables.
  153. */
  154. //Cache the margins of the original element
  155. this._cacheMargins();
  156. //Store the helper's css position
  157. this.cssPosition = this.helper.css( "position" );
  158. this.scrollParent = this.helper.scrollParent( true );
  159. this.offsetParent = this.helper.offsetParent();
  160. this.hasFixedAncestor = this.helper.parents().filter( function() {
  161. return $( this ).css( "position" ) === "fixed";
  162. } ).length > 0;
  163. //The element's absolute position on the page minus margins
  164. this.positionAbs = this.element.offset();
  165. this._refreshOffsets( event );
  166. //Generate the original position
  167. this.originalPosition = this.position = this._generatePosition( event, false );
  168. this.originalPageX = event.pageX;
  169. this.originalPageY = event.pageY;
  170. //Adjust the mouse offset relative to the helper if "cursorAt" is supplied
  171. if ( o.cursorAt ) {
  172. this._adjustOffsetFromHelper( o.cursorAt );
  173. }
  174. //Set a containment if given in the options
  175. this._setContainment();
  176. //Trigger event + callbacks
  177. if ( this._trigger( "start", event ) === false ) {
  178. this._clear();
  179. return false;
  180. }
  181. //Recache the helper size
  182. this._cacheHelperProportions();
  183. //Prepare the droppable offsets
  184. if ( $.ui.ddmanager && !o.dropBehaviour ) {
  185. $.ui.ddmanager.prepareOffsets( this, event );
  186. }
  187. // Execute the drag once - this causes the helper not to be visible before getting its
  188. // correct position
  189. this._mouseDrag( event, true );
  190. // If the ddmanager is used for droppables, inform the manager that dragging has started
  191. // (see #5003)
  192. if ( $.ui.ddmanager ) {
  193. $.ui.ddmanager.dragStart( this, event );
  194. }
  195. return true;
  196. },
  197. _refreshOffsets: function( event ) {
  198. this.offset = {
  199. top: this.positionAbs.top - this.margins.top,
  200. left: this.positionAbs.left - this.margins.left,
  201. scroll: false,
  202. parent: this._getParentOffset(),
  203. relative: this._getRelativeOffset()
  204. };
  205. this.offset.click = {
  206. left: event.pageX - this.offset.left,
  207. top: event.pageY - this.offset.top
  208. };
  209. },
  210. _mouseDrag: function( event, noPropagation ) {
  211. // reset any necessary cached properties (see #5009)
  212. if ( this.hasFixedAncestor ) {
  213. this.offset.parent = this._getParentOffset();
  214. }
  215. //Compute the helpers position
  216. this.position = this._generatePosition( event, true );
  217. this.positionAbs = this._convertPositionTo( "absolute" );
  218. //Call plugins and callbacks and use the resulting position if something is returned
  219. if ( !noPropagation ) {
  220. var ui = this._uiHash();
  221. if ( this._trigger( "drag", event, ui ) === false ) {
  222. this._mouseUp( new $.Event( "mouseup", event ) );
  223. return false;
  224. }
  225. this.position = ui.position;
  226. }
  227. this.helper[ 0 ].style.left = this.position.left + "px";
  228. this.helper[ 0 ].style.top = this.position.top + "px";
  229. if ( $.ui.ddmanager ) {
  230. $.ui.ddmanager.drag( this, event );
  231. }
  232. return false;
  233. },
  234. _mouseStop: function( event ) {
  235. //If we are using droppables, inform the manager about the drop
  236. var that = this,
  237. dropped = false;
  238. if ( $.ui.ddmanager && !this.options.dropBehaviour ) {
  239. dropped = $.ui.ddmanager.drop( this, event );
  240. }
  241. //if a drop comes from outside (a sortable)
  242. if ( this.dropped ) {
  243. dropped = this.dropped;
  244. this.dropped = false;
  245. }
  246. if ( ( this.options.revert === "invalid" && !dropped ) ||
  247. ( this.options.revert === "valid" && dropped ) ||
  248. this.options.revert === true || ( typeof this.options.revert === "function" &&
  249. this.options.revert.call( this.element, dropped ) )
  250. ) {
  251. $( this.helper ).animate(
  252. this.originalPosition,
  253. parseInt( this.options.revertDuration, 10 ),
  254. function() {
  255. if ( that._trigger( "stop", event ) !== false ) {
  256. that._clear();
  257. }
  258. }
  259. );
  260. } else {
  261. if ( this._trigger( "stop", event ) !== false ) {
  262. this._clear();
  263. }
  264. }
  265. return false;
  266. },
  267. _mouseUp: function( event ) {
  268. this._unblockFrames();
  269. // If the ddmanager is used for droppables, inform the manager that dragging has stopped
  270. // (see #5003)
  271. if ( $.ui.ddmanager ) {
  272. $.ui.ddmanager.dragStop( this, event );
  273. }
  274. // Only need to focus if the event occurred on the draggable itself, see #10527
  275. if ( this.handleElement.is( event.target ) ) {
  276. // The interaction is over; whether or not the click resulted in a drag,
  277. // focus the element
  278. this.element.trigger( "focus" );
  279. }
  280. return $.ui.mouse.prototype._mouseUp.call( this, event );
  281. },
  282. cancel: function() {
  283. if ( this.helper.is( ".ui-draggable-dragging" ) ) {
  284. this._mouseUp( new $.Event( "mouseup", { target: this.element[ 0 ] } ) );
  285. } else {
  286. this._clear();
  287. }
  288. return this;
  289. },
  290. _getHandle: function( event ) {
  291. return this.options.handle ?
  292. !!$( event.target ).closest( this.element.find( this.options.handle ) ).length :
  293. true;
  294. },
  295. _setHandleClassName: function() {
  296. this.handleElement = this.options.handle ?
  297. this.element.find( this.options.handle ) : this.element;
  298. this._addClass( this.handleElement, "ui-draggable-handle" );
  299. },
  300. _removeHandleClassName: function() {
  301. this._removeClass( this.handleElement, "ui-draggable-handle" );
  302. },
  303. _createHelper: function( event ) {
  304. var o = this.options,
  305. helperIsFunction = typeof o.helper === "function",
  306. helper = helperIsFunction ?
  307. $( o.helper.apply( this.element[ 0 ], [ event ] ) ) :
  308. ( o.helper === "clone" ?
  309. this.element.clone().removeAttr( "id" ) :
  310. this.element );
  311. if ( !helper.parents( "body" ).length ) {
  312. helper.appendTo( ( o.appendTo === "parent" ?
  313. this.element[ 0 ].parentNode :
  314. o.appendTo ) );
  315. }
  316. // Http://bugs.jqueryui.com/ticket/9446
  317. // a helper function can return the original element
  318. // which wouldn't have been set to relative in _create
  319. if ( helperIsFunction && helper[ 0 ] === this.element[ 0 ] ) {
  320. this._setPositionRelative();
  321. }
  322. if ( helper[ 0 ] !== this.element[ 0 ] &&
  323. !( /(fixed|absolute)/ ).test( helper.css( "position" ) ) ) {
  324. helper.css( "position", "absolute" );
  325. }
  326. return helper;
  327. },
  328. _setPositionRelative: function() {
  329. if ( !( /^(?:r|a|f)/ ).test( this.element.css( "position" ) ) ) {
  330. this.element[ 0 ].style.position = "relative";
  331. }
  332. },
  333. _adjustOffsetFromHelper: function( obj ) {
  334. if ( typeof obj === "string" ) {
  335. obj = obj.split( " " );
  336. }
  337. if ( Array.isArray( obj ) ) {
  338. obj = { left: +obj[ 0 ], top: +obj[ 1 ] || 0 };
  339. }
  340. if ( "left" in obj ) {
  341. this.offset.click.left = obj.left + this.margins.left;
  342. }
  343. if ( "right" in obj ) {
  344. this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
  345. }
  346. if ( "top" in obj ) {
  347. this.offset.click.top = obj.top + this.margins.top;
  348. }
  349. if ( "bottom" in obj ) {
  350. this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
  351. }
  352. },
  353. _isRootNode: function( element ) {
  354. return ( /(html|body)/i ).test( element.tagName ) || element === this.document[ 0 ];
  355. },
  356. _getParentOffset: function() {
  357. //Get the offsetParent and cache its position
  358. var po = this.offsetParent.offset(),
  359. document = this.document[ 0 ];
  360. // This is a special case where we need to modify a offset calculated on start, since the
  361. // following happened:
  362. // 1. The position of the helper is absolute, so it's position is calculated based on the
  363. // next positioned parent
  364. // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't
  365. // the document, which means that the scroll is included in the initial calculation of the
  366. // offset of the parent, and never recalculated upon drag
  367. if ( this.cssPosition === "absolute" && this.scrollParent[ 0 ] !== document &&
  368. $.contains( this.scrollParent[ 0 ], this.offsetParent[ 0 ] ) ) {
  369. po.left += this.scrollParent.scrollLeft();
  370. po.top += this.scrollParent.scrollTop();
  371. }
  372. if ( this._isRootNode( this.offsetParent[ 0 ] ) ) {
  373. po = { top: 0, left: 0 };
  374. }
  375. return {
  376. top: po.top + ( parseInt( this.offsetParent.css( "borderTopWidth" ), 10 ) || 0 ),
  377. left: po.left + ( parseInt( this.offsetParent.css( "borderLeftWidth" ), 10 ) || 0 )
  378. };
  379. },
  380. _getRelativeOffset: function() {
  381. if ( this.cssPosition !== "relative" ) {
  382. return { top: 0, left: 0 };
  383. }
  384. var p = this.element.position(),
  385. scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
  386. return {
  387. top: p.top - ( parseInt( this.helper.css( "top" ), 10 ) || 0 ) +
  388. ( !scrollIsRootNode ? this.scrollParent.scrollTop() : 0 ),
  389. left: p.left - ( parseInt( this.helper.css( "left" ), 10 ) || 0 ) +
  390. ( !scrollIsRootNode ? this.scrollParent.scrollLeft() : 0 )
  391. };
  392. },
  393. _cacheMargins: function() {
  394. this.margins = {
  395. left: ( parseInt( this.element.css( "marginLeft" ), 10 ) || 0 ),
  396. top: ( parseInt( this.element.css( "marginTop" ), 10 ) || 0 ),
  397. right: ( parseInt( this.element.css( "marginRight" ), 10 ) || 0 ),
  398. bottom: ( parseInt( this.element.css( "marginBottom" ), 10 ) || 0 )
  399. };
  400. },
  401. _cacheHelperProportions: function() {
  402. this.helperProportions = {
  403. width: this.helper.outerWidth(),
  404. height: this.helper.outerHeight()
  405. };
  406. },
  407. _setContainment: function() {
  408. var isUserScrollable, c, ce,
  409. o = this.options,
  410. document = this.document[ 0 ];
  411. this.relativeContainer = null;
  412. if ( !o.containment ) {
  413. this.containment = null;
  414. return;
  415. }
  416. if ( o.containment === "window" ) {
  417. this.containment = [
  418. $( window ).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
  419. $( window ).scrollTop() - this.offset.relative.top - this.offset.parent.top,
  420. $( window ).scrollLeft() + $( window ).width() -
  421. this.helperProportions.width - this.margins.left,
  422. $( window ).scrollTop() +
  423. ( $( window ).height() || document.body.parentNode.scrollHeight ) -
  424. this.helperProportions.height - this.margins.top
  425. ];
  426. return;
  427. }
  428. if ( o.containment === "document" ) {
  429. this.containment = [
  430. 0,
  431. 0,
  432. $( document ).width() - this.helperProportions.width - this.margins.left,
  433. ( $( document ).height() || document.body.parentNode.scrollHeight ) -
  434. this.helperProportions.height - this.margins.top
  435. ];
  436. return;
  437. }
  438. if ( o.containment.constructor === Array ) {
  439. this.containment = o.containment;
  440. return;
  441. }
  442. if ( o.containment === "parent" ) {
  443. o.containment = this.helper[ 0 ].parentNode;
  444. }
  445. c = $( o.containment );
  446. ce = c[ 0 ];
  447. if ( !ce ) {
  448. return;
  449. }
  450. isUserScrollable = /(scroll|auto)/.test( c.css( "overflow" ) );
  451. this.containment = [
  452. ( parseInt( c.css( "borderLeftWidth" ), 10 ) || 0 ) +
  453. ( parseInt( c.css( "paddingLeft" ), 10 ) || 0 ),
  454. ( parseInt( c.css( "borderTopWidth" ), 10 ) || 0 ) +
  455. ( parseInt( c.css( "paddingTop" ), 10 ) || 0 ),
  456. ( isUserScrollable ? Math.max( ce.scrollWidth, ce.offsetWidth ) : ce.offsetWidth ) -
  457. ( parseInt( c.css( "borderRightWidth" ), 10 ) || 0 ) -
  458. ( parseInt( c.css( "paddingRight" ), 10 ) || 0 ) -
  459. this.helperProportions.width -
  460. this.margins.left -
  461. this.margins.right,
  462. ( isUserScrollable ? Math.max( ce.scrollHeight, ce.offsetHeight ) : ce.offsetHeight ) -
  463. ( parseInt( c.css( "borderBottomWidth" ), 10 ) || 0 ) -
  464. ( parseInt( c.css( "paddingBottom" ), 10 ) || 0 ) -
  465. this.helperProportions.height -
  466. this.margins.top -
  467. this.margins.bottom
  468. ];
  469. this.relativeContainer = c;
  470. },
  471. _convertPositionTo: function( d, pos ) {
  472. if ( !pos ) {
  473. pos = this.position;
  474. }
  475. var mod = d === "absolute" ? 1 : -1,
  476. scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
  477. return {
  478. top: (
  479. // The absolute mouse position
  480. pos.top +
  481. // Only for relative positioned nodes: Relative offset from element to offset parent
  482. this.offset.relative.top * mod +
  483. // The offsetParent's offset without borders (offset + border)
  484. this.offset.parent.top * mod -
  485. ( ( this.cssPosition === "fixed" ?
  486. -this.offset.scroll.top :
  487. ( scrollIsRootNode ? 0 : this.offset.scroll.top ) ) * mod )
  488. ),
  489. left: (
  490. // The absolute mouse position
  491. pos.left +
  492. // Only for relative positioned nodes: Relative offset from element to offset parent
  493. this.offset.relative.left * mod +
  494. // The offsetParent's offset without borders (offset + border)
  495. this.offset.parent.left * mod -
  496. ( ( this.cssPosition === "fixed" ?
  497. -this.offset.scroll.left :
  498. ( scrollIsRootNode ? 0 : this.offset.scroll.left ) ) * mod )
  499. )
  500. };
  501. },
  502. _generatePosition: function( event, constrainPosition ) {
  503. var containment, co, top, left,
  504. o = this.options,
  505. scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] ),
  506. pageX = event.pageX,
  507. pageY = event.pageY;
  508. // Cache the scroll
  509. if ( !scrollIsRootNode || !this.offset.scroll ) {
  510. this.offset.scroll = {
  511. top: this.scrollParent.scrollTop(),
  512. left: this.scrollParent.scrollLeft()
  513. };
  514. }
  515. /*
  516. * - Position constraining -
  517. * Constrain the position to a mix of grid, containment.
  518. */
  519. // If we are not dragging yet, we won't check for options
  520. if ( constrainPosition ) {
  521. if ( this.containment ) {
  522. if ( this.relativeContainer ) {
  523. co = this.relativeContainer.offset();
  524. containment = [
  525. this.containment[ 0 ] + co.left,
  526. this.containment[ 1 ] + co.top,
  527. this.containment[ 2 ] + co.left,
  528. this.containment[ 3 ] + co.top
  529. ];
  530. } else {
  531. containment = this.containment;
  532. }
  533. if ( event.pageX - this.offset.click.left < containment[ 0 ] ) {
  534. pageX = containment[ 0 ] + this.offset.click.left;
  535. }
  536. if ( event.pageY - this.offset.click.top < containment[ 1 ] ) {
  537. pageY = containment[ 1 ] + this.offset.click.top;
  538. }
  539. if ( event.pageX - this.offset.click.left > containment[ 2 ] ) {
  540. pageX = containment[ 2 ] + this.offset.click.left;
  541. }
  542. if ( event.pageY - this.offset.click.top > containment[ 3 ] ) {
  543. pageY = containment[ 3 ] + this.offset.click.top;
  544. }
  545. }
  546. if ( o.grid ) {
  547. //Check for grid elements set to 0 to prevent divide by 0 error causing invalid
  548. // argument errors in IE (see ticket #6950)
  549. top = o.grid[ 1 ] ? this.originalPageY + Math.round( ( pageY -
  550. this.originalPageY ) / o.grid[ 1 ] ) * o.grid[ 1 ] : this.originalPageY;
  551. pageY = containment ? ( ( top - this.offset.click.top >= containment[ 1 ] ||
  552. top - this.offset.click.top > containment[ 3 ] ) ?
  553. top :
  554. ( ( top - this.offset.click.top >= containment[ 1 ] ) ?
  555. top - o.grid[ 1 ] : top + o.grid[ 1 ] ) ) : top;
  556. left = o.grid[ 0 ] ? this.originalPageX +
  557. Math.round( ( pageX - this.originalPageX ) / o.grid[ 0 ] ) * o.grid[ 0 ] :
  558. this.originalPageX;
  559. pageX = containment ? ( ( left - this.offset.click.left >= containment[ 0 ] ||
  560. left - this.offset.click.left > containment[ 2 ] ) ?
  561. left :
  562. ( ( left - this.offset.click.left >= containment[ 0 ] ) ?
  563. left - o.grid[ 0 ] : left + o.grid[ 0 ] ) ) : left;
  564. }
  565. if ( o.axis === "y" ) {
  566. pageX = this.originalPageX;
  567. }
  568. if ( o.axis === "x" ) {
  569. pageY = this.originalPageY;
  570. }
  571. }
  572. return {
  573. top: (
  574. // The absolute mouse position
  575. pageY -
  576. // Click offset (relative to the element)
  577. this.offset.click.top -
  578. // Only for relative positioned nodes: Relative offset from element to offset parent
  579. this.offset.relative.top -
  580. // The offsetParent's offset without borders (offset + border)
  581. this.offset.parent.top +
  582. ( this.cssPosition === "fixed" ?
  583. -this.offset.scroll.top :
  584. ( scrollIsRootNode ? 0 : this.offset.scroll.top ) )
  585. ),
  586. left: (
  587. // The absolute mouse position
  588. pageX -
  589. // Click offset (relative to the element)
  590. this.offset.click.left -
  591. // Only for relative positioned nodes: Relative offset from element to offset parent
  592. this.offset.relative.left -
  593. // The offsetParent's offset without borders (offset + border)
  594. this.offset.parent.left +
  595. ( this.cssPosition === "fixed" ?
  596. -this.offset.scroll.left :
  597. ( scrollIsRootNode ? 0 : this.offset.scroll.left ) )
  598. )
  599. };
  600. },
  601. _clear: function() {
  602. this._removeClass( this.helper, "ui-draggable-dragging" );
  603. if ( this.helper[ 0 ] !== this.element[ 0 ] && !this.cancelHelperRemoval ) {
  604. this.helper.remove();
  605. }
  606. this.helper = null;
  607. this.cancelHelperRemoval = false;
  608. if ( this.destroyOnClear ) {
  609. this.destroy();
  610. }
  611. },
  612. // From now on bulk stuff - mainly helpers
  613. _trigger: function( type, event, ui ) {
  614. ui = ui || this._uiHash();
  615. $.ui.plugin.call( this, type, [ event, ui, this ], true );
  616. // Absolute position and offset (see #6884 ) have to be recalculated after plugins
  617. if ( /^(drag|start|stop)/.test( type ) ) {
  618. this.positionAbs = this._convertPositionTo( "absolute" );
  619. ui.offset = this.positionAbs;
  620. }
  621. return $.Widget.prototype._trigger.call( this, type, event, ui );
  622. },
  623. plugins: {},
  624. _uiHash: function() {
  625. return {
  626. helper: this.helper,
  627. position: this.position,
  628. originalPosition: this.originalPosition,
  629. offset: this.positionAbs
  630. };
  631. }
  632. } );
  633. $.ui.plugin.add( "draggable", "connectToSortable", {
  634. start: function( event, ui, draggable ) {
  635. var uiSortable = $.extend( {}, ui, {
  636. item: draggable.element
  637. } );
  638. draggable.sortables = [];
  639. $( draggable.options.connectToSortable ).each( function() {
  640. var sortable = $( this ).sortable( "instance" );
  641. if ( sortable && !sortable.options.disabled ) {
  642. draggable.sortables.push( sortable );
  643. // RefreshPositions is called at drag start to refresh the containerCache
  644. // which is used in drag. This ensures it's initialized and synchronized
  645. // with any changes that might have happened on the page since initialization.
  646. sortable.refreshPositions();
  647. sortable._trigger( "activate", event, uiSortable );
  648. }
  649. } );
  650. },
  651. stop: function( event, ui, draggable ) {
  652. var uiSortable = $.extend( {}, ui, {
  653. item: draggable.element
  654. } );
  655. draggable.cancelHelperRemoval = false;
  656. $.each( draggable.sortables, function() {
  657. var sortable = this;
  658. if ( sortable.isOver ) {
  659. sortable.isOver = 0;
  660. // Allow this sortable to handle removing the helper
  661. draggable.cancelHelperRemoval = true;
  662. sortable.cancelHelperRemoval = false;
  663. // Use _storedCSS To restore properties in the sortable,
  664. // as this also handles revert (#9675) since the draggable
  665. // may have modified them in unexpected ways (#8809)
  666. sortable._storedCSS = {
  667. position: sortable.placeholder.css( "position" ),
  668. top: sortable.placeholder.css( "top" ),
  669. left: sortable.placeholder.css( "left" )
  670. };
  671. sortable._mouseStop( event );
  672. // Once drag has ended, the sortable should return to using
  673. // its original helper, not the shared helper from draggable
  674. sortable.options.helper = sortable.options._helper;
  675. } else {
  676. // Prevent this Sortable from removing the helper.
  677. // However, don't set the draggable to remove the helper
  678. // either as another connected Sortable may yet handle the removal.
  679. sortable.cancelHelperRemoval = true;
  680. sortable._trigger( "deactivate", event, uiSortable );
  681. }
  682. } );
  683. },
  684. drag: function( event, ui, draggable ) {
  685. $.each( draggable.sortables, function() {
  686. var innermostIntersecting = false,
  687. sortable = this;
  688. // Copy over variables that sortable's _intersectsWith uses
  689. sortable.positionAbs = draggable.positionAbs;
  690. sortable.helperProportions = draggable.helperProportions;
  691. sortable.offset.click = draggable.offset.click;
  692. if ( sortable._intersectsWith( sortable.containerCache ) ) {
  693. innermostIntersecting = true;
  694. $.each( draggable.sortables, function() {
  695. // Copy over variables that sortable's _intersectsWith uses
  696. this.positionAbs = draggable.positionAbs;
  697. this.helperProportions = draggable.helperProportions;
  698. this.offset.click = draggable.offset.click;
  699. if ( this !== sortable &&
  700. this._intersectsWith( this.containerCache ) &&
  701. $.contains( sortable.element[ 0 ], this.element[ 0 ] ) ) {
  702. innermostIntersecting = false;
  703. }
  704. return innermostIntersecting;
  705. } );
  706. }
  707. if ( innermostIntersecting ) {
  708. // If it intersects, we use a little isOver variable and set it once,
  709. // so that the move-in stuff gets fired only once.
  710. if ( !sortable.isOver ) {
  711. sortable.isOver = 1;
  712. // Store draggable's parent in case we need to reappend to it later.
  713. draggable._parent = ui.helper.parent();
  714. sortable.currentItem = ui.helper
  715. .appendTo( sortable.element )
  716. .data( "ui-sortable-item", true );
  717. // Store helper option to later restore it
  718. sortable.options._helper = sortable.options.helper;
  719. sortable.options.helper = function() {
  720. return ui.helper[ 0 ];
  721. };
  722. // Fire the start events of the sortable with our passed browser event,
  723. // and our own helper (so it doesn't create a new one)
  724. event.target = sortable.currentItem[ 0 ];
  725. sortable._mouseCapture( event, true );
  726. sortable._mouseStart( event, true, true );
  727. // Because the browser event is way off the new appended portlet,
  728. // modify necessary variables to reflect the changes
  729. sortable.offset.click.top = draggable.offset.click.top;
  730. sortable.offset.click.left = draggable.offset.click.left;
  731. sortable.offset.parent.left -= draggable.offset.parent.left -
  732. sortable.offset.parent.left;
  733. sortable.offset.parent.top -= draggable.offset.parent.top -
  734. sortable.offset.parent.top;
  735. draggable._trigger( "toSortable", event );
  736. // Inform draggable that the helper is in a valid drop zone,
  737. // used solely in the revert option to handle "valid/invalid".
  738. draggable.dropped = sortable.element;
  739. // Need to refreshPositions of all sortables in the case that
  740. // adding to one sortable changes the location of the other sortables (#9675)
  741. $.each( draggable.sortables, function() {
  742. this.refreshPositions();
  743. } );
  744. // Hack so receive/update callbacks work (mostly)
  745. draggable.currentItem = draggable.element;
  746. sortable.fromOutside = draggable;
  747. }
  748. if ( sortable.currentItem ) {
  749. sortable._mouseDrag( event );
  750. // Copy the sortable's position because the draggable's can potentially reflect
  751. // a relative position, while sortable is always absolute, which the dragged
  752. // element has now become. (#8809)
  753. ui.position = sortable.position;
  754. }
  755. } else {
  756. // If it doesn't intersect with the sortable, and it intersected before,
  757. // we fake the drag stop of the sortable, but make sure it doesn't remove
  758. // the helper by using cancelHelperRemoval.
  759. if ( sortable.isOver ) {
  760. sortable.isOver = 0;
  761. sortable.cancelHelperRemoval = true;
  762. // Calling sortable's mouseStop would trigger a revert,
  763. // so revert must be temporarily false until after mouseStop is called.
  764. sortable.options._revert = sortable.options.revert;
  765. sortable.options.revert = false;
  766. sortable._trigger( "out", event, sortable._uiHash( sortable ) );
  767. sortable._mouseStop( event, true );
  768. // Restore sortable behaviors that were modfied
  769. // when the draggable entered the sortable area (#9481)
  770. sortable.options.revert = sortable.options._revert;
  771. sortable.options.helper = sortable.options._helper;
  772. if ( sortable.placeholder ) {
  773. sortable.placeholder.remove();
  774. }
  775. // Restore and recalculate the draggable's offset considering the sortable
  776. // may have modified them in unexpected ways. (#8809, #10669)
  777. ui.helper.appendTo( draggable._parent );
  778. draggable._refreshOffsets( event );
  779. ui.position = draggable._generatePosition( event, true );
  780. draggable._trigger( "fromSortable", event );
  781. // Inform draggable that the helper is no longer in a valid drop zone
  782. draggable.dropped = false;
  783. // Need to refreshPositions of all sortables just in case removing
  784. // from one sortable changes the location of other sortables (#9675)
  785. $.each( draggable.sortables, function() {
  786. this.refreshPositions();
  787. } );
  788. }
  789. }
  790. } );
  791. }
  792. } );
  793. $.ui.plugin.add( "draggable", "cursor", {
  794. start: function( event, ui, instance ) {
  795. var t = $( "body" ),
  796. o = instance.options;
  797. if ( t.css( "cursor" ) ) {
  798. o._cursor = t.css( "cursor" );
  799. }
  800. t.css( "cursor", o.cursor );
  801. },
  802. stop: function( event, ui, instance ) {
  803. var o = instance.options;
  804. if ( o._cursor ) {
  805. $( "body" ).css( "cursor", o._cursor );
  806. }
  807. }
  808. } );
  809. $.ui.plugin.add( "draggable", "opacity", {
  810. start: function( event, ui, instance ) {
  811. var t = $( ui.helper ),
  812. o = instance.options;
  813. if ( t.css( "opacity" ) ) {
  814. o._opacity = t.css( "opacity" );
  815. }
  816. t.css( "opacity", o.opacity );
  817. },
  818. stop: function( event, ui, instance ) {
  819. var o = instance.options;
  820. if ( o._opacity ) {
  821. $( ui.helper ).css( "opacity", o._opacity );
  822. }
  823. }
  824. } );
  825. $.ui.plugin.add( "draggable", "scroll", {
  826. start: function( event, ui, i ) {
  827. if ( !i.scrollParentNotHidden ) {
  828. i.scrollParentNotHidden = i.helper.scrollParent( false );
  829. }
  830. if ( i.scrollParentNotHidden[ 0 ] !== i.document[ 0 ] &&
  831. i.scrollParentNotHidden[ 0 ].tagName !== "HTML" ) {
  832. i.overflowOffset = i.scrollParentNotHidden.offset();
  833. }
  834. },
  835. drag: function( event, ui, i ) {
  836. var o = i.options,
  837. scrolled = false,
  838. scrollParent = i.scrollParentNotHidden[ 0 ],
  839. document = i.document[ 0 ];
  840. if ( scrollParent !== document && scrollParent.tagName !== "HTML" ) {
  841. if ( !o.axis || o.axis !== "x" ) {
  842. if ( ( i.overflowOffset.top + scrollParent.offsetHeight ) - event.pageY <
  843. o.scrollSensitivity ) {
  844. scrollParent.scrollTop = scrolled = scrollParent.scrollTop + o.scrollSpeed;
  845. } else if ( event.pageY - i.overflowOffset.top < o.scrollSensitivity ) {
  846. scrollParent.scrollTop = scrolled = scrollParent.scrollTop - o.scrollSpeed;
  847. }
  848. }
  849. if ( !o.axis || o.axis !== "y" ) {
  850. if ( ( i.overflowOffset.left + scrollParent.offsetWidth ) - event.pageX <
  851. o.scrollSensitivity ) {
  852. scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft + o.scrollSpeed;
  853. } else if ( event.pageX - i.overflowOffset.left < o.scrollSensitivity ) {
  854. scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft - o.scrollSpeed;
  855. }
  856. }
  857. } else {
  858. if ( !o.axis || o.axis !== "x" ) {
  859. if ( event.pageY - $( document ).scrollTop() < o.scrollSensitivity ) {
  860. scrolled = $( document ).scrollTop( $( document ).scrollTop() - o.scrollSpeed );
  861. } else if ( $( window ).height() - ( event.pageY - $( document ).scrollTop() ) <
  862. o.scrollSensitivity ) {
  863. scrolled = $( document ).scrollTop( $( document ).scrollTop() + o.scrollSpeed );
  864. }
  865. }
  866. if ( !o.axis || o.axis !== "y" ) {
  867. if ( event.pageX - $( document ).scrollLeft() < o.scrollSensitivity ) {
  868. scrolled = $( document ).scrollLeft(
  869. $( document ).scrollLeft() - o.scrollSpeed
  870. );
  871. } else if ( $( window ).width() - ( event.pageX - $( document ).scrollLeft() ) <
  872. o.scrollSensitivity ) {
  873. scrolled = $( document ).scrollLeft(
  874. $( document ).scrollLeft() + o.scrollSpeed
  875. );
  876. }
  877. }
  878. }
  879. if ( scrolled !== false && $.ui.ddmanager && !o.dropBehaviour ) {
  880. $.ui.ddmanager.prepareOffsets( i, event );
  881. }
  882. }
  883. } );
  884. $.ui.plugin.add( "draggable", "snap", {
  885. start: function( event, ui, i ) {
  886. var o = i.options;
  887. i.snapElements = [];
  888. $( o.snap.constructor !== String ? ( o.snap.items || ":data(ui-draggable)" ) : o.snap )
  889. .each( function() {
  890. var $t = $( this ),
  891. $o = $t.offset();
  892. if ( this !== i.element[ 0 ] ) {
  893. i.snapElements.push( {
  894. item: this,
  895. width: $t.outerWidth(), height: $t.outerHeight(),
  896. top: $o.top, left: $o.left
  897. } );
  898. }
  899. } );
  900. },
  901. drag: function( event, ui, inst ) {
  902. var ts, bs, ls, rs, l, r, t, b, i, first,
  903. o = inst.options,
  904. d = o.snapTolerance,
  905. x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
  906. y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
  907. for ( i = inst.snapElements.length - 1; i >= 0; i-- ) {
  908. l = inst.snapElements[ i ].left - inst.margins.left;
  909. r = l + inst.snapElements[ i ].width;
  910. t = inst.snapElements[ i ].top - inst.margins.top;
  911. b = t + inst.snapElements[ i ].height;
  912. if ( x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d ||
  913. !$.contains( inst.snapElements[ i ].item.ownerDocument,
  914. inst.snapElements[ i ].item ) ) {
  915. if ( inst.snapElements[ i ].snapping ) {
  916. if ( inst.options.snap.release ) {
  917. inst.options.snap.release.call(
  918. inst.element,
  919. event,
  920. $.extend( inst._uiHash(), { snapItem: inst.snapElements[ i ].item } )
  921. );
  922. }
  923. }
  924. inst.snapElements[ i ].snapping = false;
  925. continue;
  926. }
  927. if ( o.snapMode !== "inner" ) {
  928. ts = Math.abs( t - y2 ) <= d;
  929. bs = Math.abs( b - y1 ) <= d;
  930. ls = Math.abs( l - x2 ) <= d;
  931. rs = Math.abs( r - x1 ) <= d;
  932. if ( ts ) {
  933. ui.position.top = inst._convertPositionTo( "relative", {
  934. top: t - inst.helperProportions.height,
  935. left: 0
  936. } ).top;
  937. }
  938. if ( bs ) {
  939. ui.position.top = inst._convertPositionTo( "relative", {
  940. top: b,
  941. left: 0
  942. } ).top;
  943. }
  944. if ( ls ) {
  945. ui.position.left = inst._convertPositionTo( "relative", {
  946. top: 0,
  947. left: l - inst.helperProportions.width
  948. } ).left;
  949. }
  950. if ( rs ) {
  951. ui.position.left = inst._convertPositionTo( "relative", {
  952. top: 0,
  953. left: r
  954. } ).left;
  955. }
  956. }
  957. first = ( ts || bs || ls || rs );
  958. if ( o.snapMode !== "outer" ) {
  959. ts = Math.abs( t - y1 ) <= d;
  960. bs = Math.abs( b - y2 ) <= d;
  961. ls = Math.abs( l - x1 ) <= d;
  962. rs = Math.abs( r - x2 ) <= d;
  963. if ( ts ) {
  964. ui.position.top = inst._convertPositionTo( "relative", {
  965. top: t,
  966. left: 0
  967. } ).top;
  968. }
  969. if ( bs ) {
  970. ui.position.top = inst._convertPositionTo( "relative", {
  971. top: b - inst.helperProportions.height,
  972. left: 0
  973. } ).top;
  974. }
  975. if ( ls ) {
  976. ui.position.left = inst._convertPositionTo( "relative", {
  977. top: 0,
  978. left: l
  979. } ).left;
  980. }
  981. if ( rs ) {
  982. ui.position.left = inst._convertPositionTo( "relative", {
  983. top: 0,
  984. left: r - inst.helperProportions.width
  985. } ).left;
  986. }
  987. }
  988. if ( !inst.snapElements[ i ].snapping && ( ts || bs || ls || rs || first ) ) {
  989. if ( inst.options.snap.snap ) {
  990. inst.options.snap.snap.call(
  991. inst.element,
  992. event,
  993. $.extend( inst._uiHash(), {
  994. snapItem: inst.snapElements[ i ].item
  995. } ) );
  996. }
  997. }
  998. inst.snapElements[ i ].snapping = ( ts || bs || ls || rs || first );
  999. }
  1000. }
  1001. } );
  1002. $.ui.plugin.add( "draggable", "stack", {
  1003. start: function( event, ui, instance ) {
  1004. var min,
  1005. o = instance.options,
  1006. group = $.makeArray( $( o.stack ) ).sort( function( a, b ) {
  1007. return ( parseInt( $( a ).css( "zIndex" ), 10 ) || 0 ) -
  1008. ( parseInt( $( b ).css( "zIndex" ), 10 ) || 0 );
  1009. } );
  1010. if ( !group.length ) {
  1011. return;
  1012. }
  1013. min = parseInt( $( group[ 0 ] ).css( "zIndex" ), 10 ) || 0;
  1014. $( group ).each( function( i ) {
  1015. $( this ).css( "zIndex", min + i );
  1016. } );
  1017. this.css( "zIndex", ( min + group.length ) );
  1018. }
  1019. } );
  1020. $.ui.plugin.add( "draggable", "zIndex", {
  1021. start: function( event, ui, instance ) {
  1022. var t = $( ui.helper ),
  1023. o = instance.options;
  1024. if ( t.css( "zIndex" ) ) {
  1025. o._zIndex = t.css( "zIndex" );
  1026. }
  1027. t.css( "zIndex", o.zIndex );
  1028. },
  1029. stop: function( event, ui, instance ) {
  1030. var o = instance.options;
  1031. if ( o._zIndex ) {
  1032. $( ui.helper ).css( "zIndex", o._zIndex );
  1033. }
  1034. }
  1035. } );
  1036. return $.ui.draggable;
  1037. } );