Skip to content

Commit 3547fd3

Browse files
committed
Provide a Polymer.setPassiveTouchGestures() function
Clean up event listener options inside gestures that use passive touch
1 parent 6312da5 commit 3547fd3

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/utils/gestures.html

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@
5959
} catch(e) {}
6060
})();
6161

62-
// decide whether to use {passive: true} for gestures listening for touch events
63-
let PASSIVE_TOUCH = Boolean(HAS_NATIVE_TA && SUPPORTS_PASSIVE && Polymer.passiveTouchGestures);
62+
/**
63+
* Generate settings for event listeners, dependant on `Polymer.passiveTouchGestures`
64+
*
65+
* @return {{passive: boolean} | undefined} Options to use for addEventListener and removeEventListener
66+
*/
67+
function PASSIVE_TOUCH() {
68+
if (HAS_NATIVE_TA && SUPPORTS_PASSIVE && Polymer.passiveTouchGestures) {
69+
return {passive: true};
70+
} else {
71+
return;
72+
}
73+
}
6474

6575
// Check for touch-only devices
6676
let IS_TOUCH_ONLY = navigator.userAgent.match(/iP(?:[oa]d|hone)|Android/);
@@ -470,7 +480,7 @@
470480
gobj[dep] = gd = {_count: 0};
471481
}
472482
if (gd._count === 0) {
473-
let options = !isMouseEvent(dep) && PASSIVE_TOUCH ? {passive: true} : undefined;
483+
let options = !isMouseEvent(dep) && PASSIVE_TOUCH();
474484
node.addEventListener(dep, this._handleNative, options);
475485
}
476486
gd[name] = (gd[name] || 0) + 1;
@@ -504,7 +514,7 @@
504514
gd[name] = (gd[name] || 1) - 1;
505515
gd._count = (gd._count || 1) - 1;
506516
if (gd._count === 0) {
507-
let options = !isMouseEvent(dep) && PASSIVE_TOUCH ? {passive: true} : undefined;
517+
let options = !isMouseEvent(dep) && PASSIVE_TOUCH();
508518
node.removeEventListener(dep, this._handleNative, options);
509519
}
510520
}

lib/utils/settings.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@
102102
*
103103
* @memberof Polymer
104104
*/
105-
Polymer.passiveTouchGestures = Polymer.passiveTouchGestures || false;
105+
let passiveTouchGestures = false;
106+
107+
Polymer.passiveTouchGestures = passiveTouchGestures;
108+
109+
/**
110+
* Sets `passiveTouchGestures` globally for all elements using Polymer Gestures.
111+
*
112+
* @memberof Polymer
113+
* @param {boolean} usePassive enable or disable passive touch gestures globally
114+
*/
115+
Polymer.setPassiveTouchGestures = function(usePassive) {
116+
Polymer.passiveTouchGestures = usePassive;
117+
};
106118
})();
107119
</script>

test/smoke/passive-gestures.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<html>
1212
<head>
1313
<script src="../../../webcomponentsjs/webcomponents-loader.js"></script>
14-
<script>Polymer = {passiveTouchGestures: true}</script>
1514
<link rel="import" href="../../polymer.html">
15+
<script>Polymer.setPassiveTouchGestures(true);</script>
1616
<style>
1717
html, body {
1818
margin: 0;

0 commit comments

Comments
 (0)