From 3547fd37852555e957707d3d07818e96872c82e2 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Mon, 11 Sep 2017 11:31:23 -0700 Subject: [PATCH] Provide a `Polymer.setPassiveTouchGestures()` function Clean up event listener options inside gestures that use passive touch --- lib/utils/gestures.html | 18 ++++++++++++++---- lib/utils/settings.html | 14 +++++++++++++- test/smoke/passive-gestures.html | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/utils/gestures.html b/lib/utils/gestures.html index ebe1eac8a3..ea2e29c4be 100644 --- a/lib/utils/gestures.html +++ b/lib/utils/gestures.html @@ -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/); @@ -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; @@ -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); } } diff --git a/lib/utils/settings.html b/lib/utils/settings.html index 0548ad3c2c..7d5163e848 100644 --- a/lib/utils/settings.html +++ b/lib/utils/settings.html @@ -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; + }; })(); diff --git a/test/smoke/passive-gestures.html b/test/smoke/passive-gestures.html index 300496065d..90e7a9363c 100644 --- a/test/smoke/passive-gestures.html +++ b/test/smoke/passive-gestures.html @@ -11,8 +11,8 @@ - +