Skip to content

Commit 2e1fa2f

Browse files
committed
Published 4.0.2: Linted code, clarified README.
1 parent 304032a commit 2e1fa2f

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

.versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
22
33
4-
chriswessels:[email protected].1
4+
chriswessels:[email protected].2
55
66
77

README.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A smart-package for integrating with [Hammer.js](https://github.com/hammerjs/hammer.js) for multi-touch gesture support in your Templates.
44

5-
*From version `4.0.0` onwards, there is a new API. For help migrating your existing implementation over to the new API, see [MIGRATION.md](https://github.com/chriswessels/meteor-hammer/blob/master/MIGRATION.md).*
5+
Out of the box it supports Panning, Pinching, (Long) Pressing, Rotating, Swiping and Tapping. You can also register and use custom compound gestures.
66

77
## Basic Usage
88

@@ -24,7 +24,7 @@ Template.foo.helpers({
2424
'swipeleft ul li': function (event, templateInstance) {
2525
/* `event` is the Hammer.js event object */
2626
/* `templateInstance` is the `Blaze.TemplateInstance` */
27-
/* `this` is the data context of the element in your template, so in this case the iteree from someArray in the template */
27+
/* `this` is the data context of the element in your template, so in this case `someField` from `someArray` in the template */
2828
}
2929
}
3030
});
@@ -40,32 +40,44 @@ In your Meteor.js project directory, run
4040

4141
## API
4242

43-
The `HammerTouchArea` template block helper is your interface for enabling multi-touch gestures in your templates. Generally, you only need one occurrence of `HammerTouchArea` in each template you want to specify gestural behaviour for.
43+
The `HammerTouchArea` template block helper is your interface for enabling gestures in your templates. Generally, you only need one occurrence of `HammerTouchArea` in each template you want to specify gestural behaviour for.
4444

45-
A template block helper consists of a start tag and an end tag. The start tag looks like this, `{{#HammerTouchArea property1=value1 property2=value2}}` (where `property1` and `property2` are properties being set on the helper), and the end tag looks like this, `{{/HammerTouchArea}}`. In between the start and end tags, you can put your own template code.
45+
### HammerTouchArea
4646

47-
You will need to pass an object into `HammerTouchArea` via the `gestureMap` property that maps selectors to callbacks. This is a required property and a warning will be printed to the browser console if you leave it out.
47+
In order to define which gestures you want to attach behaviour to, you'll need to pass an object into `HammerTouchArea` via the `gestureMap` property.
4848

49-
The API for this object, a gesture map, is identical to the event map that you pass into `Template.foo.events`. The object keys should follow the format `gestureName cssSelector` (or multiple strings in this format separated by a comma, e.g. `tap .foo, swiperight .bar`) and the value should be a callback function that is executed when the specified gestures are performed on the element(s) matching the CSS selector(s). Example of a gesture map object:
49+
An instance of `HammerTouchArea` looks like this in your HTML:
5050

51-
```javascript
52-
/* The gesture map should follow this format: */
53-
{
54-
'swiperight .foo .bar': function (event, templateInstance) {
55-
/* `event` is the Hammer.js event object */
56-
/* `templateInstance` is the `Blaze.TemplateInstance` */
57-
/* `this` is the data context of the element in your template */
58-
},
59-
'doubletap .foo .baz, swipeup .goo': function (event, templateInstance) {
60-
/* ... */
61-
}
62-
}
51+
```html
52+
<div class="hello-world">
53+
<!-- HammerTouchArea start tag below -->
54+
{{#HammerTouchArea gestureMap=myGestures}}
55+
<div class="gesture-enabled-html">
56+
<!-- Gestures on HTML in here will register in your gestureMap. See below. -->
57+
</div>
58+
{{/HammerTouchArea}}
59+
<!-- HammerTouchArea end tag above -->
60+
</div>
61+
```
62+
63+
### Gesture Maps
64+
65+
A gesture map object ties selector strings to callback functions. It lets you specify which elements inside the `HammerTouchArea` you want to attach behaviour to, and which code should run when those touch gestures are detected. It follows exactly the same format as the object you pass into `Template.fooBar.events`.
66+
67+
The object's keys should follow the format `gestureName cssSelector` (or multiple strings in this format separated by a comma, e.g. `tap .foo, swiperight .bar`) and the value should be a callback function that is executed when the specified gestures are performed on the element(s) matching the CSS selector(s).
68+
69+
Generally you should define your gesture map as a template helper so that you can pass it into the `gestureMap` property of your `HammerTouchArea`.
6370

64-
/* This is a gesture map being returned by a helper: */
71+
Example of defining a gesture map object as a helper:
72+
73+
```javascript
74+
/* This is a gesture map being returned by a helper named `templateGestures`: */
6575
Template.foo.helpers({
6676
templateGestures: {
6777
'swiperight .foo .bar': function (event, templateInstance) {
68-
/* ... */
78+
/* `event` is the Hammer.js event object */
79+
/* `templateInstance` is the `Blaze.TemplateInstance` */
80+
/* `this` is the data context of the element in your template */
6981
},
7082
'doubletap .foo .baz, swipeup .goo': function (event, templateInstance) {
7183
/* ... */
@@ -169,6 +181,10 @@ If you have provided a `configureCallback`, this is callback executed and passed
169181

170182
The gesture map that you pass into `HammerTouchArea` via the `gestureMap` property is parsed, and a series of generic gesture callbacks are attached to the `Hammer.Manager` instance. When these fire (as a result of touch actions being performed on the template DOM), they determine whether any of the current touch behaviour matches the selectors you specified in the gesture map, and if so, your callbacks are fired.
171183

184+
## Migrating from < 4.0.0
185+
186+
From version `4.0.0` onwards, there is a new API. For help migrating your existing implementation over to the new API, see [MIGRATION.md](https://github.com/chriswessels/meteor-hammer/blob/master/MIGRATION.md).
187+
172188
## License
173189

174190
Please see the `LICENSE` file for more information.

hammer_touch_area.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function getParentTemplateInstance (currentInstance, height, includeBlockHelpers
1616
}
1717

1818
// If height is null or undefined, we default to 1, the first parent.
19-
if (height == null) {
19+
if (!height) {
2020
height = 1;
2121
}
2222

@@ -87,7 +87,7 @@ function extractActions (actionString) {
8787
// Fire the necessary user-defined callbacks when Hammer.js fires associated gesture events
8888
function handleGestureEvent (templateInstance, gestureName, event) {
8989
_.each(Object.keys(templateInstance._hammer.gestureHandlers[gestureName]), function (selector, index) {
90-
var eventElem = $(event.target).get(0);
90+
var eventElem = event.target;
9191
if ($(eventElem).is(selector)) {
9292
templateInstance._hammer.gestureHandlers[gestureName][selector].call(Blaze.getData(eventElem), event);
9393
} else {
@@ -133,7 +133,7 @@ Template.HammerTouchArea.onCreated(function () {
133133
});
134134
}
135135
} else {
136-
console.warn('You haven\'t passed a gesture map into HammerTouchArea using gestureMap property.')
136+
console.warn('You haven\'t passed a gesture map into HammerTouchArea using gestureMap property.');
137137
}
138138
});
139139

@@ -158,7 +158,9 @@ Template.HammerTouchArea.onRendered(function () {
158158

159159
// Destroy Hammer.js instance on template teardown
160160
Template.HammerTouchArea.onDestroyed(function () {
161-
this._hammer.instance && this._hammer.instance.destroy();
161+
if (this._hammer.instance) {
162+
this._hammer.instance.destroy();
163+
}
162164
});
163165

164166
Template.HammerTouchArea.helpers({

package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package.describe({
22
name: 'chriswessels:hammer',
33
summary: "A smart-package for integrating with Hammer.js for multi-touch gesture support in your Templates.",
4-
version: "4.0.1",
4+
version: "4.0.2",
55
git: "https://github.com/chriswessels/meteor-hammer.git"
66
});
77

0 commit comments

Comments
 (0)