Skip to content

Commit

Permalink
Merge pull request foundation#11531 from ncoden/fix/close-on-tap-1142…
Browse files Browse the repository at this point in the history
…3-for-v6-5-0

fix: fix various bugs with "Tap" events (v6.5)
  • Loading branch information
ncoden authored Oct 11, 2018
2 parents 28d71e8 + ad06d3b commit 1f60302
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions js/foundation.util.touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@ var startPosX,
startPosY,
startTime,
elapsedTime,
isMoving = false;
startEvent,
isMoving = false,
didMoved = false;

function onTouchEnd() {
// alert(this);
function onTouchEnd(e) {
this.removeEventListener('touchmove', onTouchMove);
this.removeEventListener('touchend', onTouchEnd);

// If the touch did not move, consider it as a "tap"
if (!didMoved) {
var tapEvent = $.Event('tap', startEvent || e);
$(this).trigger(tapEvent);
}

startEvent = null;
isMoving = false;
didMoved = false;
}

function onTouchMove(e) {
if ($.spotSwipe.preventDefault) { e.preventDefault(); }

if(isMoving) {
var x = e.touches[0].pageX;
var y = e.touches[0].pageY;
var dx = startPosX - x;
var dy = startPosY - y;
var dir;
didMoved = true;
elapsedTime = new Date().getTime() - startTime;
if(Math.abs(dx) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) {
dir = dx > 0 ? 'left' : 'right';
Expand All @@ -37,17 +49,23 @@ function onTouchMove(e) {
// }
if(dir) {
e.preventDefault();
onTouchEnd.call(this);
$(this).trigger('swipe', dir).trigger(`swipe${dir}`);
onTouchEnd.apply(this, arguments);
$(this)
.trigger($.Event('swipe', e), dir)
.trigger($.Event(`swipe${dir}`, e));
}
}

}

function onTouchStart(e) {

if (e.touches.length == 1) {
startPosX = e.touches[0].pageX;
startPosY = e.touches[0].pageY;
startEvent = e;
isMoving = true;
didMoved = false;
startTime = new Date().getTime();
this.addEventListener('touchmove', onTouchMove, false);
this.addEventListener('touchend', onTouchEnd, false);
Expand Down Expand Up @@ -76,6 +94,7 @@ class SpotSwipe {
_init() {
var $ = this.$;
$.event.special.swipe = { setup: init };
$.event.special.tap = { setup: init };

$.each(['left', 'up', 'down', 'right'], function () {
$.event.special[`swipe${this}`] = { setup: function(){
Expand Down Expand Up @@ -139,7 +158,8 @@ Touch.setupTouchHandler = function($) {
};
};

Touch.init = function($) {
Touch.init = function ($) {

if(typeof($.spotSwipe) === 'undefined') {
Touch.setupSpotSwipe($);
Touch.setupTouchHandler($);
Expand Down

0 comments on commit 1f60302

Please # to comment.