Skip to content

Instantly share code, notes, and snippets.

@maccman
Last active December 18, 2015 13:29
Show Gist options
  • Select an option

  • Save maccman/5790161 to your computer and use it in GitHub Desktop.

Select an option

Save maccman/5790161 to your computer and use it in GitHub Desktop.
$ = jQuery
$.support.touch or= ('ontouchstart' of window)
# Helper functions
parentIfText = (node) ->
if 'tagName' of node then node else node.parentNode
swipeDirection = (x1, x2, y1, y2) ->
xDelta = Math.abs(x1 - x2)
yDelta = Math.abs(y1 - y2)
if xDelta >= yDelta
if x1 - x2 > 0 then 'Left' else 'Right'
else
if y1 - y2 > 0 then 'Up' else 'Down'
# jQuery helper functions
eventTypes = [
'swipe'
'swipeLeft'
'swipeRight'
'swipeUp'
'swipeDown'
'tap'
'tapactive'
'tapblur'
]
for type in eventTypes
do (type) ->
$.fn[type] = (callback) ->
if typeof callback is 'function'
@on(type, callback)
else
@trigger(type, arguments...)
# Options and events
events =
start: 'touchstart'
move: 'touchmove'
end: 'touchend'
cancel: 'touchcancel'
unless $.support.touch
$.extend(events,
start: 'mousedown'
move: 'mousemove'
end: 'mouseup'
)
defaults =
activeDelay: 100
swipeOffset: 30
# jQuery touch plugin
$.fn.touch = (options = {}) ->
options = $.extend({}, defaults, options)
touch = {}
reset = ->
clearTimeout(touch.activeTimeout)
touch = {}
@on events.start, (e) ->
reset()
e = e.originalEvent
eventTouch = e.touches?[0]
now = Date.now()
delta = now - (touch.last or now)
touch.target = parentIfText(eventTouch?.target or e.target)
touch.$target = $(touch.target)
touch.x1 = eventTouch?.pageX or 0
touch.y1 = eventTouch?.pageY or 0
touch.last = now
touch.activeTimeout = setTimeout ->
touch.$target.tapactive()
, options.activeDelay
@on events.move, (e) ->
e = e.originalEvent
eventTouch = e.touches?[0]
touch.x2 = eventTouch?.pageX or 0
touch.y2 = eventTouch?.pageY or 0
clearTimeout(touch.activeTimeout)
touch.$target?.tapblur()
@on events.end, (e) ->
if touch.x2 > 0 or touch.y2 > 0
# Trigger swipe
if Math.abs(touch.x1 - touch.x2) > options.swipeOffset or
Math.abs(touch.y1 - touch.y2) > options.swipeOffset
direction = swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)
touch.$target.swipe()
touch.$target['swipe' + direction]()
touch.x1 = touch.x2 = touch.y1 = touch.y2 = touch.last = 0
else if 'last' of touch
$target = touch.$target
$target.tapblur()
$target.tap()
reset()
@on events.cancel, ->
touch.$target?.tapblur()
reset()
// Generated by CoffeeScript 1.6.2
(function() {
var $, defaults, eventTypes, events, parentIfText, swipeDirection, type, _base, _fn, _i, _len,
__slice = [].slice;
$ = jQuery;
(_base = $.support).touch || (_base.touch = 'ontouchstart' in window);
parentIfText = function(node) {
if ('tagName' in node) {
return node;
} else {
return node.parentNode;
}
};
swipeDirection = function(x1, x2, y1, y2) {
var xDelta, yDelta;
xDelta = Math.abs(x1 - x2);
yDelta = Math.abs(y1 - y2);
if (xDelta >= yDelta) {
if (x1 - x2 > 0) {
return 'Left';
} else {
return 'Right';
}
} else {
if (y1 - y2 > 0) {
return 'Up';
} else {
return 'Down';
}
}
};
eventTypes = ['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'tap', 'tapactive', 'tapblur'];
_fn = function(type) {
return $.fn[type] = function(callback) {
if (typeof callback === 'function') {
return this.on(type, callback);
} else {
return this.trigger.apply(this, [type].concat(__slice.call(arguments)));
}
};
};
for (_i = 0, _len = eventTypes.length; _i < _len; _i++) {
type = eventTypes[_i];
_fn(type);
}
events = {
start: 'touchstart',
move: 'touchmove',
end: 'touchend',
cancel: 'touchcancel'
};
if (!$.support.touch) {
$.extend(events, {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
});
}
defaults = {
activeDelay: 100,
swipeOffset: 30
};
$.fn.touch = function(options) {
var reset, touch;
if (options == null) {
options = {};
}
options = $.extend({}, defaults, options);
touch = {};
reset = function() {
clearTimeout(touch.activeTimeout);
return touch = {};
};
this.on(events.start, function(e) {
var delta, eventTouch, now, _ref;
reset();
e = e.originalEvent;
eventTouch = (_ref = e.touches) != null ? _ref[0] : void 0;
now = Date.now();
delta = now - (touch.last || now);
touch.target = parentIfText((eventTouch != null ? eventTouch.target : void 0) || e.target);
touch.$target = $(touch.target);
touch.x1 = (eventTouch != null ? eventTouch.pageX : void 0) || 0;
touch.y1 = (eventTouch != null ? eventTouch.pageY : void 0) || 0;
touch.last = now;
return touch.activeTimeout = setTimeout(function() {
return touch.$target.tapactive();
}, options.activeDelay);
});
this.on(events.move, function(e) {
var eventTouch, _ref, _ref1;
e = e.originalEvent;
eventTouch = (_ref = e.touches) != null ? _ref[0] : void 0;
touch.x2 = (eventTouch != null ? eventTouch.pageX : void 0) || 0;
touch.y2 = (eventTouch != null ? eventTouch.pageY : void 0) || 0;
clearTimeout(touch.activeTimeout);
return (_ref1 = touch.$target) != null ? _ref1.tapblur() : void 0;
});
this.on(events.end, function(e) {
var $target, direction;
if (touch.x2 > 0 || touch.y2 > 0) {
if (Math.abs(touch.x1 - touch.x2) > options.swipeOffset || Math.abs(touch.y1 - touch.y2) > options.swipeOffset) {
direction = swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2);
touch.$target.swipe();
touch.$target['swipe' + direction]();
}
return touch.x1 = touch.x2 = touch.y1 = touch.y2 = touch.last = 0;
} else if ('last' in touch) {
$target = touch.$target;
$target.tapblur();
$target.tap();
return reset();
}
});
return this.on(events.cancel, function() {
var _ref;
if ((_ref = touch.$target) != null) {
_ref.tapblur();
}
return reset();
});
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment