draggable.js 34 KB

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