-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (61 loc) · 1.83 KB
/
index.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
/**
* Expose
*/
module.exports = GestureRecognizer;
/**
* Constructor
*/
function GestureRecognizer() {
this.pairs = [];
}
/**
* Prototype
*/
var prototype = GestureRecognizer.prototype = Object.create(null);
var states = prototype.states = Object.create(null);
states.POSSIBLE = 'possible';
states.BEGAN = 'began';
states.CHANGED = 'changed';
states.ENDED = 'ended';
states.CANCELLED = 'cancelled';
states.FAILED = 'failed';
states.RECOGNIZED = states.ENDED;
prototype.state = states.POSSIBLE;
prototype.addTarget = function (target, action) {
var pairs = this.pairs;
if (!target)
throw new Error("You must specify a target");
if (!action)
throw new Error("You must specify an action");
if (typeof action !== "string")
throw new Error("Action must be a string");
if (typeof target[action] !== "function")
throw new Error("The specified action must be a function of the target");
if (!hasTargetActionPair(target, action, pairs))
pairs.push([target, action]);
};
prototype.removeTarget = function (target, action) {
var pairs = this.pairs;
if (target === null && action === null) {
pairs.length = 0;
} else if (target === null) {
var i = pairs.length;
while (i--) if (pairs[i][1] === action) pairs.splice(i, 1);
} else if (action === null) {
var i = pairs.length;
while (i--) if (pairs[i][0] === target) pairs.splice(i, 1);
} else {
var i = indexOfTargetActionPair(target, action, pairs);
if (typeof i === 'number') pairs.splice(i, 1);
}
};
/**
* Helper functions
*/
function indexOfTargetActionPair(target, action, pairs) {
for (var i = 0, l = pairs.length; i < l; i ++)
if (pairs[i][0] === target && pairs[i][1] === action) return i;
}
function hasTargetActionPair(target, action, pairs) {
return typeof indexOfTargetActionPair(target, action, pairs) === "number";
}