jquery.ui.sortable.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*!
  2. * jQuery UI Sortable 1.8.21
  3. *
  4. * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
  5. * Dual licensed under the MIT or GPL Version 2 licenses.
  6. * http://jquery.org/license
  7. *
  8. * http://docs.jquery.com/UI/Sortables
  9. *
  10. * Depends:
  11. * jquery.ui.core.js
  12. * jquery.ui.mouse.js
  13. * jquery.ui.widget.js
  14. */
  15. (function( $, undefined ) {
  16. $.widget("ui.sortable", $.ui.mouse, {
  17. widgetEventPrefix: "sort",
  18. ready: false,
  19. options: {
  20. appendTo: "parent",
  21. axis: false,
  22. connectWith: false,
  23. containment: false,
  24. cursor: 'auto',
  25. cursorAt: false,
  26. dropOnEmpty: true,
  27. forcePlaceholderSize: false,
  28. forceHelperSize: false,
  29. grid: false,
  30. handle: false,
  31. helper: "original",
  32. items: '> *',
  33. opacity: false,
  34. placeholder: false,
  35. revert: false,
  36. scroll: true,
  37. scrollSensitivity: 20,
  38. scrollSpeed: 20,
  39. scope: "default",
  40. tolerance: "intersect",
  41. zIndex: 1000
  42. },
  43. _create: function() {
  44. var o = this.options;
  45. this.containerCache = {};
  46. this.element.addClass("ui-sortable");
  47. //Get the items
  48. this.refresh();
  49. //Let's determine if the items are being displayed horizontally
  50. this.floating = this.items.length ? o.axis === 'x' || (/left|right/).test(this.items[0].item.css('float')) || (/inline|table-cell/).test(this.items[0].item.css('display')) : false;
  51. //Let's determine the parent's offset
  52. this.offset = this.element.offset();
  53. //Initialize mouse events for interaction
  54. this._mouseInit();
  55. //We're ready to go
  56. this.ready = true
  57. },
  58. destroy: function() {
  59. $.Widget.prototype.destroy.call( this );
  60. this.element
  61. .removeClass("ui-sortable ui-sortable-disabled");
  62. this._mouseDestroy();
  63. for ( var i = this.items.length - 1; i >= 0; i-- )
  64. this.items[i].item.removeData(this.widgetName + "-item");
  65. return this;
  66. },
  67. _setOption: function(key, value){
  68. if ( key === "disabled" ) {
  69. this.options[ key ] = value;
  70. this.widget()
  71. [ value ? "addClass" : "removeClass"]( "ui-sortable-disabled" );
  72. } else {
  73. // Don't call widget base _setOption for disable as it adds ui-state-disabled class
  74. $.Widget.prototype._setOption.apply(this, arguments);
  75. }
  76. },
  77. _mouseCapture: function(event, overrideHandle) {
  78. var that = this;
  79. if (this.reverting) {
  80. return false;
  81. }
  82. if(this.options.disabled || this.options.type == 'static') return false;
  83. //We have to refresh the items data once first
  84. this._refreshItems(event);
  85. //Find out if the clicked node (or one of its parents) is a actual item in this.items
  86. var currentItem = null, self = this, nodes = $(event.target).parents().each(function() {
  87. if($.data(this, that.widgetName + '-item') == self) {
  88. currentItem = $(this);
  89. return false;
  90. }
  91. });
  92. if($.data(event.target, that.widgetName + '-item') == self) currentItem = $(event.target);
  93. if(!currentItem) return false;
  94. if(this.options.handle && !overrideHandle) {
  95. var validHandle = false;
  96. $(this.options.handle, currentItem).find("*").andSelf().each(function() { if(this == event.target) validHandle = true; });
  97. if(!validHandle) return false;
  98. }
  99. this.currentItem = currentItem;
  100. this._removeCurrentsFromItems();
  101. return true;
  102. },
  103. _mouseStart: function(event, overrideHandle, noActivation) {
  104. var o = this.options, self = this;
  105. this.currentContainer = this;
  106. //We only need to call refreshPositions, because the refreshItems call has been moved to mouseCapture
  107. this.refreshPositions();
  108. //Create and append the visible helper
  109. this.helper = this._createHelper(event);
  110. //Cache the helper size
  111. this._cacheHelperProportions();
  112. /*
  113. * - Position generation -
  114. * This block generates everything position related - it's the core of draggables.
  115. */
  116. //Cache the margins of the original element
  117. this._cacheMargins();
  118. //Get the next scrolling parent
  119. this.scrollParent = this.helper.scrollParent();
  120. //The element's absolute position on the page minus margins
  121. this.offset = this.currentItem.offset();
  122. this.offset = {
  123. top: this.offset.top - this.margins.top,
  124. left: this.offset.left - this.margins.left
  125. };
  126. $.extend(this.offset, {
  127. click: { //Where the click happened, relative to the element
  128. left: event.pageX - this.offset.left,
  129. top: event.pageY - this.offset.top
  130. },
  131. parent: this._getParentOffset(),
  132. relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
  133. });
  134. // Only after we got the offset, we can change the helper's position to absolute
  135. // TODO: Still need to figure out a way to make relative sorting possible
  136. this.helper.css("position", "absolute");
  137. this.cssPosition = this.helper.css("position");
  138. //Generate the original position
  139. this.originalPosition = this._generatePosition(event);
  140. this.originalPageX = event.pageX;
  141. this.originalPageY = event.pageY;
  142. //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied
  143. (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
  144. //Cache the former DOM position
  145. this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] };
  146. //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
  147. if(this.helper[0] != this.currentItem[0]) {
  148. this.currentItem.hide();
  149. }
  150. //Create the placeholder
  151. this._createPlaceholder();
  152. //Set a containment if given in the options
  153. if(o.containment)
  154. this._setContainment();
  155. if(o.cursor) { // cursor option
  156. if ($('body').css("cursor")) this._storedCursor = $('body').css("cursor");
  157. $('body').css("cursor", o.cursor);
  158. }
  159. if(o.opacity) { // opacity option
  160. if (this.helper.css("opacity")) this._storedOpacity = this.helper.css("opacity");
  161. this.helper.css("opacity", o.opacity);
  162. }
  163. if(o.zIndex) { // zIndex option
  164. if (this.helper.css("zIndex")) this._storedZIndex = this.helper.css("zIndex");
  165. this.helper.css("zIndex", o.zIndex);
  166. }
  167. //Prepare scrolling
  168. if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML')
  169. this.overflowOffset = this.scrollParent.offset();
  170. //Call callbacks
  171. this._trigger("start", event, this._uiHash());
  172. //Recache the helper size
  173. if(!this._preserveHelperProportions)
  174. this._cacheHelperProportions();
  175. //Post 'activate' events to possible containers
  176. if(!noActivation) {
  177. for (var i = this.containers.length - 1; i >= 0; i--) { this.containers[i]._trigger("activate", event, self._uiHash(this)); }
  178. }
  179. //Prepare possible droppables
  180. if($.ui.ddmanager)
  181. $.ui.ddmanager.current = this;
  182. if ($.ui.ddmanager && !o.dropBehaviour)
  183. $.ui.ddmanager.prepareOffsets(this, event);
  184. this.dragging = true;
  185. this.helper.addClass("ui-sortable-helper");
  186. this._mouseDrag(event); //Execute the drag once - this causes the helper not to be visible before getting its correct position
  187. return true;
  188. },
  189. _mouseDrag: function(event) {
  190. //Compute the helpers position
  191. this.position = this._generatePosition(event);
  192. this.positionAbs = this._convertPositionTo("absolute");
  193. if (!this.lastPositionAbs) {
  194. this.lastPositionAbs = this.positionAbs;
  195. }
  196. //Do scrolling
  197. if(this.options.scroll) {
  198. var o = this.options, scrolled = false;
  199. if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML') {
  200. if((this.overflowOffset.top + this.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity)
  201. this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop + o.scrollSpeed;
  202. else if(event.pageY - this.overflowOffset.top < o.scrollSensitivity)
  203. this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop - o.scrollSpeed;
  204. if((this.overflowOffset.left + this.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity)
  205. this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft + o.scrollSpeed;
  206. else if(event.pageX - this.overflowOffset.left < o.scrollSensitivity)
  207. this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft - o.scrollSpeed;
  208. } else {
  209. if(event.pageY - $(document).scrollTop() < o.scrollSensitivity)
  210. scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
  211. else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity)
  212. scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
  213. if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity)
  214. scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
  215. else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity)
  216. scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
  217. }
  218. if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour)
  219. $.ui.ddmanager.prepareOffsets(this, event);
  220. }
  221. //Regenerate the absolute position used for position checks
  222. this.positionAbs = this._convertPositionTo("absolute");
  223. //Set the helper position
  224. if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
  225. if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';
  226. //Rearrange
  227. for (var i = this.items.length - 1; i >= 0; i--) {
  228. //Cache variables and intersection, continue if no intersection
  229. var item = this.items[i], itemElement = item.item[0], intersection = this._intersectsWithPointer(item);
  230. if (!intersection) continue;
  231. if(itemElement != this.currentItem[0] //cannot intersect with itself
  232. && this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemElement //no useless actions that have been done before
  233. && !$.ui.contains(this.placeholder[0], itemElement) //no action if the item moved is the parent of the item checked
  234. && (this.options.type == 'semi-dynamic' ? !$.ui.contains(this.element[0], itemElement) : true)
  235. //&& itemElement.parentNode == this.placeholder[0].parentNode // only rearrange items within the same container
  236. ) {
  237. this.direction = intersection == 1 ? "down" : "up";
  238. if (this.options.tolerance == "pointer" || this._intersectsWithSides(item)) {
  239. this._rearrange(event, item);
  240. } else {
  241. break;
  242. }
  243. this._trigger("change", event, this._uiHash());
  244. break;
  245. }
  246. }
  247. //Post events to containers
  248. this._contactContainers(event);
  249. //Interconnect with droppables
  250. if($.ui.ddmanager) $.ui.ddmanager.drag(this, event);
  251. //Call callbacks
  252. this._trigger('sort', event, this._uiHash());
  253. this.lastPositionAbs = this.positionAbs;
  254. return false;
  255. },
  256. _mouseStop: function(event, noPropagation) {
  257. if(!event) return;
  258. //If we are using droppables, inform the manager about the drop
  259. if ($.ui.ddmanager && !this.options.dropBehaviour)
  260. $.ui.ddmanager.drop(this, event);
  261. if(this.options.revert) {
  262. var self = this;
  263. var cur = self.placeholder.offset();
  264. self.reverting = true;
  265. $(this.helper).animate({
  266. left: cur.left - this.offset.parent.left - self.margins.left + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft),
  267. top: cur.top - this.offset.parent.top - self.margins.top + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop)
  268. }, parseInt(this.options.revert, 10) || 500, function() {
  269. self._clear(event);
  270. });
  271. } else {
  272. this._clear(event, noPropagation);
  273. }
  274. return false;
  275. },
  276. cancel: function() {
  277. var self = this;
  278. if(this.dragging) {
  279. this._mouseUp({ target: null });
  280. if(this.options.helper == "original")
  281. this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
  282. else
  283. this.currentItem.show();
  284. //Post deactivating events to containers
  285. for (var i = this.containers.length - 1; i >= 0; i--){
  286. this.containers[i]._trigger("deactivate", null, self._uiHash(this));
  287. if(this.containers[i].containerCache.over) {
  288. this.containers[i]._trigger("out", null, self._uiHash(this));
  289. this.containers[i].containerCache.over = 0;
  290. }
  291. }
  292. }
  293. if (this.placeholder) {
  294. //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
  295. if(this.placeholder[0].parentNode) this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
  296. if(this.options.helper != "original" && this.helper && this.helper[0].parentNode) this.helper.remove();
  297. $.extend(this, {
  298. helper: null,
  299. dragging: false,
  300. reverting: false,
  301. _noFinalSort: null
  302. });
  303. if(this.domPosition.prev) {
  304. $(this.domPosition.prev).after(this.currentItem);
  305. } else {
  306. $(this.domPosition.parent).prepend(this.currentItem);
  307. }
  308. }
  309. return this;
  310. },
  311. serialize: function(o) {
  312. var items = this._getItemsAsjQuery(o && o.connected);
  313. var str = []; o = o || {};
  314. $(items).each(function() {
  315. var res = ($(o.item || this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/));
  316. if(res) str.push((o.key || res[1]+'[]')+'='+(o.key && o.expression ? res[1] : res[2]));
  317. });
  318. if(!str.length && o.key) {
  319. str.push(o.key + '=');
  320. }
  321. return str.join('&');
  322. },
  323. toArray: function(o) {
  324. var items = this._getItemsAsjQuery(o && o.connected);
  325. var ret = []; o = o || {};
  326. items.each(function() { ret.push($(o.item || this).attr(o.attribute || 'id') || ''); });
  327. return ret;
  328. },
  329. /* Be careful with the following core functions */
  330. _intersectsWith: function(item) {
  331. var x1 = this.positionAbs.left,
  332. x2 = x1 + this.helperProportions.width,
  333. y1 = this.positionAbs.top,
  334. y2 = y1 + this.helperProportions.height;
  335. var l = item.left,
  336. r = l + item.width,
  337. t = item.top,
  338. b = t + item.height;
  339. var dyClick = this.offset.click.top,
  340. dxClick = this.offset.click.left;
  341. var isOverElement = (y1 + dyClick) > t && (y1 + dyClick) < b && (x1 + dxClick) > l && (x1 + dxClick) < r;
  342. if( this.options.tolerance == "pointer"
  343. || this.options.forcePointerForContainers
  344. || (this.options.tolerance != "pointer" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height'])
  345. ) {
  346. return isOverElement;
  347. } else {
  348. return (l < x1 + (this.helperProportions.width / 2) // Right Half
  349. && x2 - (this.helperProportions.width / 2) < r // Left Half
  350. && t < y1 + (this.helperProportions.height / 2) // Bottom Half
  351. && y2 - (this.helperProportions.height / 2) < b ); // Top Half
  352. }
  353. },
  354. _intersectsWithPointer: function(item) {
  355. var isOverElementHeight = (this.options.axis === 'x') || $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top, item.height),
  356. isOverElementWidth = (this.options.axis === 'y') || $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left, item.width),
  357. isOverElement = isOverElementHeight && isOverElementWidth,
  358. verticalDirection = this._getDragVerticalDirection(),
  359. horizontalDirection = this._getDragHorizontalDirection();
  360. if (!isOverElement)
  361. return false;
  362. return this.floating ?
  363. ( ((horizontalDirection && horizontalDirection == "right") || verticalDirection == "down") ? 2 : 1 )
  364. : ( verticalDirection && (verticalDirection == "down" ? 2 : 1) );
  365. },
  366. _intersectsWithSides: function(item) {
  367. var isOverBottomHalf = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top + (item.height/2), item.height),
  368. isOverRightHalf = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left + (item.width/2), item.width),
  369. verticalDirection = this._getDragVerticalDirection(),
  370. horizontalDirection = this._getDragHorizontalDirection();
  371. if (this.floating && horizontalDirection) {
  372. return ((horizontalDirection == "right" && isOverRightHalf) || (horizontalDirection == "left" && !isOverRightHalf));
  373. } else {
  374. return verticalDirection && ((verticalDirection == "down" && isOverBottomHalf) || (verticalDirection == "up" && !isOverBottomHalf));
  375. }
  376. },
  377. _getDragVerticalDirection: function() {
  378. var delta = this.positionAbs.top - this.lastPositionAbs.top;
  379. return delta != 0 && (delta > 0 ? "down" : "up");
  380. },
  381. _getDragHorizontalDirection: function() {
  382. var delta = this.positionAbs.left - this.lastPositionAbs.left;
  383. return delta != 0 && (delta > 0 ? "right" : "left");
  384. },
  385. refresh: function(event) {
  386. this._refreshItems(event);
  387. this.refreshPositions();
  388. return this;
  389. },
  390. _connectWith: function() {
  391. var options = this.options;
  392. return options.connectWith.constructor == String
  393. ? [options.connectWith]
  394. : options.connectWith;
  395. },
  396. _getItemsAsjQuery: function(connected) {
  397. var self = this;
  398. var items = [];
  399. var queries = [];
  400. var connectWith = this._connectWith();
  401. if(connectWith && connected) {
  402. for (var i = connectWith.length - 1; i >= 0; i--){
  403. var cur = $(connectWith[i]);
  404. for (var j = cur.length - 1; j >= 0; j--){
  405. var inst = $.data(cur[j], this.widgetName);
  406. if(inst && inst != this && !inst.options.disabled) {
  407. queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element).not(".ui-sortable-helper").not('.ui-sortable-placeholder'), inst]);
  408. }
  409. };
  410. };
  411. }
  412. queries.push([$.isFunction(this.options.items) ? 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]);
  413. for (var i = queries.length - 1; i >= 0; i--){
  414. queries[i][0].each(function() {
  415. items.push(this);
  416. });
  417. };
  418. return $(items);
  419. },
  420. _removeCurrentsFromItems: function() {
  421. var list = this.currentItem.find(":data(" + this.widgetName + "-item)");
  422. for (var i=0; i < this.items.length; i++) {
  423. for (var j=0; j < list.length; j++) {
  424. if(list[j] == this.items[i].item[0])
  425. this.items.splice(i,1);
  426. };
  427. };
  428. },
  429. _refreshItems: function(event) {
  430. this.items = [];
  431. this.containers = [this];
  432. var items = this.items;
  433. var self = this;
  434. var queries = [[$.isFunction(this.options.items) ? this.options.items.call(this.element[0], event, { item: this.currentItem }) : $(this.options.items, this.element), this]];
  435. var connectWith = this._connectWith();
  436. if(connectWith && this.ready) { //Shouldn't be run the first time through due to massive slow-down
  437. for (var i = connectWith.length - 1; i >= 0; i--){
  438. var cur = $(connectWith[i]);
  439. for (var j = cur.length - 1; j >= 0; j--){
  440. var inst = $.data(cur[j], this.widgetName);
  441. if(inst && inst != this && !inst.options.disabled) {
  442. queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element[0], event, { item: this.currentItem }) : $(inst.options.items, inst.element), inst]);
  443. this.containers.push(inst);
  444. }
  445. };
  446. };
  447. }
  448. for (var i = queries.length - 1; i >= 0; i--) {
  449. var targetData = queries[i][1];
  450. var _queries = queries[i][0];
  451. for (var j=0, queriesLength = _queries.length; j < queriesLength; j++) {
  452. var item = $(_queries[j]);
  453. item.data(this.widgetName + '-item', targetData); // Data for target checking (mouse manager)
  454. items.push({
  455. item: item,
  456. instance: targetData,
  457. width: 0, height: 0,
  458. left: 0, top: 0
  459. });
  460. };
  461. };
  462. },
  463. refreshPositions: function(fast) {
  464. //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change
  465. if(this.offsetParent && this.helper) {
  466. this.offset.parent = this._getParentOffset();
  467. }
  468. for (var i = this.items.length - 1; i >= 0; i--){
  469. var item = this.items[i];
  470. //We ignore calculating positions of all connected containers when we're not over them
  471. if(item.instance != this.currentContainer && this.currentContainer && item.item[0] != this.currentItem[0])
  472. continue;
  473. var t = this.options.toleranceElement ? $(this.options.toleranceElement, item.item) : item.item;
  474. if (!fast) {
  475. item.width = t.outerWidth();
  476. item.height = t.outerHeight();
  477. }
  478. var p = t.offset();
  479. item.left = p.left;
  480. item.top = p.top;
  481. };
  482. if(this.options.custom && this.options.custom.refreshContainers) {
  483. this.options.custom.refreshContainers.call(this);
  484. } else {
  485. for (var i = this.containers.length - 1; i >= 0; i--){
  486. var p = this.containers[i].element.offset();
  487. this.containers[i].containerCache.left = p.left;
  488. this.containers[i].containerCache.top = p.top;
  489. this.containers[i].containerCache.width = this.containers[i].element.outerWidth();
  490. this.containers[i].containerCache.height = this.containers[i].element.outerHeight();
  491. };
  492. }
  493. return this;
  494. },
  495. _createPlaceholder: function(that) {
  496. var self = that || this, o = self.options;
  497. if(!o.placeholder || o.placeholder.constructor == String) {
  498. var className = o.placeholder;
  499. o.placeholder = {
  500. element: function() {
  501. var el = $(document.createElement(self.currentItem[0].nodeName))
  502. .addClass(className || self.currentItem[0].className+" ui-sortable-placeholder")
  503. .removeClass("ui-sortable-helper")[0];
  504. if(!className)
  505. el.style.visibility = "hidden";
  506. return el;
  507. },
  508. update: function(container, p) {
  509. // 1. If a className is set as 'placeholder option, we don't force sizes - the class is responsible for that
  510. // 2. The option 'forcePlaceholderSize can be enabled to force it even if a class name is specified
  511. if(className && !o.forcePlaceholderSize) return;
  512. //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
  513. if(!p.height()) { p.height(self.currentItem.innerHeight() - parseInt(self.currentItem.css('paddingTop')||0, 10) - parseInt(self.currentItem.css('paddingBottom')||0, 10)); };
  514. if(!p.width()) { p.width(self.currentItem.innerWidth() - parseInt(self.currentItem.css('paddingLeft')||0, 10) - parseInt(self.currentItem.css('paddingRight')||0, 10)); };
  515. }
  516. };
  517. }
  518. //Create the placeholder
  519. self.placeholder = $(o.placeholder.element.call(self.element, self.currentItem));
  520. //Append it after the actual current item
  521. self.currentItem.after(self.placeholder);
  522. //Update the size of the placeholder (TODO: Logic to fuzzy, see line 316/317)
  523. o.placeholder.update(self, self.placeholder);
  524. },
  525. _contactContainers: function(event) {
  526. // get innermost container that intersects with item
  527. var innermostContainer = null, innermostIndex = null;
  528. for (var i = this.containers.length - 1; i >= 0; i--){
  529. // never consider a container that's located within the item itself
  530. if($.ui.contains(this.currentItem[0], this.containers[i].element[0]))
  531. continue;
  532. if(this._intersectsWith(this.containers[i].containerCache)) {
  533. // if we've already found a container and it's more "inner" than this, then continue
  534. if(innermostContainer && $.ui.contains(this.containers[i].element[0], innermostContainer.element[0]))
  535. continue;
  536. innermostContainer = this.containers[i];
  537. innermostIndex = i;
  538. } else {
  539. // container doesn't intersect. trigger "out" event if necessary
  540. if(this.containers[i].containerCache.over) {
  541. this.containers[i]._trigger("out", event, this._uiHash(this));
  542. this.containers[i].containerCache.over = 0;
  543. }
  544. }
  545. }
  546. // if no intersecting containers found, return
  547. if(!innermostContainer) return;
  548. // move the item into the container if it's not there already
  549. if(this.containers.length === 1) {
  550. this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
  551. this.containers[innermostIndex].containerCache.over = 1;
  552. } else if(this.currentContainer != this.containers[innermostIndex]) {
  553. //When entering a new container, we will find the item with the least distance and append our item near it
  554. var dist = 10000; var itemWithLeastDistance = null; var base = this.positionAbs[this.containers[innermostIndex].floating ? 'left' : 'top'];
  555. for (var j = this.items.length - 1; j >= 0; j--) {
  556. if(!$.ui.contains(this.containers[innermostIndex].element[0], this.items[j].item[0])) continue;
  557. var cur = this.containers[innermostIndex].floating ? this.items[j].item.offset().left : this.items[j].item.offset().top;
  558. if(Math.abs(cur - base) < dist) {
  559. dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j];
  560. this.direction = (cur - base > 0) ? 'down' : 'up';
  561. }
  562. }
  563. if(!itemWithLeastDistance && !this.options.dropOnEmpty) //Check if dropOnEmpty is enabled
  564. return;
  565. this.currentContainer = this.containers[innermostIndex];
  566. itemWithLeastDistance ? this._rearrange(event, itemWithLeastDistance, null, true) : this._rearrange(event, null, this.containers[innermostIndex].element, true);
  567. this._trigger("change", event, this._uiHash());
  568. this.containers[innermostIndex]._trigger("change", event, this._uiHash(this));
  569. //Update the placeholder
  570. this.options.placeholder.update(this.currentContainer, this.placeholder);
  571. this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
  572. this.containers[innermostIndex].containerCache.over = 1;
  573. }
  574. },
  575. _createHelper: function(event) {
  576. var o = this.options;
  577. var helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[0], [event, this.currentItem])) : (o.helper == 'clone' ? this.currentItem.clone() : this.currentItem);
  578. if(!helper.parents('body').length) //Add the helper to the DOM if that didn't happen already
  579. $(o.appendTo != 'parent' ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(helper[0]);
  580. if(helper[0] == this.currentItem[0])
  581. 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") };
  582. if(helper[0].style.width == '' || o.forceHelperSize) helper.width(this.currentItem.width());
  583. if(helper[0].style.height == '' || o.forceHelperSize) helper.height(this.currentItem.height());
  584. return helper;
  585. },
  586. _adjustOffsetFromHelper: function(obj) {
  587. if (typeof obj == 'string') {
  588. obj = obj.split(' ');
  589. }
  590. if ($.isArray(obj)) {
  591. obj = {left: +obj[0], top: +obj[1] || 0};
  592. }
  593. if ('left' in obj) {
  594. this.offset.click.left = obj.left + this.margins.left;
  595. }
  596. if ('right' in obj) {
  597. this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
  598. }
  599. if ('top' in obj) {
  600. this.offset.click.top = obj.top + this.margins.top;
  601. }
  602. if ('bottom' in obj) {
  603. this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
  604. }
  605. },
  606. _getParentOffset: function() {
  607. //Get the offsetParent and cache its position
  608. this.offsetParent = this.helper.offsetParent();
  609. var po = this.offsetParent.offset();
  610. // This is a special case where we need to modify a offset calculated on start, since the following happened:
  611. // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
  612. // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
  613. // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
  614. if(this.cssPosition == 'absolute' && this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) {
  615. po.left += this.scrollParent.scrollLeft();
  616. po.top += this.scrollParent.scrollTop();
  617. }
  618. if((this.offsetParent[0] == document.body) //This needs to be actually done for all browsers, since pageX/pageY includes this information
  619. || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.browser.msie)) //Ugly IE fix
  620. po = { top: 0, left: 0 };
  621. return {
  622. top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
  623. left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
  624. };
  625. },
  626. _getRelativeOffset: function() {
  627. if(this.cssPosition == "relative") {
  628. var p = this.currentItem.position();
  629. return {
  630. top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(),
  631. left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft()
  632. };
  633. } else {
  634. return { top: 0, left: 0 };
  635. }
  636. },
  637. _cacheMargins: function() {
  638. this.margins = {
  639. left: (parseInt(this.currentItem.css("marginLeft"),10) || 0),
  640. top: (parseInt(this.currentItem.css("marginTop"),10) || 0)
  641. };
  642. },
  643. _cacheHelperProportions: function() {
  644. this.helperProportions = {
  645. width: this.helper.outerWidth(),
  646. height: this.helper.outerHeight()
  647. };
  648. },
  649. _setContainment: function() {
  650. var o = this.options;
  651. if(o.containment == 'parent') o.containment = this.helper[0].parentNode;
  652. if(o.containment == 'document' || o.containment == 'window') this.containment = [
  653. 0 - this.offset.relative.left - this.offset.parent.left,
  654. 0 - this.offset.relative.top - this.offset.parent.top,
  655. $(o.containment == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left,
  656. ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
  657. ];
  658. if(!(/^(document|window|parent)$/).test(o.containment)) {
  659. var ce = $(o.containment)[0];
  660. var co = $(o.containment).offset();
  661. var over = ($(ce).css("overflow") != 'hidden');
  662. this.containment = [
  663. co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0) - this.margins.left,
  664. co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0) - this.margins.top,
  665. 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,
  666. 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
  667. ];
  668. }
  669. },
  670. _convertPositionTo: function(d, pos) {
  671. if(!pos) pos = this.position;
  672. var mod = d == "absolute" ? 1 : -1;
  673. 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);
  674. return {
  675. top: (
  676. pos.top // The absolute mouse position
  677. + this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent
  678. + this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border)
  679. - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod)
  680. ),
  681. left: (
  682. pos.left // The absolute mouse position
  683. + this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent
  684. + this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border)
  685. - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod)
  686. )
  687. };
  688. },
  689. _generatePosition: function(event) {
  690. 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);
  691. // This is another very weird special case that only happens for relative elements:
  692. // 1. If the css position is relative
  693. // 2. and the scroll parent is the document or similar to the offset parent
  694. // we have to refresh the relative offset during the scroll so there are no jumps
  695. if(this.cssPosition == 'relative' && !(this.scrollParent[0] != document && this.scrollParent[0] != this.offsetParent[0])) {
  696. this.offset.relative = this._getRelativeOffset();
  697. }
  698. var pageX = event.pageX;
  699. var pageY = event.pageY;
  700. /*
  701. * - Position constraining -
  702. * Constrain the position to a mix of grid, containment.
  703. */
  704. if(this.originalPosition) { //If we are not dragging yet, we won't check for options
  705. if(this.containment) {
  706. if(event.pageX - this.offset.click.left < this.containment[0]) pageX = this.containment[0] + this.offset.click.left;
  707. if(event.pageY - this.offset.click.top < this.containment[1]) pageY = this.containment[1] + this.offset.click.top;
  708. if(event.pageX - this.offset.click.left > this.containment[2]) pageX = this.containment[2] + this.offset.click.left;
  709. if(event.pageY - this.offset.click.top > this.containment[3]) pageY = this.containment[3] + this.offset.click.top;
  710. }
  711. if(o.grid) {
  712. var top = this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1];
  713. 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;
  714. var left = this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0];
  715. 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;
  716. }
  717. }
  718. return {
  719. top: (
  720. pageY // The absolute mouse position
  721. - this.offset.click.top // Click offset (relative to the element)
  722. - this.offset.relative.top // Only for relative positioned nodes: Relative offset from element to offset parent
  723. - this.offset.parent.top // The offsetParent's offset without borders (offset + border)
  724. + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ))
  725. ),
  726. left: (
  727. pageX // The absolute mouse position
  728. - this.offset.click.left // Click offset (relative to the element)
  729. - this.offset.relative.left // Only for relative positioned nodes: Relative offset from element to offset parent
  730. - this.offset.parent.left // The offsetParent's offset without borders (offset + border)
  731. + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ))
  732. )
  733. };
  734. },
  735. _rearrange: function(event, i, a, hardRefresh) {
  736. 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));
  737. //Various things done here to improve the performance:
  738. // 1. we create a setTimeout, that calls refreshPositions
  739. // 2. on the instance, we have a counter variable, that get's higher after every append
  740. // 3. on the local scope, we copy the counter variable, and check in the timeout, if it's still the same
  741. // 4. this lets only the last addition to the timeout stack through
  742. this.counter = this.counter ? ++this.counter : 1;
  743. var self = this, counter = this.counter;
  744. window.setTimeout(function() {
  745. if(counter == self.counter) self.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove
  746. },0);
  747. },
  748. _clear: function(event, noPropagation) {
  749. this.reverting = false;
  750. // We delay all events that have to be triggered to after the point where the placeholder has been removed and
  751. // everything else normalized again
  752. var delayedTriggers = [], self = this;
  753. // We first have to update the dom position of the actual currentItem
  754. // Note: don't do it if the current item is already removed (by a user), or it gets reappended (see #4088)
  755. if(!this._noFinalSort && this.currentItem.parent().length) this.placeholder.before(this.currentItem);
  756. this._noFinalSort = null;
  757. if(this.helper[0] == this.currentItem[0]) {
  758. for(var i in this._storedCSS) {
  759. if(this._storedCSS[i] == 'auto' || this._storedCSS[i] == 'static') this._storedCSS[i] = '';
  760. }
  761. this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
  762. } else {
  763. this.currentItem.show();
  764. }
  765. if(this.fromOutside && !noPropagation) delayedTriggers.push(function(event) { this._trigger("receive", event, this._uiHash(this.fromOutside)); });
  766. if((this.fromOutside || this.domPosition.prev != this.currentItem.prev().not(".ui-sortable-helper")[0] || this.domPosition.parent != this.currentItem.parent()[0]) && !noPropagation) delayedTriggers.push(function(event) { this._trigger("update", event, this._uiHash()); }); //Trigger update callback if the DOM position has changed
  767. if(!$.ui.contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element
  768. if(!noPropagation) delayedTriggers.push(function(event) { this._trigger("remove", event, this._uiHash()); });
  769. for (var i = this.containers.length - 1; i >= 0; i--){
  770. if($.ui.contains(this.containers[i].element[0], this.currentItem[0]) && !noPropagation) {
  771. delayedTriggers.push((function(c) { return function(event) { c._trigger("receive", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
  772. delayedTriggers.push((function(c) { return function(event) { c._trigger("update", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
  773. }
  774. };
  775. };
  776. //Post events to containers
  777. for (var i = this.containers.length - 1; i >= 0; i--){
  778. if(!noPropagation) delayedTriggers.push((function(c) { return function(event) { c._trigger("deactivate", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
  779. if(this.containers[i].containerCache.over) {
  780. delayedTriggers.push((function(c) { return function(event) { c._trigger("out", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
  781. this.containers[i].containerCache.over = 0;
  782. }
  783. }
  784. //Do what was originally in plugins
  785. if(this._storedCursor) $('body').css("cursor", this._storedCursor); //Reset cursor
  786. if(this._storedOpacity) this.helper.css("opacity", this._storedOpacity); //Reset opacity
  787. if(this._storedZIndex) this.helper.css("zIndex", this._storedZIndex == 'auto' ? '' : this._storedZIndex); //Reset z-index
  788. this.dragging = false;
  789. if(this.cancelHelperRemoval) {
  790. if(!noPropagation) {
  791. this._trigger("beforeStop", event, this._uiHash());
  792. for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events
  793. this._trigger("stop", event, this._uiHash());
  794. }
  795. return false;
  796. }
  797. if(!noPropagation) this._trigger("beforeStop", event, this._uiHash());
  798. //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
  799. this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
  800. if(this.helper[0] != this.currentItem[0]) this.helper.remove(); this.helper = null;
  801. if(!noPropagation) {
  802. for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events
  803. this._trigger("stop", event, this._uiHash());
  804. }
  805. this.fromOutside = false;
  806. return true;
  807. },
  808. _trigger: function() {
  809. if ($.Widget.prototype._trigger.apply(this, arguments) === false) {
  810. this.cancel();
  811. }
  812. },
  813. _uiHash: function(inst) {
  814. var self = inst || this;
  815. return {
  816. helper: self.helper,
  817. placeholder: self.placeholder || $([]),
  818. position: self.position,
  819. originalPosition: self.originalPosition,
  820. offset: self.positionAbs,
  821. item: self.currentItem,
  822. sender: inst ? inst.element : null
  823. };
  824. }
  825. });
  826. $.extend($.ui.sortable, {
  827. version: "1.8.21"
  828. });
  829. })(jQuery);