Skip to content

Commit e0ecadb

Browse files
committed
tolerance option accepts negative values
Items with child containers no longer use sameResultBox closes johnny#22
1 parent d3de90f commit e0ecadb

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

source/js/jquery-sortable.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
event.preventDefault()
9494
},
9595
// Template for the placeholder. Can be any valid jQuery input
96-
// e.g. a string, a DOM element
96+
// e.g. a string, a DOM element.
97+
// The placeholder must have the class "placeholder"
9798
placeholder: '<li class="placeholder"/>',
9899
// If true, the position of the placeholder is calculated on every mousemove.
99100
// If false, it is only calculated when the mouse is above a container.
@@ -115,11 +116,18 @@
115116

116117
return result
117118
},
118-
// Set tolerance while dragging. Positive values will decrease sensitivity.
119+
// Set tolerance while dragging. Positive values decrease sensitivity,
120+
// negative values increase it.
119121
tolerance: 0
120122
}, // end group defaults
121123
containerGroups = {},
122124
groupCounter = 0,
125+
emptyBox = {
126+
left: 0,
127+
top: 0,
128+
bottom: 0,
129+
right:0
130+
}
123131
eventNames = {
124132
start: "touchstart.sortable mousedown.sortable",
125133
end: "touchend.sortable touchcancel.sortable mouseup.sortable",
@@ -142,20 +150,22 @@
142150
return array.push.apply(array, rest);
143151
}
144152

145-
function setDimensions(array, dimensions, useOffset) {
153+
function setDimensions(array, dimensions, tolerance, useOffset) {
146154
var i = array.length,
147155
offsetMethod = useOffset ? "offset" : "position"
156+
tolerance = tolerance || 0
157+
148158
while(i--){
149159
var el = array[i].el ? array[i].el : $(array[i]),
150160
// use fitting method
151161
pos = el[offsetMethod]()
152162
pos.left += parseInt(el.css('margin-left'), 10)
153-
pos.right += parseInt(el.css('margin-right'),10)
163+
pos.top += parseInt(el.css('margin-top'),10)
154164
dimensions[i] = [
155-
pos.left,
156-
pos.left + el.outerWidth(),
157-
pos.top,
158-
pos.top + el.outerHeight()
165+
pos.left - tolerance,
166+
pos.left + el.outerWidth() + tolerance,
167+
pos.top - tolerance,
168+
pos.top + el.outerHeight() + tolerance
159169
]
160170
}
161171
}
@@ -330,7 +340,7 @@
330340
},
331341
getContainerDimensions: function () {
332342
if(!this.containerDimensions)
333-
setDimensions(this.containers, this.containerDimensions = [], !this.$getOffsetParent())
343+
setDimensions(this.containers, this.containerDimensions = [], this.options.tolerance, !this.$getOffsetParent())
334344
return this.containerDimensions
335345
},
336346
getContainer: function (element) {
@@ -453,7 +463,7 @@
453463
while(i--){
454464
var index = distances[i][0],
455465
distance = distances[i][1]
456-
if(!distance && this.options.nested && this.getContainerGroup(index)){
466+
if(!distance && this.hasChildGroup(index)){
457467
var found = this.getContainerGroup(index).searchValidTarget(pointer, lastPointer)
458468
if(found)
459469
return true
@@ -494,12 +504,14 @@
494504
} else
495505
sameResultBox.left += width / 2
496506
}
507+
if(this.hasChildGroup(index))
508+
sameResultBox = emptyBox
497509
this.rootGroup.movePlaceholder(this, item, method, sameResultBox)
498510
},
499511
getItemDimensions: function () {
500512
if(!this.itemDimensions){
501-
this.items = this.$getChildren(this.el, "item").filter(":not(.dragged)").get()
502-
setDimensions(this.items, this.itemDimensions = [])
513+
this.items = this.$getChildren(this.el, "item").filter(":not(.placeholder)").get()
514+
setDimensions(this.items, this.itemDimensions = [], this.options.tolerance)
503515
}
504516
return this.itemDimensions
505517
},
@@ -514,6 +526,9 @@
514526
offsetParent = el.offsetParent()
515527
return offsetParent
516528
},
529+
hasChildGroup: function (index) {
530+
return this.options.nested && this.getContainerGroup(index)
531+
},
517532
getContainerGroup: function (index) {
518533
var childGroup = $.data(this.items[index], "subContainer")
519534
if( childGroup === undefined){

0 commit comments

Comments
 (0)