forked from max-degterev/Zepto-onPress
-
Notifications
You must be signed in to change notification settings - Fork 5
/
zepto.onpress.js
145 lines (123 loc) · 4.31 KB
/
zepto.onpress.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Author:
Max Degterev @suprMax
Valerio Di Donato @drowne
JQM fast buttons without nasty ghostclicks.
Highly inspired by http://code.google.com/mobile/articles/fast_buttons.html
Usage
live:
$('#someid').onpress(function(event){});
$('#someid').offpress(function(event){});
TODO: find a way to remove handleTouchStart handler as well
*/
var isTouch = {};
(function($) {
isTouch = (window.hasOwnProperty('ontouchstart'));
var ghostsLifeTime = 1000;
var normalizeArgs = function(args) {
var callback,
selector;
if (typeof args[0] === 'function') {
callback = args[0];
}
else {
selector = args[0];
callback = args[1];
}
return [selector, callback];
};
if (isTouch) {
var ghosts = [];
var touches = {},
$doc = $(document),
hasMoved = false,
handlers = {};
var handleTouchStart = function(e) {
e.stopPropagation();
touches.x = e.originalEvent.touches[0].pageX;
touches.y = e.originalEvent.touches[0].pageY;
hasMoved = false;
};
var handleTouchMove = function(e) {
if (Math.abs(e.originalEvent.touches[0].pageX - touches.x) > 10 || Math.abs(e.originalEvent.touches[0].pageX - touches.y) > 10) {
hasMoved = true;
}
};
var removeGhosts = function() {
ghosts.splice(0, 2);
};
var handleGhosts = function(e) {
var i, l;
for (i = 0, l = ghosts.length; i < l; i += 2) {
if (Math.abs(e.pageX - ghosts[i]) < 25 && Math.abs(e.pageY - ghosts[i + 1]) < 25) {
e.stopPropagation();
e.preventDefault();
}
}
};
$doc.live('click', handleGhosts);
$doc.live('touchmove', handleTouchMove);
$.fn.onpress = function() {
var args = normalizeArgs(arguments);
var handleTouchEnd = function(e) {
e.stopPropagation();
if (!hasMoved) {
args[1].call(this, e);
ghosts.push(touches.x, touches.y);
window.setTimeout(removeGhosts, ghostsLifeTime);
}
};
handlers[args[1]] = handleTouchEnd;
if (args[0]) {
this.live('touchstart.onpress', args[0], handleTouchStart);
this.live('touchend.onpress', args[0], handleTouchEnd);
this.live('press', args[0], args[1]);
}
else {
this.live('touchstart.onpress', handleTouchStart);
this.live('touchend.onpress', handleTouchEnd);
this.live('press', args[1]);
}
};
$.fn.offpress = function() {
var args = normalizeArgs(arguments);
if (args[1]) {
if (args[0]) {
this.die('.onpress', args[0], handlers[args[1]]);
this.die('press', args[0], args[1]);
}
else {
this.die('.onpress', handlers[args[1]]);
this.die('press', args[1]);
}
delete handlers[args[1]];
}
else {
if (args[0]) {
this.die('.onpress', args[0]);
this.die('press', args[0]);
}
else {
this.die('.onpress');
this.die('press');
}
}
};
}
else {
$.fn.onpress = function() {
var args = normalizeArgs(arguments);
if (args[0]) {
this.live('click.onpress', args[0], args[1]);
this.live('press.onpress', args[0], args[1]);
}
else {
this.live('click.onpress', args[1]);
this.live('press.onpress', args[1]);
}
};
$.fn.offpress = function() {
var args = normalizeArgs(arguments);
args[0] ? this.die('.onpress', args[0], args[1]) : this.die('.onpress', args[1]);
};
}
}(jQuery));