draggable.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*!
  2. * jQuery UI Draggable 1.11.2
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2014 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.2",
  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. sortable.currentItem = ui.helper
  659. .appendTo( sortable.element )
  660. .data( "ui-sortable-item", true );
  661. // Store helper option to later restore it
  662. sortable.options._helper = sortable.options.helper;
  663. sortable.options.helper = function() {
  664. return ui.helper[ 0 ];
  665. };
  666. // Fire the start events of the sortable with our passed browser event,
  667. // and our own helper (so it doesn't create a new one)
  668. event.target = sortable.currentItem[ 0 ];
  669. sortable._mouseCapture( event, true );
  670. sortable._mouseStart( event, true, true );
  671. // Because the browser event is way off the new appended portlet,
  672. // modify necessary variables to reflect the changes
  673. sortable.offset.click.top = draggable.offset.click.top;
  674. sortable.offset.click.left = draggable.offset.click.left;
  675. sortable.offset.parent.left -= draggable.offset.parent.left -
  676. sortable.offset.parent.left;
  677. sortable.offset.parent.top -= draggable.offset.parent.top -
  678. sortable.offset.parent.top;
  679. draggable._trigger( "toSortable", event );
  680. // Inform draggable that the helper is in a valid drop zone,
  681. // used solely in the revert option to handle "valid/invalid".
  682. draggable.dropped = sortable.element;
  683. // Need to refreshPositions of all sortables in the case that
  684. // adding to one sortable changes the location of the other sortables (#9675)
  685. $.each( draggable.sortables, function() {
  686. this.refreshPositions();
  687. });
  688. // hack so receive/update callbacks work (mostly)
  689. draggable.currentItem = draggable.element;
  690. sortable.fromOutside = draggable;
  691. }
  692. if ( sortable.currentItem ) {
  693. sortable._mouseDrag( event );
  694. // Copy the sortable's position because the draggable's can potentially reflect
  695. // a relative position, while sortable is always absolute, which the dragged
  696. // element has now become. (#8809)
  697. ui.position = sortable.position;
  698. }
  699. } else {
  700. // If it doesn't intersect with the sortable, and it intersected before,
  701. // we fake the drag stop of the sortable, but make sure it doesn't remove
  702. // the helper by using cancelHelperRemoval.
  703. if ( sortable.isOver ) {
  704. sortable.isOver = 0;
  705. sortable.cancelHelperRemoval = true;
  706. // Calling sortable's mouseStop would trigger a revert,
  707. // so revert must be temporarily false until after mouseStop is called.
  708. sortable.options._revert = sortable.options.revert;
  709. sortable.options.revert = false;
  710. sortable._trigger( "out", event, sortable._uiHash( sortable ) );
  711. sortable._mouseStop( event, true );
  712. // restore sortable behaviors that were modfied
  713. // when the draggable entered the sortable area (#9481)
  714. sortable.options.revert = sortable.options._revert;
  715. sortable.options.helper = sortable.options._helper;
  716. if ( sortable.placeholder ) {
  717. sortable.placeholder.remove();
  718. }
  719. // Recalculate the draggable's offset considering the sortable
  720. // may have modified them in unexpected ways (#8809)
  721. draggable._refreshOffsets( event );
  722. ui.position = draggable._generatePosition( event, true );
  723. draggable._trigger( "fromSortable", event );
  724. // Inform draggable that the helper is no longer in a valid drop zone
  725. draggable.dropped = false;
  726. // Need to refreshPositions of all sortables just in case removing
  727. // from one sortable changes the location of other sortables (#9675)
  728. $.each( draggable.sortables, function() {
  729. this.refreshPositions();
  730. });
  731. }
  732. }
  733. });
  734. }
  735. });
  736. $.ui.plugin.add("draggable", "cursor", {
  737. start: function( event, ui, instance ) {
  738. var t = $( "body" ),
  739. o = instance.options;
  740. if (t.css("cursor")) {
  741. o._cursor = t.css("cursor");
  742. }
  743. t.css("cursor", o.cursor);
  744. },
  745. stop: function( event, ui, instance ) {
  746. var o = instance.options;
  747. if (o._cursor) {
  748. $("body").css("cursor", o._cursor);
  749. }
  750. }
  751. });
  752. $.ui.plugin.add("draggable", "opacity", {
  753. start: function( event, ui, instance ) {
  754. var t = $( ui.helper ),
  755. o = instance.options;
  756. if (t.css("opacity")) {
  757. o._opacity = t.css("opacity");
  758. }
  759. t.css("opacity", o.opacity);
  760. },
  761. stop: function( event, ui, instance ) {
  762. var o = instance.options;
  763. if (o._opacity) {
  764. $(ui.helper).css("opacity", o._opacity);
  765. }
  766. }
  767. });
  768. $.ui.plugin.add("draggable", "scroll", {
  769. start: function( event, ui, i ) {
  770. if ( !i.scrollParentNotHidden ) {
  771. i.scrollParentNotHidden = i.helper.scrollParent( false );
  772. }
  773. if ( i.scrollParentNotHidden[ 0 ] !== i.document[ 0 ] && i.scrollParentNotHidden[ 0 ].tagName !== "HTML" ) {
  774. i.overflowOffset = i.scrollParentNotHidden.offset();
  775. }
  776. },
  777. drag: function( event, ui, i ) {
  778. var o = i.options,
  779. scrolled = false,
  780. scrollParent = i.scrollParentNotHidden[ 0 ],
  781. document = i.document[ 0 ];
  782. if ( scrollParent !== document && scrollParent.tagName !== "HTML" ) {
  783. if ( !o.axis || o.axis !== "x" ) {
  784. if ( ( i.overflowOffset.top + scrollParent.offsetHeight ) - event.pageY < o.scrollSensitivity ) {
  785. scrollParent.scrollTop = scrolled = scrollParent.scrollTop + o.scrollSpeed;
  786. } else if ( event.pageY - i.overflowOffset.top < o.scrollSensitivity ) {
  787. scrollParent.scrollTop = scrolled = scrollParent.scrollTop - o.scrollSpeed;
  788. }
  789. }
  790. if ( !o.axis || o.axis !== "y" ) {
  791. if ( ( i.overflowOffset.left + scrollParent.offsetWidth ) - event.pageX < o.scrollSensitivity ) {
  792. scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft + o.scrollSpeed;
  793. } else if ( event.pageX - i.overflowOffset.left < o.scrollSensitivity ) {
  794. scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft - o.scrollSpeed;
  795. }
  796. }
  797. } else {
  798. if (!o.axis || o.axis !== "x") {
  799. if (event.pageY - $(document).scrollTop() < o.scrollSensitivity) {
  800. scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
  801. } else if ($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) {
  802. scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
  803. }
  804. }
  805. if (!o.axis || o.axis !== "y") {
  806. if (event.pageX - $(document).scrollLeft() < o.scrollSensitivity) {
  807. scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
  808. } else if ($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) {
  809. scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
  810. }
  811. }
  812. }
  813. if (scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) {
  814. $.ui.ddmanager.prepareOffsets(i, event);
  815. }
  816. }
  817. });
  818. $.ui.plugin.add("draggable", "snap", {
  819. start: function( event, ui, i ) {
  820. var o = i.options;
  821. i.snapElements = [];
  822. $(o.snap.constructor !== String ? ( o.snap.items || ":data(ui-draggable)" ) : o.snap).each(function() {
  823. var $t = $(this),
  824. $o = $t.offset();
  825. if (this !== i.element[0]) {
  826. i.snapElements.push({
  827. item: this,
  828. width: $t.outerWidth(), height: $t.outerHeight(),
  829. top: $o.top, left: $o.left
  830. });
  831. }
  832. });
  833. },
  834. drag: function( event, ui, inst ) {
  835. var ts, bs, ls, rs, l, r, t, b, i, first,
  836. o = inst.options,
  837. d = o.snapTolerance,
  838. x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
  839. y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
  840. for (i = inst.snapElements.length - 1; i >= 0; i--){
  841. l = inst.snapElements[i].left - inst.margins.left;
  842. r = l + inst.snapElements[i].width;
  843. t = inst.snapElements[i].top - inst.margins.top;
  844. b = t + inst.snapElements[i].height;
  845. if ( x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d || !$.contains( inst.snapElements[ i ].item.ownerDocument, inst.snapElements[ i ].item ) ) {
  846. if (inst.snapElements[i].snapping) {
  847. (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
  848. }
  849. inst.snapElements[i].snapping = false;
  850. continue;
  851. }
  852. if (o.snapMode !== "inner") {
  853. ts = Math.abs(t - y2) <= d;
  854. bs = Math.abs(b - y1) <= d;
  855. ls = Math.abs(l - x2) <= d;
  856. rs = Math.abs(r - x1) <= d;
  857. if (ts) {
  858. ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top;
  859. }
  860. if (bs) {
  861. ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top;
  862. }
  863. if (ls) {
  864. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left;
  865. }
  866. if (rs) {
  867. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left;
  868. }
  869. }
  870. first = (ts || bs || ls || rs);
  871. if (o.snapMode !== "outer") {
  872. ts = Math.abs(t - y1) <= d;
  873. bs = Math.abs(b - y2) <= d;
  874. ls = Math.abs(l - x1) <= d;
  875. rs = Math.abs(r - x2) <= d;
  876. if (ts) {
  877. ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top;
  878. }
  879. if (bs) {
  880. ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top;
  881. }
  882. if (ls) {
  883. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left;
  884. }
  885. if (rs) {
  886. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left;
  887. }
  888. }
  889. if (!inst.snapElements[i].snapping && (ts || bs || ls || rs || first)) {
  890. (inst.options.snap.snap && inst.options.snap.snap.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
  891. }
  892. inst.snapElements[i].snapping = (ts || bs || ls || rs || first);
  893. }
  894. }
  895. });
  896. $.ui.plugin.add("draggable", "stack", {
  897. start: function( event, ui, instance ) {
  898. var min,
  899. o = instance.options,
  900. group = $.makeArray($(o.stack)).sort(function(a, b) {
  901. return (parseInt($(a).css("zIndex"), 10) || 0) - (parseInt($(b).css("zIndex"), 10) || 0);
  902. });
  903. if (!group.length) { return; }
  904. min = parseInt($(group[0]).css("zIndex"), 10) || 0;
  905. $(group).each(function(i) {
  906. $(this).css("zIndex", min + i);
  907. });
  908. this.css("zIndex", (min + group.length));
  909. }
  910. });
  911. $.ui.plugin.add("draggable", "zIndex", {
  912. start: function( event, ui, instance ) {
  913. var t = $( ui.helper ),
  914. o = instance.options;
  915. if (t.css("zIndex")) {
  916. o._zIndex = t.css("zIndex");
  917. }
  918. t.css("zIndex", o.zIndex);
  919. },
  920. stop: function( event, ui, instance ) {
  921. var o = instance.options;
  922. if (o._zIndex) {
  923. $(ui.helper).css("zIndex", o._zIndex);
  924. }
  925. }
  926. });
  927. return $.ui.draggable;
  928. }));