mirror of
https://github.com/jquery/jquery-ui.git
synced 2025-01-07 20:34:24 +00:00
Sortable - Tolerance accuracy sortable improved
This commit is contained in:
parent
664f2ada64
commit
429a36fee9
@ -110,46 +110,48 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_intersectsWithEdge: function(item) {
|
_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 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'])) {
|
var isOverElementHeight = ((helperTop + dyClick) > itemTop) &&
|
||||||
if(!isOverElement) return false;
|
((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 (this.floating) {
|
||||||
if ((x1 + dxClick) > l && (x1 + dxClick) < l + item.width/2) return 2;
|
if (isOverElementWidth) {
|
||||||
if ((x1 + dxClick) > l + item.width/2 && (x1 + dxClick) < r) return 1;
|
return horizontalDirection == "right" ? 2 : 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
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
else {
|
||||||
if (!(l < x1 + (this.helperProportions.width / 2) // Right Half
|
if (isOverElement) {
|
||||||
&& x2 - (this.helperProportions.width / 2) < r // Left Half
|
return verticalDirection == "down" ? 2 : 1;
|
||||||
&& 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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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() {
|
refresh: function() {
|
||||||
@ -224,15 +226,21 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (var i = queries.length - 1; i >= 0; i--) {
|
for (var i = queries.length - 1; i >= 0; i--) {
|
||||||
queries[i][0].each(function() {
|
var targetData = queries[i][1];
|
||||||
$.data(this, 'sortable-item', queries[i][1]); // Data for target checking (mouse manager)
|
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({
|
items.push({
|
||||||
item: $(this),
|
item: item,
|
||||||
instance: queries[i][1],
|
instance: targetData,
|
||||||
width: 0, height: 0,
|
width: 0, height: 0,
|
||||||
left: 0, top: 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--){
|
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
|
//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;
|
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) {
|
if (!fast) {
|
||||||
this.items[i].width = t[0].offsetWidth;
|
item.width = t[0].offsetWidth;
|
||||||
this.items[i].height = t[0].offsetHeight;
|
item.height = t[0].offsetHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = t.offset();
|
var p = t.offset();
|
||||||
this.items[i].left = p.left;
|
item.left = p.left;
|
||||||
this.items[i].top = p.top;
|
item.top = p.top;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if(this.options.custom && this.options.custom.refreshContainers) {
|
if(this.options.custom && this.options.custom.refreshContainers) {
|
||||||
@ -609,6 +617,10 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
|
|||||||
this.position = this._generatePosition(e);
|
this.position = this._generatePosition(e);
|
||||||
this.positionAbs = this._convertPositionTo("absolute");
|
this.positionAbs = this._convertPositionTo("absolute");
|
||||||
|
|
||||||
|
if (!this.lastPositionAbs) {
|
||||||
|
this.lastPositionAbs = this.positionAbs;
|
||||||
|
}
|
||||||
|
|
||||||
//Call the internal plugins
|
//Call the internal plugins
|
||||||
$.ui.plugin.call(this, "sort", [e, this.ui()]);
|
$.ui.plugin.call(this, "sort", [e, this.ui()]);
|
||||||
|
|
||||||
@ -621,7 +633,9 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
|
|||||||
|
|
||||||
//Rearrange
|
//Rearrange
|
||||||
for (var i = this.items.length - 1; i >= 0; i--) {
|
for (var i = this.items.length - 1; i >= 0; i--) {
|
||||||
|
|
||||||
var intersection = this._intersectsWithEdge(this.items[i]);
|
var intersection = this._intersectsWithEdge(this.items[i]);
|
||||||
|
|
||||||
if(!intersection) continue;
|
if(!intersection) continue;
|
||||||
|
|
||||||
if(this.items[i].item[0] != this.currentItem[0] //cannot intersect with itself
|
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
|
//Call callbacks
|
||||||
this.element.triggerHandler("sort", [e, this.ui()], this.options["sort"]);
|
this.element.triggerHandler("sort", [e, this.ui()], this.options["sort"]);
|
||||||
|
|
||||||
|
this.lastPositionAbs = this.positionAbs;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user