Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Rename addGesture/removeGesture to add and remove event listeners
Browse files Browse the repository at this point in the history
Keep old behavior as activateGesture/removeGesture for Polymer uses
  • Loading branch information
dfreedm committed Jul 28, 2014
1 parent 1efb8a2 commit de83df4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion samples/simple/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
}
find('capture', 'output', 'enterleave');
events.forEach(function(en) {
capture.addEventListener(en, function(inEvent) {
PolymerGestures.addGesture(capture, en, function(inEvent) {
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']');
inEvent.preventDefault();
});
Expand Down
49 changes: 36 additions & 13 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,17 @@
scope.dispatcher = dispatcher;

/**
* Listen for `gesture` on `node` with the `handler` function
*
* Initializes `node` for receiving events of type `gesture`
*
* Optionally, `touchAction` will set the touch-action of the node.
* The default is `auto`.
*
* If `gesture` is the only listener set up, the underlying gesture is then enabled.
* If `handler` is the first listener for `gesture`, the underlying gesture recognizer is then enabled.
*
* @param {Element} node
* @param {string} gesture
* @param {string} touchAction
* @return Boolean `gesture` is a valid gesture
*/
scope.addGesture = function(node, gesture, touchAction) {
scope.activateGesture = function(node, gesture) {
var dep = dispatcher.dependencyMap[gesture];
if (dep) {
if (touchAction && !node.hasAttribute('touch-action')) {
node.setAttribute('touch-action', touchAction);
}
if (dep.listeners === 0) {
dispatcher.gestures[dep.index].enabled = true;
}
Expand All @@ -334,17 +327,33 @@
}
node._pgListeners++;
}
return Boolean(dep);
};

/**
*
* Listen for `gesture` from `node` with `handler` function.
*
* @param {Element} node
* @param {string} gesture
* @param {Function} handler
*/
scope.addGesture = function(node, gesture, handler) {
if (handler && scope.activateGesture(node, gesture)) {
node.addEventListener(gesture, handler);
}
};

/**
* Tears down the gesture configuration for `node`
*
* If no more listeners for `gesture` exist, the underlying gesture recognizer is disabled.
* If `handler` is the last listener for `gesture`, the underlying gesture recognizer is disabled.
*
* @param {Element} node
* @param {string} gesture
* @return Boolean `gesture` is a valid gesture
*/
scope.removeGesture = function(node, gesture) {
scope.deactivateGesture = function(node, gesture) {
var dep = dispatcher.dependencyMap[gesture];
if (dep) {
if (dep.listeners > 0) {
Expand All @@ -360,5 +369,19 @@
dispatcher.unregister(node);
}
}
return Boolean(dep);
};

/**
* Stop listening for `gesture` from `node` with `handler` function.
*
* @param {Element} node
* @param {string} gesture
* @param {Function} handler
*/
scope.removeGesture = function(node, gesture, handler) {
if (handler && scope.deactivateGesture(node, gesture)) {
node.removeEventListener(gesture, handler);
}
};
})(window.PolymerGestures);

0 comments on commit de83df4

Please sign in to comment.