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

Commit 6250b9b

Browse files
committed
Merge branch 'feature/convert_to_component' into next
# Conflicts: # README.md # example/example.js # example/index.html # package.json # vue-touch.min.js
2 parents 33b9d20 + eb0678e commit 6250b9b

32 files changed

+17867
-66
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
node_modules
22
.DS_Store
33
example/example.build.js
4+
example/vendor.build.js
5+
example/*.map
6+
npm-debug.log

README.md

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,122 @@
22

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

77
This version is only compatible with Vue 2.0.
88

99
## Install
1010

11-
#### CommonJS
11+
> This plugin requires Vue >= 2.0. For the Vue 1.\*-compatible version, see the `1.0` branch
12+
1213

1314
- Available through npm as `vue-touch`.
1415

15-
``` js
16-
var VueTouch = require('vue-touch')
17-
Vue.use(VueTouch)
18-
```
16+
```Javascript
17+
var VueTouch = require('vue-touch')
18+
Vue.use(VueTouch, {name: 'v-touch'})
19+
```
20+
You can pass an options object as the second argument, which at the moment accepts one property, `name`. It's used to define the name of the component that is registered with Vue and defaults to `'v-touch'`.
1921

2022
#### Direct include
2123

2224
- You can also directly include it with a `<script>` tag when you have Vue and Hammer.js already included globally. It will automatically install itself, and will add a global `VueTouch`.
2325

2426
## Usage
2527

26-
#### Using the `v-touch` directive
28+
#### Using the `<v-touch>` component
2729

2830
``` html
29-
<a v-touch:tap="onTap">Tap me!</a>
31+
<!-- Renders a div element by default -->
32+
<v-touch v-on:swipeleft="onSwipeLeft">Swipe me!</v-touch>
33+
34+
<!-- Render as other elements with the 'tag' prop -->
35+
<v-touch tag="a" v-on:tap="onTap">Tap me!</v-touch>
36+
```
37+
38+
## API
39+
40+
### Component Events
41+
42+
vue-touch supports all Hammer Events ot of the box, just bind a listener to the component with `v-on` and vue-touch will setup the Hammer Manager & Recognizer for you.
43+
44+
|Recognizer|Events|Example|
45+
|---|----|----|----|
46+
|**Pan**|`pan`, `panstart`, `panmove`, `panend`, `pancancel`, `panleft`, `panright`, `panup`, `pandown` |`v-on:panstart="callback"`|
47+
|**Pinch**|`pinch`, `pinchstart`, `pinchmove`,`pinchend`, `pinchcancel`, `pinchin`, `pinchout`| `v-on:pinchout="callback"`|
48+
|**Press**|`press`, `pressup`|`v-on:pressup="callback"`|
49+
|**Rotate**|`rotate`, `rotatestart`, `rotatemove`, `rotateend`, `rotatecancel`, |`v-on:rotateend="callback"`|
50+
|**Swipe**|`swipe`, `swipeleft`, `swiperight`, `swipeup`, `swipedown`|`v-on:swipeleft="callback"`|
51+
|**Tap**|`tap`|`v-on:tap="callback"`|
52+
53+
### Component Props
54+
55+
You can use the matching `*-options` props to pass Hammer options such as `direction` and `threshold`:
56+
57+
``` html
58+
<!-- detect only horizontal pans with a threshold of 100 -->
59+
<v-touch
60+
v-on:panstart="onPanStart"
61+
v-bind:pan-options="{ direction: 'horizontal', threshold: 100 }">
62+
</v-touch>
63+
```
64+
There's one prop per `Recognizer` available.
3065

31-
<div v-touch:swipeleft="onSwipeLeft">Swipe me!</div>
66+
|Recognizer|Prop|
67+
|----------|----|
68+
|**Pan**|`v-bind:pan-options`|
69+
|**Pinch**|`v-bind:pinch-options`|
70+
|**Rotate**|`v-bind:rotate-options`|
71+
|**Swipe**|`v-bind:swipe-options`|
72+
|**Tap**|`v-bind:tap-options`|
73+
74+
See [Hammer.js documentation](http://hammerjs.github.io/getting-started/) for all available options for events.
75+
76+
#### Directions
77+
78+
In the above example, not that we used `direction: 'horizontal'`. Hammer's directions interface is a little ugly (Hammer['DIRECTION_HORIZONTAL']).
79+
80+
VueTouch keeps that from you and accepts simple strings as directions:
81+
82+
```javascript
83+
const directions = ['up', 'down', 'left', 'right', 'horizontal', 'vertical', 'all']
3284
```
3385

34-
```js
35-
// add the callback method to the methods of our component:
36-
methods: {
37-
onSwipeLeft(event) {
38-
// event is a Hammer Event Object
86+
## Public Component Methods
87+
88+
The component exposes a few convenience methods to enable and disable Recognizers:
89+
90+
|Method|Explanation|
91+
|------|-----------|
92+
|`disable(event)`|disable the Recognizer for `event`|
93+
|`enable(event)`|disable the Recognizer for `event`|
94+
|`disableAll`|disable all Recognizers|
95+
|`enableAll`|enable all Recognizers|
96+
|`isEnabled(event)`|returns `true` if Recognizer for `event` is currently enabled|
97+
98+
```html
99+
<template>
100+
<v-touch ref="tapper" @tap="callback"></v-touch>
101+
</template>
102+
<script>
103+
export default {
104+
methods: {
105+
disableTap() {
106+
this.$refs.tapper.disable('tap')
107+
},
108+
enableTap() {
109+
this.$refs.tapper.enable('tap')
110+
}
111+
}
39112
}
40-
}
113+
</script>
41114
```
42115

43-
See the [Hammer.js API documentation for the Event Object](https://hammerjs.github.io/api/#event-object) to learn about all of the event object's properties
116+
### Plugin Methods
44117

45-
#### Configuring Global Recognizer Options
118+
#### Global Event Options
46119

47-
You can set global options for your recognizers like this:
120+
You can define global defaults for the builtin recognizers
48121

49122
``` js
50123
// change the threshold for all swipe recognizers
@@ -55,7 +128,7 @@ VueTouch.config.swipe = {
55128

56129
#### Registering Custom Events
57130

58-
If you want to use different options from the global ones for a Hammer event, you can create a custom event.
131+
You can register custom events with vue-touch.
59132

60133
``` js
61134
// example registering a custom doubletap event.
@@ -66,14 +139,28 @@ VueTouch.registerCustomEvent('doubletap', {
66139
taps: 2
67140
})
68141
```
142+
> **Warning**: You have to register your custom events *before* installing the plugin with `Vue.use(VueTouch)`.
143+
VueTouch will log a warning to the console (in dev mode) if you try to do that afterwards, and the event will not work.
144+
145+
This will make it possible to listen for this event on `<v-touch>`. Additionally, just like for "normal" events, you can pass further options as the corresponding prop.
146+
69147
``` html
70-
<a v-touch:doubletap="onDoubleTap"></a>
148+
<v-touch v-on:doubletap="onDoubleTap"></v-touch>
149+
<!-- with local options -->
150+
<v-touch v-on:doubletap="onDoubleTap" v-bind:doubletap-options="{intervall: 250}"></v-touch>
71151
```
72152

73-
See [Hammer.js documentation](http://hammerjs.github.io/getting-started/) for all available events.
74-
75153
See `/example` for a multi-event demo. To build it, run `npm install && npm run build`.
76154

155+
## Known Limitations & Bugs
156+
157+
* Curently, changing `-options` props will not change recogizer settings. The initial values will stay in place until the component is re-created.
158+
159+
## TODO
160+
161+
* [ ] Support updating recognizer options when props change.
162+
* [ ] Find out if e2e tests are possible(contribution welcome)
163+
77164
## License
78165

79166
[MIT](http://opensource.org/licenses/MIT)

TODO.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
* [x] refactor HammerJS out of this so it can be an external dependency passed into the plugin via options
2+
* [x] Improve Test cases in /examples/index.html
3+
* [x] Test prod build process with rollup
4+
* [x] Add test run with prod build
5+
* [x] clean up package.json
6+
* [x] add unit tests
7+
* [x] Write README docs
8+
* [ ] Write Release notes
9+
* [ ] create 1.0 Branch for old version
10+
* [x] include assign polyfill
11+
* [x] create function to define props to reduce size.
12+
* [ ] use DefinePlugin / ReplacePlugin to get rid of warnings in production.
13+
* [x] add public Methods `isEnabled(event)`

build/devserver.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var webpack = require('webpack')
2+
var DevServer = require('webpack-dev-server')
3+
var config = require('./webpack.config.dev.js')
4+
config.entry.example.unshift("webpack-dev-server/client?http://localhost:8080/", "webpack/hot/dev-server");
5+
var compiler = webpack(config)
6+
var server = new DevServer(compiler, {
7+
contentBase: './example/',
8+
clientLogLevel: 'warning',
9+
hot: true,
10+
})
11+
12+
server.listen(8080)
13+
14+
module.exports = server

build/rollup.config.prod.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import buble from 'rollup-plugin-buble'
2+
import commonjs from 'rollup-plugin-commonjs'
3+
import nodeResolve from 'rollup-plugin-node-resolve'
4+
import cleanup from 'rollup-plugin-cleanup'
5+
6+
export default {
7+
entry: './src/index.js',
8+
dest: './dist/vue-touch.js',
9+
// Module settings
10+
format: 'umd',
11+
external: ['hammerjs'],
12+
globals: {
13+
hammerjs: 'Hammer'
14+
},
15+
moduleName: 'VueTouch',
16+
17+
plugins: [
18+
buble(),
19+
nodeResolve({ jsnext: true, main: true }),
20+
commonjs(),
21+
cleanup()
22+
]
23+
}

build/webpack.config.dev.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var path = require('path')
2+
var webpack = require('webpack')
3+
4+
const config = {
5+
entry: {
6+
example: ['./example/example.js'],
7+
vendor: ['vue', 'hammerjs'],
8+
},
9+
output: {
10+
path: path.resolve(__dirname, "../example"),
11+
publicPath: '/',
12+
library: 'VueTouch',
13+
libraryTarget: 'umd',
14+
filename: '[name].build.js'
15+
},
16+
resolve: {
17+
alias: {
18+
'vue$': 'vue/dist/vue.common'
19+
}
20+
},
21+
module: {
22+
rules: [
23+
{
24+
test: /\.js$/,
25+
loader: 'buble-loader',
26+
exclude: path.resolve(__dirname, "../node_modules")
27+
},
28+
{
29+
test: /\.css$/,
30+
loader: 'style-loader!css-loader',
31+
exclude: path.resolve(__dirname, "../node_modules")
32+
}
33+
]
34+
},
35+
plugins: [
36+
new webpack.HotModuleReplacementPlugin(),
37+
new webpack.DefinePlugin({
38+
'process.env': {
39+
NODE_ENV: process.env.NODE_ENV ? JSON.stringify(process.env.NODE_ENV) : "'development'"
40+
}
41+
}),
42+
new webpack.optimize.CommonsChunkPlugin({
43+
name: 'vendor'
44+
})
45+
],
46+
devtool: 'source-map',
47+
performance: {
48+
hints: false
49+
},
50+
/*devServer: {
51+
contentBase: '/example/'
52+
}*/
53+
}
54+
55+
module.exports = config

0 commit comments

Comments
 (0)