draggable.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*!
  2. * jQuery UI Draggable @VERSION
  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: "@VERSION",
  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" && !(/^(?:r|a|f)/).test(this.element.css("position"))) {
  60. this.element[0].style.position = "relative";
  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 document = this.document[ 0 ],
  89. o = this.options;
  90. // support: IE9
  91. // IE9 throws an "Unspecified error" accessing document.activeElement from an <iframe>
  92. try {
  93. // Support: IE9+
  94. // If the <body> is blurred, IE will switch windows, see #9520
  95. if ( document.activeElement && document.activeElement.nodeName.toLowerCase() !== "body" ) {
  96. // Blur any element that currently has focus, see #4261
  97. $( document.activeElement ).blur();
  98. }
  99. } catch ( error ) {}
  100. // among others, prevent a drag on a resizable-handle
  101. if (this.helper || o.disabled || $(event.target).closest(".ui-resizable-handle").length > 0) {
  102. return false;
  103. }
  104. //Quit if we're not on a valid handle
  105. this.handle = this._getHandle(event);
  106. if (!this.handle) {
  107. return false;
  108. }
  109. $(o.iframeFix === true ? "iframe" : o.iframeFix).each(function() {
  110. $("<div class='ui-draggable-iframeFix' style='background: #fff;'></div>")
  111. .css({
  112. width: this.offsetWidth + "px", height: this.offsetHeight + "px",
  113. position: "absolute", opacity: "0.001", zIndex: 1000
  114. })
  115. .css($(this).offset())
  116. .appendTo("body");
  117. });
  118. return true;
  119. },
  120. _mouseStart: function(event) {
  121. var o = this.options;
  122. //Create and append the visible helper
  123. this.helper = this._createHelper(event);
  124. this.helper.addClass("ui-draggable-dragging");
  125. //Cache the helper size
  126. this._cacheHelperProportions();
  127. //If ddmanager is used for droppables, set the global draggable
  128. if ($.ui.ddmanager) {
  129. $.ui.ddmanager.current = this;
  130. }
  131. /*
  132. * - Position generation -
  133. * This block generates everything position related - it's the core of draggables.
  134. */
  135. //Cache the margins of the original element
  136. this._cacheMargins();
  137. //Store the helper's css position
  138. this.cssPosition = this.helper.css( "position" );
  139. this.scrollParent = this.helper.scrollParent();
  140. this.offsetParent = this.helper.offsetParent();
  141. this.offsetParentCssPosition = this.offsetParent.css( "position" );
  142. //The element's absolute position on the page minus margins
  143. this.offset = this.positionAbs = this.element.offset();
  144. this.offset = {
  145. top: this.offset.top - this.margins.top,
  146. left: this.offset.left - this.margins.left
  147. };
  148. //Reset scroll cache
  149. this.offset.scroll = false;
  150. $.extend(this.offset, {
  151. click: { //Where the click happened, relative to the element
  152. left: event.pageX - this.offset.left,
  153. top: event.pageY - this.offset.top
  154. },
  155. parent: this._getParentOffset(),
  156. relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
  157. });
  158. //Generate the original position
  159. this.originalPosition = this.position = this._generatePosition( event, false );
  160. this.originalPageX = event.pageX;
  161. this.originalPageY = event.pageY;
  162. //Adjust the mouse offset relative to the helper if "cursorAt" is supplied
  163. (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
  164. //Set a containment if given in the options
  165. this._setContainment();
  166. //Trigger event + callbacks
  167. if (this._trigger("start", event) === false) {
  168. this._clear();
  169. return false;
  170. }
  171. //Recache the helper size
  172. this._cacheHelperProportions();
  173. //Prepare the droppable offsets
  174. if ($.ui.ddmanager && !o.dropBehaviour) {
  175. $.ui.ddmanager.prepareOffsets(this, event);
  176. }
  177. this._mouseDrag(event, true); //Execute the drag once - this causes the helper not to be visible before getting its correct position
  178. //If the ddmanager is used for droppables, inform the manager that dragging has started (see #5003)
  179. if ( $.ui.ddmanager ) {
  180. $.ui.ddmanager.dragStart(this, event);
  181. }
  182. return true;
  183. },
  184. _mouseDrag: function(event, noPropagation) {
  185. // reset any necessary cached properties (see #5009)
  186. if ( this.offsetParentCssPosition === "fixed" ) {
  187. this.offset.parent = this._getParentOffset();
  188. }
  189. //Compute the helpers position
  190. this.position = this._generatePosition( event, true );
  191. this.positionAbs = this._convertPositionTo("absolute");
  192. //Call plugins and callbacks and use the resulting position if something is returned
  193. if (!noPropagation) {
  194. var ui = this._uiHash();
  195. if (this._trigger("drag", event, ui) === false) {
  196. this._mouseUp({});
  197. return false;
  198. }
  199. this.position = ui.position;
  200. }
  201. this.helper[ 0 ].style.left = this.position.left + "px";
  202. this.helper[ 0 ].style.top = this.position.top + "px";
  203. if ($.ui.ddmanager) {
  204. $.ui.ddmanager.drag(this, event);
  205. }
  206. return false;
  207. },
  208. _mouseStop: function(event) {
  209. //If we are using droppables, inform the manager about the drop
  210. var that = this,
  211. dropped = false;
  212. if ($.ui.ddmanager && !this.options.dropBehaviour) {
  213. dropped = $.ui.ddmanager.drop(this, event);
  214. }
  215. //if a drop comes from outside (a sortable)
  216. if (this.dropped) {
  217. dropped = this.dropped;
  218. this.dropped = false;
  219. }
  220. 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))) {
  221. $(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
  222. if (that._trigger("stop", event) !== false) {
  223. that._clear();
  224. }
  225. });
  226. } else {
  227. if (this._trigger("stop", event) !== false) {
  228. this._clear();
  229. }
  230. }
  231. return false;
  232. },
  233. _mouseUp: function(event) {
  234. //Remove frame helpers
  235. $("div.ui-draggable-iframeFix").each(function() {
  236. this.parentNode.removeChild(this);
  237. });
  238. //If the ddmanager is used for droppables, inform the manager that dragging has stopped (see #5003)
  239. if ( $.ui.ddmanager ) {
  240. $.ui.ddmanager.dragStop(this, event);
  241. }
  242. // The interaction is over; whether or not the click resulted in a drag, focus the element
  243. this.element.focus();
  244. return $.ui.mouse.prototype._mouseUp.call(this, event);
  245. },
  246. cancel: function() {
  247. if (this.helper.is(".ui-draggable-dragging")) {
  248. this._mouseUp({});
  249. } else {
  250. this._clear();
  251. }
  252. return this;
  253. },
  254. _getHandle: function(event) {
  255. return this.options.handle ?
  256. !!$( event.target ).closest( this.element.find( this.options.handle ) ).length :
  257. true;
  258. },
  259. _setHandleClassName: function() {
  260. this.handleElement = this.options.handle ?
  261. this.element.find( this.options.handle ) : this.element;
  262. this.handleElement.addClass( "ui-draggable-handle" );
  263. },
  264. _removeHandleClassName: function() {
  265. this.handleElement.removeClass( "ui-draggable-handle" );
  266. },
  267. _createHelper: function(event) {
  268. var o = this.options,
  269. helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[ 0 ], [ event ])) : (o.helper === "clone" ? this.element.clone().removeAttr("id") : this.element);
  270. if (!helper.parents("body").length) {
  271. helper.appendTo((o.appendTo === "parent" ? this.element[0].parentNode : o.appendTo));
  272. }
  273. if (helper[0] !== this.element[0] && !(/(fixed|absolute)/).test(helper.css("position"))) {
  274. helper.css("position", "absolute");
  275. }
  276. return helper;
  277. },
  278. _adjustOffsetFromHelper: function(obj) {
  279. if (typeof obj === "string") {
  280. obj = obj.split(" ");
  281. }
  282. if ($.isArray(obj)) {
  283. obj = { left: +obj[0], top: +obj[1] || 0 };
  284. }
  285. if ("left" in obj) {
  286. this.offset.click.left = obj.left + this.margins.left;
  287. }
  288. if ("right" in obj) {
  289. this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
  290. }
  291. if ("top" in obj) {
  292. this.offset.click.top = obj.top + this.margins.top;
  293. }
  294. if ("bottom" in obj) {
  295. this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
  296. }
  297. },
  298. _isRootNode: function( element ) {
  299. return ( /(html|body)/i ).test( element.tagName ) || element === this.document[ 0 ];
  300. },
  301. _getParentOffset: function() {
  302. //Get the offsetParent and cache its position
  303. var po = this.offsetParent.offset(),
  304. document = this.document[ 0 ];
  305. // This is a special case where we need to modify a offset calculated on start, since the following happened:
  306. // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
  307. // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
  308. // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
  309. if (this.cssPosition === "absolute" && this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) {
  310. po.left += this.scrollParent.scrollLeft();
  311. po.top += this.scrollParent.scrollTop();
  312. }
  313. if ( this._isRootNode( this.offsetParent[ 0 ] ) ) {
  314. po = { top: 0, left: 0 };
  315. }
  316. return {
  317. top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
  318. left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
  319. };
  320. },
  321. _getRelativeOffset: function() {
  322. if ( this.cssPosition !== "relative" ) {
  323. return { top: 0, left: 0 };
  324. }
  325. var p = this.element.position(),
  326. scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
  327. return {
  328. top: p.top - ( parseInt(this.helper.css( "top" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollTop() : 0 ),
  329. left: p.left - ( parseInt(this.helper.css( "left" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollLeft() : 0 )
  330. };
  331. },
  332. _cacheMargins: function() {
  333. this.margins = {
  334. left: (parseInt(this.element.css("marginLeft"),10) || 0),
  335. top: (parseInt(this.element.css("marginTop"),10) || 0),
  336. right: (parseInt(this.element.css("marginRight"),10) || 0),
  337. bottom: (parseInt(this.element.css("marginBottom"),10) || 0)
  338. };
  339. },
  340. _cacheHelperProportions: function() {
  341. this.helperProportions = {
  342. width: this.helper.outerWidth(),
  343. height: this.helper.outerHeight()
  344. };
  345. },
  346. _setContainment: function() {
  347. var over, c, ce,
  348. o = this.options,
  349. document = this.document[ 0 ];
  350. this.relative_container = null;
  351. if ( !o.containment ) {
  352. this.containment = null;
  353. return;
  354. }
  355. if ( o.containment === "window" ) {
  356. this.containment = [
  357. $( window ).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
  358. $( window ).scrollTop() - this.offset.relative.top - this.offset.parent.top,
  359. $( window ).scrollLeft() + $( window ).width() - this.helperProportions.width - this.margins.left,
  360. $( window ).scrollTop() + ( $( window ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
  361. ];
  362. return;
  363. }
  364. if ( o.containment === "document") {
  365. this.containment = [
  366. 0,
  367. 0,
  368. $( document ).width() - this.helperProportions.width - this.margins.left,
  369. ( $( document ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
  370. ];
  371. return;
  372. }
  373. if ( o.containment.constructor === Array ) {
  374. this.containment = o.containment;
  375. return;
  376. }
  377. if ( o.containment === "parent" ) {
  378. o.containment = this.helper[ 0 ].parentNode;
  379. }
  380. c = $( o.containment );
  381. ce = c[ 0 ];
  382. if ( !ce ) {
  383. return;
  384. }
  385. over = c.css( "overflow" ) !== "hidden";
  386. this.containment = [
  387. ( parseInt( c.css( "borderLeftWidth" ), 10 ) || 0 ) + ( parseInt( c.css( "paddingLeft" ), 10 ) || 0 ),
  388. ( parseInt( c.css( "borderTopWidth" ), 10 ) || 0 ) + ( parseInt( c.css( "paddingTop" ), 10 ) || 0 ),
  389. ( over ? Math.max( ce.scrollWidth, ce.offsetWidth ) : ce.offsetWidth ) - ( parseInt( c.css( "borderRightWidth" ), 10 ) || 0 ) - ( parseInt( c.css( "paddingRight" ), 10 ) || 0 ) - this.helperProportions.width - this.margins.left - this.margins.right,
  390. ( over ? Math.max( ce.scrollHeight, ce.offsetHeight ) : ce.offsetHeight ) - ( parseInt( c.css( "borderBottomWidth" ), 10 ) || 0 ) - ( parseInt( c.css( "paddingBottom" ), 10 ) || 0 ) - this.helperProportions.height - this.margins.top - this.margins.bottom
  391. ];
  392. this.relative_container = c;
  393. },
  394. _convertPositionTo: function(d, pos) {
  395. if (!pos) {
  396. pos = this.position;
  397. }
  398. var mod = d === "absolute" ? 1 : -1,
  399. scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
  400. return {
  401. top: (
  402. pos.top + // The absolute mouse position
  403. this.offset.relative.top * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
  404. this.offset.parent.top * mod - // The offsetParent's offset without borders (offset + border)
  405. ( ( this.cssPosition === "fixed" ? -this.offset.scroll.top : ( scrollIsRootNode ? 0 : this.offset.scroll.top ) ) * mod)
  406. ),
  407. left: (
  408. pos.left + // The absolute mouse position
  409. this.offset.relative.left * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
  410. this.offset.parent.left * mod - // The offsetParent's offset without borders (offset + border)
  411. ( ( this.cssPosition === "fixed" ? -this.offset.scroll.left : ( scrollIsRootNode ? 0 : this.offset.scroll.left ) ) * mod)
  412. )
  413. };
  414. },
  415. _generatePosition: function( event, constrainPosition ) {
  416. var containment, co, top, left,
  417. o = this.options,
  418. scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] ),
  419. pageX = event.pageX,
  420. pageY = event.pageY;
  421. // Cache the scroll
  422. if ( !scrollIsRootNode || !this.offset.scroll ) {
  423. this.offset.scroll = {
  424. top: this.scrollParent.scrollTop(),
  425. left: this.scrollParent.scrollLeft()
  426. };
  427. }
  428. /*
  429. * - Position constraining -
  430. * Constrain the position to a mix of grid, containment.
  431. */
  432. // If we are not dragging yet, we won't check for options
  433. if ( constrainPosition ) {
  434. if ( this.containment ) {
  435. if ( this.relative_container ){
  436. co = this.relative_container.offset();
  437. containment = [
  438. this.containment[ 0 ] + co.left,
  439. this.containment[ 1 ] + co.top,
  440. this.containment[ 2 ] + co.left,
  441. this.containment[ 3 ] + co.top
  442. ];
  443. } else {
  444. containment = this.containment;
  445. }
  446. if (event.pageX - this.offset.click.left < containment[0]) {
  447. pageX = containment[0] + this.offset.click.left;
  448. }
  449. if (event.pageY - this.offset.click.top < containment[1]) {
  450. pageY = containment[1] + this.offset.click.top;
  451. }
  452. if (event.pageX - this.offset.click.left > containment[2]) {
  453. pageX = containment[2] + this.offset.click.left;
  454. }
  455. if (event.pageY - this.offset.click.top > containment[3]) {
  456. pageY = containment[3] + this.offset.click.top;
  457. }
  458. }
  459. if (o.grid) {
  460. //Check for grid elements set to 0 to prevent divide by 0 error causing invalid argument errors in IE (see ticket #6950)
  461. top = o.grid[1] ? this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1] : this.originalPageY;
  462. 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;
  463. left = o.grid[0] ? this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0] : this.originalPageX;
  464. 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;
  465. }
  466. if ( o.axis === "y" ) {
  467. pageX = this.originalPageX;
  468. }
  469. if ( o.axis === "x" ) {
  470. pageY = this.originalPageY;
  471. }
  472. }
  473. return {
  474. top: (
  475. pageY - // The absolute mouse position
  476. this.offset.click.top - // Click offset (relative to the element)
  477. this.offset.relative.top - // Only for relative positioned nodes: Relative offset from element to offset parent
  478. this.offset.parent.top + // The offsetParent's offset without borders (offset + border)
  479. ( this.cssPosition === "fixed" ? -this.offset.scroll.top : ( scrollIsRootNode ? 0 : this.offset.scroll.top ) )
  480. ),
  481. left: (
  482. pageX - // The absolute mouse position
  483. this.offset.click.left - // Click offset (relative to the element)
  484. this.offset.relative.left - // Only for relative positioned nodes: Relative offset from element to offset parent
  485. this.offset.parent.left + // The offsetParent's offset without borders (offset + border)
  486. ( this.cssPosition === "fixed" ? -this.offset.scroll.left : ( scrollIsRootNode ? 0 : this.offset.scroll.left ) )
  487. )
  488. };
  489. },
  490. _clear: function() {
  491. this.helper.removeClass("ui-draggable-dragging");
  492. if (this.helper[0] !== this.element[0] && !this.cancelHelperRemoval) {
  493. this.helper.remove();
  494. }
  495. this.helper = null;
  496. this.cancelHelperRemoval = false;
  497. if ( this.destroyOnClear ) {
  498. this.destroy();
  499. }
  500. },
  501. // From now on bulk stuff - mainly helpers
  502. _trigger: function(type, event, ui) {
  503. ui = ui || this._uiHash();
  504. $.ui.plugin.call( this, type, [ event, ui, this ], true );
  505. //The absolute position has to be recalculated after plugins
  506. if (type === "drag") {
  507. this.positionAbs = this._convertPositionTo("absolute");
  508. }
  509. return $.Widget.prototype._trigger.call(this, type, event, ui);
  510. },
  511. plugins: {},
  512. _uiHash: function() {
  513. return {
  514. helper: this.helper,
  515. position: this.position,
  516. originalPosition: this.originalPosition,
  517. offset: this.positionAbs
  518. };
  519. }
  520. });
  521. $.ui.plugin.add("draggable", "connectToSortable", {
  522. start: function( event, ui, inst ) {
  523. var o = inst.options,
  524. uiSortable = $.extend({}, ui, { item: inst.element });
  525. inst.sortables = [];
  526. $(o.connectToSortable).each(function() {
  527. var sortable = $( this ).sortable( "instance" );
  528. if (sortable && !sortable.options.disabled) {
  529. inst.sortables.push({
  530. instance: sortable,
  531. shouldRevert: sortable.options.revert
  532. });
  533. sortable.refreshPositions(); // Call the sortable's refreshPositions at drag start to refresh the containerCache since the sortable container cache is used in drag and needs to be up to date (this will ensure it's initialised as well as being kept in step with any changes that might have happened on the page).
  534. sortable._trigger("activate", event, uiSortable);
  535. }
  536. });
  537. },
  538. stop: function( event, ui, inst ) {
  539. //If we are still over the sortable, we fake the stop event of the sortable, but also remove helper
  540. var uiSortable = $.extend( {}, ui, {
  541. item: inst.element
  542. });
  543. $.each(inst.sortables, function() {
  544. if (this.instance.isOver) {
  545. this.instance.isOver = 0;
  546. inst.cancelHelperRemoval = true; //Don't remove the helper in the draggable instance
  547. this.instance.cancelHelperRemoval = false; //Remove it in the sortable instance (so sortable plugins like revert still work)
  548. //The sortable revert is supported, and we have to set a temporary dropped variable on the draggable to support revert: "valid/invalid"
  549. if (this.shouldRevert) {
  550. this.instance.options.revert = this.shouldRevert;
  551. }
  552. //Trigger the stop of the sortable
  553. this.instance._mouseStop(event);
  554. this.instance.options.helper = this.instance.options._helper;
  555. //If the helper has been the original item, restore properties in the sortable
  556. if (inst.options.helper === "original") {
  557. this.instance.currentItem.css({ top: "auto", left: "auto" });
  558. }
  559. } else {
  560. this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
  561. this.instance._trigger("deactivate", event, uiSortable);
  562. }
  563. });
  564. },
  565. drag: function( event, ui, inst ) {
  566. var that = this;
  567. $.each(inst.sortables, function() {
  568. var innermostIntersecting = false,
  569. thisSortable = this;
  570. //Copy over some variables to allow calling the sortable's native _intersectsWith
  571. this.instance.positionAbs = inst.positionAbs;
  572. this.instance.helperProportions = inst.helperProportions;
  573. this.instance.offset.click = inst.offset.click;
  574. if (this.instance._intersectsWith(this.instance.containerCache)) {
  575. innermostIntersecting = true;
  576. $.each(inst.sortables, function() {
  577. this.instance.positionAbs = inst.positionAbs;
  578. this.instance.helperProportions = inst.helperProportions;
  579. this.instance.offset.click = inst.offset.click;
  580. if (this !== thisSortable &&
  581. this.instance._intersectsWith(this.instance.containerCache) &&
  582. $.contains(thisSortable.instance.element[0], this.instance.element[0])
  583. ) {
  584. innermostIntersecting = false;
  585. }
  586. return innermostIntersecting;
  587. });
  588. }
  589. if (innermostIntersecting) {
  590. //If it intersects, we use a little isOver variable and set it once, so our move-in stuff gets fired only once
  591. if (!this.instance.isOver) {
  592. this.instance.isOver = 1;
  593. //Now we fake the start of dragging for the sortable instance,
  594. //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
  595. //We can then fire the start event of the sortable with our passed browser event, and our own helper (so it doesn't create a new one)
  596. this.instance.currentItem = $(that).clone().removeAttr("id").appendTo(this.instance.element).data("ui-sortable-item", true);
  597. this.instance.options._helper = this.instance.options.helper; //Store helper option to later restore it
  598. this.instance.options.helper = function() { return ui.helper[0]; };
  599. event.target = this.instance.currentItem[0];
  600. this.instance._mouseCapture(event, true);
  601. this.instance._mouseStart(event, true, true);
  602. //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
  603. this.instance.offset.click.top = inst.offset.click.top;
  604. this.instance.offset.click.left = inst.offset.click.left;
  605. this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
  606. this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
  607. inst._trigger("toSortable", event);
  608. inst.dropped = this.instance.element; //draggable revert needs that
  609. //hack so receive/update callbacks work (mostly)
  610. inst.currentItem = inst.element;
  611. this.instance.fromOutside = inst;
  612. }
  613. //Provided we did all the previous steps, we can fire the drag event of the sortable on every draggable drag, when it intersects with the sortable
  614. if (this.instance.currentItem) {
  615. this.instance._mouseDrag(event);
  616. }
  617. } else {
  618. //If it doesn't intersect with the sortable, and it intersected before,
  619. //we fake the drag stop of the sortable, but make sure it doesn't remove the helper by using cancelHelperRemoval
  620. if (this.instance.isOver) {
  621. this.instance.isOver = 0;
  622. this.instance.cancelHelperRemoval = true;
  623. //Prevent reverting on this forced stop
  624. this.instance.options.revert = false;
  625. // The out event needs to be triggered independently
  626. this.instance._trigger("out", event, this.instance._uiHash(this.instance));
  627. this.instance._mouseStop(event, true);
  628. this.instance.options.helper = this.instance.options._helper;
  629. //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
  630. this.instance.currentItem.remove();
  631. if (this.instance.placeholder) {
  632. this.instance.placeholder.remove();
  633. }
  634. inst._trigger("fromSortable", event);
  635. inst.dropped = false; //draggable revert needs that
  636. }
  637. }
  638. });
  639. }
  640. });
  641. $.ui.plugin.add("draggable", "cursor", {
  642. start: function( event, ui, instance ) {
  643. var t = $( "body" ),
  644. o = instance.options;
  645. if (t.css("cursor")) {
  646. o._cursor = t.css("cursor");
  647. }
  648. t.css("cursor", o.cursor);
  649. },
  650. stop: function( event, ui, instance ) {
  651. var o = instance.options;
  652. if (o._cursor) {
  653. $("body").css("cursor", o._cursor);
  654. }
  655. }
  656. });
  657. $.ui.plugin.add("draggable", "opacity", {
  658. start: function( event, ui, instance ) {
  659. var t = $( ui.helper ),
  660. o = instance.options;
  661. if (t.css("opacity")) {
  662. o._opacity = t.css("opacity");
  663. }
  664. t.css("opacity", o.opacity);
  665. },
  666. stop: function( event, ui, instance ) {
  667. var o = instance.options;
  668. if (o._opacity) {
  669. $(ui.helper).css("opacity", o._opacity);
  670. }
  671. }
  672. });
  673. $.ui.plugin.add("draggable", "scroll", {
  674. start: function( event, ui, i ) {
  675. if ( i.scrollParent[ 0 ] !== i.document[ 0 ] && i.scrollParent[ 0 ].tagName !== "HTML" ) {
  676. i.overflowOffset = i.scrollParent.offset();
  677. }
  678. },
  679. drag: function( event, ui, i ) {
  680. var o = i.options,
  681. scrolled = false,
  682. document = i.document[ 0 ];
  683. if ( i.scrollParent[ 0 ] !== document && i.scrollParent[ 0 ].tagName !== "HTML" ) {
  684. if (!o.axis || o.axis !== "x") {
  685. if ((i.overflowOffset.top + i.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity) {
  686. i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop + o.scrollSpeed;
  687. } else if (event.pageY - i.overflowOffset.top < o.scrollSensitivity) {
  688. i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop - o.scrollSpeed;
  689. }
  690. }
  691. if (!o.axis || o.axis !== "y") {
  692. if ((i.overflowOffset.left + i.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity) {
  693. i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft + o.scrollSpeed;
  694. } else if (event.pageX - i.overflowOffset.left < o.scrollSensitivity) {
  695. i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft - o.scrollSpeed;
  696. }
  697. }
  698. } else {
  699. if (!o.axis || o.axis !== "x") {
  700. if (event.pageY - $(document).scrollTop() < o.scrollSensitivity) {
  701. scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
  702. } else if ($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) {
  703. scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
  704. }
  705. }
  706. if (!o.axis || o.axis !== "y") {
  707. if (event.pageX - $(document).scrollLeft() < o.scrollSensitivity) {
  708. scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
  709. } else if ($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) {
  710. scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
  711. }
  712. }
  713. }
  714. if (scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) {
  715. $.ui.ddmanager.prepareOffsets(i, event);
  716. }
  717. }
  718. });
  719. $.ui.plugin.add("draggable", "snap", {
  720. start: function( event, ui, i ) {
  721. var o = i.options;
  722. i.snapElements = [];
  723. $(o.snap.constructor !== String ? ( o.snap.items || ":data(ui-draggable)" ) : o.snap).each(function() {
  724. var $t = $(this),
  725. $o = $t.offset();
  726. if (this !== i.element[0]) {
  727. i.snapElements.push({
  728. item: this,
  729. width: $t.outerWidth(), height: $t.outerHeight(),
  730. top: $o.top, left: $o.left
  731. });
  732. }
  733. });
  734. },
  735. drag: function( event, ui, inst ) {
  736. var ts, bs, ls, rs, l, r, t, b, i, first,
  737. o = inst.options,
  738. d = o.snapTolerance,
  739. x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
  740. y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
  741. for (i = inst.snapElements.length - 1; i >= 0; i--){
  742. l = inst.snapElements[i].left;
  743. r = l + inst.snapElements[i].width;
  744. t = inst.snapElements[i].top;
  745. b = t + inst.snapElements[i].height;
  746. if ( x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d || !$.contains( inst.snapElements[ i ].item.ownerDocument, inst.snapElements[ i ].item ) ) {
  747. if (inst.snapElements[i].snapping) {
  748. (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
  749. }
  750. inst.snapElements[i].snapping = false;
  751. continue;
  752. }
  753. if (o.snapMode !== "inner") {
  754. ts = Math.abs(t - y2) <= d;
  755. bs = Math.abs(b - y1) <= d;
  756. ls = Math.abs(l - x2) <= d;
  757. rs = Math.abs(r - x1) <= d;
  758. if (ts) {
  759. ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
  760. }
  761. if (bs) {
  762. ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top;
  763. }
  764. if (ls) {
  765. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left;
  766. }
  767. if (rs) {
  768. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left;
  769. }
  770. }
  771. first = (ts || bs || ls || rs);
  772. if (o.snapMode !== "outer") {
  773. ts = Math.abs(t - y1) <= d;
  774. bs = Math.abs(b - y2) <= d;
  775. ls = Math.abs(l - x1) <= d;
  776. rs = Math.abs(r - x2) <= d;
  777. if (ts) {
  778. ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top - inst.margins.top;
  779. }
  780. if (bs) {
  781. ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
  782. }
  783. if (ls) {
  784. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left - inst.margins.left;
  785. }
  786. if (rs) {
  787. ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left - inst.margins.left;
  788. }
  789. }
  790. if (!inst.snapElements[i].snapping && (ts || bs || ls || rs || first)) {
  791. (inst.options.snap.snap && inst.options.snap.snap.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
  792. }
  793. inst.snapElements[i].snapping = (ts || bs || ls || rs || first);
  794. }
  795. }
  796. });
  797. $.ui.plugin.add("draggable", "stack", {
  798. start: function( event, ui, instance ) {
  799. var min,
  800. o = instance.options,
  801. group = $.makeArray($(o.stack)).sort(function(a,b) {
  802. return (parseInt($(a).css("zIndex"),10) || 0) - (parseInt($(b).css("zIndex"),10) || 0);
  803. });
  804. if (!group.length) { return; }
  805. min = parseInt($(group[0]).css("zIndex"), 10) || 0;
  806. $(group).each(function(i) {
  807. $(this).css("zIndex", min + i);
  808. });
  809. this.css("zIndex", (min + group.length));
  810. }
  811. });
  812. $.ui.plugin.add("draggable", "zIndex", {
  813. start: function( event, ui, instance ) {
  814. var t = $( ui.helper ),
  815. o = instance.options;
  816. if (t.css("zIndex")) {
  817. o._zIndex = t.css("zIndex");
  818. }
  819. t.css("zIndex", o.zIndex);
  820. },
  821. stop: function( event, ui, instance ) {
  822. var o = instance.options;
  823. if (o._zIndex) {
  824. $(ui.helper).css("zIndex", o._zIndex);
  825. }
  826. }
  827. });
  828. return $.ui.draggable;
  829. }));