-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
383 lines (315 loc) · 13.2 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
/**
*
* @author Jerry Bendy
* @since 4/12/2017
*/
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;
})();
// Save last touch time globally (touch start time or touch end time), if a `click` event triggered,
// and the time near by the last touch time, this `click` event will be ignored. This is used for
// resolve touch through issue.
var globalLastTouchTime = 0;
var vueTouchEvents = {
install: function (Vue, constructorOptions) {
var globalOptions = Object.assign({}, {
disableClick: false,
tapTolerance: 10, // px
swipeTolerance: 30, // px
touchHoldTolerance: 400, // ms
longTapTimeInterval: 400, // ms
touchClass: '',
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) {
globalLastTouchTime = event.timeStamp;
}
if (isMouseEvent && globalLastTouchTime && event.timeStamp - globalLastTouchTime < 350) {
return;
}
if ($this.touchStarted) {
return;
}
addTouchClass(this);
$this.touchStarted = true;
$this.touchMoved = false;
$this.swipeOutBounded = false;
$this.startX = touchX(event);
$this.startY = touchY(event);
$this.currentX = 0;
$this.currentY = 0;
$this.touchStartTime = event.timeStamp;
// Trigger touchhold event after `touchHoldTolerance`ms
$this.touchHoldTimer = setTimeout(function() {
$this.touchHoldTimer = null;
triggerEvent(event, $el, 'touchhold');
}, $this.options.touchHoldTolerance);
triggerEvent(event, this, 'start');
}
function touchMoveEvent(event) {
var $this = this.$$touchObj;
$this.currentX = touchX(event);
$this.currentY = touchY(event);
if (!$this.touchMoved) {
var tapTolerance = $this.options.tapTolerance;
$this.touchMoved = Math.abs($this.startX - $this.currentX) > tapTolerance ||
Math.abs($this.startY - $this.currentY) > tapTolerance;
if($this.touchMoved){
cancelTouchHoldTimer($this);
triggerEvent(event, this, 'moved');
}
} else if (!$this.swipeOutBounded) {
var swipeOutBounded = $this.options.swipeTolerance;
$this.swipeOutBounded = Math.abs($this.startX - $this.currentX) > swipeOutBounded &&
Math.abs($this.startY - $this.currentY) > swipeOutBounded;
}
if($this.touchMoved){
triggerEvent(event, this, 'moving');
}
}
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) {
globalLastTouchTime = event.timeStamp;
}
var touchholdEnd = isTouchEvent && !$this.touchHoldTimer;
cancelTouchHoldTimer($this);
$this.touchStarted = false;
removeTouchClass(this);
if (isMouseEvent && globalLastTouchTime && event.timeStamp - globalLastTouchTime < 350) {
return;
}
// Fix #33, Trigger `end` event when touch stopped
triggerEvent(event, this, 'end');
if (!$this.touchMoved) {
// detect if this is a longTap event or not
if ($this.callbacks.longtap && event.timeStamp - $this.touchStartTime > $this.options.longTapTimeInterval) {
if (event.cancelable) {
event.preventDefault();
}
triggerEvent(event, this, 'longtap');
} else if ($this.callbacks.touchhold && touchholdEnd) {
if (event.cancelable) {
event.preventDefault();
}
return;
} else {
// emit tap event
triggerEvent(event, this, 'tap');
}
} else if (!$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 ($this.callbacks['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 triggerEvent(e, $el, eventType, param) {
var $this = $el.$$touchObj;
// get the callback list
var callbacks = $this && $this.callbacks[eventType] || [];
if (callbacks.length === 0) {
return null;
}
for (var i = 0; i < callbacks.length; i++) {
var binding = callbacks[i];
if (binding.modifiers.stop) {
e.stopPropagation();
}
if (binding.modifiers.prevent && e.cancelable) {
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.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;
}
Vue.directive(globalOptions.namespace, {
bind: 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 'start':
case 'moving':
if (binding.modifiers.disablePassive) {
// change the passive option for the moving event if disablePassive modifier exists
passiveOpt = false;
}
// fallthrough
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;
},
unbind: function ($el) {
$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;
}
});
Vue.directive(globalOptions.namespace + '-class', {
bind: function ($el, binding) {
buildTouchObj($el, {
touchClass: binding.value
});
}
});
Vue.directive(globalOptions.namespace + '-options', {
bind: function($el, binding) {
buildTouchObj($el, binding.value);
}
});
}
};
/*
* Exports
*/
if (typeof module === 'object') {
module.exports = vueTouchEvents;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return vueTouchEvents;
});
} else if (window.Vue) {
window.vueTouchEvents = vueTouchEvents;
Vue.use(vueTouchEvents);
}