forked from OpenNHP/opennhp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.js
217 lines (179 loc) · 6.16 KB
/
touch.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
// Based on Zeptos touch.js
// https://raw.github.com/madrobby/zepto/master/src/touch.js
// Zepto.js may be freely distributed under the MIT license.
(function(root) {
'use strict';
var $ = root.jQuery;
if (!$ || $.fn.swipeLeft) {
return;
}
var touch = {};
var touchTimeout;
var tapTimeout;
var swipeTimeout;
var longTapTimeout;
var longTapDelay = 750;
var gesture;
function swipeDirection(x1, x2, y1, y2) {
return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ?
'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down');
}
function longTap() {
longTapTimeout = null;
if (touch.last) {
touch.el.trigger('longTap');
touch = {};
}
}
function cancelLongTap() {
if (longTapTimeout) {
clearTimeout(longTapTimeout);
}
longTapTimeout = null;
}
function cancelAll() {
if (touchTimeout) {
clearTimeout(touchTimeout);
}
if (tapTimeout) {
clearTimeout(tapTimeout);
}
if (swipeTimeout) {
clearTimeout(swipeTimeout);
}
if (longTapTimeout) {
clearTimeout(longTapTimeout);
}
touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null;
touch = {};
}
function isPrimaryTouch(event) {
return event.pointerType == event.MSPOINTER_TYPE_TOUCH && event.isPrimary;
}
$(function() {
var now;
var delta;
var deltaX = 0;
var deltaY = 0;
var firstTouch;
if ('MSGesture' in window) {
gesture = new MSGesture();
gesture.target = document.body;
}
$(document)
.on('MSGestureEnd gestureend', function(e) {
var swipeDirectionFromVelocity = e.originalEvent.velocityX > 1 ?
'Right' : e.originalEvent.velocityX < -1 ?
'Left' : e.originalEvent.velocityY > 1 ?
'Down' : e.originalEvent.velocityY < -1 ? 'Up' : null;
if (swipeDirectionFromVelocity) {
touch.el.trigger('swipe');
touch.el.trigger('swipe' + swipeDirectionFromVelocity);
}
})
// MSPointerDown: for IE10
// pointerdown: for IE11
.on('touchstart MSPointerDown pointerdown', function(e) {
if (e.type == 'MSPointerDown' && !isPrimaryTouch(e.originalEvent)) {
return;
}
firstTouch = (e.type == 'MSPointerDown' || e.type == 'pointerdown') ?
e : e.originalEvent.touches[0];
now = Date.now();
delta = now - (touch.last || now);
touch.el = $('tagName' in firstTouch.target ?
firstTouch.target : firstTouch.target.parentNode);
if (touchTimeout) {
clearTimeout(touchTimeout);
}
touch.x1 = firstTouch.pageX;
touch.y1 = firstTouch.pageY;
if (delta > 0 && delta <= 250) {
touch.isDoubleTap = true;
}
touch.last = now;
longTapTimeout = setTimeout(longTap, longTapDelay);
// adds the current touch contact for IE gesture recognition
if (gesture && (e.type == 'MSPointerDown' || e.type == 'pointerdown' ||
e.type == 'touchstart')) {
gesture.addPointer(e.originalEvent.pointerId);
}
})
// MSPointerMove: for IE10
// pointermove: for IE11
.on('touchmove MSPointerMove pointermove', function(e) {
if (e.type == 'MSPointerMove' && !isPrimaryTouch(e.originalEvent)) {
return;
}
firstTouch = (e.type == 'MSPointerMove' || e.type == 'pointermove') ?
e : e.originalEvent.touches[0];
cancelLongTap();
touch.x2 = firstTouch.pageX;
touch.y2 = firstTouch.pageY;
deltaX += Math.abs(touch.x1 - touch.x2);
deltaY += Math.abs(touch.y1 - touch.y2);
})
// MSPointerUp: for IE10
// pointerup: for IE11
.on('touchend MSPointerUp pointerup', function(e) {
if (e.type == 'MSPointerUp' && !isPrimaryTouch(e.originalEvent)) {
return;
}
cancelLongTap();
// swipe
if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
(touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)) {
swipeTimeout = setTimeout(function() {
touch.el.trigger('swipe');
touch.el.trigger('swipe' +
(swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)));
touch = {};
}, 0);
// normal tap
} else if ('last' in touch) {
// don't fire tap when delta position changed by more than 30 pixels,
// for instance when moving to a point and back to origin
if (isNaN(deltaX) || (deltaX < 30 && deltaY < 30)) {
// delay by one tick so we can cancel the 'tap' event if 'scroll' fires
// ('tap' fires before 'scroll')
tapTimeout = setTimeout(function() {
// trigger universal 'tap' with the option to cancelTouch()
// (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
var event = $.Event('tap');
event.cancelTouch = cancelAll;
touch.el.trigger(event);
// trigger double tap immediately
if (touch.isDoubleTap) {
touch.el.trigger('doubleTap');
touch = {};
}
// trigger single tap after 250ms of inactivity
else {
touchTimeout = setTimeout(function() {
touchTimeout = null;
touch.el.trigger('singleTap');
touch = {};
}, 250);
}
}, 0);
} else {
touch = {};
}
deltaX = deltaY = 0;
}
})
// when the browser window loses focus,
// for example when a modal dialog is shown,
// cancel all ongoing events
.on('touchcancel MSPointerCancel', cancelAll);
// scrolling the window indicates intention of the user
// to scroll, not tap or swipe, so cancel all ongoing events
$(window).on('scroll', cancelAll);
});
['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',
'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName) {
$.fn[eventName] = function(callback) {
return $(this).on(eventName, callback);
};
});
})(this);