Sortable - Tolerance accuracy sortable improved

This commit is contained in:
Eduardo Lundgren 2008-10-28 05:44:17 +00:00
parent 664f2ada64
commit 429a36fee9

View File

@ -110,46 +110,48 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
},
_intersectsWithEdge: 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 isOverElement = (y1 + dyClick) > t && (y1 + dyClick) < b && (x1 + dxClick) > l && (x1 + dxClick) < r;
var helperHeight = this.helperProportions.height, helperWidth = this.helperProportions.width;
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.options.tolerance == "pointer" || (this.options.tolerance == "guess" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height'])) {
if(!isOverElement) return false;
var isOverElementHeight = ((helperTop + dyClick) > itemTop) &&
((helperTop + dyClick) < (itemTop + itemHeight));
var isOverElementWidth = ((helperLeft + dxClick) > itemLeft) &&
((helperLeft + dxClick) < (itemLeft + itemWidth));
var isOverElement = isOverElementHeight && isOverElementWidth;
var verticalDirection = this._getDragVerticalDirection();
var horizontalDirection = this._getDragHorizontalDirection();
if (this.floating) {
if ((x1 + dxClick) > l && (x1 + dxClick) < l + item.width/2) return 2;
if ((x1 + dxClick) > l + item.width/2 && (x1 + dxClick) < r) return 1;
} else {
var height = item.height;
var direction = y1 - this.updateOriginalPosition.top < 0 ? 2 : 1; // 2 = up
if (direction == 1 && (y1 + dyClick) < t + height/2) { return 2; } // up
else if (direction == 2 && (y1 + dyClick) > t + height/2) { return 1; } // down
if (isOverElementWidth) {
return horizontalDirection == "right" ? 2 : 1;
}
} else {
if (!(l < x1 + (this.helperProportions.width / 2) // Right Half
&& x2 - (this.helperProportions.width / 2) < r // Left Half
&& t < y1 + (this.helperProportions.height / 2) // Bottom Half
&& y2 - (this.helperProportions.height / 2) < b )) return false; // Top Half
if(this.floating) {
if(x2 > l && x1 < l) return 2; //Crosses left edge
if(x1 < r && x2 > r) return 1; //Crosses right edge
} else {
if(y2 > t && y1 < t) return 1; //Crosses top edge
if(y1 < b && y2 > b) return 2; //Crosses bottom edge
}
else {
if (isOverElement) {
return verticalDirection == "down" ? 2 : 1;
}
}
return false;
},
_getDragVerticalDirection: function() {
var helperTop = this.positionAbs.top;
var lastTop = this.lastPositionAbs.top;
var direction = helperTop - lastTop > 0 ? "down" : "up";
return direction;
},
_getDragHorizontalDirection: function() {
var helperLeft = this.positionAbs.left;
var lastLeft = this.lastPositionAbs.left;
var direction = helperLeft - lastLeft > 0 ? "right" : "left";
return direction;
},
refresh: function() {
@ -224,15 +226,21 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
}
for (var i = queries.length - 1; i >= 0; i--) {
queries[i][0].each(function() {
$.data(this, 'sortable-item', queries[i][1]); // Data for target checking (mouse manager)
var targetData = queries[i][1];
var _queries = queries[i][0];
for (var j=0, queriesLength = _queries.length; j < queriesLength; j++) {
var item = $(_queries[j]);
item.data('sortable-item', targetData); // Data for target checking (mouse manager)
items.push({
item: $(this),
instance: queries[i][1],
item: item,
instance: targetData,
width: 0, height: 0,
left: 0, top: 0
});
});
};
};
},
@ -246,22 +254,22 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
}
for (var i = this.items.length - 1; i >= 0; i--){
var item = this.items[i];
//We ignore calculating positions of all connected containers when we're not over them
if(this.items[i].instance != this.currentContainer && this.currentContainer && this.items[i].item[0] != this.currentItem[0])
if(item.instance != this.currentContainer && this.currentContainer && item.item[0] != this.currentItem[0])
continue;
var t = this.options.toleranceElement ? $(this.options.toleranceElement, this.items[i].item) : this.items[i].item;
var t = this.options.toleranceElement ? $(this.options.toleranceElement, item.item) : item.item;
if (!fast) {
this.items[i].width = t[0].offsetWidth;
this.items[i].height = t[0].offsetHeight;
item.width = t[0].offsetWidth;
item.height = t[0].offsetHeight;
}
var p = t.offset();
this.items[i].left = p.left;
this.items[i].top = p.top;
item.left = p.left;
item.top = p.top;
};
if(this.options.custom && this.options.custom.refreshContainers) {
@ -609,6 +617,10 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
this.position = this._generatePosition(e);
this.positionAbs = this._convertPositionTo("absolute");
if (!this.lastPositionAbs) {
this.lastPositionAbs = this.positionAbs;
}
//Call the internal plugins
$.ui.plugin.call(this, "sort", [e, this.ui()]);
@ -621,7 +633,9 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
//Rearrange
for (var i = this.items.length - 1; i >= 0; i--) {
var intersection = this._intersectsWithEdge(this.items[i]);
if(!intersection) continue;
if(this.items[i].item[0] != this.currentItem[0] //cannot intersect with itself
@ -648,6 +662,8 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
//Call callbacks
this.element.triggerHandler("sort", [e, this.ui()], this.options["sort"]);
this.lastPositionAbs = this.positionAbs;
return false;
},