Created
May 4, 2012 09:08
-
-
Save hayeah/2593471 to your computer and use it in GitHub Desktop.
jquery ui patch to allow draggable element that is larger than container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| drag: function(event, ui) { | |
| var inst = $(this).data("draggable"), o = inst.options; | |
| var d = o.snapTolerance; | |
| var x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width, | |
| y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height; | |
| for (var i = inst.snapElements.length - 1; i >= 0; i--){ | |
| var l = inst.snapElements[i].left, r = l + inst.snapElements[i].width, | |
| 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) || // conditions that check if element is going to snap | |
| (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) || | |
| // Howard Yeh: patch that adds snappable condition for when draggable is larger than snap element (for inner snap mode only) | |
| (o.snapMode == "inner" && ((Math.abs(t-y1) <= d) || | |
| (Math.abs(b-y2) <= d) || | |
| (Math.abs(l-x1) <= d) || | |
| (Math.abs(r-x2) <= d))) | |
| )) { | |
| if(inst.snapElements[i].snapping) (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item }))); | |
| inst.snapElements[i].snapping = false; | |
| continue; | |
| } | |
| if(o.snapMode != 'inner') { | |
| var ts = Math.abs(t - y2) <= d; | |
| var bs = Math.abs(b - y1) <= d; | |
| var ls = Math.abs(l - x2) <= d; | |
| var rs = Math.abs(r - x1) <= d; | |
| if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top; | |
| if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top; | |
| if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left; | |
| if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left; | |
| } | |
| var first = (ts || bs || ls || rs); | |
| if(o.snapMode != 'outer') { | |
| var ts = Math.abs(t - y1) <= d; | |
| var bs = Math.abs(b - y2) <= d; | |
| var ls = Math.abs(l - x1) <= d; | |
| var rs = Math.abs(r - x2) <= d; | |
| if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top - inst.margins.top; | |
| if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top - inst.margins.top; | |
| if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left - inst.margins.left; | |
| if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left - inst.margins.left; | |
| } | |
| if(!inst.snapElements[i].snapping && (ts || bs || ls || rs || first)) | |
| (inst.options.snap.snap && inst.options.snap.snap.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item }))); | |
| inst.snapElements[i].snapping = (ts || bs || ls || rs || first); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment