Skip to content

Commit

Permalink
Android not dragging with delay fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DomenZajc authored Nov 9, 2018
1 parent 4ef871c commit 4514b28
Showing 1 changed file with 38 additions and 55 deletions.
93 changes: 38 additions & 55 deletions Sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@

moved,

forRepaintDummy,

/** @const */
R_SPACE = /\s+/g,
R_FLOAT = /left|right|inline/,
Expand Down Expand Up @@ -93,8 +91,6 @@
savedInputChecked = [],
touchDragOverListeners = [],

alwaysFalse = function () { return false; },

_autoScroll = _throttle(function (/**Event*/evt, /**Object*/options, /**HTMLElement*/rootEl) {
// Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=505521
if (rootEl && options.scroll) {
Expand Down Expand Up @@ -167,9 +163,7 @@
scrollOffsetX = vx ? vx * speed : 0;

if ('function' === typeof(scrollCustomFn)) {
if (scrollCustomFn.call(_this, scrollOffsetX, scrollOffsetY, evt, touchEvt, el) !== 'continue') {
return;
}
return scrollCustomFn.call(_this, scrollOffsetX, scrollOffsetY, evt);
}

if (el === win) {
Expand All @@ -186,11 +180,8 @@

_prepareGroup = function (options) {
function toFn(value, pull) {
if (value == null || value === true) {
if (value === void 0 || value === true) {
value = group.name;
if (value == null) {
return alwaysFalse;
}
}

if (typeof value === 'function') {
Expand Down Expand Up @@ -259,12 +250,12 @@

// Default options
var defaults = {
group: null,
group: Math.random(),
sort: true,
disabled: false,
store: null,
handle: null,
scroll: true,
scroll: true,
scrollSensitivity: 30,
scrollSpeed: 10,
draggable: /[uo]l/i.test(el.nodeName) ? 'li' : '>*',
Expand All @@ -282,7 +273,6 @@
dragoverBubble: false,
dataIdAttr: 'data-id',
delay: 0,
touchStartThreshold: parseInt(window.devicePixelRatio, 10) || 1,
forceFallback: false,
fallbackClass: 'sortable-fallback',
fallbackOnBody: false,
Expand Down Expand Up @@ -434,15 +424,15 @@

// Make the element draggable
dragEl.draggable = _this.nativeDraggable;


// Chosen item
_toggleClass(dragEl, options.chosenClass, true);

// Bind the events: dragstart/dragend
_this._triggerDragStart(evt, touch);

// Drag start event
_dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, rootEl, oldIndex);

// Chosen item
_toggleClass(dragEl, options.chosenClass, true);
};

// Disable "draggable"
Expand All @@ -464,10 +454,10 @@
_on(ownerDocument, 'touchend', _this._disableDelayedDrag);
_on(ownerDocument, 'touchcancel', _this._disableDelayedDrag);
_on(ownerDocument, 'mousemove', _this._disableDelayedDrag);
_on(ownerDocument, 'touchmove', _this._delayedDragTouchMoveHandler);
options.supportPointer && _on(ownerDocument, 'pointermove', _this._delayedDragTouchMoveHandler);
_on(ownerDocument, 'touchmove', _this._disableDelayedDrag);
options.supportPointer && _on(ownerDocument, 'pointermove', _this._disableDelayedDrag);

_this._dragStartTimer = setTimeout(dragStartFn.bind(_this), options.delay);
_this._dragStartTimer = setTimeout(dragStartFn, options.delay);
} else {
dragStartFn();
}
Expand All @@ -476,16 +466,13 @@
}
},

_delayedDragTouchMoveHandler: function (/** TouchEvent|PointerEvent **/e) {
if (min(abs(e.clientX - this._lastX), abs(e.clientY - this._lastY)) >= this.options.touchStartThreshold) {
this._disableDelayedDrag();
}
},

_disableDelayedDrag: function () {
const userAgent = navigator.userAgent || navigator.vendor;
var ownerDocument = this.el.ownerDocument;

clearTimeout(this._dragStartTimer);
if (!/android/i.test(userAgent)) {
clearTimeout(this._dragStartTimer);
}
_off(ownerDocument, 'mouseup', this._disableDelayedDrag);
_off(ownerDocument, 'touchend', this._disableDelayedDrag);
_off(ownerDocument, 'touchcancel', this._disableDelayedDrag);
Expand All @@ -495,7 +482,9 @@
},

_triggerDragStart: function (/** Event */evt, /** Touch */touch) {
touch = touch || (evt.pointerType == 'touch' ? evt : null);
var touchType = evt && (evt.pointerType == 'touch' || evt.type == 'pointerDown' || evt.type == 'pointerdown')
touch = touch || (touchType ? evt : null);
//touch = touch || (evt.pointerType == 'touch' ? evt : null);

if (touch) {
// Touch device support
Expand Down Expand Up @@ -562,7 +551,7 @@
var parent = target;
var i = touchDragOverListeners.length;

while (target && target.shadowRoot) {
if (target && target.shadowRoot) {
target = target.shadowRoot.elementFromPoint(touchEvt.clientX, touchEvt.clientY);
parent = target;
}
Expand Down Expand Up @@ -899,7 +888,7 @@
+ (prevRect.top - currentRect.top) + 'px,0)'
);

forRepaintDummy = target.offsetWidth; // repaint
target.offsetWidth; // repaint

_css(target, 'transition', 'all ' + ms + 'ms');
_css(target, 'transform', 'translate3d(0,0,0)');
Expand Down Expand Up @@ -974,21 +963,21 @@
_toggleClass(dragEl, this.options.chosenClass, false);

// Drag stop event
_dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex, null, evt);
_dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex);

if (rootEl !== parentEl) {
newIndex = _index(dragEl, options.draggable);

if (newIndex >= 0) {
// Add event
_dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
_dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex);

// Remove event
_dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
_dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex);

// drag from one list and drop into another
_dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
_dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
_dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex);
}
}
else {
Expand All @@ -998,8 +987,8 @@

if (newIndex >= 0) {
// drag & drop within the same list
_dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
_dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
_dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex);
}
}
}
Expand All @@ -1010,7 +999,7 @@
newIndex = oldIndex;
}

_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex);

// Save sorting
this.save();
Expand Down Expand Up @@ -1319,7 +1308,7 @@



function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex, originalEvt) {
function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex) {
sortable = (sortable || rootEl[expando]);

var evt = document.createEvent('Event'),
Expand All @@ -1336,8 +1325,6 @@
evt.oldIndex = startIndex;
evt.newIndex = newIndex;

evt.originalEvent = originalEvt;

rootEl.dispatchEvent(evt);

if (options[onName]) {
Expand All @@ -1363,8 +1350,6 @@
evt.relatedRect = targetRect || toEl.getBoundingClientRect();
evt.willInsertAfter = willInsertAfter;

evt.originalEvent = originalEvt;

fromEl.dispatchEvent(evt);

if (onMoveFn) {
Expand Down Expand Up @@ -1440,15 +1425,15 @@

function _matches(/**HTMLElement*/el, /**String*/selector) {
if (el) {
try {
if (el.matches) {
return el.matches(selector);
} else if (el.msMatchesSelector) {
return el.msMatchesSelector(selector);
}
} catch(_) {
return false;
}
selector = selector.split('.');

var tag = selector.shift().toUpperCase(),
re = new RegExp('\\s(' + selector.join('|') + ')(?=\\s)', 'g');

return (
(tag === '' || el.nodeName.toUpperCase() == tag) &&
(!selector.length || ((' ' + el.className + ' ').match(re) || []).length == selector.length)
);
}

return false;
Expand Down Expand Up @@ -1500,8 +1485,6 @@
}

function _saveInputCheckedState(root) {
savedInputChecked.length = 0;

var inputs = root.getElementsByTagName('input');
var idx = inputs.length;

Expand Down

0 comments on commit 4514b28

Please sign in to comment.