@@ -29,22 +29,20 @@ var isPassiveSupported = (function() {
29
29
window . addEventListener ( 'test' , null , opts ) ;
30
30
} catch ( e ) { }
31
31
return supportsPassive ;
32
- } ) ( )
32
+ } ) ( ) ;
33
33
34
34
35
35
var vueTouchEvents = {
36
- install : function ( Vue , options ) {
36
+ install : function ( Vue , constructorOptions ) {
37
37
38
- // Set default options
39
- options = Object . assign ( { } , {
38
+ var globalOptions = Object . assign ( { } , {
40
39
disableClick : false ,
41
40
tapTolerance : 10 , // px
42
41
swipeTolerance : 30 , // px
43
42
touchHoldTolerance : 400 , // ms
44
43
longTapTimeInterval : 400 , // ms
45
44
touchClass : ''
46
- } , options || { } ) ;
47
-
45
+ } , constructorOptions ) ;
48
46
49
47
function touchStartEvent ( event ) {
50
48
var $this = this . $$touchObj ,
@@ -82,7 +80,7 @@ var vueTouchEvents = {
82
80
// Trigger touchhold event after `touchHoldTolerance`ms
83
81
$this . touchHoldTimer = setTimeout ( function ( ) {
84
82
triggerEvent ( event , $el , 'touchhold' ) ;
85
- } , options . touchHoldTolerance ) ;
83
+ } , $this . options . touchHoldTolerance ) ;
86
84
87
85
triggerEvent ( event , this , 'start' ) ;
88
86
}
@@ -94,7 +92,7 @@ var vueTouchEvents = {
94
92
$this . currentY = touchY ( event ) ;
95
93
96
94
if ( ! $this . touchMoved ) {
97
- var tapTolerance = options . tapTolerance ;
95
+ var tapTolerance = $this . options . tapTolerance ;
98
96
99
97
$this . touchMoved = Math . abs ( $this . startX - $this . currentX ) > tapTolerance ||
100
98
Math . abs ( $this . startY - $this . currentY ) > tapTolerance ;
@@ -105,7 +103,7 @@ var vueTouchEvents = {
105
103
}
106
104
107
105
} else if ( ! $this . swipeOutBounded ) {
108
- var swipeOutBounded = options . swipeTolerance ;
106
+ var swipeOutBounded = $this . options . swipeTolerance ;
109
107
110
108
$this . swipeOutBounded = Math . abs ( $this . startX - $this . currentX ) > swipeOutBounded &&
111
109
Math . abs ( $this . startY - $this . currentY ) > swipeOutBounded ;
@@ -150,7 +148,7 @@ var vueTouchEvents = {
150
148
151
149
if ( ! $this . touchMoved ) {
152
150
// detect if this is a longTap event or not
153
- if ( $this . callbacks . longtap && event . timeStamp - $this . touchStartTime > options . longTapTimeInterval ) {
151
+ if ( $this . callbacks . longtap && event . timeStamp - $this . touchStartTime > $this . options . longTapTimeInterval ) {
154
152
event . preventDefault ( ) ;
155
153
triggerEvent ( event , this , 'longtap' ) ;
156
154
@@ -160,7 +158,7 @@ var vueTouchEvents = {
160
158
}
161
159
162
160
} else if ( ! $this . swipeOutBounded ) {
163
- var swipeOutBounded = options . swipeTolerance ,
161
+ var swipeOutBounded = $this . options . swipeTolerance ,
164
162
direction ;
165
163
166
164
if ( Math . abs ( $this . startX - $this . currentX ) < swipeOutBounded ) {
@@ -225,12 +223,12 @@ var vueTouchEvents = {
225
223
}
226
224
227
225
function addTouchClass ( $el ) {
228
- var className = $el . $$touchClass || options . touchClass ;
226
+ var className = $el . $$touchObj . options . touchClass ;
229
227
className && $el . classList . add ( className ) ;
230
228
}
231
229
232
230
function removeTouchClass ( $el ) {
233
- var className = $el . $$touchClass || options . touchClass ;
231
+ var className = $el . $$touchObj . options . touchClass ;
234
232
className && $el . classList . remove ( className ) ;
235
233
}
236
234
@@ -241,17 +239,27 @@ var vueTouchEvents = {
241
239
}
242
240
}
243
241
242
+ function buildTouchObj ( $el , extraOptions ) {
243
+ var touchObj = $el . $$touchObj || {
244
+ // an object contains all callbacks registered,
245
+ // key is event name, value is an array
246
+ callbacks : { } ,
247
+ // prevent bind twice, set to true when event bound
248
+ hasBindTouchEvents : false ,
249
+ // default options, would be override by v-touch-options
250
+ options : globalOptions
251
+ } ;
252
+ if ( extraOptions ) {
253
+ touchObj . options = Object . assign ( { } , touchObj . options , extraOptions ) ;
254
+ }
255
+ $el . $$touchObj = touchObj ;
256
+ return $el . $$touchObj ;
257
+ }
258
+
244
259
Vue . directive ( 'touch' , {
245
260
bind : function ( $el , binding ) {
246
-
247
- $el . $$touchObj = $el . $$touchObj || {
248
- // an object contains all callbacks registered,
249
- // key is event name, value is an array
250
- callbacks : { } ,
251
- // prevent bind twice, set to true when event bound
252
- hasBindTouchEvents : false
253
- } ;
254
-
261
+ // build a touch configuration object
262
+ var $this = buildTouchObj ( $el ) ;
255
263
256
264
// register callback
257
265
var eventType = binding . arg || 'tap' ;
@@ -262,23 +270,23 @@ var vueTouchEvents = {
262
270
for ( var i in binding . modifiers ) {
263
271
if ( [ 'left' , 'right' , 'top' , 'bottom' ] . indexOf ( i ) >= 0 ) {
264
272
var _e = 'swipe.' + i ;
265
- $el . $$touchObj . callbacks [ _e ] = $el . $$touchObj . callbacks [ _e ] || [ ] ;
266
- $el . $$touchObj . callbacks [ _e ] . push ( binding ) ;
273
+ $this . callbacks [ _e ] = $this . callbacks [ _e ] || [ ] ;
274
+ $this . callbacks [ _e ] . push ( binding ) ;
267
275
}
268
276
}
269
277
} else {
270
- $el . $$touchObj . callbacks . swipe = $el . $$touchObj . callbacks . swipe || [ ] ;
271
- $el . $$touchObj . callbacks . swipe . push ( binding ) ;
278
+ $this . callbacks . swipe = $this . callbacks . swipe || [ ] ;
279
+ $this . callbacks . swipe . push ( binding ) ;
272
280
}
273
281
break ;
274
282
275
283
default :
276
- $el . $$touchObj . callbacks [ eventType ] = $el . $$touchObj . callbacks [ eventType ] || [ ] ;
277
- $el . $$touchObj . callbacks [ eventType ] . push ( binding ) ;
284
+ $this . callbacks [ eventType ] = $this . callbacks [ eventType ] || [ ] ;
285
+ $this . callbacks [ eventType ] . push ( binding ) ;
278
286
}
279
287
280
288
// prevent bind twice
281
- if ( $el . $$touchObj . hasBindTouchEvents ) {
289
+ if ( $this . hasBindTouchEvents ) {
282
290
return ;
283
291
}
284
292
@@ -288,7 +296,7 @@ var vueTouchEvents = {
288
296
$el . addEventListener ( 'touchcancel' , touchCancelEvent ) ;
289
297
$el . addEventListener ( 'touchend' , touchEndEvent ) ;
290
298
291
- if ( ! options . disableClick ) {
299
+ if ( ! $this . options . disableClick ) {
292
300
$el . addEventListener ( 'mousedown' , touchStartEvent ) ;
293
301
$el . addEventListener ( 'mousemove' , touchMoveEvent ) ;
294
302
$el . addEventListener ( 'mouseup' , touchEndEvent ) ;
@@ -297,7 +305,7 @@ var vueTouchEvents = {
297
305
}
298
306
299
307
// set bind mark to true
300
- $el . $$touchObj . hasBindTouchEvents = true ;
308
+ $this . hasBindTouchEvents = true ;
301
309
} ,
302
310
303
311
unbind : function ( $el ) {
@@ -306,7 +314,7 @@ var vueTouchEvents = {
306
314
$el . removeEventListener ( 'touchcancel' , touchCancelEvent ) ;
307
315
$el . removeEventListener ( 'touchend' , touchEndEvent ) ;
308
316
309
- if ( ! options . disableClick ) {
317
+ if ( ! $el . $$touchObj . options . disableClick ) {
310
318
$el . removeEventListener ( 'mousedown' , touchStartEvent ) ;
311
319
$el . removeEventListener ( 'mousemove' , touchMoveEvent ) ;
312
320
$el . removeEventListener ( 'mouseup' , touchEndEvent ) ;
@@ -321,10 +329,15 @@ var vueTouchEvents = {
321
329
322
330
Vue . directive ( 'touch-class' , {
323
331
bind : function ( $el , binding ) {
324
- $el . $$touchClass = binding . value ;
325
- } ,
326
- unbind : function ( $el ) {
327
- delete $el . $$touchClass ;
332
+ buildTouchObj ( $el , {
333
+ touchClass : binding . value
334
+ } ) ;
335
+ }
336
+ } ) ;
337
+
338
+ Vue . directive ( 'touch-options' , {
339
+ bind : function ( $el , binding ) {
340
+ buildTouchObj ( $el , binding . value ) ;
328
341
}
329
342
} ) ;
330
343
}
0 commit comments