-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
419 lines (342 loc) · 15 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* @project vue3-touch-events
* @author Robin Rodricks, Xavier Julien, Jerry Bendy
* @since 30/4/2021
* @url https://github.com/robinrodricks/vue3-touch-events
*/
function touchX(event) {
if(event.type.indexOf('mouse') !== -1){
return event.clientX;
}
return event.touches[0].clientX;
}
function touchY(event) {
if(event.type.indexOf('mouse') !== -1){
return event.clientY;
}
return event.touches[0].clientY;
}
var isPassiveSupported = (function() {
var supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassive = true;
}
});
window.addEventListener('test', null, opts);
} catch (e) {}
return supportsPassive;
})();
var vueTouchEvents = {
install: function (app, constructorOptions) {
var globalOptions = Object.assign({}, {
disableClick: false,
tapTolerance: 10, // px
swipeTolerance: 30, // px
touchHoldTolerance: 400, // ms
longTapTimeInterval: 400, // ms
touchClass: '',
dragFrequency: 100, // ms
rollOverFrequency: 100, // ms
namespace: 'touch',
}, constructorOptions);
function touchStartEvent(event) {
var $this = this.$$touchObj,
isTouchEvent = event.type.indexOf('touch') >= 0,
isMouseEvent = event.type.indexOf('mouse') >= 0,
$el = this;
if (isTouchEvent) {
$this.lastTouchStartTime = event.timeStamp;
}
if (isMouseEvent && $this.lastTouchStartTime && event.timeStamp - $this.lastTouchStartTime < 350) {
return;
}
if ($this.touchStarted) {
return;
}
addTouchClass(this);
$this.touchStarted = true; // always true while the element is being PRESSED
$this.touchMoved = false; // true only when the element is PRESSED and DRAGGED a bit
$this.swipeOutBounded = false;
$this.startX = touchX(event);
$this.startY = touchY(event);
$this.currentX = 0; // always updated with the last mouse X/Y while over the element
$this.currentY = 0;
$this.touchStartTime = event.timeStamp;
// performance: only process swipe events if `swipe.*` event is registered on this element
$this.hasSwipe = hasEvent(this, 'swipe')
|| hasEvent(this, 'swipe.left') || hasEvent(this, 'swipe.right')
|| hasEvent(this, 'swipe.top') || hasEvent(this, 'swipe.bottom');
// performance: only start hold timer if the `hold` event is registered on this element
if (hasEvent(this, 'hold')){
// Trigger touchhold event after `touchHoldTolerance` MS
$this.touchHoldTimer = setTimeout(function() {
$this.touchHoldTimer = null;
triggerEvent(event, $el, 'hold');
}, $this.options.touchHoldTolerance);
}
triggerEvent(event, this, 'press');
}
function touchMoveEvent(event) {
var $this = this.$$touchObj;
var curX = touchX(event);
var curY = touchY(event);
var movedAgain = ($this.currentX != curX) || ($this.currentY != curY);
$this.currentX = curX;
$this.currentY = curY;
if (!$this.touchMoved) {
var tapTolerance = $this.options.tapTolerance;
$this.touchMoved = Math.abs($this.startX - $this.currentX) > tapTolerance ||
Math.abs($this.startY - $this.currentY) > tapTolerance;
// trigger `drag.once` only once after mouse FIRST moved while dragging the element
// (`touchMoved` is the flag that indicates we no longer need to trigger this)
if($this.touchMoved){
cancelTouchHoldTimer($this);
triggerEvent(event, this, 'drag.once');
}
// performance: only process swipe events if `swipe.*` event is registered on this element
} else if ($this.hasSwipe && !$this.swipeOutBounded) {
var swipeOutBounded = $this.options.swipeTolerance;
$this.swipeOutBounded = Math.abs($this.startX - $this.currentX) > swipeOutBounded &&
Math.abs($this.startY - $this.currentY) > swipeOutBounded;
}
// only trigger `rollover` event if cursor actually moved over this element
if(hasEvent(this, 'rollover') && movedAgain){
// throttle the `rollover` event based on `rollOverFrequency`
var now = event.timeStamp;
var throttle = $this.options.rollOverFrequency;
if ($this.touchRollTime == null || now > ($this.touchRollTime + throttle)){
$this.touchRollTime = now;
triggerEvent(event, this, 'rollover');
}
}
// only trigger `drag` event if cursor actually moved and if we are still dragging this element
if(hasEvent(this, 'drag') && $this.touchStarted && $this.touchMoved && movedAgain){
// throttle the `drag` event based on `dragFrequency`
var now = event.timeStamp;
var throttle = $this.options.dragFrequency;
if ($this.touchDragTime == null || now > ($this.touchDragTime + throttle)){
$this.touchDragTime = now;
triggerEvent(event, this, 'drag');
}
}
}
function touchCancelEvent() {
var $this = this.$$touchObj;
cancelTouchHoldTimer($this);
removeTouchClass(this);
$this.touchStarted = $this.touchMoved = false;
$this.startX = $this.startY = 0;
}
function touchEndEvent(event) {
var $this = this.$$touchObj,
isTouchEvent = event.type.indexOf('touch') >= 0,
isMouseEvent = event.type.indexOf('mouse') >= 0;
if (isTouchEvent) {
$this.lastTouchEndTime = event.timeStamp;
}
var touchholdEnd = isTouchEvent && !$this.touchHoldTimer;
cancelTouchHoldTimer($this);
$this.touchStarted = false;
removeTouchClass(this);
if (isMouseEvent && $this.lastTouchEndTime && event.timeStamp - $this.lastTouchEndTime < 350) {
return;
}
// trigger `end` event when touch stopped
triggerEvent(event, this, 'release');
if (!$this.touchMoved) {
// detect if this is a longTap event or not
if (hasEvent(this, 'longtap') && event.timeStamp - $this.touchStartTime > $this.options.longTapTimeInterval) {
if (event.cancelable) {
event.preventDefault();
}
triggerEvent(event, this, 'longtap');
} else if (hasEvent(this, 'hold') && touchholdEnd) {
if (event.cancelable) {
event.preventDefault();
}
return;
} else {
// emit tap event
triggerEvent(event, this, 'tap');
}
// performance: only process swipe events if `swipe.*` event is registered on this element
} else if ($this.hasSwipe && !$this.swipeOutBounded) {
var swipeOutBounded = $this.options.swipeTolerance,
direction,
distanceY = Math.abs($this.startY - $this.currentY),
distanceX = Math.abs($this.startX - $this.currentX);
if (distanceY > swipeOutBounded || distanceX > swipeOutBounded) {
if (distanceY > distanceX) {
direction = $this.startY > $this.currentY ? 'top' : 'bottom';
} else {
direction = $this.startX > $this.currentX ? 'left' : 'right';
}
// Only emit the specified event when it has modifiers
if (hasEvent(this, 'swipe.' + direction)) {
triggerEvent(event, this, 'swipe.' + direction, direction);
} else {
// Emit a common event when it has no any modifier
triggerEvent(event, this, 'swipe', direction);
}
}
}
}
function mouseEnterEvent() {
addTouchClass(this);
}
function mouseLeaveEvent() {
removeTouchClass(this);
}
function hasEvent($el, eventType) {
var callbacks = $el.$$touchObj.callbacks[eventType];
return (callbacks != null && callbacks.length > 0);
}
function triggerEvent(e, $el, eventType, param) {
var $this = $el.$$touchObj;
// get the subscribers for this event
var callbacks = $this.callbacks[eventType];
// exit if no subscribers to this particular event
if (callbacks == null || callbacks.length === 0) {
return null;
}
// per callback
for (var i = 0; i < callbacks.length; i++) {
var binding = callbacks[i];
if (binding.modifiers.stop) {
e.stopPropagation();
}
if (binding.modifiers.prevent) {
e.preventDefault();
}
// handle `self` modifier`
if (binding.modifiers.self && e.target !== e.currentTarget) {
continue;
}
if (typeof binding.value === 'function') {
if (param) {
binding.value(param, e);
} else {
binding.value(e);
}
}
}
}
function addTouchClass($el) {
var className = $el.$$touchObj.options.touchClass;
className && $el.classList.add(className);
}
function removeTouchClass($el) {
var className = $el.$$touchObj.options.touchClass;
className && $el.classList.remove(className);
}
function cancelTouchHoldTimer($this) {
if ($this && $this.touchHoldTimer) {
clearTimeout($this.touchHoldTimer);
$this.touchHoldTimer = null;
}
}
function buildTouchObj($el, extraOptions) {
var touchObj = $el.$$touchObj || {
// an object contains all callbacks registered,
// key is event name, value is an array
callbacks: {},
// prevent bind twice, set to true when event bound
hasBindTouchEvents: false,
// default options, would be override by v-touch-options
options: globalOptions
};
if (extraOptions) {
touchObj.options = Object.assign({}, touchObj.options, extraOptions);
}
$el.$$touchObj = touchObj;
return $el.$$touchObj;
}
app.directive(globalOptions.namespace, {
beforeMount: function ($el, binding) {
// build a touch configuration object
var $this = buildTouchObj($el);
// declare passive option for the event listener. Defaults to { passive: true } if supported
var passiveOpt = isPassiveSupported ? { passive: true } : false;
// register callback
var eventType = binding.arg || 'tap';
switch (eventType) {
case 'swipe':
var _m = binding.modifiers;
if (_m.left || _m.right || _m.top || _m.bottom) {
for (var i in binding.modifiers) {
if (['left', 'right', 'top', 'bottom'].indexOf(i) >= 0) {
var _e = 'swipe.' + i;
$this.callbacks[_e] = $this.callbacks[_e] || [];
$this.callbacks[_e].push(binding);
}
}
} else {
$this.callbacks.swipe = $this.callbacks.swipe || [];
$this.callbacks.swipe.push(binding);
}
break;
case 'press':
case 'drag':
if (binding.modifiers.disablePassive) {
// change the passive option for the `drag` event if disablePassive modifier exists
passiveOpt = false;
}
default:
$this.callbacks[eventType] = $this.callbacks[eventType] || [];
$this.callbacks[eventType].push(binding);
}
// prevent bind twice
if ($this.hasBindTouchEvents) {
return;
}
$el.addEventListener('touchstart', touchStartEvent, passiveOpt);
$el.addEventListener('touchmove', touchMoveEvent, passiveOpt);
$el.addEventListener('touchcancel', touchCancelEvent);
$el.addEventListener('touchend', touchEndEvent);
if (!$this.options.disableClick) {
$el.addEventListener('mousedown', touchStartEvent);
$el.addEventListener('mousemove', touchMoveEvent);
$el.addEventListener('mouseup', touchEndEvent);
$el.addEventListener('mouseenter', mouseEnterEvent);
$el.addEventListener('mouseleave', mouseLeaveEvent);
}
// set bind mark to true
$this.hasBindTouchEvents = true;
},
unmounted: function ($el) {
cancelTouchHoldTimer($el.$$touchObj)
$el.removeEventListener('touchstart', touchStartEvent);
$el.removeEventListener('touchmove', touchMoveEvent);
$el.removeEventListener('touchcancel', touchCancelEvent);
$el.removeEventListener('touchend', touchEndEvent);
if ($el.$$touchObj && !$el.$$touchObj.options.disableClick) {
$el.removeEventListener('mousedown', touchStartEvent);
$el.removeEventListener('mousemove', touchMoveEvent);
$el.removeEventListener('mouseup', touchEndEvent);
$el.removeEventListener('mouseenter', mouseEnterEvent);
$el.removeEventListener('mouseleave', mouseLeaveEvent);
}
// remove vars
delete $el.$$touchObj;
}
});
app.directive(`${globalOptions.namespace}-class`, {
beforeMount: function ($el, binding) {
buildTouchObj($el, {
touchClass: binding.value
});
}
});
app.directive(`${globalOptions.namespace}-options`, {
beforeMount: function($el, binding) {
buildTouchObj($el, binding.value);
}
});
}
};
/*
* Exports
*/
export default vueTouchEvents