-
Notifications
You must be signed in to change notification settings - Fork 1
/
swipe.js
349 lines (323 loc) · 8.74 KB
/
swipe.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Triggers swipe events based on user touch interaction.
* @class Swipe
*/
(function (scope) {
function Swipe (scope, options) {
this.init(scope, options);
}
var p = Swipe.prototype;
/**
* The element within which to handle touches.
* @property scope
* @type {HTMLElement}
*/
p.scope = null;
/**
* Stores cached string values of the touch events.
* @property events
* @type {object}
*/
p.events = null;
/**
* How sensitive the util is to triggering a swipe. The higher the number the greater the sense.
* Allows a number between 1-100.
* @property sensitivity
* @type {number}
*/
p.sensitivity = 75;
/**
* The element being interacted with.
* @property activeElement
* @type {HTMLElement}
*/
p.activeElement = null;
/**
* Whether or not to cancel the swipe event trigger
* @property cancelSwipe
* @type {boolean}
*/
p.cancelSwipe = false;
/**
* Whether or not a swipe event is in progress.
* @property swipeInProgress
* @type {boolean}
*/
p.swipeInProgress = false;
/**
* Decides whether or not to prevent default scroll behaviour during a touchmove event.
* @property doPreventScroll
* @type {boolean}
*/
p.doPreventScroll = false;
/**
* Whether or not to detect scroll prevention.
* @property detectScrollPrevention
* @type {boolean}
*/
p.detectScrollPrevention = false;
/**
* Whether or not to continue preventing the scroll.
* @property continuePreventingScroll
* @type {boolean}
*/
p.continuePreventingScroll = false;
/**
* An object of directions that have been listened to for swipe events.
* @property listenTo
* @type {Object}
*/
p.listenTo = {
up: 0,
down: 0,
left: 0,
right: 0
};
/**
* The screen location on the X axis of when the touch was started.
* @property swipeStart_clientX
* @type {number}
*/
p.swipeStart_clientX = null;
/**
* The screen location on the Y axis of when the touch was started.
* @property swipeStart_clientY
* @type {number}
*/
p.swipeStart_clientY = null;
/**
* The screen location on the X axis of when the touch was ended.
* @property swipeEnd_clientX
* @type {number}
*/
p.swipeEnd_clientX = null;
/**
* The screen location on the Y axis of when the touch was ended.
* @property swipeEnd_clientY
* @type {number}
*/
p.swipeEnd_clientY = null;
/**
* Universal time at the beginning of the swipe.
* @property timeState
* @type {number}
*/
p.timeStart = null;
/**
* Universal time at the end of the swipe.
* @property timeEnd
* @type {number}
*/
p.timeEnd = null;
/**
* A string used for easy event listener application.
* @property up
* @type {string}
*/
p.up = 'up';
/**
* A string used for easy event listener application.
* @property down
* @type {string}
*/
p.down = 'down';
/**
* A string used for easy event listener application.
* @property left
* @type {string}
*/
p.left = 'left';
/**
* A string used for easy event listener application.
* @property right
* @type {string}
*/
p.right = 'right';
/**
* Set our options, scope, and listeners. Also detect support.
* @method init
* @param scope
* @param options
*/
p.init = function (scope, options) {
if (!options) {
this.doPreventScroll = this.detectScrollPrevention = options.doPreventScroll;
}
try {
this.detectEventSupport();
// accept a scope, default to the body
this.scope = scope || window.document.body;
// apply event listeners
this.scope.addEventListener(this.events.start, this._handleTouchStart.bind(this));
this.scope.addEventListener(this.events.move, this._handleTouchMove.bind(this));
this.scope.addEventListener(this.events.end, this._handleTouchEnd.bind(this));
} catch (err) {
console.warn(err);
}
};
/**
* Remove all event listeners.
* @method destroy
*/
p.destroy = function () {
this.scope.removeEventListener(this.events.start, this._handleTouchStart.bind(this));
this.scope.removeEventListener(this.events.move, this._handleTouchMove.bind(this));
this.scope.removeEventListener(this.events.end, this._handleTouchEnd.bind(this));
};
/**
* Detect TouchEvent support, throwing an error if there is none.
* TODO: PointerEvents. Maybe mozilla pointer.js ?
* An error is thrown if touch isn't supported because this util shouldn't be initialized unless
*/
p.detectEventSupport = function () {
if ('ontouchstart' in window || 'maxTouchPoints' in window.navigator > 1 || 'msMaxTouchPoints' in window.navigator > 1) {
this.events = {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
};
} else {
throw new Error('TouchEvent not supported.');
}
};
/**
* Adds a swipe listener to the scope, caching the direction in the listenTo object.
* @method addListener
* @param type
* @param handler
*/
p.addListener = function (type, handler) {
this.listenTo[type]++;
this.scope.addEventListener(type, handler);
};
/**
* Removes an applied listener if it exists.
* @method removeListener
* @param type
* @param handler
*/
p.removeListener = function (type, handler) {
// only remove listeners if they actually exist
this.listenTo[type] > 0 && this.listenTo[type]--;
this.scope.removeEventListener(type, handler);
};
/**
* Checks if the passed swipe direction is being listened to.
* @method directionHasListener
* @param direction
* @returns {boolean}
* @private
*/
p._directionHasListener = function (direction) {
return this.listenTo[direction] > 0;
};
/**
* Decides whether or not to trigger a swipe.
* @method calculateSwipe
* @private
*/
p._calculateSwipe = function () {
var threshold = this.timeEnd - this.timeStart;
var diffX = this.swipeStart_clientX - this.swipeEnd_clientX;
var diffY = this.swipeStart_clientY - this.swipeEnd_clientY;
var direction = this._findSwipeDirection(diffX, diffY, threshold);
// only trigger swipe if a direction was calculated.
direction && this._triggerSwipe(direction);
};
/**
* Calculates which direction the swipe was in.
* @method findSwipeDirection
* @param diffX
* @param diffY
* @param threshold
* @returns {string|boolean}
* @private
*/
p._findSwipeDirection = function (diffX, diffY, threshold) {
var absX = Math.abs(diffX);
var absY = Math.abs(diffY);
threshold -= this.sensitivity;
if (absX > absY) {
// horizontal
if (diffX >= threshold) {
return 'left';
} else if (absX >= threshold) {
return 'right';
}
} else {
// vertical
if (diffY >= threshold) {
return 'up';
} else if (absY >= threshold) {
return 'down';
}
}
return false;
};
/**
* Triggers a swipe event in the direction calculated.
* @method triggerSwipe
* @param direction
* @private
*/
p._triggerSwipe = function (direction) {
if (this.swipeInProgress && !this.cancelSwipe) {
this.swipeInProgress = false;
var swipeEvent = document.createEvent('Event');
swipeEvent.initEvent(direction, true, true);
this.activeElement.dispatchEvent(swipeEvent);
}
};
/**
* Handles the touch start event.
* @method handleTouchStart
* @param event
* @private
*/
p._handleTouchStart = function (event) {
this.activeElement = event.currentTarget;
this.timeStart = new Date().getTime();
this.cancelSwipe = true;
// check for the TouchList before getting X/Y data
var touches = !!event.touches;
this.swipeStart_clientX = touches ? event.touches[0].clientX : event.clientX;
this.swipeStart_clientY = touches ? event.touches[0].clientY : event.clientY;
};
/**
* Handles the touch move event (a swipe).
* @method handleTouchMove
* @param event
* @private
*/
p._handleTouchMove = function (event) {
if (this.detectScrollPrevention) {
var touches = !!event.touches;
var diffX = this.swipeStart_clientX - (touches ? event.touches[0].clientX : event.clientX);
var diffY = this.swipeStart_clientY - (touches ? event.touches[0].clientY : event.clientY);
var direction = this._findSwipeDirection(diffX, diffY, 0, 0);
this.continuePreventingScroll = this._directionHasListener(direction);
this.detectScrollPrevention = false;
}
if (this.doPreventScroll && this.continuePreventingScroll) { event.preventDefault(); }
this.swipeInProgress = true;
this.cancelSwipe = false;
};
/**
* Handles the touch end event
* @method handleTouchEnd
* @param event
* @private
*/
p._handleTouchEnd = function (event) {
this.timeEnd = new Date().getTime();
// cache swipe data
// check for the TouchList before getting X/Y data
var touches = !!event.touches;
this.swipeEnd_clientX = touches ? event.changedTouches[0].clientX : event.clientX;
this.swipeEnd_clientY = touches ? event.changedTouches[0].clientY : event.clientY;
this._calculateSwipe();
this.detectScrollPrevention = true;
this.continuePreventingScroll = false;
};
scope.Swipe = Swipe;
})(window);