Skip to content

Commit 3bdd4c9

Browse files
committed
Add v-touch-options directive #52
1 parent fd3711e commit 3bdd4c9

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Features:
1010
* Optimized touch effects with `touchClass` option and `v-touch-class` directive
1111
* Binding multiple touch events on one DOM element
1212
* Customizable events with native-likely events handler
13+
* Allow splitting configurations for different DOM elements by `v-touch-options` directive
1314

1415

1516
## Install
@@ -68,12 +69,22 @@ In your `.vue` file:
6869
v-touch:start="startHandler"
6970
v-touch:end="endHandler"
7071
v-touch:swipe.right="swipeRightHandler">Mix Multiple Events</span>
72+
73+
<!-- using different options for specified element -->
74+
<span v-touch:tap="tapHandler"
75+
v-touch-options="{touchClass: 'active', swipeTolerance: 80, touchHoldTolerance: 300}">Different options</span>
76+
77+
<!-- customize touch effects by CSS class -->
78+
<span v-touch:tap="tapHandler" v-touch-class="active">Customize touch class</span>
79+
<!-- or -->
80+
<span v-touch:tap="tapHandler" v-touch-options="{touchClass: 'active'}">Customize touch class</span>
81+
7182
```
7283

7384

7485
## APIs
7586

76-
### Global config (optional)
87+
### Global configuration (optional)
7788

7889
```js
7990
Vue.use(Vue2TouchEvents, {
@@ -126,6 +137,10 @@ export default {
126137
}
127138
```
128139

140+
### v-touch-options
141+
142+
`v-touch-options` directive allows you set a different configuration for a specified component. It will override global configurations.
143+
129144
#### v-touch-class
130145

131146
`v-touch-class` directive allows you set an extra class on your components. If you already have a global config `touchClass`, this value will **overwrite** it.

index.js

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@ var isPassiveSupported = (function() {
2929
window.addEventListener('test', null, opts);
3030
} catch (e) {}
3131
return supportsPassive;
32-
})()
32+
})();
3333

3434

3535
var vueTouchEvents = {
36-
install: function (Vue, options) {
36+
install: function (Vue, constructorOptions) {
3737

38-
// Set default options
39-
options = Object.assign({}, {
38+
var globalOptions = Object.assign({}, {
4039
disableClick: false,
4140
tapTolerance: 10, // px
4241
swipeTolerance: 30, // px
4342
touchHoldTolerance: 400, // ms
4443
longTapTimeInterval: 400, // ms
4544
touchClass: ''
46-
}, options || {});
47-
45+
}, constructorOptions);
4846

4947
function touchStartEvent(event) {
5048
var $this = this.$$touchObj,
@@ -82,7 +80,7 @@ var vueTouchEvents = {
8280
// Trigger touchhold event after `touchHoldTolerance`ms
8381
$this.touchHoldTimer = setTimeout(function() {
8482
triggerEvent(event, $el, 'touchhold');
85-
}, options.touchHoldTolerance);
83+
}, $this.options.touchHoldTolerance);
8684

8785
triggerEvent(event, this, 'start');
8886
}
@@ -94,7 +92,7 @@ var vueTouchEvents = {
9492
$this.currentY = touchY(event);
9593

9694
if (!$this.touchMoved) {
97-
var tapTolerance = options.tapTolerance;
95+
var tapTolerance = $this.options.tapTolerance;
9896

9997
$this.touchMoved = Math.abs($this.startX - $this.currentX) > tapTolerance ||
10098
Math.abs($this.startY - $this.currentY) > tapTolerance;
@@ -105,7 +103,7 @@ var vueTouchEvents = {
105103
}
106104

107105
} else if (!$this.swipeOutBounded) {
108-
var swipeOutBounded = options.swipeTolerance;
106+
var swipeOutBounded = $this.options.swipeTolerance;
109107

110108
$this.swipeOutBounded = Math.abs($this.startX - $this.currentX) > swipeOutBounded &&
111109
Math.abs($this.startY - $this.currentY) > swipeOutBounded;
@@ -150,7 +148,7 @@ var vueTouchEvents = {
150148

151149
if (!$this.touchMoved) {
152150
// 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) {
154152
event.preventDefault();
155153
triggerEvent(event, this, 'longtap');
156154

@@ -160,7 +158,7 @@ var vueTouchEvents = {
160158
}
161159

162160
} else if (!$this.swipeOutBounded) {
163-
var swipeOutBounded = options.swipeTolerance,
161+
var swipeOutBounded = $this.options.swipeTolerance,
164162
direction;
165163

166164
if (Math.abs($this.startX - $this.currentX) < swipeOutBounded) {
@@ -225,12 +223,12 @@ var vueTouchEvents = {
225223
}
226224

227225
function addTouchClass($el) {
228-
var className = $el.$$touchClass || options.touchClass;
226+
var className = $el.$$touchObj.options.touchClass;
229227
className && $el.classList.add(className);
230228
}
231229

232230
function removeTouchClass($el) {
233-
var className = $el.$$touchClass || options.touchClass;
231+
var className = $el.$$touchObj.options.touchClass;
234232
className && $el.classList.remove(className);
235233
}
236234

@@ -241,17 +239,27 @@ var vueTouchEvents = {
241239
}
242240
}
243241

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+
244259
Vue.directive('touch', {
245260
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);
255263

256264
// register callback
257265
var eventType = binding.arg || 'tap';
@@ -262,23 +270,23 @@ var vueTouchEvents = {
262270
for (var i in binding.modifiers) {
263271
if (['left', 'right', 'top', 'bottom'].indexOf(i) >= 0) {
264272
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);
267275
}
268276
}
269277
} 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);
272280
}
273281
break;
274282

275283
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);
278286
}
279287

280288
// prevent bind twice
281-
if ($el.$$touchObj.hasBindTouchEvents) {
289+
if ($this.hasBindTouchEvents) {
282290
return;
283291
}
284292

@@ -288,7 +296,7 @@ var vueTouchEvents = {
288296
$el.addEventListener('touchcancel', touchCancelEvent);
289297
$el.addEventListener('touchend', touchEndEvent);
290298

291-
if (!options.disableClick) {
299+
if (!$this.options.disableClick) {
292300
$el.addEventListener('mousedown', touchStartEvent);
293301
$el.addEventListener('mousemove', touchMoveEvent);
294302
$el.addEventListener('mouseup', touchEndEvent);
@@ -297,7 +305,7 @@ var vueTouchEvents = {
297305
}
298306

299307
// set bind mark to true
300-
$el.$$touchObj.hasBindTouchEvents = true;
308+
$this.hasBindTouchEvents = true;
301309
},
302310

303311
unbind: function ($el) {
@@ -306,7 +314,7 @@ var vueTouchEvents = {
306314
$el.removeEventListener('touchcancel', touchCancelEvent);
307315
$el.removeEventListener('touchend', touchEndEvent);
308316

309-
if (!options.disableClick) {
317+
if (!$el.$$touchObj.options.disableClick) {
310318
$el.removeEventListener('mousedown', touchStartEvent);
311319
$el.removeEventListener('mousemove', touchMoveEvent);
312320
$el.removeEventListener('mouseup', touchEndEvent);
@@ -321,10 +329,15 @@ var vueTouchEvents = {
321329

322330
Vue.directive('touch-class', {
323331
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);
328341
}
329342
});
330343
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue2-touch-events",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Simple touch events support for vueJS2",
55
"main": "index.js",
66
"types": "index.d.ts",

0 commit comments

Comments
 (0)