ui.draggable.js 29 KB

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