forked from walmartlabs/thorax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththorax.fastclick.js
73 lines (66 loc) · 2.28 KB
/
thorax.fastclick.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//removal of click delay on mobile webkit
(function() {
var TAP_RANGE = 5; // +-5px is still considered a tap
var _configure = Thorax.configure;
Thorax.configure = function() {
if ('ontouchstart' in document.documentElement) {
Thorax.fastClick = 'fast-click';
var events = Thorax.Layout.prototype.events;
events['fast-click a'] = events['click a'];
delete events['click a'];
var start,
clickRedRum;
function onTouchStart(event) {
if (event.touches.length === 1) {
var touch = event.touches[0];
start = {x: touch.clientX, y: touch.clientY};
} else {
start = false;
}
clickRedRum = false;
}
function onTouchMove() {
if (event.touches.length > 1) {
start = false;
}
}
function onTouchEnd(event) {
var touch = event.changedTouches[0];
if (start
&& Math.abs(touch.clientX-start.x) <= TAP_RANGE
&& Math.abs(touch.clientY-start.y) <= TAP_RANGE) {
var target = document.elementFromPoint(touch.clientX, touch.clientY);
if(target.nodeType == 3) {
target = target.parentNode;
}
event = $.Event(Thorax.fastClick, {original: event});
$(target).trigger(event);
// If the fast-click was handled, prevent futher operations
if (event.defaultPrevented) {
clickRedRum = true;
event.original.preventDefault();
}
}
}
function clickKiller(event) {
if (clickRedRum) {
event.preventDefault();
event.stopPropagation();
clickRedRum = false;
}
}
document.body.addEventListener('touchstart', onTouchStart, true);
document.body.addEventListener('touchmove', onTouchMove, true);
document.body.addEventListener('touchend', onTouchEnd, true);
document.body.addEventListener('click', clickKiller, true);
var _addEvent = Thorax.View.prototype._addEvent;
Thorax.View.prototype._addEvent = function(params) {
params.name = params.name.replace(/^click\b/, Thorax.fastClick);
_addEvent.call(this, params);
};
} else {
Thorax.fastClick = 'click';
}
_configure.apply(this, arguments);
};
})();