Skip to content

Commit

Permalink
support for both touch and mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
dbushell committed Aug 26, 2014
1 parent 52a168b commit 71a12a8
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions jquery.nestable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
return !!supports;
})();

var eStart = hasTouch ? 'touchstart' : 'mousedown',
eMove = hasTouch ? 'touchmove' : 'mousemove',
eEnd = hasTouch ? 'touchend' : 'mouseup';
eCancel = hasTouch ? 'touchcancel' : 'mouseup';

var defaults = {
listNodeName : 'ol',
itemNodeName : 'li',
Expand Down Expand Up @@ -75,7 +70,7 @@
});

list.el.on('click', 'button', function(e) {
if (list.dragEl || (!hasTouch && e.button !== 0)) {
if (list.dragEl) {
return;
}
var target = $(e.currentTarget),
Expand All @@ -98,40 +93,46 @@
}
handle = handle.closest('.' + list.options.handleClass);
}
if (!handle.length || list.dragEl || (!hasTouch && e.button !== 0) || (hasTouch && e.touches.length !== 1)) {
if (!handle.length || list.dragEl) {
return;
}

list.isTouch = /^touch/.test(e.type);
if (list.isTouch && e.touches.length !== 1) {
return;
}

e.preventDefault();
list.dragStart(hasTouch ? e.touches[0] : e);
list.dragStart(e.touches ? e.touches[0] : e);
};

var onMoveEvent = function(e)
{
if (list.dragEl) {
e.preventDefault();
list.dragMove(hasTouch ? e.touches[0] : e);
list.dragMove(e.touches ? e.touches[0] : e);
}
};

var onEndEvent = function(e)
{
if (list.dragEl) {
e.preventDefault();
list.dragStop(hasTouch ? e.touches[0] : e);
list.dragStop(e.touches ? e.touches[0] : e);
}
};

if (hasTouch) {
list.el[0].addEventListener(eStart, onStartEvent, false);
window.addEventListener(eMove, onMoveEvent, false);
window.addEventListener(eEnd, onEndEvent, false);
window.addEventListener(eCancel, onEndEvent, false);
} else {
list.el.on(eStart, onStartEvent);
list.w.on(eMove, onMoveEvent);
list.w.on(eEnd, onEndEvent);
list.el[0].addEventListener('touchstart', onStartEvent, false);
window.addEventListener('touchmove', onMoveEvent, false);
window.addEventListener('touchend', onEndEvent, false);
window.addEventListener('touchcancel', onEndEvent, false);
}

list.el.on('mousedown', onStartEvent);
list.w.on('mousemove', onMoveEvent);
list.w.on('mouseup', onEndEvent);

},

serialize: function()
Expand Down Expand Up @@ -185,6 +186,7 @@
distAxX : 0,
distAxY : 0
};
this.isTouch = false;
this.moving = false;
this.dragEl = null;
this.dragRootEl = null;
Expand Down

0 comments on commit 71a12a8

Please sign in to comment.