diff options
author | Paul Bakaus <paul.bakaus@googlemail.com> | 2008-11-25 09:57:41 +0000 |
---|---|---|
committer | Paul Bakaus <paul.bakaus@googlemail.com> | 2008-11-25 09:57:41 +0000 |
commit | 91232f6ed53abdc2e2d36929a5129f9a7d8069b9 (patch) | |
tree | a97e40aea0c6cc1c2be280afee7ee362e60d24ce /ui | |
parent | 2b92a02170babdf2d970ecabd2a326a0a7d64f5b (diff) | |
download | jquery-ui-91232f6ed53abdc2e2d36929a5129f9a7d8069b9.tar.gz jquery-ui-91232f6ed53abdc2e2d36929a5129f9a7d8069b9.zip |
sortable:
- merged back all position and scroll fixed from draggables
- renamed accurate to accurateIntersection and set it to true by default
- refactored intersection code (no change in logic)
- added forceHelperSize option (default: false), a forced helper size is also triggered when the generated helper doesn't have any styles set for width/height, so this option won't be needed in most cases
Diffstat (limited to 'ui')
-rw-r--r-- | ui/ui.sortable.js | 502 |
1 files changed, 252 insertions, 250 deletions
diff --git a/ui/ui.sortable.js b/ui/ui.sortable.js index d2b337837..1ecf0bc4a 100644 --- a/ui/ui.sortable.js +++ b/ui/ui.sortable.js @@ -87,59 +87,58 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { this.refreshPositions(); //Create and append the visible helper - this.helper = this.createHelper(event); + this.helper = this._createHelper(event); + + //Cache the helper size + this._cacheHelperProportions(); /* * - Position generation - * This block generates everything position related - it's the core of draggables. */ - this.margins = { //Cache the margins - left: (parseInt(this.currentItem.css("marginLeft"),10) || 0), - top: (parseInt(this.currentItem.css("marginTop"),10) || 0) - }; - - this.offset = this.currentItem.offset(); //The element's absolute position on the page - this.offset = { //Substract the margins from the element's absolute offset + //Cache the margins of the original element + this._cacheMargins(); + + //Store the helper's css position + this.cssPosition = this.helper.css("position"); + this.scrollParent = this.helper.scrollParent(); + + //The element's absolute position on the page minus margins + this.offset = this.currentItem.offset(); + this.offset = { top: this.offset.top - this.margins.top, left: this.offset.left - this.margins.left }; + + $.extend(this.offset, { + click: { //Where the click happened, relative to the element + left: event.pageX - this.offset.left, + top: event.pageY - this.offset.top + }, + parent: this._getParentOffset(), + relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper + }); - this.offset.click = { //Where the click happened, relative to the element - left: event.pageX - this.offset.left, - top: event.pageY - this.offset.top - }; - - this.offsetParent = this.helper.offsetParent(); //Get the offsetParent and cache its position - var po = this.offsetParent.offset(); - - this.offsetParentBorders = { - top: (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0), - left: (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0) - }; - - this.offset.parent = { //Store its position plus border - top: po.top + this.offsetParentBorders.top, - left: po.left + this.offsetParentBorders.left - }; + //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied + if(o.cursorAt) + this._adjustOffsetFromHelper(o.cursorAt); - this.originalPosition = this._generatePosition(event); //Generate the original position - this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] }; //Cache the former DOM position + //Generate the original position + this.originalPosition = this._generatePosition(event); - //If o.placeholder is used, create a new element at the given position with the class - this.helperProportions = { width: this.helper.outerWidth(), height: this.helper.outerHeight() };//Cache the helper size + //Cache the former DOM position + this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] }; + + //Set a containment if given in the options + if(o.containment) + this._setContainment(); - if(o.helper == "original") { - this._storedCSS = { position: this.currentItem.css("position"), top: this.currentItem.css("top"), left: this.currentItem.css("left"), clear: this.currentItem.css("clear") }; - } else { - this.currentItem.hide(); //Hide the original, won't cause anything bad this way + //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 + if(this.helper[0] != this.currentItem[0]) { + this.currentItem.hide(); } - //Position it absolutely and add a helper class - this.helper - .css({ position: 'absolute', clear: 'both' }) - .addClass('ui-sortable-helper'); - //Create the placeholder this._createPlaceholder(); @@ -148,42 +147,8 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { //Recache the helper size if(!this._preserveHelperProportions) - this.helperProportions = { width: this.helper.outerWidth(), height: this.helper.outerHeight() }; + this._cacheHelperProportions(); - if(o.cursorAt) { - if(o.cursorAt.left != undefined) this.offset.click.left = o.cursorAt.left; - if(o.cursorAt.right != undefined) this.offset.click.left = this.helperProportions.width - o.cursorAt.right; - if(o.cursorAt.top != undefined) this.offset.click.top = o.cursorAt.top; - if(o.cursorAt.bottom != undefined) this.offset.click.top = this.helperProportions.height - o.cursorAt.bottom; - } - - /* - * - Position constraining - - * Here we prepare position constraining like grid and containment. - */ - - if(o.containment) { - if(o.containment == 'parent') o.containment = this.helper[0].parentNode; - if(o.containment == 'document' || o.containment == 'window') this.containment = [ - 0 - this.offset.parent.left, - 0 - this.offset.parent.top, - $(o.containment == 'document' ? document : window).width() - this.offset.parent.left - this.helperProportions.width - this.margins.left - (parseInt(this.element.css("marginRight"),10) || 0), - ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.offset.parent.top - this.helperProportions.height - this.margins.top - (parseInt(this.element.css("marginBottom"),10) || 0) - ]; - - if(!(/^(document|window|parent)$/).test(o.containment)) { - var ce = $(o.containment)[0]; - var co = $(o.containment).offset(); - var over = ($(ce).css("overflow") != 'hidden'); - - this.containment = [ - co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.parent.left, - co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.parent.top, - co.left+(over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.parent.left - this.helperProportions.width - this.margins.left - (parseInt(this.currentItem.css("marginRight"),10) || 0), - co.top+(over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.parent.top - this.helperProportions.height - this.margins.top - (parseInt(this.currentItem.css("marginBottom"),10) || 0) - ]; - } - } //Post 'activate' events to possible containers if(!noActivation) { @@ -199,12 +164,14 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { this.dragging = true; + this.helper.addClass('ui-sortable-helper'); this._mouseDrag(event); //Execute the drag once - this causes the helper not to be visible before getting its correct position return true; }, _mouseDrag: function(event) { + //Compute the helpers position this.position = this._generatePosition(event); this.positionAbs = this._convertPositionTo("absolute"); @@ -219,32 +186,26 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { //Regenerate the absolute position used for position checks this.positionAbs = this._convertPositionTo("absolute"); - //Set the helper's position - if (this.helper) { - this.helper[0].style.left = this.position.left+'px'; - this.helper[0].style.top = this.position.top+'px'; - } + //Set the helper position + if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px'; + if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px'; + //Rearrange for (var i = this.items.length - 1; i >= 0; i--) { - var item = this.items[i], intersection = this._intersectsWithPointer(item); - + //Cache variables and intersection, continue if no intersection + var item = this.items[i], itemElement = item.item[0], intersection = this._intersectsWithPointer(item); if (!intersection) continue; - var itemChecked = this.items[i].item[0]; - - if(itemChecked != this.currentItem[0] //cannot intersect with itself - && this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemChecked //no useless actions that have been done before - && !$.ui.contains(this.placeholder[0], itemChecked) //no action if the item moved is the parent of the item checked - && (this.options.type == 'semi-dynamic' ? !$.ui.contains(this.element[0], itemChecked) : true) + if(itemElement != this.currentItem[0] //cannot intersect with itself + && this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemElement //no useless actions that have been done before + && !$.ui.contains(this.placeholder[0], itemElement) //no action if the item moved is the parent of the item checked + && (this.options.type == 'semi-dynamic' ? !$.ui.contains(this.element[0], itemElement) : true) ) { this.direction = intersection == 1 ? "down" : "up"; - if (this.options.tolerance == "pointer") { - this.options.sortIndicator.call(this, event, item); - } - else if (this.options.tolerance == "guess" && this._intersectsGuess(item)) { + if (this.options.tolerance == "pointer" || (this.options.tolerance == "guess" && this._intersectsGuess(item))) { this.options.sortIndicator.call(this, event, item); } @@ -260,10 +221,9 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { if($.ui.ddmanager) $.ui.ddmanager.drag(this, event); //Call callbacks - this.element.triggerHandler("sort", [event, this._ui()], this.options["sort"]); + this._trigger('sort', event, this._ui()); this.lastPositionAbs = this.positionAbs; - return false; }, @@ -365,15 +325,26 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { /* Be careful with the following core functions */ _intersectsWith: function(item) { - var x1 = this.positionAbs.left, x2 = x1 + this.helperProportions.width, - y1 = this.positionAbs.top, y2 = y1 + this.helperProportions.height; - var l = item.left, r = l + item.width, - t = item.top, b = t + item.height; - - var dyClick = this.offset.click.top, dxClick = this.offset.click.left; + + var x1 = this.positionAbs.left, + x2 = x1 + this.helperProportions.width, + y1 = this.positionAbs.top, + y2 = y1 + this.helperProportions.height; + + var l = item.left, + r = l + item.width, + t = item.top, + b = t + item.height; + + var dyClick = this.offset.click.top, + dxClick = this.offset.click.left; + var isOverElement = (y1 + dyClick) > t && (y1 + dyClick) < b && (x1 + dxClick) > l && (x1 + dxClick) < r; - if(this.options.tolerance == "pointer" || this.options.forcePointerForContainers || (this.options.tolerance == "guess" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height'])) { + if( this.options.tolerance == "pointer" + || this.options.forcePointerForContainers + || (this.options.tolerance == "guess" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height']) + ) { return isOverElement; } else { @@ -386,110 +357,45 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { }, _intersectsWithPointer: function(item) { - var dyClick = this.offset.click.top, dxClick = this.offset.click.left; - var helperTop = this.positionAbs.top, helperLeft = this.positionAbs.left; - var itemHeight = item.height, itemWidth = item.width; - var itemTop = item.top, itemLeft = item.left; - - if (this.cancelIntersection) { - return false; - } - - var isOverElementHeight = $.ui.isOverAxis(helperTop + dyClick, itemTop, itemHeight); - var isOverElementWidth = $.ui.isOverAxis(helperLeft + dxClick, itemLeft, itemWidth); - var isOverElement = isOverElementHeight && isOverElementWidth; - var verticalDirection = this._getDragVerticalDirection(); - var horizontalDirection = this._getDragHorizontalDirection(); - - if (!isOverElement) return false; - - if (this.floating) { - if (!horizontalDirection) { - return verticalDirection == "down" ? 2 : 1; - } - - return horizontalDirection == "right" ? 2 : 1; - } - else { + var isOverElementHeight = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top, item.height), + isOverElementWidth = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left, item.width), + isOverElement = isOverElementHeight && isOverElementWidth, + verticalDirection = this._getDragVerticalDirection(), + horizontalDirection = this._getDragHorizontalDirection(); - if (!verticalDirection) { - return false; - } + if (!isOverElement) + return false; - return verticalDirection == "down" ? 2 : 1; - } + return this.floating ? + ( ((horizontalDirection && horizontalDirection == "right") || verticalDirection == "down") ? 2 : 1 ) + : ( verticalDirection && (verticalDirection == "down" ? 2 : 1) ); - return false; }, _intersectsGuess: function(item) { - var dyClick = this.offset.click.top, dxClick = this.offset.click.left; - var helperTop = this.positionAbs.top, helperLeft = this.positionAbs.left; - var itemHeight = item.height, itemWidth = item.width; - var itemTop = item.top, itemLeft = item.left; - - var isOverBottomHalf = $.ui.isOverAxis(helperTop + dyClick, itemTop + (itemHeight/2), itemHeight); - var isOverTopHalf = !isOverBottomHalf; - var isOverRightHalf = $.ui.isOverAxis(helperLeft + dxClick, itemLeft + (itemWidth/2), itemWidth); - var isOverLeftHalf = !isOverRightHalf; - var verticalDirection = this._getDragVerticalDirection(); - var horizontalDirection = this._getDragHorizontalDirection(); - - var verticalIntersection = function() { - if (!verticalDirection) { - return false; - } - if (verticalDirection == "down" && isOverBottomHalf || - verticalDirection == "up" && isOverTopHalf) { - return true; - } - }; - - var horizontalIntersection = function() { - if (!horizontalDirection) { - return verticalIntersection(); - } - if (horizontalDirection == "left" && isOverLeftHalf || - horizontalDirection == "right" && isOverRightHalf) { - return true; - } - }; - - if (this.floating) { - return horizontalIntersection(); - } - else { - return verticalIntersection(); + + var isOverBottomHalf = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top + (item.height/2), item.height), + isOverRightHalf = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left + (item.width/2), item.width), + verticalDirection = this._getDragVerticalDirection(), + horizontalDirection = this._getDragHorizontalDirection(); + + if (this.floating && horizontalDirection) { + return ((horizontalDirection == "right" && isOverRightHalf) || (horizontalDirection == "left" && !isOverRightHalf)); + } else { + return verticalDirection && ((verticalDirection == "down" && isOverBottomHalf) || (verticalDirection == "up" && !isOverBottomHalf)); } - return false; }, _getDragVerticalDirection: function() { - var helperTop = this.positionAbs.top; - var lastTop = this.lastPositionAbs.top; - var delta = helperTop - lastTop; - - if (delta == 0) { - return false; - } - - var direction = delta > 0 ? "down" : "up"; - return direction; + var delta = this.positionAbs.top - this.lastPositionAbs.top; + return delta != 0 && (delta > 0 ? "down" : "up"); }, _getDragHorizontalDirection: function() { - var helperLeft = this.positionAbs.left; - var lastLeft = this.lastPositionAbs.left; - var delta = helperLeft - lastLeft; - - if (delta == 0) { - return false; - } - - var direction = delta > 0 ? "right" : "left"; - return direction; + var delta = this.positionAbs.left - this.lastPositionAbs.left; + return delta != 0 && (delta > 0 ? "right" : "left"); }, refresh: function(event) { @@ -586,9 +492,8 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { refreshPositions: function(fast) { //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change - if(this.offsetParent) { - var po = this.offsetParent.offset(); - this.offset.parent = { top: po.top + this.offsetParentBorders.top, left: po.left + this.offsetParentBorders.left }; + if(this.offsetParent && this.helper) { + this.offset.parent = this._getParentOffset(); } for (var i = this.items.length - 1; i >= 0; i--){ @@ -601,7 +506,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { var t = this.options.toleranceElement ? $(this.options.toleranceElement, item.item) : item.item; if (!fast) { - if (this.options.accurate) { + if (this.options.accurateIntersection) { item.width = t.width(); item.height = t.height(); } @@ -717,52 +622,145 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { }; }, - createHelper: function(event) { + _createHelper: function(event) { var o = this.options; - var helper = typeof o.helper == 'function' ? $(o.helper.apply(this.element[0], [event, this.currentItem])) : (o.helper == "original" ? this.currentItem : this.currentItem.clone()); + var helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[0], [event, this.currentItem])) : (o.helper == 'clone' ? this.currentItem.clone() : this.currentItem); + + if(!helper.parents('body').length) //Add the helper to the DOM if that didn't happen already + $(o.appendTo != 'parent' ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(helper[0]); - if (!helper.parents('body').length) - $(o.appendTo != 'parent' ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(helper[0]); //Add the helper to the DOM if that didn't happen already + if(helper[0] == this.currentItem[0]) + 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") }; + if(helper[0].style.width == '' || o.forceHelperSize) helper.width(this.currentItem.width()); + if(helper[0].style.height == '' || o.forceHelperSize) helper.height(this.currentItem.height()); + + //TODO: Still need to figure out a way to make relative sorting possible + helper.css("position", "absolute"); + return helper; }, + + _adjustOffsetFromHelper: function(obj) { + if(obj.left != undefined) this.offset.click.left = obj.left + this.margins.left; + if(obj.right != undefined) this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left; + if(obj.top != undefined) this.offset.click.top = obj.top + this.margins.top; + if(obj.bottom != undefined) this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top; + }, + + _getParentOffset: function() { + + //Get the offsetParent and cache its position + this.offsetParent = this.helper.offsetParent(); var po = this.offsetParent.offset(); + + if((this.offsetParent[0] == document.body && $.browser.mozilla) //Ugly FF3 fix + || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.browser.msie)) //Ugly IE fix + po = { top: 0, left: 0 }; + + return { + top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0), + left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0) + }; + + }, + + _getRelativeOffset: function() { + + if(this.cssPosition == "relative") { + var p = this.currentItem.position(); + return { + top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(), + left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft() + }; + } else { + return { top: 0, left: 0 }; + } + + }, + + _cacheMargins: function() { + this.margins = { + left: (parseInt(this.currentItem.css("marginLeft"),10) || 0), + top: (parseInt(this.currentItem.css("marginTop"),10) || 0) + }; + }, + + _cacheHelperProportions: function() { + this.helperProportions = { + width: this.helper.outerWidth(), + height: this.helper.outerHeight() + }; + }, + + _setContainment: function() { + + var o = this.options; + if(o.containment == 'parent') o.containment = this.helper[0].parentNode; + if(o.containment == 'document' || o.containment == 'window') this.containment = [ + 0 - this.offset.relative.left - this.offset.parent.left, + 0 - this.offset.relative.top - this.offset.parent.top, + $(o.containment == 'document' ? document : window).width() - this.offset.relative.left - this.offset.parent.left - this.helperProportions.width - this.margins.left - (parseInt(this.currentItem.css("marginRight"),10) || 0), + ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.offset.relative.top - this.offset.parent.top - this.helperProportions.height - this.margins.top - (parseInt(this.currentItem.css("marginBottom"),10) || 0) + ]; + + if(!(/^(document|window|parent)$/).test(o.containment)) { + var ce = $(o.containment)[0]; + var co = $(o.containment).offset(); + var over = ($(ce).css("overflow") != 'hidden'); + + this.containment = [ + co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.relative.left - this.offset.parent.left, + co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.relative.top - this.offset.parent.top, + co.left+(over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.relative.left - this.offset.parent.left - this.helperProportions.width - this.margins.left - (parseInt(this.currentItem.css("marginRight"),10) || 0), + co.top+(over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.relative.top - this.offset.parent.top - this.helperProportions.height - this.margins.top - (parseInt(this.currentItem.css("marginBottom"),10) || 0) + ]; + } + + }, _convertPositionTo: function(d, pos) { + if(!pos) pos = this.position; var mod = d == "absolute" ? 1 : -1; + return { top: ( pos.top // the calculated relative position + + this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent + this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border) - - (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop) * mod // The offsetParent's scroll position + + ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : this[(this.cssPosition == 'absolute' ? 'offset' : 'scroll')+'Parent'].scrollTop() ) * mod + this.margins.top * mod //Add the margin (you don't want the margin counting in intersection methods) ), left: ( pos.left // the calculated relative position + + this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent + this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border) - - (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft) * mod // The offsetParent's scroll position + + ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : this[(this.cssPosition == 'absolute' ? 'offset' : 'scroll')+'Parent'].scrollLeft() ) * mod + this.margins.left * mod //Add the margin (you don't want the margin counting in intersection methods) ) }; }, - + _generatePosition: function(event) { - var o = this.options; + var o = this.options, scroll = this[(this.cssPosition == 'absolute' ? 'offset' : 'scroll')+'Parent'], scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName); + var position = { top: ( - event.pageY // The absolute mouse position + event.pageY // The absolute mouse position - this.offset.click.top // Click offset (relative to the element) + - this.offset.relative.top // Only for relative positioned nodes: Relative offset from element to offset parent - this.offset.parent.top // The offsetParent's offset without borders (offset + border) - + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop) // The offsetParent's scroll position, not if the element is fixed + + ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) ), left: ( - event.pageX // The absolute mouse position + event.pageX // The absolute mouse position - this.offset.click.left // Click offset (relative to the element) + - this.offset.relative.left // Only for relative positioned nodes: Relative offset from element to offset parent - this.offset.parent.left // The offsetParent's offset without borders (offset + border) - + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft) // The offsetParent's scroll position, not if the element is fixed + + ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) ) }; @@ -816,10 +814,14 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { if(!this._noFinalSort) this.placeholder.before(this.currentItem); this._noFinalSort = null; - if(this.options.helper == "original") + if(this.helper[0] == this.currentItem[0]) { + for(var i in this._storedCSS) { + if(this._storedCSS[i] == 'auto' || this._storedCSS[i] == 'static') this._storedCSS[i] = ''; + } this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper"); - else + } else { this.currentItem.show(); + } if(this.domPosition.prev != this.currentItem.prev().not(".ui-sortable-helper")[0] || this.domPosition.parent != this.currentItem.parent()[0]) this._propagate("update", event, null, noPropagation); //Trigger update callback if the DOM position has changed if(!$.ui.contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element @@ -886,13 +888,14 @@ $.extend($.ui.sortable, { getter: "serialize toArray", version: "@VERSION", defaults: { - accurate: false, + accurateIntersection: true, appendTo: "parent", cancel: ":input", delay: 0, distance: 1, dropOnEmpty: true, forcePlaceholderSize: false, + forceHelperSize: false, helper: "original", items: '> *', scope: "default", @@ -909,14 +912,6 @@ $.extend($.ui.sortable, { * Sortable Extensions */ -$.ui.plugin.add("sortable", "axis", { - sort: function(event, ui) { - var i = $(this).data('sortable'), o = i.options; - if(o.axis == "y") i.position.left = i.originalPosition.left; - if(o.axis == "x") i.position.top = i.originalPosition.top; - } -}); - $.ui.plugin.add("sortable", "cursor", { start: function(event, ui) { var t = $('body'), i = $(this).data('sortable'); @@ -943,47 +938,54 @@ $.ui.plugin.add("sortable", "opacity", { $.ui.plugin.add("sortable", "scroll", { start: function(event, ui) { - var i = $(this).data('sortable'), o = i.options; - - i.overflowY = function(el) { - do { if(/auto|scroll/.test(el.css('overflow')) || (/auto|scroll/).test(el.css('overflow-y'))) return el; el = el.parent(); } while (el[0].parentNode); - return $(document); - }(i.currentItem); - i.overflowX = function(el) { - do { if(/auto|scroll/.test(el.css('overflow')) || (/auto|scroll/).test(el.css('overflow-x'))) return el; el = el.parent(); } while (el[0].parentNode); - return $(document); - }(i.currentItem); + var o = ui.options; + var i = $(this).data("sortable"); - if(i.overflowY[0] != document && i.overflowY[0].tagName != 'HTML') i.overflowYOffset = i.overflowY.offset(); - if(i.overflowX[0] != document && i.overflowX[0].tagName != 'HTML') i.overflowXOffset = i.overflowX.offset(); + if(i.scrollParent[0] != document && i.scrollParent[0].tagName != 'HTML') i.overflowOffset = i.scrollParent.offset(); }, - sort: function(event, ui) { - - var i = $(this).data('sortable'), o = i.options; - - if(i.overflowY[0] != document && i.overflowY[0].tagName != 'HTML') { - if((i.overflowYOffset.top + i.overflowY[0].offsetHeight) - event.pageY < o.scrollSensitivity) - i.overflowY[0].scrollTop = i.overflowY[0].scrollTop + o.scrollSpeed; - if(event.pageY - i.overflowYOffset.top < o.scrollSensitivity) - i.overflowY[0].scrollTop = i.overflowY[0].scrollTop - o.scrollSpeed; + drag: function(event, ui) { + + var o = ui.options, scrolled = false; + var i = $(this).data("sortable"); + + if(i.scrollParent[0] != document && i.scrollParent[0].tagName != 'HTML') { + + if((i.overflowOffset.top + i.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity) + i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop + o.scrollSpeed; + else if(event.pageY - i.overflowOffset.top < o.scrollSensitivity) + i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop - o.scrollSpeed; + + if((i.overflowOffset.left + i.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity) + i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft + o.scrollSpeed; + else if(event.pageX - i.overflowOffset.left < o.scrollSensitivity) + i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft - o.scrollSpeed; + } else { + if(event.pageY - $(document).scrollTop() < o.scrollSensitivity) - $(document).scrollTop($(document).scrollTop() - o.scrollSpeed); - if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) - $(document).scrollTop($(document).scrollTop() + o.scrollSpeed); - } + scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed); + else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) + scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed); - if(i.overflowX[0] != document && i.overflowX[0].tagName != 'HTML') { - if((i.overflowXOffset.left + i.overflowX[0].offsetWidth) - event.pageX < o.scrollSensitivity) - i.overflowX[0].scrollLeft = i.overflowX[0].scrollLeft + o.scrollSpeed; - if(event.pageX - i.overflowXOffset.left < o.scrollSensitivity) - i.overflowX[0].scrollLeft = i.overflowX[0].scrollLeft - o.scrollSpeed; - } else { if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity) - $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed); - if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) - $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed); + scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed); + else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) + scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed); + + } + + if(scrolled !== false && $.ui.ddmanager) + $.ui.ddmanager.prepareOffsets(i, event); + + + + //This is a special case where we need to modify a offset calculated on start, since the following happened: + // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent + // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that + // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag + if(scrolled !== false && i.cssPosition == 'absolute' && i.scrollParent[0] != document && $.ui.contains(i.scrollParent[0], i.offsetParent[0])) { + i.offset.parent = i._getParentOffset(); } } @@ -997,7 +999,7 @@ $.ui.plugin.add("sortable", "zIndex", { }, beforeStop: function(event, ui) { var i = $(this).data('sortable'); - if(i.options._zIndex) $(ui.helper).css('zIndex', i.options._zIndex); + if(i.options._zIndex) $(ui.helper).css('zIndex', i.options._zIndex == 'auto' ? '' : i.options._zIndex); } }); |