jquery.ui.sortable.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /**************************************************
  2. * This file has been modified to support Beaver Builder.
  3. * The following methods have been changed:
  4. *
  5. * isFloating:
  6. * Modified to support flexbox and grid.
  7. *
  8. * _cacheMargins:
  9. * The _cacheMargins method has been modified
  10. * to fix an issue that was causing a helper
  11. * position bug in the builder.
  12. *
  13. * refreshPositions:
  14. * _convertPositionTo:
  15. * _mouseDrag:
  16. * _getElementOffset:
  17. * These methods have been modified or added to
  18. * support BB's iframe UI.
  19. *
  20. * _mouseDrag:
  21. * Use client X and Y for helper position if touch.
  22. **************************************************/
  23. /*!
  24. * jQuery UI Sortable 1.10.4
  25. * http://jqueryui.com
  26. *
  27. * Copyright 2014 jQuery Foundation and other contributors
  28. * Released under the MIT license.
  29. * http://jquery.org/license
  30. *
  31. * http://api.jqueryui.com/sortable/
  32. *
  33. * Depends:
  34. * jquery.ui.core.js
  35. * jquery.ui.mouse.js
  36. * jquery.ui.widget.js
  37. */
  38. (function( $, undefined ) {
  39. function isOverAxis( x, reference, size ) {
  40. return ( x > reference ) && ( x < ( reference + size ) );
  41. }
  42. function isFloating(item) {
  43. var floating = (/left|right/).test(item.css("float")) ||
  44. (/inline|table-cell/).test(item.css("display")) ||
  45. ((/flex/).test(item.parent().css("display")) && (/row|row-reverse/).test(item.parent().css( "flex-direction" ))) ||
  46. ((/grid/).test(item.parent().css("display")) && 1 < window.getComputedStyle( item.parent()[0] ).getPropertyValue( 'grid-template-columns' ).split( ' ' ).length);
  47. return floating && ! item.hasClass( 'fl-builder-block' );
  48. }
  49. $.widget("ui.sortable", $.ui.mouse, {
  50. version: "1.10.4",
  51. widgetEventPrefix: "sort",
  52. ready: false,
  53. options: {
  54. appendTo: "parent",
  55. axis: false,
  56. connectWith: false,
  57. containment: false,
  58. cursor: "auto",
  59. cursorAt: false,
  60. dropOnEmpty: true,
  61. forcePlaceholderSize: false,
  62. forceHelperSize: false,
  63. grid: false,
  64. handle: false,
  65. helper: "original",
  66. items: "> *",
  67. opacity: false,
  68. placeholder: false,
  69. revert: false,
  70. scroll: true,
  71. scrollSensitivity: 20,
  72. scrollSpeed: 20,
  73. scope: "default",
  74. tolerance: "intersect",
  75. zIndex: 1000,
  76. // callbacks
  77. activate: null,
  78. beforeStop: null,
  79. change: null,
  80. deactivate: null,
  81. out: null,
  82. over: null,
  83. receive: null,
  84. remove: null,
  85. sort: null,
  86. start: null,
  87. stop: null,
  88. update: null
  89. },
  90. _create: function() {
  91. var o = this.options;
  92. this.containerCache = {};
  93. this.element.addClass("ui-sortable");
  94. //Get the items
  95. this.refresh();
  96. //Let's determine if the items are being displayed horizontally
  97. this.floating = this.items.length ? o.axis === "x" || isFloating(this.items[0].item) : false;
  98. //Let's determine the parent's offset
  99. this.offset = this._getElementOffset(this.element);
  100. //Initialize mouse events for interaction
  101. this._mouseInit();
  102. //We're ready to go
  103. this.ready = true;
  104. },
  105. _destroy: function() {
  106. this.element
  107. .removeClass("ui-sortable ui-sortable-disabled");
  108. this._mouseDestroy();
  109. for ( var i = this.items.length - 1; i >= 0; i-- ) {
  110. this.items[i].item.removeData(this.widgetName + "-item");
  111. }
  112. return this;
  113. },
  114. _setOption: function(key, value){
  115. if ( key === "disabled" ) {
  116. this.options[ key ] = value;
  117. this.widget().toggleClass( "ui-sortable-disabled", !!value );
  118. } else {
  119. // Don't call widget base _setOption for disable as it adds ui-state-disabled class
  120. $.Widget.prototype._setOption.apply(this, arguments);
  121. }
  122. },
  123. _mouseCapture: function(event, overrideHandle) {
  124. var currentItem = null,
  125. validHandle = false,
  126. that = this;
  127. if (this.reverting) {
  128. return false;
  129. }
  130. if(this.options.disabled || this.options.type === "static") {
  131. return false;
  132. }
  133. //We have to refresh the items data once first
  134. this._refreshItems(event);
  135. //Find out if the clicked node (or one of its parents) is a actual item in this.items
  136. $(event.target).parents().each(function() {
  137. if($.data(this, that.widgetName + "-item") === that) {
  138. currentItem = $(this);
  139. return false;
  140. }
  141. });
  142. if($.data(event.target, that.widgetName + "-item") === that) {
  143. currentItem = $(event.target);
  144. }
  145. if(!currentItem) {
  146. return false;
  147. }
  148. if(this.options.handle && !overrideHandle) {
  149. $(this.options.handle, currentItem).find("*").addBack().each(function() {
  150. if(this === event.target) {
  151. validHandle = true;
  152. }
  153. });
  154. if(!validHandle) {
  155. return false;
  156. }
  157. }
  158. this.currentItem = currentItem;
  159. this._removeCurrentsFromItems();
  160. return true;
  161. },
  162. _mouseStart: function(event, overrideHandle, noActivation) {
  163. var i, body,
  164. o = this.options;
  165. this.currentContainer = this;
  166. //We only need to call refreshPositions, because the refreshItems call has been moved to mouseCapture
  167. this.refreshPositions();
  168. //Create and append the visible helper
  169. this.helper = this._createHelper(event);
  170. //Cache the helper size
  171. this._cacheHelperProportions();
  172. /*
  173. * - Position generation -
  174. * This block generates everything position related - it's the core of draggables.
  175. */
  176. //Cache the margins of the original element
  177. this._cacheMargins();
  178. //Get the next scrolling parent
  179. this.scrollParent = this.helper.scrollParent();
  180. //The element's absolute position on the page minus margins
  181. this.offset = this._getElementOffset(this.currentItem);
  182. this.offset = {
  183. top: this.offset.top - this.margins.top,
  184. left: this.offset.left - this.margins.left
  185. };
  186. $.extend(this.offset, {
  187. click: { //Where the click happened, relative to the element
  188. left: event.pageX - this.offset.left,
  189. top: event.pageY - this.offset.top
  190. },
  191. parent: this._getParentOffset(),
  192. relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
  193. });
  194. // Only after we got the offset, we can change the helper's position to absolute
  195. // TODO: Still need to figure out a way to make relative sorting possible
  196. this.helper.css("position", "absolute");
  197. this.cssPosition = this.helper.css("position");
  198. //Generate the original position
  199. this.originalPosition = this._generatePosition(event);
  200. this.originalPageX = event.pageX;
  201. this.originalPageY = event.pageY;
  202. //Adjust the mouse offset relative to the helper if "cursorAt" is supplied
  203. (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
  204. //Cache the former DOM position
  205. this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] };
  206. //If the helper is not the original, hide the original so it's not playing any role during the drag, won't cause anything bad this way
  207. if(this.helper[0] !== this.currentItem[0]) {
  208. this.currentItem.hide();
  209. }
  210. //Create the placeholder
  211. this._createPlaceholder();
  212. //Set a containment if given in the options
  213. if(o.containment) {
  214. this._setContainment();
  215. }
  216. if( o.cursor && o.cursor !== "auto" ) { // cursor option
  217. body = this.document.find( "body" );
  218. // support: IE
  219. this.storedCursor = body.css( "cursor" );
  220. body.css( "cursor", o.cursor );
  221. this.storedStylesheet = $( "<style>*{ cursor: "+o.cursor+" !important; }</style>" ).appendTo( body );
  222. }
  223. if(o.opacity) { // opacity option
  224. if (this.helper.css("opacity")) {
  225. this._storedOpacity = this.helper.css("opacity");
  226. }
  227. this.helper.css("opacity", o.opacity);
  228. }
  229. if(o.zIndex) { // zIndex option
  230. if (this.helper.css("zIndex")) {
  231. this._storedZIndex = this.helper.css("zIndex");
  232. }
  233. this.helper.css("zIndex", o.zIndex);
  234. }
  235. //Prepare scrolling
  236. if(this.scrollParent[0] !== window.parent.document && this.scrollParent[0].tagName !== "HTML") {
  237. this.overflowOffset = this._getElementOffset(this.scrollParent);
  238. }
  239. //Call callbacks
  240. this._trigger("start", event, this._uiHash());
  241. //Recache the helper size
  242. if(!this._preserveHelperProportions) {
  243. this._cacheHelperProportions();
  244. }
  245. //Post "activate" events to possible containers
  246. if( !noActivation ) {
  247. for ( i = this.containers.length - 1; i >= 0; i-- ) {
  248. this.containers[ i ]._trigger( "activate", event, this._uiHash( this ) );
  249. }
  250. }
  251. //Prepare possible droppables
  252. if($.ui.ddmanager) {
  253. $.ui.ddmanager.current = this;
  254. }
  255. if ($.ui.ddmanager && !o.dropBehaviour) {
  256. $.ui.ddmanager.prepareOffsets(this, event);
  257. }
  258. this.dragging = true;
  259. this.helper.addClass("ui-sortable-helper");
  260. this._mouseDrag(event); //Execute the drag once - this causes the helper not to be visible before getting its correct position
  261. return true;
  262. },
  263. _mouseDrag: function(event) {
  264. var frame = this.options.frame ? this.options.frame : null;
  265. var frameOffset = frame && ! this._touchMoved ? frame.offset() : { left: 0, top: 0 };
  266. var i, item, itemElement, intersection,
  267. o = this.options,
  268. scrolled = false;
  269. //Compute the helpers position
  270. this.position = this._generatePosition(event);
  271. this.positionAbs = this._convertPositionTo("absolute");
  272. if (!this.lastPositionAbs) {
  273. this.lastPositionAbs = this.positionAbs;
  274. }
  275. //Do scrolling
  276. if(this.options.scroll) {
  277. if(this.scrollParent[0] !== document && this.scrollParent[0].tagName !== "HTML") {
  278. if((this.overflowOffset.top + this.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity) {
  279. this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop + o.scrollSpeed;
  280. } else if(event.pageY - this.overflowOffset.top < o.scrollSensitivity) {
  281. this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop - o.scrollSpeed;
  282. }
  283. if((this.overflowOffset.left + this.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity) {
  284. this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft + o.scrollSpeed;
  285. } else if(event.pageX - this.overflowOffset.left < o.scrollSensitivity) {
  286. this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft - o.scrollSpeed;
  287. }
  288. } else {
  289. if(event.pageY - $(document).scrollTop() < o.scrollSensitivity) {
  290. scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
  291. } else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) {
  292. scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
  293. }
  294. if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity) {
  295. scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
  296. } else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) {
  297. scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
  298. }
  299. }
  300. if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) {
  301. $.ui.ddmanager.prepareOffsets(this, event);
  302. }
  303. }
  304. //Regenerate the absolute position used for position checks
  305. this.positionAbs = this._convertPositionTo("absolute");
  306. //Set the helper position
  307. if(!this.options.axis || this.options.axis !== "y") {
  308. if (this._touchMoved) {
  309. this.helper[0].style.left = (event.originalEvent.clientX + frameOffset.left) + "px";
  310. } else {
  311. this.helper[0].style.left = (this.position.left + frameOffset.left) + "px";
  312. }
  313. }
  314. if(!this.options.axis || this.options.axis !== "x") {
  315. if (this._touchMoved) {
  316. this.helper[0].style.top = (event.originalEvent.clientY + frameOffset.top) + "px";
  317. } else {
  318. this.helper[0].style.top = (this.position.top + frameOffset.top) + "px";
  319. }
  320. }
  321. //Rearrange
  322. for (i = this.items.length - 1; i >= 0; i--) {
  323. //Cache variables and intersection, continue if no intersection
  324. item = this.items[i];
  325. itemElement = item.item[0];
  326. intersection = this._intersectsWithPointer(item);
  327. if (!intersection) {
  328. continue;
  329. }
  330. // Only put the placeholder inside the current Container, skip all
  331. // items from other containers. This works because when moving
  332. // an item from one container to another the
  333. // currentContainer is switched before the placeholder is moved.
  334. //
  335. // Without this, moving items in "sub-sortables" can cause
  336. // the placeholder to jitter beetween the outer and inner container.
  337. if (item.instance !== this.currentContainer) {
  338. continue;
  339. }
  340. // cannot intersect with itself
  341. // no useless actions that have been done before
  342. // no action if the item moved is the parent of the item checked
  343. if (itemElement !== this.currentItem[0] &&
  344. this.placeholder[intersection === 1 ? "next" : "prev"]()[0] !== itemElement &&
  345. !$.contains(this.placeholder[0], itemElement) &&
  346. (this.options.type === "semi-dynamic" ? !$.contains(this.element[0], itemElement) : true)
  347. ) {
  348. this.direction = intersection === 1 ? "down" : "up";
  349. if (this.options.tolerance === "pointer" || this._intersectsWithSides(item)) {
  350. this._rearrange(event, item);
  351. } else {
  352. break;
  353. }
  354. this._trigger("change", event, this._uiHash());
  355. break;
  356. }
  357. }
  358. //Post events to containers
  359. this._contactContainers(event);
  360. //Interconnect with droppables
  361. if($.ui.ddmanager) {
  362. $.ui.ddmanager.drag(this, event);
  363. }
  364. //Call callbacks
  365. this._trigger("sort", event, this._uiHash());
  366. this.lastPositionAbs = this.positionAbs;
  367. return false;
  368. },
  369. _mouseStop: function(event, noPropagation) {
  370. if(!event) {
  371. return;
  372. }
  373. //If we are using droppables, inform the manager about the drop
  374. if ($.ui.ddmanager && !this.options.dropBehaviour) {
  375. $.ui.ddmanager.drop(this, event);
  376. }
  377. if(this.options.revert) {
  378. var that = this,
  379. cur = this._getElementOffset(this.placeholder),
  380. axis = this.options.axis,
  381. animation = {};
  382. if ( !axis || axis === "x" ) {
  383. animation.left = cur.left - this.offset.parent.left - this.margins.left + (this.offsetParent[0] === document.body ? 0 : this.offsetParent[0].scrollLeft);
  384. }
  385. if ( !axis || axis === "y" ) {
  386. animation.top = cur.top - this.offset.parent.top - this.margins.top + (this.offsetParent[0] === document.body ? 0 : this.offsetParent[0].scrollTop);
  387. }
  388. this.reverting = true;
  389. $(this.helper).animate( animation, parseInt(this.options.revert, 10) || 500, function() {
  390. that._clear(event);
  391. });
  392. } else {
  393. this._clear(event, noPropagation);
  394. }
  395. return false;
  396. },
  397. cancel: function() {
  398. if(this.dragging) {
  399. this._mouseUp({ target: null });
  400. if(this.options.helper === "original") {
  401. this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
  402. } else {
  403. this.currentItem.show();
  404. }
  405. //Post deactivating events to containers
  406. for (var i = this.containers.length - 1; i >= 0; i--){
  407. this.containers[i]._trigger("deactivate", null, this._uiHash(this));
  408. if(this.containers[i].containerCache.over) {
  409. this.containers[i]._trigger("out", null, this._uiHash(this));
  410. this.containers[i].containerCache.over = 0;
  411. }
  412. }
  413. }
  414. if (this.placeholder) {
  415. //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
  416. if(this.placeholder[0].parentNode) {
  417. this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
  418. }
  419. if(this.options.helper !== "original" && this.helper && this.helper[0].parentNode) {
  420. this.helper.remove();
  421. }
  422. $.extend(this, {
  423. helper: null,
  424. dragging: false,
  425. reverting: false,
  426. _noFinalSort: null
  427. });
  428. if(this.domPosition.prev) {
  429. $(this.domPosition.prev).after(this.currentItem);
  430. } else {
  431. $(this.domPosition.parent).prepend(this.currentItem);
  432. }
  433. }
  434. return this;
  435. },
  436. serialize: function(o) {
  437. var items = this._getItemsAsjQuery(o && o.connected),
  438. str = [];
  439. o = o || {};
  440. $(items).each(function() {
  441. var res = ($(o.item || this).attr(o.attribute || "id") || "").match(o.expression || (/(.+)[\-=_](.+)/));
  442. if (res) {
  443. str.push((o.key || res[1]+"[]")+"="+(o.key && o.expression ? res[1] : res[2]));
  444. }
  445. });
  446. if(!str.length && o.key) {
  447. str.push(o.key + "=");
  448. }
  449. return str.join("&");
  450. },
  451. toArray: function(o) {
  452. var items = this._getItemsAsjQuery(o && o.connected),
  453. ret = [];
  454. o = o || {};
  455. items.each(function() { ret.push($(o.item || this).attr(o.attribute || "id") || ""); });
  456. return ret;
  457. },
  458. /* Be careful with the following core functions */
  459. _intersectsWith: function(item) {
  460. var x1 = this.positionAbs.left,
  461. x2 = x1 + this.helperProportions.width,
  462. y1 = this.positionAbs.top,
  463. y2 = y1 + this.helperProportions.height,
  464. l = item.left,
  465. r = l + item.width,
  466. t = item.top,
  467. b = t + item.height,
  468. dyClick = this.offset.click.top,
  469. dxClick = this.offset.click.left,
  470. isOverElementHeight = ( this.options.axis === "x" ) || ( ( y1 + dyClick ) > t && ( y1 + dyClick ) < b ),
  471. isOverElementWidth = ( this.options.axis === "y" ) || ( ( x1 + dxClick ) > l && ( x1 + dxClick ) < r ),
  472. isOverElement = isOverElementHeight && isOverElementWidth;
  473. if ( this.options.tolerance === "pointer" ||
  474. this.options.forcePointerForContainers ||
  475. (this.options.tolerance !== "pointer" && this.helperProportions[this.floating ? "width" : "height"] > item[this.floating ? "width" : "height"])
  476. ) {
  477. return isOverElement;
  478. } else {
  479. return (l < x1 + (this.helperProportions.width / 2) && // Right Half
  480. x2 - (this.helperProportions.width / 2) < r && // Left Half
  481. t < y1 + (this.helperProportions.height / 2) && // Bottom Half
  482. y2 - (this.helperProportions.height / 2) < b ); // Top Half
  483. }
  484. },
  485. _intersectsWithPointer: function(item) {
  486. var isOverElementHeight = (this.options.axis === "x") || isOverAxis(this.positionAbs.top + this.offset.click.top, item.top, item.height),
  487. isOverElementWidth = (this.options.axis === "y") || isOverAxis(this.positionAbs.left + this.offset.click.left, item.left, item.width),
  488. isOverElement = isOverElementHeight && isOverElementWidth,
  489. verticalDirection = this._getDragVerticalDirection(),
  490. horizontalDirection = this._getDragHorizontalDirection();
  491. if (!isOverElement) {
  492. return false;
  493. }
  494. return this.floating ?
  495. ( ((horizontalDirection && horizontalDirection === "right") || verticalDirection === "down") ? 2 : 1 )
  496. : ( verticalDirection && (verticalDirection === "down" ? 2 : 1) );
  497. },
  498. _intersectsWithSides: function(item) {
  499. var isOverBottomHalf = isOverAxis(this.positionAbs.top + this.offset.click.top, item.top + (item.height/2), item.height),
  500. isOverRightHalf = isOverAxis(this.positionAbs.left + this.offset.click.left, item.left + (item.width/2), item.width),
  501. verticalDirection = this._getDragVerticalDirection(),
  502. horizontalDirection = this._getDragHorizontalDirection();
  503. if (this.floating && horizontalDirection) {
  504. return ((horizontalDirection === "right" && isOverRightHalf) || (horizontalDirection === "left" && !isOverRightHalf));
  505. } else {
  506. return verticalDirection && ((verticalDirection === "down" && isOverBottomHalf) || (verticalDirection === "up" && !isOverBottomHalf));
  507. }
  508. },
  509. _getDragVerticalDirection: function() {
  510. var delta = this.positionAbs.top - this.lastPositionAbs.top;
  511. return delta !== 0 && (delta > 0 ? "down" : "up");
  512. },
  513. _getDragHorizontalDirection: function() {
  514. var delta = this.positionAbs.left - this.lastPositionAbs.left;
  515. return delta !== 0 && (delta > 0 ? "right" : "left");
  516. },
  517. refresh: function(event) {
  518. this._refreshItems(event);
  519. this.refreshPositions();
  520. return this;
  521. },
  522. _connectWith: function() {
  523. var options = this.options;
  524. return options.connectWith.constructor === String ? [options.connectWith] : options.connectWith;
  525. },
  526. _getItemsAsjQuery: function(connected) {
  527. var i, j, cur, inst,
  528. items = [],
  529. queries = [],
  530. connectWith = this._connectWith();
  531. if(connectWith && connected) {
  532. for (i = connectWith.length - 1; i >= 0; i--){
  533. cur = $(connectWith[i]);
  534. for ( j = cur.length - 1; j >= 0; j--){
  535. inst = $.data(cur[j], this.widgetFullName);
  536. if(inst && inst !== this && !inst.options.disabled) {
  537. queries.push([typeof inst.options.items === 'function' ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element).not(".ui-sortable-helper").not(".ui-sortable-placeholder"), inst]);
  538. }
  539. }
  540. }
  541. }
  542. queries.push([typeof this.options.items === 'function' ? this.options.items.call(this.element, null, { options: this.options, item: this.currentItem }) : $(this.options.items, this.element).not(".ui-sortable-helper").not(".ui-sortable-placeholder"), this]);
  543. function addItems() {
  544. items.push( this );
  545. }
  546. for (i = queries.length - 1; i >= 0; i--){
  547. queries[i][0].each( addItems );
  548. }
  549. return $(items);
  550. },
  551. _removeCurrentsFromItems: function() {
  552. var list = this.currentItem.find(":data(" + this.widgetName + "-item)");
  553. this.items = $.grep(this.items, function (item) {
  554. for (var j=0; j < list.length; j++) {
  555. if(list[j] === item.item[0]) {
  556. return false;
  557. }
  558. }
  559. return true;
  560. });
  561. },
  562. _refreshItems: function(event) {
  563. this.items = [];
  564. this.containers = [this];
  565. var i, j, cur, inst, targetData, _queries, item, queriesLength,
  566. items = this.items,
  567. queries = [[typeof this.options.items === 'function' ? this.options.items.call(this.element[0], event, { item: this.currentItem }) : $(this.options.items, this.element), this]],
  568. connectWith = this._connectWith();
  569. if(connectWith && this.ready) { //Shouldn't be run the first time through due to massive slow-down
  570. for (i = connectWith.length - 1; i >= 0; i--){
  571. cur = $(connectWith[i]);
  572. for (j = cur.length - 1; j >= 0; j--){
  573. inst = $.data(cur[j], this.widgetFullName);
  574. if(inst && inst !== this && !inst.options.disabled) {
  575. queries.push([typeof inst.options.items === 'function' ? inst.options.items.call(inst.element[0], event, { item: this.currentItem }) : $(inst.options.items, inst.element), inst]);
  576. this.containers.push(inst);
  577. }
  578. }
  579. }
  580. }
  581. for (i = queries.length - 1; i >= 0; i--) {
  582. targetData = queries[i][1];
  583. _queries = queries[i][0];
  584. for (j=0, queriesLength = _queries.length; j < queriesLength; j++) {
  585. item = $(_queries[j]);
  586. item.data(this.widgetName + "-item", targetData); // Data for target checking (mouse manager)
  587. items.push({
  588. item: item,
  589. instance: targetData,
  590. width: 0, height: 0,
  591. left: 0, top: 0
  592. });
  593. }
  594. }
  595. },
  596. refreshPositions: function(fast) {
  597. //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change
  598. if(this.offsetParent && this.helper) {
  599. this.offset.parent = this._getParentOffset();
  600. }
  601. var i, item, t, p;
  602. for (i = this.items.length - 1; i >= 0; i--){
  603. item = this.items[i];
  604. //We ignore calculating positions of all connected containers when we're not over them
  605. if(item.instance !== this.currentContainer && this.currentContainer && item.item[0] !== this.currentItem[0]) {
  606. continue;
  607. }
  608. t = this.options.toleranceElement ? $(this.options.toleranceElement, item.item) : item.item;
  609. if (!fast) {
  610. item.width = t.outerWidth();
  611. item.height = t.outerHeight();
  612. }
  613. p = this._getElementOffset(t);
  614. item.left = p.left;
  615. item.top = p.top;
  616. }
  617. if(this.options.custom && this.options.custom.refreshContainers) {
  618. this.options.custom.refreshContainers.call(this);
  619. } else {
  620. for (i = this.containers.length - 1; i >= 0; i--){
  621. p = this._getElementOffset(this.containers[i].element);
  622. this.containers[i].containerCache.left = p.left;
  623. this.containers[i].containerCache.top = p.top;
  624. this.containers[i].containerCache.width = this.containers[i].element.outerWidth();
  625. this.containers[i].containerCache.height = this.containers[i].element.outerHeight();
  626. }
  627. }
  628. return this;
  629. },
  630. _createPlaceholder: function(that) {
  631. that = that || this;
  632. var className,
  633. o = that.options;
  634. if(!o.placeholder || o.placeholder.constructor === String) {
  635. className = o.placeholder;
  636. o.placeholder = {
  637. element: function() {
  638. var nodeName = that.currentItem[0].nodeName.toLowerCase(),
  639. element = $( "<" + nodeName + ">", that.document[0] )
  640. .addClass(className || that.currentItem[0].className+" ui-sortable-placeholder")
  641. .removeClass("ui-sortable-helper");
  642. if ( nodeName === "tr" ) {
  643. that.currentItem.children().each(function() {
  644. $( "<td>&#160;</td>", that.document[0] )
  645. .attr( "colspan", $( this ).attr( "colspan" ) || 1 )
  646. .appendTo( element );
  647. });
  648. } else if ( nodeName === "img" ) {
  649. element.attr( "src", that.currentItem.attr( "src" ) );
  650. }
  651. if ( !className ) {
  652. element.css( "visibility", "hidden" );
  653. }
  654. return element;
  655. },
  656. update: function(container, p) {
  657. // 1. If a className is set as 'placeholder option, we don't force sizes - the class is responsible for that
  658. // 2. The option 'forcePlaceholderSize can be enabled to force it even if a class name is specified
  659. if(className && !o.forcePlaceholderSize) {
  660. return;
  661. }
  662. //If the element doesn't have a actual height by itself (without styles coming from a stylesheet), it receives the inline height from the dragged item
  663. if(!p.height()) { p.height(that.currentItem.innerHeight() - parseInt(that.currentItem.css("paddingTop")||0, 10) - parseInt(that.currentItem.css("paddingBottom")||0, 10)); }
  664. if(!p.width()) { p.width(that.currentItem.innerWidth() - parseInt(that.currentItem.css("paddingLeft")||0, 10) - parseInt(that.currentItem.css("paddingRight")||0, 10)); }
  665. }
  666. };
  667. }
  668. //Create the placeholder
  669. that.placeholder = $(o.placeholder.element.call(that.element, that.currentItem));
  670. //Append it after the actual current item
  671. that.currentItem.after(that.placeholder);
  672. //Update the size of the placeholder (TODO: Logic to fuzzy, see line 316/317)
  673. o.placeholder.update(that, that.placeholder);
  674. },
  675. _contactContainers: function(event) {
  676. var i, j, dist, itemWithLeastDistance, posProperty, sizeProperty, base, cur, nearBottom, floating,
  677. innermostContainer = null,
  678. innermostIndex = null;
  679. // get innermost container that intersects with item
  680. for (i = this.containers.length - 1; i >= 0; i--) {
  681. // never consider a container that's located within the item itself
  682. if($.contains(this.currentItem[0], this.containers[i].element[0])) {
  683. continue;
  684. }
  685. if(this._intersectsWith(this.containers[i].containerCache)) {
  686. // if we've already found a container and it's more "inner" than this, then continue
  687. if(innermostContainer && $.contains(this.containers[i].element[0], innermostContainer.element[0])) {
  688. continue;
  689. }
  690. innermostContainer = this.containers[i];
  691. innermostIndex = i;
  692. } else {
  693. // container doesn't intersect. trigger "out" event if necessary
  694. if(this.containers[i].containerCache.over) {
  695. this.containers[i]._trigger("out", event, this._uiHash(this));
  696. this.containers[i].containerCache.over = 0;
  697. }
  698. }
  699. }
  700. // if no intersecting containers found, return
  701. if(!innermostContainer) {
  702. return;
  703. }
  704. // move the item into the container if it's not there already
  705. if(this.containers.length === 1) {
  706. if (!this.containers[innermostIndex].containerCache.over) {
  707. this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
  708. this.containers[innermostIndex].containerCache.over = 1;
  709. }
  710. } else {
  711. //When entering a new container, we will find the item with the least distance and append our item near it
  712. dist = 10000;
  713. itemWithLeastDistance = null;
  714. floating = innermostContainer.floating || isFloating(this.currentItem);
  715. posProperty = floating ? "left" : "top";
  716. sizeProperty = floating ? "width" : "height";
  717. base = this.positionAbs[posProperty] + this.offset.click[posProperty];
  718. for (j = this.items.length - 1; j >= 0; j--) {
  719. if(!$.contains(this.containers[innermostIndex].element[0], this.items[j].item[0])) {
  720. continue;
  721. }
  722. if(this.items[j].item[0] === this.currentItem[0]) {
  723. continue;
  724. }
  725. if (floating && !isOverAxis(this.positionAbs.top + this.offset.click.top, this.items[j].top, this.items[j].height)) {
  726. continue;
  727. }
  728. cur = this._getElementOffset(this.items[j].item)[posProperty];
  729. nearBottom = false;
  730. if(Math.abs(cur - base) > Math.abs(cur + this.items[j][sizeProperty] - base)){
  731. nearBottom = true;
  732. cur += this.items[j][sizeProperty];
  733. }
  734. if(Math.abs(cur - base) < dist) {
  735. dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j];
  736. this.direction = nearBottom ? "up": "down";
  737. }
  738. }
  739. //Check if dropOnEmpty is enabled
  740. if(!itemWithLeastDistance && !this.options.dropOnEmpty) {
  741. return;
  742. }
  743. if(this.currentContainer === this.containers[innermostIndex]) {
  744. return;
  745. }
  746. itemWithLeastDistance ? this._rearrange(event, itemWithLeastDistance, null, true) : this._rearrange(event, null, this.containers[innermostIndex].element, true);
  747. this._trigger("change", event, this._uiHash());
  748. this.containers[innermostIndex]._trigger("change", event, this._uiHash(this));
  749. this.currentContainer = this.containers[innermostIndex];
  750. //Update the placeholder
  751. this.options.placeholder.update(this.currentContainer, this.placeholder);
  752. this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
  753. this.containers[innermostIndex].containerCache.over = 1;
  754. }
  755. },
  756. _createHelper: function(event) {
  757. var o = this.options,
  758. helper = typeof o.helper === 'function' ? $(o.helper.apply(this.element[0], [event, this.currentItem])) : (o.helper === "clone" ? this.currentItem.clone() : this.currentItem);
  759. //Add the helper to the DOM if that didn't happen already
  760. if(!helper.parents("body").length) {
  761. $(o.appendTo !== "parent" ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(helper[0]);
  762. }
  763. if(helper[0] === this.currentItem[0]) {
  764. this._storedCSS = { width: this.currentItem[0].style.width, height: this.currentItem[0].style.height, position: this.currentItem.css("position"), top: this.currentItem.css("top"), left: this.currentItem.css("left") };
  765. }
  766. if(!helper[0].style.width || o.forceHelperSize) {
  767. helper.width(this.currentItem.width());
  768. }
  769. if(!helper[0].style.height || o.forceHelperSize) {
  770. helper.height(this.currentItem.height());
  771. }
  772. return helper;
  773. },
  774. _adjustOffsetFromHelper: function(obj) {
  775. if (typeof obj === "string") {
  776. obj = obj.split(" ");
  777. }
  778. if (Array.isArray(obj)) {
  779. obj = {left: +obj[0], top: +obj[1] || 0};
  780. }
  781. if ("left" in obj) {
  782. this.offset.click.left = obj.left + this.margins.left;
  783. }
  784. if ("right" in obj) {
  785. this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
  786. }
  787. if ("top" in obj) {
  788. this.offset.click.top = obj.top + this.margins.top;
  789. }
  790. if ("bottom" in obj) {
  791. this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
  792. }
  793. },
  794. _getParentOffset: function() {
  795. //Get the offsetParent and cache its position
  796. this.offsetParent = this.helper.offsetParent();
  797. var po = this._getElementOffset(this.offsetParent);
  798. // This is a special case where we need to modify a offset calculated on start, since the following happened:
  799. // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
  800. // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
  801. // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
  802. if(this.cssPosition === "absolute" && this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) {
  803. po.left += this.scrollParent.scrollLeft();
  804. po.top += this.scrollParent.scrollTop();
  805. }
  806. // This needs to be actually done for all browsers, since pageX/pageY includes this information
  807. // with an ugly IE fix
  808. if( this.offsetParent[0] === document.body || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() === "html" && $.ui.ie)) {
  809. po = { top: 0, left: 0 };
  810. }
  811. return {
  812. top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
  813. left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
  814. };
  815. },
  816. _getRelativeOffset: function() {
  817. if(this.cssPosition === "relative") {
  818. var p = this.currentItem.position();
  819. return {
  820. top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(),
  821. left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft()
  822. };
  823. } else {
  824. return { top: 0, left: 0 };
  825. }
  826. },
  827. _getElementOffset: function( ele ) {
  828. var frame = this.options.frame ? this.options.frame : null;
  829. var frameOffset = frame && ! this._touchMoved ? frame.offset() : { left: 0, top: 0 };
  830. var offset = ele.offset();
  831. if ( ele[0].ownerDocument !== window.parent.document ) {
  832. offset = {
  833. top: offset.top + frameOffset.top,
  834. left: offset.left + frameOffset.left
  835. };
  836. }
  837. return offset;
  838. },
  839. _cacheMargins: function() {
  840. this.margins = {
  841. left: 0, // Changed for builder
  842. //left: (parseInt(this.currentItem.css("marginLeft"),10) || 0),
  843. top: (parseInt(this.currentItem.css("marginTop"),10) || 0)
  844. };
  845. },
  846. _cacheHelperProportions: function() {
  847. this.helperProportions = {
  848. width: this.helper.outerWidth(),
  849. height: this.helper.outerHeight()
  850. };
  851. },
  852. _setContainment: function() {
  853. var ce, co, over,
  854. o = this.options;
  855. if(o.containment === "parent") {
  856. o.containment = this.helper[0].parentNode;
  857. }
  858. if(o.containment === "document" || o.containment === "window") {
  859. this.containment = [
  860. 0 - this.offset.relative.left - this.offset.parent.left,
  861. 0 - this.offset.relative.top - this.offset.parent.top,
  862. $(o.containment === "document" ? document : window).width() - this.helperProportions.width - this.margins.left,
  863. ($(o.containment === "document" ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
  864. ];
  865. }
  866. if(!(/^(document|window|parent)$/).test(o.containment)) {
  867. ce = $(o.containment)[0];
  868. co = this._getElementOffset($(o.containment));
  869. over = ($(ce).css("overflow") !== "hidden");
  870. this.containment = [
  871. co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0) - this.margins.left,
  872. co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0) - this.margins.top,
  873. 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,
  874. 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
  875. ];
  876. }
  877. },
  878. _convertPositionTo: function(d, pos) {
  879. var frame = this.options.frame ? this.options.frame : null;
  880. var frameScrollTop = frame && ! this._touchMoved ? frame.contents().scrollTop() : 0;
  881. if(!pos) {
  882. pos = this.position;
  883. }
  884. var mod = d === "absolute" ? 1 : -1,
  885. scroll = this.cssPosition === "absolute" && !(this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent,
  886. scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName);
  887. return {
  888. top: (
  889. pos.top + // The absolute mouse position
  890. this.offset.relative.top * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
  891. this.offset.parent.top * mod - // The offsetParent's offset without borders (offset + border)
  892. ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod) +
  893. frameScrollTop
  894. ),
  895. left: (
  896. pos.left + // The absolute mouse position
  897. this.offset.relative.left * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
  898. this.offset.parent.left * mod - // The offsetParent's offset without borders (offset + border)
  899. ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod)
  900. )
  901. };
  902. },
  903. _generatePosition: function(event) {
  904. var top, left,
  905. o = this.options,
  906. pageX = event.pageX,
  907. pageY = event.pageY,
  908. 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);
  909. // This is another very weird special case that only happens for relative elements:
  910. // 1. If the css position is relative
  911. // 2. and the scroll parent is the document or similar to the offset parent
  912. // we have to refresh the relative offset during the scroll so there are no jumps
  913. if(this.cssPosition === "relative" && !(this.scrollParent[0] !== document && this.scrollParent[0] !== this.offsetParent[0])) {
  914. this.offset.relative = this._getRelativeOffset();
  915. }
  916. /*
  917. * - Position constraining -
  918. * Constrain the position to a mix of grid, containment.
  919. */
  920. if(this.originalPosition) { //If we are not dragging yet, we won't check for options
  921. if(this.containment) {
  922. if(event.pageX - this.offset.click.left < this.containment[0]) {
  923. pageX = this.containment[0] + this.offset.click.left;
  924. }
  925. if(event.pageY - this.offset.click.top < this.containment[1]) {
  926. pageY = this.containment[1] + this.offset.click.top;
  927. }
  928. if(event.pageX - this.offset.click.left > this.containment[2]) {
  929. pageX = this.containment[2] + this.offset.click.left;
  930. }
  931. if(event.pageY - this.offset.click.top > this.containment[3]) {
  932. pageY = this.containment[3] + this.offset.click.top;
  933. }
  934. }
  935. if(o.grid) {
  936. top = this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1];
  937. 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;
  938. left = this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0];
  939. 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;
  940. }
  941. }
  942. return {
  943. top: (
  944. pageY - // The absolute mouse position
  945. this.offset.click.top - // Click offset (relative to the element)
  946. this.offset.relative.top - // Only for relative positioned nodes: Relative offset from element to offset parent
  947. this.offset.parent.top + // The offsetParent's offset without borders (offset + border)
  948. ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ))
  949. ),
  950. left: (
  951. pageX - // The absolute mouse position
  952. this.offset.click.left - // Click offset (relative to the element)
  953. this.offset.relative.left - // Only for relative positioned nodes: Relative offset from element to offset parent
  954. this.offset.parent.left + // The offsetParent's offset without borders (offset + border)
  955. ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ))
  956. )
  957. };
  958. },
  959. _rearrange: function(event, i, a, hardRefresh) {
  960. a ? a[0].appendChild(this.placeholder[0]) : i.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction === "down" ? i.item[0] : i.item[0].nextSibling));
  961. //Various things done here to improve the performance:
  962. // 1. we create a setTimeout, that calls refreshPositions
  963. // 2. on the instance, we have a counter variable, that get's higher after every append
  964. // 3. on the local scope, we copy the counter variable, and check in the timeout, if it's still the same
  965. // 4. this lets only the last addition to the timeout stack through
  966. this.counter = this.counter ? ++this.counter : 1;
  967. var counter = this.counter;
  968. this._delay(function() {
  969. if(counter === this.counter) {
  970. this.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove
  971. }
  972. });
  973. },
  974. _clear: function(event, noPropagation) {
  975. this.reverting = false;
  976. // We delay all events that have to be triggered to after the point where the placeholder has been removed and
  977. // everything else normalized again
  978. var i,
  979. delayedTriggers = [];
  980. // We first have to update the dom position of the actual currentItem
  981. // Note: don't do it if the current item is already removed (by a user), or it gets reappended (see #4088)
  982. if(!this._noFinalSort && this.currentItem.parent().length) {
  983. this.placeholder.before(this.currentItem);
  984. }
  985. this._noFinalSort = null;
  986. if(this.helper[0] === this.currentItem[0]) {
  987. for(i in this._storedCSS) {
  988. if(this._storedCSS[i] === "auto" || this._storedCSS[i] === "static") {
  989. this._storedCSS[i] = "";
  990. }
  991. }
  992. this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
  993. } else {
  994. this.currentItem.show();
  995. }
  996. if(this.fromOutside && !noPropagation) {
  997. delayedTriggers.push(function(event) { this._trigger("receive", event, this._uiHash(this.fromOutside)); });
  998. }
  999. if((this.fromOutside || this.domPosition.prev !== this.currentItem.prev().not(".ui-sortable-helper")[0] || this.domPosition.parent !== this.currentItem.parent()[0]) && !noPropagation) {
  1000. delayedTriggers.push(function(event) { this._trigger("update", event, this._uiHash()); }); //Trigger update callback if the DOM position has changed
  1001. }
  1002. // Check if the items Container has Changed and trigger appropriate
  1003. // events.
  1004. if (this !== this.currentContainer) {
  1005. if(!noPropagation) {
  1006. delayedTriggers.push(function(event) { this._trigger("remove", event, this._uiHash()); });
  1007. delayedTriggers.push((function(c) { return function(event) { c._trigger("receive", event, this._uiHash(this)); }; }).call(this, this.currentContainer));
  1008. delayedTriggers.push((function(c) { return function(event) { c._trigger("update", event, this._uiHash(this)); }; }).call(this, this.currentContainer));
  1009. }
  1010. }
  1011. //Post events to containers
  1012. function delayEvent( type, instance, container ) {
  1013. return function( event ) {
  1014. container._trigger( type, event, instance._uiHash( instance ) );
  1015. };
  1016. }
  1017. for (i = this.containers.length - 1; i >= 0; i--){
  1018. if (!noPropagation) {
  1019. delayedTriggers.push( delayEvent( "deactivate", this, this.containers[ i ] ) );
  1020. }
  1021. if(this.containers[i].containerCache.over) {
  1022. delayedTriggers.push( delayEvent( "out", this, this.containers[ i ] ) );
  1023. this.containers[i].containerCache.over = 0;
  1024. }
  1025. }
  1026. //Do what was originally in plugins
  1027. if ( this.storedCursor ) {
  1028. this.document.find( "body" ).css( "cursor", this.storedCursor );
  1029. this.storedStylesheet.remove();
  1030. }
  1031. if(this._storedOpacity) {
  1032. this.helper.css("opacity", this._storedOpacity);
  1033. }
  1034. if(this._storedZIndex) {
  1035. this.helper.css("zIndex", this._storedZIndex === "auto" ? "" : this._storedZIndex);
  1036. }
  1037. this.dragging = false;
  1038. if(this.cancelHelperRemoval) {
  1039. if(!noPropagation) {
  1040. this._trigger("beforeStop", event, this._uiHash());
  1041. for (i=0; i < delayedTriggers.length; i++) {
  1042. delayedTriggers[i].call(this, event);
  1043. } //Trigger all delayed events
  1044. this._trigger("stop", event, this._uiHash());
  1045. }
  1046. this.fromOutside = false;
  1047. return false;
  1048. }
  1049. if(!noPropagation) {
  1050. this._trigger("beforeStop", event, this._uiHash());
  1051. }
  1052. //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
  1053. this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
  1054. if(this.helper[0] !== this.currentItem[0]) {
  1055. this.helper.remove();
  1056. }
  1057. this.helper = null;
  1058. if(!noPropagation) {
  1059. for (i=0; i < delayedTriggers.length; i++) {
  1060. delayedTriggers[i].call(this, event);
  1061. } //Trigger all delayed events
  1062. this._trigger("stop", event, this._uiHash());
  1063. }
  1064. this.fromOutside = false;
  1065. return true;
  1066. },
  1067. _trigger: function() {
  1068. if ($.Widget.prototype._trigger.apply(this, arguments) === false) {
  1069. this.cancel();
  1070. }
  1071. },
  1072. _uiHash: function(_inst) {
  1073. var inst = _inst || this;
  1074. return {
  1075. helper: inst.helper,
  1076. placeholder: inst.placeholder || $([]),
  1077. position: inst.position,
  1078. originalPosition: inst.originalPosition,
  1079. offset: inst.positionAbs,
  1080. item: inst.currentItem,
  1081. sender: _inst ? _inst.element : null
  1082. };
  1083. }
  1084. });
  1085. })(jQuery);