jquery.ui.draggable.js 30 KB

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