forked from jerrybendy/vue-touch-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
290 lines (240 loc) · 8.1 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
/**
*
* @author Jerry Bendy
* @since 4/12/2017
* @fix softart
*/
function touchX (event) {
return event.touches[0].clientX
}
function touchY (event) {
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 (Vue, options) {
// Set default options
options = Object.assign({}, {
disableClick: false,
tapTolerance: 10,
swipeTolerance: 30,
longTapTimeInterval: 400,
touchClass: ''
}, options || {})
function touchStartEvent (event) {
var $this = this.$$touchObj
$this.supportTouch = true
if ($this.touchStarted) {
return
}
clearTimeout($this.longtapTimeout)
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
$this.longtapTimeout = setTimeout(
triggerEvent.bind(this, event, this, 'longtap'),
options.longTapTimeInterval
)
}
function touchMoveEvent (event) {
var $this = this.$$touchObj
$this.currentX = touchX(event)
$this.currentY = touchY(event)
if (!$this.touchMoved) {
var tapTolerance = options.tapTolerance
$this.touchMoved = Math.abs($this.startX - $this.currentX) > tapTolerance ||
Math.abs($this.startY - $this.currentY) > tapTolerance
if ($this.touchMoved) {
clearTimeout($this.longtapTimeout)
}
} else if (!$this.swipeOutBounded) {
var swipeOutBounded = options.swipeTolerance
$this.swipeOutBounded = Math.abs($this.startX - $this.currentX) > swipeOutBounded &&
Math.abs($this.startY - $this.currentY) > swipeOutBounded
}
}
function touchCancelEvent () {
var $this = this.$$touchObj
removeTouchClass(this)
clearTimeout($this.longTapTimeInterval)
$this.touchStarted = $this.touchMoved = false
$this.startX = $this.startY = 0
}
function touchEndEvent (event) {
var $this = this.$$touchObj
$this.touchStarted = false
removeTouchClass(this)
clearTimeout($this.longTapTimeInterval)
if (!$this.touchMoved) {
// detect if this is a longTap event or not
if ($this.callbacks.longtap && event.timeStamp - $this.touchStartTime > options.longTapTimeInterval) {
event.preventDefault()
triggerEvent(event, this, 'longtap')
} else {
// emit tap event
triggerEvent(event, this, 'tap')
}
} else if (!$this.swipeOutBounded) {
var swipeOutBounded = options.swipeTolerance, direction
if (Math.abs($this.startX - $this.currentX) < swipeOutBounded) {
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 clickEvent (event) {
var $this = this.$$touchObj
clearTimeout($this.longtapTimeout)
if (!$this.supportTouch && !options.disableClick) {
triggerEvent(event, this, 'tap')
}
}
function mouseEnterEvent () {
addTouchClass(this)
}
function mouseLeaveEvent () {
removeTouchClass(this)
}
function triggerEvent (e, $el, eventType, param) {
var $this = $el.$$touchObj
clearTimeout($this.longtapTimeout)
// get the callback list
var callbacks = $this.callbacks[eventType] || []
if (callbacks.length === 0) {
return null
}
for (var i = 0; i < callbacks.length; i++) {
var binding = callbacks[i]
// 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.$$touchClass || options.touchClass
className && $el.classList.add(className)
}
function removeTouchClass ($el) {
var className = $el.$$touchClass || options.touchClass
className && $el.classList.remove(className)
}
Vue.directive('touch', {
bind: function ($el, binding) {
$el.$$touchObj = $el.$$touchObj || {
// will change to true when `touchstart` event first trigger
supportTouch: false,
// 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
}
// 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
$el.$$touchObj.callbacks[_e] = $el.$$touchObj.callbacks[_e] || []
$el.$$touchObj.callbacks[_e].push(binding)
}
}
} else {
$el.$$touchObj.callbacks.swipe = $el.$$touchObj.callbacks.swipe || []
$el.$$touchObj.callbacks.swipe.push(binding)
}
break
default:
$el.$$touchObj.callbacks[eventType] = $el.$$touchObj.callbacks[eventType] || []
$el.$$touchObj.callbacks[eventType].push(binding)
}
// prevent bind twice
if ($el.$$touchObj.hasBindTouchEvents) {
return
}
var passiveOpt = isPassiveSupported ? { passive: true } : false
$el.addEventListener('touchstart', touchStartEvent, passiveOpt)
$el.addEventListener('touchmove', touchMoveEvent, passiveOpt)
$el.addEventListener('touchcancel', touchCancelEvent)
$el.addEventListener('touchend', touchEndEvent)
if (!options.disableClick) {
$el.addEventListener('click', clickEvent)
$el.addEventListener('mouseenter', mouseEnterEvent)
$el.addEventListener('mouseleave', mouseLeaveEvent)
}
// set bind mark to true
$el.$$touchObj.hasBindTouchEvents = true
},
unbind: function ($el) {
$el.removeEventListener('touchstart', touchStartEvent)
$el.removeEventListener('touchmove', touchMoveEvent)
$el.removeEventListener('touchcancel', touchCancelEvent)
$el.removeEventListener('touchend', touchEndEvent)
if (!options.disableClick) {
$el.removeEventListener('click', clickEvent)
$el.removeEventListener('mouseenter', mouseEnterEvent)
$el.removeEventListener('mouseleave', mouseLeaveEvent)
}
// remove vars
delete $el.$$touchObj
}
})
Vue.directive('touch-class', {
bind: function ($el, binding) {
$el.$$touchClass = binding.value
},
unbind: function ($el) {
delete $el.$$touchClass
}
})
}
}
/*
* 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)
}