-
Notifications
You must be signed in to change notification settings - Fork 7
/
FastClick.js
66 lines (61 loc) · 2.38 KB
/
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
/**
* FastClick is an override shim which maps event pairs of
* 'touchstart' and 'touchend' which differ by less than a certain
* threshold to the 'click' event.
* This is used to speed up clicks on some browsers.
*/
if (!window.CustomEvent) return;
var clickThreshold = 300;
var clickWindow = 500;
var potentialClicks = {};
var recentlyDispatched = {};
var _now = Date.now;
window.addEventListener('touchstart', function(event) {
var timestamp = _now();
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i];
potentialClicks[touch.identifier] = timestamp;
}
});
window.addEventListener('touchmove', function(event) {
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i];
delete potentialClicks[touch.identifier];
}
});
window.addEventListener('touchend', function(event) {
var currTime = _now();
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i];
var startTime = potentialClicks[touch.identifier];
if (startTime && currTime - startTime < clickThreshold) {
var clickEvt = new window.CustomEvent('click', {
'bubbles': true,
'detail': touch
});
recentlyDispatched[currTime] = event;
event.target.dispatchEvent(clickEvt);
}
delete potentialClicks[touch.identifier];
}
});
window.addEventListener('click', function(event) {
var currTime = _now();
for (var i in recentlyDispatched) {
var previousEvent = recentlyDispatched[i];
if (currTime - i < clickWindow) {
if (event instanceof window.MouseEvent && event.target === previousEvent.target) event.stopPropagation();
}
else delete recentlyDispatched[i];
}
}, true);
});