Skip to content

Commit

Permalink
Add argument to
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtheclark committed Feb 13, 2016
1 parent e6f0c89 commit 3141b52
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Head

- Add `useCapture` argument to `createTapListener`.

## 0.1.0

- Initial release.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ That's it. Very simple. You create and remove listeners.

### API

#### `var tapListenerInstance = createTapListener(element, callback)`
#### `var tapListenerInstance = createTapListener(element, callback[, useCapture])`

Adds a tap listener on `element`. When there's a tap, `callback` is invoked with
Adds a tap listener on `element`, using `addEventListener()`.
When there's a tap, `callback` is invoked with
the relevant `event` as its argument (either a `click` or `touchend` event).

**Returns an object with a `remove` function, for removing the listener.**
Expand Down
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function createTapListener(el, callback) {
module.exports = function createTapListener(el, callback, useCapture) {
var startX = 0;
var startY = 0;
var touchStarted = false;
Expand All @@ -9,8 +9,8 @@ module.exports = function createTapListener(el, callback) {
// twice: once on touchend, once on the subsequent "click".
var usingTouch = false;

el.addEventListener('click', handleClick, false);
el.addEventListener('touchstart', handleTouchstart, false);
el.addEventListener('click', handleClick, useCapture);
el.addEventListener('touchstart', handleTouchstart, useCapture);

function handleClick(e) {
if (usingTouch) return;
Expand All @@ -23,9 +23,9 @@ module.exports = function createTapListener(el, callback) {
if (touchStarted) return;
touchStarted = true;

el.addEventListener('touchmove', handleTouchmove, false);
el.addEventListener('touchend', handleTouchend, false);
el.addEventListener('touchcancel', handleTouchcancel, false);
el.addEventListener('touchmove', handleTouchmove, useCapture);
el.addEventListener('touchend', handleTouchend, useCapture);
el.addEventListener('touchcancel', handleTouchcancel, useCapture);

touchMoved = false;
startX = e.touches[0].clientX;
Expand Down Expand Up @@ -59,14 +59,14 @@ module.exports = function createTapListener(el, callback) {
}

function removeSecondaryTouchListeners() {
el.removeEventListener('touchmove', handleTouchmove, false);
el.removeEventListener('touchend', handleTouchend, false);
el.removeEventListener('touchcancel', handleTouchcancel, false);
el.removeEventListener('touchmove', handleTouchmove, useCapture);
el.removeEventListener('touchend', handleTouchend, useCapture);
el.removeEventListener('touchcancel', handleTouchcancel, useCapture);
}

function removeTapListener() {
el.removeEventListener('click', handleClick, false);
el.removeEventListener('touchstart', handleTouchstart, false);
el.removeEventListener('click', handleClick, useCapture);
el.removeEventListener('touchstart', handleTouchstart, useCapture);
removeSecondaryTouchListeners();
}

Expand Down

0 comments on commit 3141b52

Please sign in to comment.