jquery.ui.draggable.js 31 KB

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