Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag threshold #666

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion dragula.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function dragula (initialContainers, options) {
var _offsetY; // reference y
var _moveX; // reference move x
var _moveY; // reference move y
var _initialX; // initial x before drag
var _initialY; // initial y before drag
var _initialSibling; // reference sibling when grabbed
var _currentSibling; // reference sibling now
var _copy; // item used for copying
Expand All @@ -39,6 +41,7 @@ function dragula (initialContainers, options) {
if (o.direction === void 0) { o.direction = 'vertical'; }
if (o.ignoreInputTextSelection === void 0) { o.ignoreInputTextSelection = true; }
if (o.mirrorContainer === void 0) { o.mirrorContainer = doc.body; }
if (o.dragThreshold === void 0) { o.dragThreshold = 20;}

var drake = emitter({
containers: o.containers,
Expand Down Expand Up @@ -145,6 +148,9 @@ function dragula (initialContainers, options) {
end();
start(grabbed);

_initialX = e.clientX; //save coordinates before any drag
_initialY = e.clientY;

var offset = getOffset(_item);
_offsetX = getCoord('pageX', e) - offset.left;
_offsetY = getCoord('pageY', e) - offset.top;
Expand Down Expand Up @@ -362,11 +368,15 @@ function dragula (initialContainers, options) {
return;
}
e.preventDefault();

var clientX = getCoord('clientX', e) || 0;
var clientY = getCoord('clientY', e) || 0;
var x = clientX - _offsetX;
var y = clientY - _offsetY;

// calculates whether the drag has exceeded minimum threshold to be considered a drag event, exits if not
if(Math.abs(_initialX - clientX) < o.dragThreshold && Math.abs(_initialY - clientY) < o.dragThreshold){
return;
}

_mirror.style.left = x + 'px';
_mirror.style.top = y + 'px';
Expand Down