Skip to content

Instantly share code, notes, and snippets.

@reikop
Last active July 6, 2021 07:16
Show Gist options
  • Save reikop/6eaae109726418b0d6e0fc00f17e5a62 to your computer and use it in GitHub Desktop.
Save reikop/6eaae109726418b0d6e0fc00f17e5a62 to your computer and use it in GitHub Desktop.
drag: function( event, ui, inst ) {
var ts, bs, ls, rs, cs, ms, l, r, t, b, i, first,
o = inst.options,
d = o.snapTolerance,
x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
for (i = inst.snapElements.length - 1; i >= 0; i--){
var cw = inst.snapElements[i].width * 0.5;
var ch = inst.snapElements[i].height * 0.5;
l = inst.snapElements[i].left - inst.margins.left;
r = l + inst.snapElements[i].width;
t = inst.snapElements[i].top - inst.margins.top;
b = t + inst.snapElements[i].height;
if ( x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d || !$.contains( inst.snapElements[ i ].item.ownerDocument, inst.snapElements[ i ].item ) ) {
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") {
ts = Math.abs(t - y2) <= d;
bs = Math.abs(b - y1) <= d;
ls = Math.abs(l - x2) <= d;
rs = Math.abs(r - x1) <= d;
if (ts) {
ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top;
}
if (bs) {
ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top;
}
if (ls) {
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left;
}
if (rs) {
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left;
}
}
first = (ts || bs || ls || rs);
if (o.snapMode !== "outer") {
ts = Math.abs(t - y1) <= d;
ms = Math.abs(t - y1 + (( b - t ) * 0.5) - (y2 - y1) * 0.5) <= d;
bs = Math.abs(b - y2) <= d;
ls = Math.abs(l - x1) <= d;
cs = Math.abs(l - x1 + (( r - l ) * 0.5) - (x2 - x1) * 0.5) <= d;
rs = Math.abs(r - x2) <= d;
if (ts) {
ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top;
}
if (ms) {
ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height * 0.5 - ch, left: 0 }).top;
}
if (bs) {
ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top;
}
if (ls) {
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left;
}
if (cs){
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width * 0.5 -cw }).left;
}
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, 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