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

Commit 1262bc9

Browse files
author
Evan You
committed
0.1.0: umd, +bower
1 parent b18ba88 commit 1262bc9

File tree

6 files changed

+165
-107
lines changed

6 files changed

+165
-107
lines changed

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,42 @@ This is a directive wrapper for Hammer.js 2.0.
66

77
## Usage
88

9-
Currently only available via Component or Browserify. For other use cases just take a look at the source. It's really simple.
9+
### CommonJS
1010

11-
``` js
12-
var vueTouch = require('vue-touch')
13-
Vue.use(vueTouch)
14-
```
11+
- Available through npm as `vue-touch`. For Duo/Component you can isntall as `vuejs/vue-touch`.
12+
13+
``` js
14+
var vueTouch = require('vue-touch')
15+
Vue.use(vueTouch)
16+
```
17+
18+
### Direct include
19+
20+
- You can also directly include it with a `<script>` tag when you have Vue itself included globally. It will automatically install itself, and will add a global `VueTouch`.
21+
22+
### Use in templates
23+
24+
Then you can do this:
1525

1626
``` html
1727
<a v-touch="tap:onTap">Tap me!</a>
1828
```
1929

30+
### Register a custom event
31+
32+
``` js
33+
// example registering a custom doubletap event.
34+
// the `type` indicates the base recognizer to use from Hammer
35+
// all other options are Hammer recognizer options.
36+
VueTouch.registerCustomEvent('doubletap', {
37+
type: 'tap',
38+
taps: 2
39+
})
40+
```
41+
2042
See [Hammer.js documentation](http://hammerjs.github.io/getting-started.html) for all available events.
2143

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`.
44+
See `/example` for a multi-event demo. To build the example, you need to have Browserify installed and then `npm run build`.
2345

2446
## License
2547

bower.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "vue-touch",
3+
"version": "0.1.0",
4+
"main": "vue-touch.js",
5+
"description": "Hammer.js based touch events plugin for Vue.js",
6+
"authors": ["Evan You <[email protected]>"],
7+
"license": "MIT",
8+
"dependencies": {
9+
"hammerjs": "^2.0.1"
10+
},
11+
"ignore": [
12+
".*",
13+
"example",
14+
"*.json",
15+
"*.md"
16+
]
17+
}

component.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"name": "vue-touch",
3-
"version": "0.0.1",
4-
"main": "index.js",
5-
"scripts": ["index.js"],
3+
"version": "0.1.0",
4+
"main": "vue-touch.js",
5+
"description": "Hammer.js based touch events plugin for Vue.js",
6+
"license": "MIT",
7+
"scripts": ["vue-touch.js"],
68
"dependencies": {
79
"hammerjs/hammer.js": "^2.0.1"
810
},

index.js

Lines changed: 0 additions & 95 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "vue-touch",
3-
"version": "0.0.2",
4-
"main": "index.js",
3+
"version": "0.1.0",
4+
"main": "vue-touch.js",
5+
"description": "Hammer.js based touch events plugin for Vue.js",
6+
"license": "MIT",
57
"repository": {
68
"type": "git",
79
"url": "https://github.com/vuejs/vue-touch.git"
@@ -14,6 +16,7 @@
1416
"vue": "^0.10.5"
1517
},
1618
"scripts": {
17-
"build": "browserify -e example/example.js -o example/example.build.js"
19+
"build": "browserify -e example/example.js -o example/example.build.js",
20+
"dev": "watchify -e example/example.js -o example/example.build.js"
1821
}
1922
}

vue-touch.js

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

0 commit comments

Comments
 (0)