Skip to content

Fixes #6 mouse events on touch laptop #7

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
59 changes: 34 additions & 25 deletions drag-arrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,21 @@

/**
* Javascript events for touch device/desktop
* @return {Object}
* A Touch-screen device will also likely be compatible with mouse events,
* so on Touch enabled devices we should also listen for mouse-related events.
* However we register Touch-related events only on touch-enabled screens.
* @type {Object}
*/
var dragEvents = (function () {
if (IS_TOUCH_DEVICE) {
return {
START: 'touchstart',
MOVE: 'touchmove',
END: 'touchend'
};
}
else {
return {
START: 'mousedown',
MOVE: 'mousemove',
END: 'mouseup'
};
}
}());
var dragEventsMouse = {
START: 'mousedown',
MOVE: 'mousemove',
END: 'mouseup'
};
var dragEventsTouch = {
START: 'touchstart',
MOVE: 'touchmove',
END: 'touchend'
};

$.fn.arrangeable = function(options) {
var dragging = false;
Expand Down Expand Up @@ -84,9 +81,11 @@
var $this = $(this);

if (dragSelector) {
$this.on(dragEvents.START + eventNamespace, dragSelector, dragStartHandler);
$this.on(dragEventsMouse.START + eventNamespace, dragSelector, dragStartHandler);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zzgab will it not bind 2 times for touch screen devices? Just curious! haven't tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vishalmindtickle You mean, for touch-only devices (smartpones/tablets)? Hmm, could be...
Any idea how I could check this?

if (IS_TOUCH_DEVICE) $this.on(dragEventsTouch.START + eventNamespace, dragSelector, dragStartHandler);
} else {
$this.on(dragEvents.START + eventNamespace, dragStartHandler);
$this.on(dragEventsMouse.START + eventNamespace, dragStartHandler);
if (IS_TOUCH_DEVICE) $this.on(dragEventsTouch .START + eventNamespace, dragStartHandler);
}

function dragStartHandler(e) {
Expand All @@ -102,8 +101,12 @@

// bind mouse-move/touchmove on document
// (as it is not compulsory that event will trigger on dragging element)
$(document).on(dragEvents.MOVE + eventNamespace, dragMoveHandler)
.on(dragEvents.END + eventNamespace, dragEndHandler);
$(document).on(dragEventsMouse.MOVE + eventNamespace, dragMoveHandler)
.on(dragEventsMouse.END + eventNamespace, dragEndHandler);
if (IS_TOUCH_DEVICE) {
$(document).on(dragEventsTouch.MOVE + eventNamespace, dragMoveHandler)
.on(dragEventsTouch.END + eventNamespace, dragEndHandler);
}

function dragMoveHandler(e) {
if (!touchDown) { return; }
Expand Down Expand Up @@ -171,14 +174,20 @@
var $this = $(this);

if (dragSelector) {
$this.off(dragEvents.START + eventNamespace, dragSelector);
$this.off(dragEventsMouse.START + eventNamespace, dragSelector);
if (IS_TOUCH_DEVICE) $this.off(dragEventsTouch.START + eventNamespace, dragSelector);
} else {
$this.off(dragEvents.START + eventNamespace);
$this.off(dragEventsMouse.START + eventNamespace);
if (IS_TOUCH_DEVICE) $this.off(dragEventsTouch.START + eventNamespace);
}
});

$(document).off(dragEvents.MOVE + eventNamespace)
.off(dragEvents.END + eventNamespace);
$(document).off(dragEventsMouse.MOVE + eventNamespace)
.off(dragEventsMouse.END + eventNamespace);
if (IS_TOUCH_DEVICE) {
$(document).off(dragEventsTouch.MOVE + eventNamespace)
.off(dragEventsTouch.END + eventNamespace);
}

// remove data
$elements.eq(0).data('drag-arrange-destroy', null);
Expand Down
2 changes: 1 addition & 1 deletion drag-arrange.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.