forked from senchalabs/jQTouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjqtouch-jquery2.js
237 lines (201 loc) · 7.26 KB
/
jqtouch-jquery2.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
(function($){
var touch = {}, touchTimeout;
var jQT;
var touchSelector = 'a, .touch';
function parentIfText(node){
return 'tagName' in node ? node : node.parentNode;
}
function swipeDirection(x1, x2, y1, y2){
var xDelta = Math.abs(x1 - x2), yDelta = Math.abs(y1 - y2);
if (xDelta >= yDelta) {
return (x1 - x2 > 0 ? 'Left' : 'Right');
} else {
return (y1 - y2 > 0 ? 'Up' : 'Down');
}
}
var longTapDelay = 750;
function longTap(){
if (touch.last && (Date.now() - touch.last >= longTapDelay)) {
touch.el.trigger('longTap');
touch = {};
}
}
if ($.jQTouch) {
$.jQTouch.addExtension(function GetInstance(jqt) {
jQT = jqt;
touchSelector = jQT.settings.touchSelector;
});
} else {
console.warn('Error: jQTouch not found.');
}
$(document).ready(function(){
var SUPPORT_TOUCH = (typeof Touch != "undefined");
var START_EVENT = SUPPORT_TOUCH? 'touchstart' : 'mousedown';
var MOVE_EVENT = SUPPORT_TOUCH? 'touchmove' : 'mousemove';
var END_EVENT = SUPPORT_TOUCH? 'touchend' : 'mouseup';
var CANCEL_EVENT = SUPPORT_TOUCH? 'touchcancel' : 'mouseout'; // mouseout on document
function isRightClick(e) {
var rightclick = false;
if (!SUPPORT_TOUCH) {
// http://www.quirksmode.org/js/events_properties.html
if (!e) var e = window.event;
if (e.which) rightclick = (e.which == 3);
else if (e.button) rightclick = (e.button == 2);
}
return rightclick;
}
function touchstartHandler(e) {
console.warn('touch started');
try {
var $oel, $el, $marked;
var elX, elY;
if (isRightClick(e)) {
return;
}
$oel = $(e.target);
$el = $oel;
elStartY = $el.offset().top;
elStartX = $el.offset().left;
var hovertimeout = null;
var presstimeout = null;
var startX, startY, startTime;
var deltaX, deltaY, deltaT;
var endX, endY, endTime;
var swipped = false, tapped = false, moved = false, inprogress = false, pressed = false;
function bindEvents($el) {
$el.bind(MOVE_EVENT, handlemove).bind(END_EVENT, handleend);
if ($.support.touch) {
$el.bind(CANCEL_EVENT, handlecancel);
} else {
$(document).bind('mouseout', handleend);
}
}
function unbindEvents($el) {
$el.unbind(MOVE_EVENT, handlemove).unbind(END_EVENT, handleend);
if ($.support.touch) {
$el.unbind(CANCEL_EVENT, handlecancel);
} else {
$(document).unbind('mouseout', handlecancel);
}
}
function updateChanges(e) {
var point = e.originalEvent;
var first = $.support.touch? point.changedTouches[0]: point;
deltaX = first.pageX - startX;
deltaY = first.pageY - startY;
deltaT = (new Date).getTime() - startTime;
var absElOffset = $el.offset();
elX = absElOffset.left - elStartX;
elY = absElOffset.top - elStartY;
}
function handlestart(e) {
var point;
inprogress = true, swipped = false, tapped = false,
moved = false, timed = false, pressed = false;
point = e.originalEvent;
startX = $.support.touch? point.changedTouches[0].pageX: point.pageX;
startY = $.support.touch? point.changedTouches[0].pageY: point.pageY;
startTime = (new Date).getTime();
endX = null, endY = null, endTime = null;
deltaX = 0;
deltaY = 0;
deltaT = 0;
// Let's bind these after the fact, so we can keep some internal values
bindEvents($el);
setTimeout(function() {
$marked = $el;
var mySelectors = touchSelector;
while ($marked.parent().is(mySelectors)) {
$marked = $marked.parent();
}
handlehover();
}, 50);
setTimeout(function() {
$el.trigger("touch");
}, 50);
setTimeout(function() {
handlepress(e);
}, 1000);
};
function handlemove(e) {
updateChanges(e);
if (!inprogress)
return;
var absX = Math.abs(deltaX);
var absY = Math.abs(deltaY);
if (absX > 1 || absY > 1) {
moved = true;
}
if (absY <= 5 && elX === 0 && elY === 0) {
if (absX > (3 * absY) && (absX > 10) && deltaT < 1000) {
inprogress = false;
if ($marked) $marked.removeClass('active');
unbindEvents($el);
swipped = true;
$el.trigger('swipe', {direction: (deltaX < 0) ? 'left' : 'right', deltaX: deltaX, deltaY: deltaY });
} else if (absY > (3 * absX) && (absY > 10) && deltaT < 1000) {
inprogress = false;
if ($marked) $marked.removeClass('active');
unbindEvents($el);
swipped = true;
$el.trigger('swipe', {direction: (deltaY < 0) ? 'up' : 'down', deltaX: deltaX, deltaY: deltaY });
}
} else {
// moved too much, can't swipe anymore
inprogress = false;
if ($marked) $marked.removeClass('active');
unbindEvents($el);
}
};
function handleend(e) {
updateChanges(e);
var absX = Math.abs(deltaX);
var absY = Math.abs(deltaY);
inprogress = false;
unbindEvents($el);
if (!tapped && (absX <= 1 && absY <= 1) && (elX === 0 && elY === 0)) {
tapped = true;
$oel.trigger('tap');
setTimeout(function() {
if ($marked) $marked.removeClass('active');
}, 1000);
} else {
if ($marked) $marked.removeClass('active');
//e.preventDefault();
}
};
function handlecancel(e) {
inprogress = false;
if ($marked) $marked.removeClass('active');
unbindEvents();
};
function handlehover() {
timed = true;
if (tapped) {
// flash the selection
$marked.addClass('active');
hovertimeout = setTimeout(function() {
$marked.removeClass('active');
}, 1000);
} else if (inprogress && !moved) {
$marked.addClass('active');
}
};
function handlepress(e) {
if (inprogress && !tapped && !moved) {
pressed = true;
tapped = true;
$el.trigger('press');
}
}
handlestart(e);
} catch (err) {
console.error('tap error: ' + err);
}
} // End touch handler
$(document.body).bind('touchstart', touchstartHandler);
});
['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(m){
$.fn[m] = function(callback){ return this.bind(m, callback) }
});
})($);