jquery.ui.draggable.js 30KB

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