Skip to content
This repository was archived by the owner on Dec 26, 2018. It is now read-only.

Commit 20f11b9

Browse files
author
Evan You
committed
use Hammer 2.0
1 parent 86413f2 commit 20f11b9

File tree

7 files changed

+150
-27
lines changed

7 files changed

+150
-27
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
components
22
node_modules
3-
.DS_Store
3+
.DS_Store
4+
example/example.build.js

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Touch events plugin for Vue.js
44
5-
This is simply a directive wrapper for Hammer.js.
5+
This is a directive wrapper for Hammer.js 2.0.
66

77
## Usage
88

@@ -17,6 +17,10 @@ Vue.use(vueTouch)
1717
<a v-touch="tap:onTap">Tap me!</a>
1818
```
1919

20+
See [Hammer.js documentation](http://hammerjs.github.io/getting-started.html) for all available events.
21+
22+
See `/example` for listening to multiple events together and how to register a custom event like `'doubletap'`. To build the example, you need to have Browserify installed and then `npm run build`.
23+
2024
## License
2125

2226
MIT

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "index.js",
55
"scripts": ["index.js"],
66
"dependencies": {
7-
"EightMedia/hammer.js": "*"
7+
"hammerjs/hammer.js": "^2.0.1"
88
},
99
"development": {
1010
"yyx990803/vue": "*"

example/example.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var Vue = require('vue')
2+
var VueTouch = require('../')
3+
4+
// use the plugin
5+
Vue.use(VueTouch)
6+
7+
// example registering a custom doubletap event.
8+
// the `type` indicates the base recognizer to use from Hammer
9+
// all other options are Hammer recognizer options.
10+
VueTouch.registerCustomEvent('doubletap', {
11+
type: 'tap',
12+
taps: 2
13+
})
14+
15+
new Vue({
16+
el: 'div',
17+
data: {
18+
event: ''
19+
},
20+
methods: {
21+
test: function (e) {
22+
this.event = e.type
23+
}
24+
}
25+
})

example/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<style>
2+
div {
3+
width: 300px;
4+
height: 300px;
5+
background-color: #f00;
6+
}
7+
</style>
8+
9+
<div v-touch="
10+
tap:test,
11+
pan:test,
12+
press:test,
13+
swipe:test,
14+
pinch:test,
15+
rotate:test,
16+
doubletap: test
17+
" v-text="event"></div>
18+
19+
<script src="example.build.js"></script>

index.js

Lines changed: 90 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,95 @@
11
var Hammer = require('hammerjs')
2+
var gestures = ['tap', 'pan', 'pinch', 'press', 'rotate', 'swipe']
3+
var customeEvents = {}
24

35
exports.install = function (Vue) {
4-
Vue.directive('touch', {
5-
isFn: true,
6-
bind: function () {
7-
if (!this.el.hammer) {
8-
this.el.hammer = Hammer(this.el)
9-
}
10-
},
11-
update: function (fn) {
12-
var vm = this.vm
13-
this.handler = function (e) {
14-
e.targetVM = vm
15-
fn.call(vm, e)
16-
}
17-
this.el.hammer.on(this.arg, this.handler)
18-
},
19-
unbind: function () {
20-
this.el.hammer.off(this.arg, this.handler)
21-
if (!this.el.hammer.eventHandlers.length) {
22-
this.el.hammer.dispose()
23-
this.el.hammer = null
24-
}
6+
7+
Vue.directive('touch', {
8+
9+
isFn: true,
10+
11+
bind: function () {
12+
if (!this.el.hammer) {
13+
this.el.hammer = new Hammer.Manager(this.el)
14+
}
15+
var mc = this.mc = this.el.hammer
16+
// determine event type
17+
var event = this.arg
18+
var recognizerType, recognizer
19+
20+
if (customeEvents[event]) { // custom event
21+
22+
var custom = customeEvents[event]
23+
recognizerType = custom.type
24+
recognizer = new Hammer[capitalize(recognizerType)](custom)
25+
recognizer.recognizeWith(mc.recognizers)
26+
mc.add(recognizer)
27+
28+
} else { // built-in event
29+
30+
for (var i = 0; i < gestures.length; i++) {
31+
if (event.indexOf(gestures[i]) === 0) {
32+
recognizerType = gestures[i]
33+
break
34+
}
35+
}
36+
if (!recognizerType) {
37+
console.warn('Invalid v-touch event: ' + event)
38+
return
39+
}
40+
recognizer = mc.get(recognizerType)
41+
if (!recognizer) {
42+
// add recognizer
43+
recognizer = new Hammer[capitalize(recognizerType)]()
44+
// make sure multiple recognizers work together...
45+
recognizer.recognizeWith(mc.recognizers)
46+
mc.add(recognizer)
2547
}
26-
})
48+
49+
}
50+
},
51+
52+
update: function (fn) {
53+
var mc = this.mc
54+
var vm = this.vm
55+
var event = this.arg
56+
// teardown old handler
57+
if (this.handler) {
58+
mc.off(event, this.handler)
59+
}
60+
// define new handler
61+
this.handler = function (e) {
62+
e.targetVM = vm
63+
fn.call(vm, e)
64+
}
65+
mc.on(event, this.handler)
66+
},
67+
68+
unbind: function () {
69+
this.mc.off(this.arg, this.handler)
70+
if (!this.mc.handlers.length) {
71+
this.mc.destroy()
72+
this.el.hammer = null
73+
}
74+
}
75+
76+
})
2777
}
78+
79+
/**
80+
* Register a custom event.
81+
*
82+
* @param {String} event
83+
* @param {Object} options - a Hammer.js recognizer option object.
84+
* required fields:
85+
* - type: the base recognizer to use for this event
86+
*/
87+
88+
exports.registerCustomEvent = function (event, options) {
89+
options.event = event
90+
customeEvents[event] = options
91+
}
92+
93+
function capitalize (str) {
94+
return str.charAt(0).toUpperCase() + str.slice(1)
95+
}

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
{
22
"name": "vue-touch",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"main": "index.js",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/vuejs/vue-touch.git"
88
},
99
"bugs": "https://github.com/vuejs/vue-touch/issues",
1010
"dependencies": {
11-
"hammerjs": "~1.1.3"
11+
"hammerjs": "^2.0.1"
12+
},
13+
"devDependencies": {
14+
"vue": "^0.10.5"
15+
},
16+
"scripts": {
17+
"build": "browserify -e example/example.js -o example/example.build.js"
1218
}
1319
}

0 commit comments

Comments
 (0)