2 * jQuery UI Sortable 1.8.11
4 * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
5 * Dual licensed under the MIT or GPL Version 2 licenses.
6 * http://jquery.org/license
8 * http://docs.jquery.com/UI/Sortables
15 (function( $, undefined ) {
17 $.widget("ui.sortable", $.ui.mouse, {
18 widgetEventPrefix: "sort",
27 forcePlaceholderSize: false,
28 forceHelperSize: false,
37 scrollSensitivity: 20,
40 tolerance: "intersect",
46 this.containerCache = {};
47 this.element.addClass("ui-sortable");
52 //Let's determine if the items are being displayed horizontally
53 this.floating = this.items.length ? (/left|right/).test(this.items[0].item.css('float')) || (/inline|table-cell/).test(this.items[0].item.css('display')) : false;
55 //Let's determine the parent's offset
56 this.offset = this.element.offset();
58 //Initialize mouse events for interaction
65 .removeClass("ui-sortable ui-sortable-disabled")
66 .removeData("sortable")
70 for ( var i = this.items.length - 1; i >= 0; i-- )
71 this.items[i].item.removeData("sortable-item");
76 _setOption: function(key, value){
77 if ( key === "disabled" ) {
78 this.options[ key ] = value;
81 [ value ? "addClass" : "removeClass"]( "ui-sortable-disabled" );
83 // Don't call widget base _setOption for disable as it adds ui-state-disabled class
84 $.Widget.prototype._setOption.apply(this, arguments);
88 _mouseCapture: function(event, overrideHandle) {
94 if(this.options.disabled || this.options.type == 'static') return false;
96 //We have to refresh the items data once first
97 this._refreshItems(event);
99 //Find out if the clicked node (or one of its parents) is a actual item in this.items
100 var currentItem = null, self = this, nodes = $(event.target).parents().each(function() {
101 if($.data(this, 'sortable-item') == self) {
102 currentItem = $(this);
106 if($.data(event.target, 'sortable-item') == self) currentItem = $(event.target);
108 if(!currentItem) return false;
109 if(this.options.handle && !overrideHandle) {
110 var validHandle = false;
112 $(this.options.handle, currentItem).find("*").andSelf().each(function() { if(this == event.target) validHandle = true; });
113 if(!validHandle) return false;
116 this.currentItem = currentItem;
117 this._removeCurrentsFromItems();
122 _mouseStart: function(event, overrideHandle, noActivation) {
124 var o = this.options, self = this;
125 this.currentContainer = this;
127 //We only need to call refreshPositions, because the refreshItems call has been moved to mouseCapture
128 this.refreshPositions();
130 //Create and append the visible helper
131 this.helper = this._createHelper(event);
133 //Cache the helper size
134 this._cacheHelperProportions();
137 * - Position generation -
138 * This block generates everything position related - it's the core of draggables.
141 //Cache the margins of the original element
142 this._cacheMargins();
144 //Get the next scrolling parent
145 this.scrollParent = this.helper.scrollParent();
147 //The element's absolute position on the page minus margins
148 this.offset = this.currentItem.offset();
150 top: this.offset.top - this.margins.top,
151 left: this.offset.left - this.margins.left
154 // Only after we got the offset, we can change the helper's position to absolute
155 // TODO: Still need to figure out a way to make relative sorting possible
156 this.helper.css("position", "absolute");
157 this.cssPosition = this.helper.css("position");
159 $.extend(this.offset, {
160 click: { //Where the click happened, relative to the element
161 left: event.pageX - this.offset.left,
162 top: event.pageY - this.offset.top
164 parent: this._getParentOffset(),
165 relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
168 //Generate the original position
169 this.originalPosition = this._generatePosition(event);
170 this.originalPageX = event.pageX;
171 this.originalPageY = event.pageY;
173 //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied
174 (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
176 //Cache the former DOM position
177 this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] };
179 //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
180 if(this.helper[0] != this.currentItem[0]) {
181 this.currentItem.hide();
184 //Create the placeholder
185 this._createPlaceholder();
187 //Set a containment if given in the options
189 this._setContainment();
191 if(o.cursor) { // cursor option
192 if ($('body').css("cursor")) this._storedCursor = $('body').css("cursor");
193 $('body').css("cursor", o.cursor);
196 if(o.opacity) { // opacity option
197 if (this.helper.css("opacity")) this._storedOpacity = this.helper.css("opacity");
198 this.helper.css("opacity", o.opacity);
201 if(o.zIndex) { // zIndex option
202 if (this.helper.css("zIndex")) this._storedZIndex = this.helper.css("zIndex");
203 this.helper.css("zIndex", o.zIndex);
207 if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML')
208 this.overflowOffset = this.scrollParent.offset();
211 this._trigger("start", event, this._uiHash());
213 //Recache the helper size
214 if(!this._preserveHelperProportions)
215 this._cacheHelperProportions();
218 //Post 'activate' events to possible containers
220 for (var i = this.containers.length - 1; i >= 0; i--) { this.containers[i]._trigger("activate", event, self._uiHash(this)); }
223 //Prepare possible droppables
225 $.ui.ddmanager.current = this;
227 if ($.ui.ddmanager && !o.dropBehaviour)
228 $.ui.ddmanager.prepareOffsets(this, event);
230 this.dragging = true;
232 this.helper.addClass("ui-sortable-helper");
233 this._mouseDrag(event); //Execute the drag once - this causes the helper not to be visible before getting its correct position
238 _mouseDrag: function(event) {
240 //Compute the helpers position
241 this.position = this._generatePosition(event);
242 this.positionAbs = this._convertPositionTo("absolute");
244 if (!this.lastPositionAbs) {
245 this.lastPositionAbs = this.positionAbs;
249 if(this.options.scroll) {
250 var o = this.options, scrolled = false;
251 if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML') {
253 if((this.overflowOffset.top + this.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity)
254 this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop + o.scrollSpeed;
255 else if(event.pageY - this.overflowOffset.top < o.scrollSensitivity)
256 this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop - o.scrollSpeed;
258 if((this.overflowOffset.left + this.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity)
259 this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft + o.scrollSpeed;
260 else if(event.pageX - this.overflowOffset.left < o.scrollSensitivity)
261 this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft - o.scrollSpeed;
265 if(event.pageY - $(document).scrollTop() < o.scrollSensitivity)
266 scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
267 else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity)
268 scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
270 if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity)
271 scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
272 else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity)
273 scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
277 if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour)
278 $.ui.ddmanager.prepareOffsets(this, event);
281 //Regenerate the absolute position used for position checks
282 this.positionAbs = this._convertPositionTo("absolute");
284 //Set the helper position
285 if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
286 if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';
289 for (var i = this.items.length - 1; i >= 0; i--) {
291 //Cache variables and intersection, continue if no intersection
292 var item = this.items[i], itemElement = item.item[0], intersection = this._intersectsWithPointer(item);
293 if (!intersection) continue;
295 if(itemElement != this.currentItem[0] //cannot intersect with itself
296 && this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemElement //no useless actions that have been done before
297 && !$.ui.contains(this.placeholder[0], itemElement) //no action if the item moved is the parent of the item checked
298 && (this.options.type == 'semi-dynamic' ? !$.ui.contains(this.element[0], itemElement) : true)
299 //&& itemElement.parentNode == this.placeholder[0].parentNode // only rearrange items within the same container
302 this.direction = intersection == 1 ? "down" : "up";
304 if (this.options.tolerance == "pointer" || this._intersectsWithSides(item)) {
305 this._rearrange(event, item);
310 this._trigger("change", event, this._uiHash());
315 //Post events to containers
316 this._contactContainers(event);
318 //Interconnect with droppables
319 if($.ui.ddmanager) $.ui.ddmanager.drag(this, event);
322 this._trigger('sort', event, this._uiHash());
324 this.lastPositionAbs = this.positionAbs;
329 _mouseStop: function(event, noPropagation) {
333 //If we are using droppables, inform the manager about the drop
334 if ($.ui.ddmanager && !this.options.dropBehaviour)
335 $.ui.ddmanager.drop(this, event);
337 if(this.options.revert) {
339 var cur = self.placeholder.offset();
341 self.reverting = true;
343 $(this.helper).animate({
344 left: cur.left - this.offset.parent.left - self.margins.left + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft),
345 top: cur.top - this.offset.parent.top - self.margins.top + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop)
346 }, parseInt(this.options.revert, 10) || 500, function() {
350 this._clear(event, noPropagation);
363 this._mouseUp({ target: null });
365 if(this.options.helper == "original")
366 this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
368 this.currentItem.show();
370 //Post deactivating events to containers
371 for (var i = this.containers.length - 1; i >= 0; i--){
372 this.containers[i]._trigger("deactivate", null, self._uiHash(this));
373 if(this.containers[i].containerCache.over) {
374 this.containers[i]._trigger("out", null, self._uiHash(this));
375 this.containers[i].containerCache.over = 0;
381 if (this.placeholder) {
382 //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
383 if(this.placeholder[0].parentNode) this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
384 if(this.options.helper != "original" && this.helper && this.helper[0].parentNode) this.helper.remove();
393 if(this.domPosition.prev) {
394 $(this.domPosition.prev).after(this.currentItem);
396 $(this.domPosition.parent).prepend(this.currentItem);
404 serialize: function(o) {
406 var items = this._getItemsAsjQuery(o && o.connected);
407 var str = []; o = o || {};
409 $(items).each(function() {
410 var res = ($(o.item || this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/));
411 if(res) str.push((o.key || res[1]+'[]')+'='+(o.key && o.expression ? res[1] : res[2]));
414 if(!str.length && o.key) {
415 str.push(o.key + '=');
418 return str.join('&');
422 toArray: function(o) {
424 var items = this._getItemsAsjQuery(o && o.connected);
425 var ret = []; o = o || {};
427 items.each(function() { ret.push($(o.item || this).attr(o.attribute || 'id') || ''); });
432 /* Be careful with the following core functions */
433 _intersectsWith: function(item) {
435 var x1 = this.positionAbs.left,
436 x2 = x1 + this.helperProportions.width,
437 y1 = this.positionAbs.top,
438 y2 = y1 + this.helperProportions.height;
445 var dyClick = this.offset.click.top,
446 dxClick = this.offset.click.left;
448 var isOverElement = (y1 + dyClick) > t && (y1 + dyClick) < b && (x1 + dxClick) > l && (x1 + dxClick) < r;
450 if( this.options.tolerance == "pointer"
451 || this.options.forcePointerForContainers
452 || (this.options.tolerance != "pointer" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height'])
454 return isOverElement;
457 return (l < x1 + (this.helperProportions.width / 2) // Right Half
458 && x2 - (this.helperProportions.width / 2) < r // Left Half
459 && t < y1 + (this.helperProportions.height / 2) // Bottom Half
460 && y2 - (this.helperProportions.height / 2) < b ); // Top Half
465 _intersectsWithPointer: function(item) {
467 var isOverElementHeight = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top, item.height),
468 isOverElementWidth = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left, item.width),
469 isOverElement = isOverElementHeight && isOverElementWidth,
470 verticalDirection = this._getDragVerticalDirection(),
471 horizontalDirection = this._getDragHorizontalDirection();
476 return this.floating ?
477 ( ((horizontalDirection && horizontalDirection == "right") || verticalDirection == "down") ? 2 : 1 )
478 : ( verticalDirection && (verticalDirection == "down" ? 2 : 1) );
482 _intersectsWithSides: function(item) {
484 var isOverBottomHalf = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top + (item.height/2), item.height),
485 isOverRightHalf = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left + (item.width/2), item.width),
486 verticalDirection = this._getDragVerticalDirection(),
487 horizontalDirection = this._getDragHorizontalDirection();
489 if (this.floating && horizontalDirection) {
490 return ((horizontalDirection == "right" && isOverRightHalf) || (horizontalDirection == "left" && !isOverRightHalf));
492 return verticalDirection && ((verticalDirection == "down" && isOverBottomHalf) || (verticalDirection == "up" && !isOverBottomHalf));
497 _getDragVerticalDirection: function() {
498 var delta = this.positionAbs.top - this.lastPositionAbs.top;
499 return delta != 0 && (delta > 0 ? "down" : "up");
502 _getDragHorizontalDirection: function() {
503 var delta = this.positionAbs.left - this.lastPositionAbs.left;
504 return delta != 0 && (delta > 0 ? "right" : "left");
507 refresh: function(event) {
508 this._refreshItems(event);
509 this.refreshPositions();
513 _connectWith: function() {
514 var options = this.options;
515 return options.connectWith.constructor == String
516 ? [options.connectWith]
517 : options.connectWith;
520 _getItemsAsjQuery: function(connected) {
525 var connectWith = this._connectWith();
527 if(connectWith && connected) {
528 for (var i = connectWith.length - 1; i >= 0; i--){
529 var cur = $(connectWith[i]);
530 for (var j = cur.length - 1; j >= 0; j--){
531 var inst = $.data(cur[j], 'sortable');
532 if(inst && inst != this && !inst.options.disabled) {
533 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]);
539 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]);
541 for (var i = queries.length - 1; i >= 0; i--){
542 queries[i][0].each(function() {
551 _removeCurrentsFromItems: function() {
553 var list = this.currentItem.find(":data(sortable-item)");
555 for (var i=0; i < this.items.length; i++) {
557 for (var j=0; j < list.length; j++) {
558 if(list[j] == this.items[i].item[0])
559 this.items.splice(i,1);
566 _refreshItems: function(event) {
569 this.containers = [this];
570 var items = this.items;
572 var queries = [[$.isFunction(this.options.items) ? this.options.items.call(this.element[0], event, { item: this.currentItem }) : $(this.options.items, this.element), this]];
573 var connectWith = this._connectWith();
576 for (var i = connectWith.length - 1; i >= 0; i--){
577 var cur = $(connectWith[i]);
578 for (var j = cur.length - 1; j >= 0; j--){
579 var inst = $.data(cur[j], 'sortable');
580 if(inst && inst != this && !inst.options.disabled) {
581 queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element[0], event, { item: this.currentItem }) : $(inst.options.items, inst.element), inst]);
582 this.containers.push(inst);
588 for (var i = queries.length - 1; i >= 0; i--) {
589 var targetData = queries[i][1];
590 var _queries = queries[i][0];
592 for (var j=0, queriesLength = _queries.length; j < queriesLength; j++) {
593 var item = $(_queries[j]);
595 item.data('sortable-item', targetData); // Data for target checking (mouse manager)
599 instance: targetData,
608 refreshPositions: function(fast) {
610 //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change
611 if(this.offsetParent && this.helper) {
612 this.offset.parent = this._getParentOffset();
615 for (var i = this.items.length - 1; i >= 0; i--){
616 var item = this.items[i];
618 var t = this.options.toleranceElement ? $(this.options.toleranceElement, item.item) : item.item;
621 item.width = t.outerWidth();
622 item.height = t.outerHeight();
630 if(this.options.custom && this.options.custom.refreshContainers) {
631 this.options.custom.refreshContainers.call(this);
633 for (var i = this.containers.length - 1; i >= 0; i--){
634 var p = this.containers[i].element.offset();
635 this.containers[i].containerCache.left = p.left;
636 this.containers[i].containerCache.top = p.top;
637 this.containers[i].containerCache.width = this.containers[i].element.outerWidth();
638 this.containers[i].containerCache.height = this.containers[i].element.outerHeight();
645 _createPlaceholder: function(that) {
647 var self = that || this, o = self.options;
649 if(!o.placeholder || o.placeholder.constructor == String) {
650 var className = o.placeholder;
652 element: function() {
654 var el = $(document.createElement(self.currentItem[0].nodeName))
655 .addClass(className || self.currentItem[0].className+" ui-sortable-placeholder")
656 .removeClass("ui-sortable-helper")[0];
659 el.style.visibility = "hidden";
663 update: function(container, p) {
665 // 1. If a className is set as 'placeholder option, we don't force sizes - the class is responsible for that
666 // 2. The option 'forcePlaceholderSize can be enabled to force it even if a class name is specified
667 if(className && !o.forcePlaceholderSize) return;
669 //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
670 if(!p.height()) { p.height(self.currentItem.innerHeight() - parseInt(self.currentItem.css('paddingTop')||0, 10) - parseInt(self.currentItem.css('paddingBottom')||0, 10)); };
671 if(!p.width()) { p.width(self.currentItem.innerWidth() - parseInt(self.currentItem.css('paddingLeft')||0, 10) - parseInt(self.currentItem.css('paddingRight')||0, 10)); };
676 //Create the placeholder
677 self.placeholder = $(o.placeholder.element.call(self.element, self.currentItem));
679 //Append it after the actual current item
680 self.currentItem.after(self.placeholder);
682 //Update the size of the placeholder (TODO: Logic to fuzzy, see line 316/317)
683 o.placeholder.update(self, self.placeholder);
687 _contactContainers: function(event) {
689 // get innermost container that intersects with item
690 var innermostContainer = null, innermostIndex = null;
693 for (var i = this.containers.length - 1; i >= 0; i--){
695 // never consider a container that's located within the item itself
696 if($.ui.contains(this.currentItem[0], this.containers[i].element[0]))
699 if(this._intersectsWith(this.containers[i].containerCache)) {
701 // if we've already found a container and it's more "inner" than this, then continue
702 if(innermostContainer && $.ui.contains(this.containers[i].element[0], innermostContainer.element[0]))
705 innermostContainer = this.containers[i];
709 // container doesn't intersect. trigger "out" event if necessary
710 if(this.containers[i].containerCache.over) {
711 this.containers[i]._trigger("out", event, this._uiHash(this));
712 this.containers[i].containerCache.over = 0;
718 // if no intersecting containers found, return
719 if(!innermostContainer) return;
721 // move the item into the container if it's not there already
722 if(this.containers.length === 1) {
723 this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
724 this.containers[innermostIndex].containerCache.over = 1;
725 } else if(this.currentContainer != this.containers[innermostIndex]) {
727 //When entering a new container, we will find the item with the least distance and append our item near it
728 var dist = 10000; var itemWithLeastDistance = null; var base = this.positionAbs[this.containers[innermostIndex].floating ? 'left' : 'top'];
729 for (var j = this.items.length - 1; j >= 0; j--) {
730 if(!$.ui.contains(this.containers[innermostIndex].element[0], this.items[j].item[0])) continue;
731 var cur = this.items[j][this.containers[innermostIndex].floating ? 'left' : 'top'];
732 if(Math.abs(cur - base) < dist) {
733 dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j];
737 if(!itemWithLeastDistance && !this.options.dropOnEmpty) //Check if dropOnEmpty is enabled
740 this.currentContainer = this.containers[innermostIndex];
741 itemWithLeastDistance ? this._rearrange(event, itemWithLeastDistance, null, true) : this._rearrange(event, null, this.containers[innermostIndex].element, true);
742 this._trigger("change", event, this._uiHash());
743 this.containers[innermostIndex]._trigger("change", event, this._uiHash(this));
745 //Update the placeholder
746 this.options.placeholder.update(this.currentContainer, this.placeholder);
748 this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
749 this.containers[innermostIndex].containerCache.over = 1;
755 _createHelper: function(event) {
757 var o = this.options;
758 var helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[0], [event, this.currentItem])) : (o.helper == 'clone' ? this.currentItem.clone() : this.currentItem);
760 if(!helper.parents('body').length) //Add the helper to the DOM if that didn't happen already
761 $(o.appendTo != 'parent' ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(helper[0]);
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") };
766 if(helper[0].style.width == '' || o.forceHelperSize) helper.width(this.currentItem.width());
767 if(helper[0].style.height == '' || o.forceHelperSize) helper.height(this.currentItem.height());
773 _adjustOffsetFromHelper: function(obj) {
774 if (typeof obj == 'string') {
775 obj = obj.split(' ');
777 if ($.isArray(obj)) {
778 obj = {left: +obj[0], top: +obj[1] || 0};
781 this.offset.click.left = obj.left + this.margins.left;
783 if ('right' in obj) {
784 this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
787 this.offset.click.top = obj.top + this.margins.top;
789 if ('bottom' in obj) {
790 this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
794 _getParentOffset: function() {
797 //Get the offsetParent and cache its position
798 this.offsetParent = this.helper.offsetParent();
799 var po = this.offsetParent.offset();
801 // This is a special case where we need to modify a offset calculated on start, since the following happened:
802 // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
803 // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
804 // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
805 if(this.cssPosition == 'absolute' && this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) {
806 po.left += this.scrollParent.scrollLeft();
807 po.top += this.scrollParent.scrollTop();
810 if((this.offsetParent[0] == document.body) //This needs to be actually done for all browsers, since pageX/pageY includes this information
811 || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.browser.msie)) //Ugly IE fix
812 po = { top: 0, left: 0 };
815 top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
816 left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
821 _getRelativeOffset: function() {
823 if(this.cssPosition == "relative") {
824 var p = this.currentItem.position();
826 top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(),
827 left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft()
830 return { top: 0, left: 0 };
835 _cacheMargins: function() {
837 left: (parseInt(this.currentItem.css("marginLeft"),10) || 0),
838 top: (parseInt(this.currentItem.css("marginTop"),10) || 0)
842 _cacheHelperProportions: function() {
843 this.helperProportions = {
844 width: this.helper.outerWidth(),
845 height: this.helper.outerHeight()
849 _setContainment: function() {
851 var o = this.options;
852 if(o.containment == 'parent') o.containment = this.helper[0].parentNode;
853 if(o.containment == 'document' || o.containment == 'window') this.containment = [
854 0 - this.offset.relative.left - this.offset.parent.left,
855 0 - this.offset.relative.top - this.offset.parent.top,
856 $(o.containment == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left,
857 ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
860 if(!(/^(document|window|parent)$/).test(o.containment)) {
861 var ce = $(o.containment)[0];
862 var co = $(o.containment).offset();
863 var over = ($(ce).css("overflow") != 'hidden');
866 co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0) - this.margins.left,
867 co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0) - this.margins.top,
868 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,
869 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 _convertPositionTo: function(d, pos) {
877 if(!pos) pos = this.position;
878 var mod = d == "absolute" ? 1 : -1;
879 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);
883 pos.top // The absolute mouse position
884 + this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent
885 + this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border)
886 - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod)
889 pos.left // The absolute mouse position
890 + this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent
891 + this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border)
892 - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod)
898 _generatePosition: function(event) {
900 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);
902 // This is another very weird special case that only happens for relative elements:
903 // 1. If the css position is relative
904 // 2. and the scroll parent is the document or similar to the offset parent
905 // we have to refresh the relative offset during the scroll so there are no jumps
906 if(this.cssPosition == 'relative' && !(this.scrollParent[0] != document && this.scrollParent[0] != this.offsetParent[0])) {
907 this.offset.relative = this._getRelativeOffset();
910 var pageX = event.pageX;
911 var pageY = event.pageY;
914 * - Position constraining -
915 * Constrain the position to a mix of grid, containment.
918 if(this.originalPosition) { //If we are not dragging yet, we won't check for options
920 if(this.containment) {
921 if(event.pageX - this.offset.click.left < this.containment[0]) pageX = this.containment[0] + this.offset.click.left;
922 if(event.pageY - this.offset.click.top < this.containment[1]) pageY = this.containment[1] + this.offset.click.top;
923 if(event.pageX - this.offset.click.left > this.containment[2]) pageX = this.containment[2] + this.offset.click.left;
924 if(event.pageY - this.offset.click.top > this.containment[3]) pageY = this.containment[3] + this.offset.click.top;
928 var top = this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1];
929 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;
931 var left = this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0];
932 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;
939 pageY // The absolute mouse position
940 - this.offset.click.top // Click offset (relative to the element)
941 - this.offset.relative.top // Only for relative positioned nodes: Relative offset from element to offset parent
942 - this.offset.parent.top // The offsetParent's offset without borders (offset + border)
943 + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ))
946 pageX // The absolute mouse position
947 - this.offset.click.left // Click offset (relative to the element)
948 - this.offset.relative.left // Only for relative positioned nodes: Relative offset from element to offset parent
949 - this.offset.parent.left // The offsetParent's offset without borders (offset + border)
950 + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ))
956 _rearrange: function(event, i, a, hardRefresh) {
958 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));
960 //Various things done here to improve the performance:
961 // 1. we create a setTimeout, that calls refreshPositions
962 // 2. on the instance, we have a counter variable, that get's higher after every append
963 // 3. on the local scope, we copy the counter variable, and check in the timeout, if it's still the same
964 // 4. this lets only the last addition to the timeout stack through
965 this.counter = this.counter ? ++this.counter : 1;
966 var self = this, counter = this.counter;
968 window.setTimeout(function() {
969 if(counter == self.counter) self.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove
974 _clear: function(event, noPropagation) {
976 this.reverting = false;
977 // We delay all events that have to be triggered to after the point where the placeholder has been removed and
978 // everything else normalized again
979 var delayedTriggers = [], self = this;
981 // We first have to update the dom position of the actual currentItem
982 // Note: don't do it if the current item is already removed (by a user), or it gets reappended (see #4088)
983 if(!this._noFinalSort && this.currentItem[0].parentNode) this.placeholder.before(this.currentItem);
984 this._noFinalSort = null;
986 if(this.helper[0] == this.currentItem[0]) {
987 for(var i in this._storedCSS) {
988 if(this._storedCSS[i] == 'auto' || this._storedCSS[i] == 'static') this._storedCSS[i] = '';
990 this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
992 this.currentItem.show();
995 if(this.fromOutside && !noPropagation) delayedTriggers.push(function(event) { this._trigger("receive", event, this._uiHash(this.fromOutside)); });
996 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
997 if(!$.ui.contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element
998 if(!noPropagation) delayedTriggers.push(function(event) { this._trigger("remove", event, this._uiHash()); });
999 for (var i = this.containers.length - 1; i >= 0; i--){
1000 if($.ui.contains(this.containers[i].element[0], this.currentItem[0]) && !noPropagation) {
1001 delayedTriggers.push((function(c) { return function(event) { c._trigger("receive", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
1002 delayedTriggers.push((function(c) { return function(event) { c._trigger("update", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
1007 //Post events to containers
1008 for (var i = this.containers.length - 1; i >= 0; i--){
1009 if(!noPropagation) delayedTriggers.push((function(c) { return function(event) { c._trigger("deactivate", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
1010 if(this.containers[i].containerCache.over) {
1011 delayedTriggers.push((function(c) { return function(event) { c._trigger("out", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
1012 this.containers[i].containerCache.over = 0;
1016 //Do what was originally in plugins
1017 if(this._storedCursor) $('body').css("cursor", this._storedCursor); //Reset cursor
1018 if(this._storedOpacity) this.helper.css("opacity", this._storedOpacity); //Reset opacity
1019 if(this._storedZIndex) this.helper.css("zIndex", this._storedZIndex == 'auto' ? '' : this._storedZIndex); //Reset z-index
1021 this.dragging = false;
1022 if(this.cancelHelperRemoval) {
1023 if(!noPropagation) {
1024 this._trigger("beforeStop", event, this._uiHash());
1025 for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events
1026 this._trigger("stop", event, this._uiHash());
1031 if(!noPropagation) this._trigger("beforeStop", event, this._uiHash());
1033 //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
1034 this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
1036 if(this.helper[0] != this.currentItem[0]) this.helper.remove(); this.helper = null;
1038 if(!noPropagation) {
1039 for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events
1040 this._trigger("stop", event, this._uiHash());
1043 this.fromOutside = false;
1048 _trigger: function() {
1049 if ($.Widget.prototype._trigger.apply(this, arguments) === false) {
1054 _uiHash: function(inst) {
1055 var self = inst || this;
1057 helper: self.helper,
1058 placeholder: self.placeholder || $([]),
1059 position: self.position,
1060 originalPosition: self.originalPosition,
1061 offset: self.positionAbs,
1062 item: self.currentItem,
1063 sender: inst ? inst.element : null
1069 $.extend($.ui.sortable, {