forked from mud/JSGestureRecognizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtap.js
101 lines (76 loc) · 2.45 KB
/
tap.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module.exports = TapGestureRecognizer;
var GestureRecognizer = require("./gesture-recognizer"),
Point = require("geometry/point.js");
function TapGestureRecognizer()
{
this.numberOfTapsRequired = 1;
this.numberOfTouchesRequired = 1;
GestureRecognizer.call(this);
}
TapGestureRecognizer.MoveTolerance = 40;
TapGestureRecognizer.WaitingForNextTapToStartTimeout = 350;
TapGestureRecognizer.WaitingForTapCompletionTimeout = 750;
TapGestureRecognizer.prototype = {
constructor: TapGestureRecognizer,
__proto__: GestureRecognizer.prototype,
touchesBegan: function(event)
{
if (event.currentTarget !== this.target)
return;
GestureRecognizer.prototype.touchesBegan.call(this, event);
if (this.numberOfTouches !== this.numberOfTouchesRequired) {
this.enterFailedState();
return;
}
this._startPoint = GestureRecognizer.prototype.locationInElement.call(this);
this._rewindTimer(TapGestureRecognizer.WaitingForTapCompletionTimeout);
event.preventDefault();
},
touchesMoved: function(event)
{
if (!GestureRecognizer.SupportsTouches) {
event.preventDefault();
this.enterFailedState();
return;
}
if (this._startPoint.distanceToPoint(GestureRecognizer.prototype.locationInElement.call(this)) > TapGestureRecognizer.MoveTolerance)
this.enterFailedState();
},
touchesEnded: function(event)
{
this._taps++;
if (this._taps === this.numberOfTapsRequired) {
this.enterRecognizedState();
this.reset();
}
this._rewindTimer(TapGestureRecognizer.WaitingForNextTapToStartTimeout);
},
reset: function()
{
this._taps = 0;
this._clearTimer();
},
locationInElement: function(element)
{
var p = this._startPoint || new Point;
if (!element)
return p;
var wkPoint = window.webkitConvertPointFromPageToNode(element, new WebKitPoint(p.x, p.y));
return new Point(wkPoint.x, wkPoint.y);
},
// Private
_clearTimer: function()
{
window.clearTimeout(this._timerId);
delete this._timerId;
},
_rewindTimer: function(timeout)
{
this._clearTimer();
this._timerId = window.setTimeout(this._timerFired.bind(this), timeout);
},
_timerFired: function()
{
this.enterFailedState();
}
};