Skip to content

Commit

Permalink
Provide a Polymer.setPassiveTouchGestures() function
Browse files Browse the repository at this point in the history
Clean up event listener options inside gestures that use passive touch
  • Loading branch information
dfreedm committed Sep 11, 2017
1 parent 6312da5 commit 3547fd3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
18 changes: 14 additions & 4 deletions lib/utils/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@
} catch(e) {}
})();

// decide whether to use {passive: true} for gestures listening for touch events
let PASSIVE_TOUCH = Boolean(HAS_NATIVE_TA && SUPPORTS_PASSIVE && Polymer.passiveTouchGestures);
/**
* Generate settings for event listeners, dependant on `Polymer.passiveTouchGestures`
*
* @return {{passive: boolean} | undefined} Options to use for addEventListener and removeEventListener
*/
function PASSIVE_TOUCH() {
if (HAS_NATIVE_TA && SUPPORTS_PASSIVE && Polymer.passiveTouchGestures) {
return {passive: true};
} else {
return;
}
}

// Check for touch-only devices
let IS_TOUCH_ONLY = navigator.userAgent.match(/iP(?:[oa]d|hone)|Android/);
Expand Down Expand Up @@ -470,7 +480,7 @@
gobj[dep] = gd = {_count: 0};
}
if (gd._count === 0) {
let options = !isMouseEvent(dep) && PASSIVE_TOUCH ? {passive: true} : undefined;
let options = !isMouseEvent(dep) && PASSIVE_TOUCH();
node.addEventListener(dep, this._handleNative, options);
}
gd[name] = (gd[name] || 0) + 1;
Expand Down Expand Up @@ -504,7 +514,7 @@
gd[name] = (gd[name] || 1) - 1;
gd._count = (gd._count || 1) - 1;
if (gd._count === 0) {
let options = !isMouseEvent(dep) && PASSIVE_TOUCH ? {passive: true} : undefined;
let options = !isMouseEvent(dep) && PASSIVE_TOUCH();
node.removeEventListener(dep, this._handleNative, options);
}
}
Expand Down
14 changes: 13 additions & 1 deletion lib/utils/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@
*
* @memberof Polymer
*/
Polymer.passiveTouchGestures = Polymer.passiveTouchGestures || false;
let passiveTouchGestures = false;

Polymer.passiveTouchGestures = passiveTouchGestures;

/**
* Sets `passiveTouchGestures` globally for all elements using Polymer Gestures.
*
* @memberof Polymer
* @param {boolean} usePassive enable or disable passive touch gestures globally
*/
Polymer.setPassiveTouchGestures = function(usePassive) {
Polymer.passiveTouchGestures = usePassive;
};
})();
</script>
2 changes: 1 addition & 1 deletion test/smoke/passive-gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<html>
<head>
<script src="../../../webcomponentsjs/webcomponents-loader.js"></script>
<script>Polymer = {passiveTouchGestures: true}</script>
<link rel="import" href="../../polymer.html">
<script>Polymer.setPassiveTouchGestures(true);</script>
<style>
html, body {
margin: 0;
Expand Down

0 comments on commit 3547fd3

Please sign in to comment.