2019-01-10 15:39:37 +03:00
|
|
|
define(['dom', 'events'], function (dom, events) {
|
|
|
|
'use strict';
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function getTouches(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return e.changedTouches || e.targetTouches || e.touches;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function TouchHelper(elem, options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
options = options || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
var touchTarget;
|
|
|
|
var touchStartX;
|
|
|
|
var touchStartY;
|
|
|
|
var lastDeltaX;
|
|
|
|
var lastDeltaY;
|
|
|
|
var thresholdYMet;
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var swipeXThreshold = options.swipeXThreshold || 50;
|
|
|
|
var swipeYThreshold = options.swipeYThreshold || 50;
|
|
|
|
var swipeXMaxY = 30;
|
|
|
|
|
|
|
|
var excludeTagNames = options.ignoreTagNames || [];
|
|
|
|
|
|
|
|
var touchStart = function (e) {
|
|
|
|
|
|
|
|
var touch = getTouches(e)[0];
|
|
|
|
touchTarget = null;
|
|
|
|
touchStartX = 0;
|
|
|
|
touchStartY = 0;
|
|
|
|
lastDeltaX = null;
|
|
|
|
lastDeltaY = null;
|
|
|
|
thresholdYMet = false;
|
|
|
|
|
|
|
|
if (touch) {
|
|
|
|
|
|
|
|
var currentTouchTarget = touch.target;
|
|
|
|
|
|
|
|
if (dom.parentWithTag(currentTouchTarget, excludeTagNames)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
touchTarget = currentTouchTarget;
|
|
|
|
touchStartX = touch.clientX;
|
|
|
|
touchStartY = touch.clientY;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var touchEnd = function (e) {
|
|
|
|
|
|
|
|
var isTouchMove = e.type === 'touchmove';
|
|
|
|
|
|
|
|
if (touchTarget) {
|
2018-10-23 01:05:09 +03:00
|
|
|
var touch = getTouches(e)[0];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var deltaX;
|
|
|
|
var deltaY;
|
|
|
|
|
|
|
|
var clientX;
|
|
|
|
var clientY;
|
|
|
|
|
|
|
|
if (touch) {
|
|
|
|
clientX = touch.clientX || 0;
|
|
|
|
clientY = touch.clientY || 0;
|
|
|
|
deltaX = clientX - (touchStartX || 0);
|
|
|
|
deltaY = clientY - (touchStartY || 0);
|
|
|
|
} else {
|
|
|
|
deltaX = 0;
|
|
|
|
deltaY = 0;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var currentDeltaX = lastDeltaX == null ? deltaX : (deltaX - lastDeltaX);
|
|
|
|
var currentDeltaY = lastDeltaY == null ? deltaY : (deltaY - lastDeltaY);
|
|
|
|
|
|
|
|
lastDeltaX = deltaX;
|
|
|
|
lastDeltaY = deltaY;
|
|
|
|
|
|
|
|
if (deltaX > swipeXThreshold && Math.abs(deltaY) < swipeXMaxY) {
|
|
|
|
events.trigger(self, 'swiperight', [touchTarget]);
|
2019-11-23 00:29:38 +09:00
|
|
|
} else if (deltaX < (0 - swipeXThreshold) && Math.abs(deltaY) < swipeXMaxY) {
|
2019-01-10 15:39:37 +03:00
|
|
|
events.trigger(self, 'swipeleft', [touchTarget]);
|
2019-11-23 00:29:38 +09:00
|
|
|
} else if ((deltaY < (0 - swipeYThreshold) || thresholdYMet) && Math.abs(deltaX) < swipeXMaxY) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
thresholdYMet = true;
|
|
|
|
|
|
|
|
events.trigger(self, 'swipeup', [touchTarget, {
|
2018-10-23 01:05:09 +03:00
|
|
|
deltaY: deltaY,
|
|
|
|
deltaX: deltaX,
|
|
|
|
clientX: clientX,
|
|
|
|
clientY: clientY,
|
|
|
|
currentDeltaX: currentDeltaX,
|
|
|
|
currentDeltaY: currentDeltaY
|
2019-01-10 15:39:37 +03:00
|
|
|
}]);
|
2019-11-23 00:29:38 +09:00
|
|
|
} else if ((deltaY > swipeYThreshold || thresholdYMet) && Math.abs(deltaX) < swipeXMaxY) {
|
2019-01-10 15:39:37 +03:00
|
|
|
thresholdYMet = true;
|
|
|
|
|
|
|
|
events.trigger(self, 'swipedown', [touchTarget, {
|
2018-10-23 01:05:09 +03:00
|
|
|
deltaY: deltaY,
|
|
|
|
deltaX: deltaX,
|
|
|
|
clientX: clientX,
|
|
|
|
clientY: clientY,
|
|
|
|
currentDeltaX: currentDeltaX,
|
|
|
|
currentDeltaY: currentDeltaY
|
2019-01-10 15:39:37 +03:00
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isTouchMove && options.preventDefaultOnMove) {
|
|
|
|
e.preventDefault();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isTouchMove) {
|
|
|
|
touchTarget = null;
|
|
|
|
touchStartX = 0;
|
|
|
|
touchStartY = 0;
|
|
|
|
lastDeltaX = null;
|
|
|
|
lastDeltaY = null;
|
|
|
|
thresholdYMet = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.touchStart = touchStart;
|
|
|
|
this.touchEnd = touchEnd;
|
|
|
|
|
|
|
|
dom.addEventListener(elem, 'touchstart', touchStart, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
if (options.triggerOnMove) {
|
|
|
|
dom.addEventListener(elem, 'touchmove', touchEnd, {
|
|
|
|
passive: !options.preventDefaultOnMove
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dom.addEventListener(elem, 'touchend', touchEnd, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
dom.addEventListener(elem, 'touchcancel', touchEnd, {
|
|
|
|
passive: true
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
TouchHelper.prototype.destroy = function () {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var elem = this.elem;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (elem) {
|
2019-01-10 15:39:37 +03:00
|
|
|
var touchStart = this.touchStart;
|
|
|
|
var touchEnd = this.touchEnd;
|
|
|
|
|
|
|
|
dom.removeEventListener(elem, 'touchstart', touchStart, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
dom.removeEventListener(elem, 'touchmove', touchEnd, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
dom.removeEventListener(elem, 'touchend', touchEnd, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
dom.removeEventListener(elem, 'touchcancel', touchEnd, {
|
|
|
|
passive: true
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
this.touchStart = null;
|
|
|
|
this.touchEnd = null;
|
|
|
|
|
|
|
|
this.elem = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
return TouchHelper;
|
2020-02-22 11:47:03 -05:00
|
|
|
});
|