Skip to content
Permalink
Browse files

draggable: implemented more advanced features for the snap option - y…

…ou can now pass in a object instead of a bolean into "snap", possible keys are the callbacks snap/release (which both receive ui.snapItem) and items (jQuery selector, defaults to :data(draggable))
  • Loading branch information
Paul Bakaus
Paul Bakaus committed Jul 17, 2008
1 parent db258ed commit 83f858aa286c6351c0a48004ca11f205b2833887
Showing with 15 additions and 3 deletions.
  1. +15 −3 ui/ui.draggable.js
@@ -379,7 +379,8 @@ $.ui.plugin.add("draggable", "snap", {

var inst = $(this).data("draggable");
inst.snapElements = [];
$(ui.options.snap === true ? ':data(draggable)' : ui.options.snap).each(function() {

$(ui.options.snap.constructor != String ? ( ui.options.snap.items || ':data(draggable)' ) : ui.options.snap).each(function() {
var $t = $(this); var $o = $t.offset();
if(this != inst.element[0]) inst.snapElements.push({
item: this,
@@ -390,7 +391,7 @@ $.ui.plugin.add("draggable", "snap", {

},
drag: function(e, ui) {

var inst = $(this).data("draggable");
var d = ui.options.snapTolerance || 20;
var x1 = ui.absolutePosition.left, x2 = x1 + inst.helperProportions.width,
@@ -402,7 +403,11 @@ $.ui.plugin.add("draggable", "snap", {
t = inst.snapElements[i].top, b = t + inst.snapElements[i].height;

//Yes, I know, this is insane ;)
if(!((l-d < x1 && x1 < r+d && t-d < y1 && y1 < b+d) || (l-d < x1 && x1 < r+d && t-d < y2 && y2 < b+d) || (l-d < x2 && x2 < r+d && t-d < y1 && y1 < b+d) || (l-d < x2 && x2 < r+d && t-d < y2 && y2 < b+d))) continue;
if(!((l-d < x1 && x1 < r+d && t-d < y1 && y1 < b+d) || (l-d < x1 && x1 < r+d && t-d < y2 && y2 < b+d) || (l-d < x2 && x2 < r+d && t-d < y1 && y1 < b+d) || (l-d < x2 && x2 < r+d && t-d < y2 && y2 < b+d))) {
if(inst.snapElements[i].snapping) (inst.options.snap.release && inst.options.snap.release.call(inst.element, null, $.extend(inst.uiHash(), { snapItem: inst.snapElements[i].item })));
inst.snapElements[i].snapping = false;
continue;
}

if(ui.options.snapMode != 'inner') {
var ts = Math.abs(t - y2) <= 20;
@@ -415,6 +420,8 @@ $.ui.plugin.add("draggable", "snap", {
if(rs) ui.position.left = inst.convertPositionTo("relative", { top: 0, left: r }).left;
}

var first = (ts || bs || ls || rs);

if(ui.options.snapMode != 'outer') {
var ts = Math.abs(t - y1) <= 20;
var bs = Math.abs(b - y2) <= 20;
@@ -426,7 +433,12 @@ $.ui.plugin.add("draggable", "snap", {
if(rs) ui.position.left = inst.convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left;
}

if(!inst.snapElements[i].snapping && (ts || bs || ls || rs || first))
(inst.options.snap.snap && inst.options.snap.snap.call(inst.element, null, $.extend(inst.uiHash(), { snapItem: inst.snapElements[i].item })));
inst.snapElements[i].snapping = (ts || bs || ls || rs || first);

};

}
});

0 comments on commit 83f858a

Please sign in to comment.
You can’t perform that action at this time.