Skip to content

Commit 0b9c659

Browse files
authored
Merge pull request #4836 from Polymer/optional-passive-gestures-1.x
Backport passive touch gestures #4829
2 parents d491f7c + 6775a60 commit 0b9c659

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

src/lib/settings.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
settings.isIE = navigator.userAgent.match('Trident');
5959

60+
settings.passiveTouchGestures = settings.passiveTouchGestures || false;
61+
6062
return settings;
6163
})()
6264
};

src/standard/gestures.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
}
3939
})();
4040

41+
/**
42+
* @param {string} name Possible mouse event name
43+
* @return {boolean} true if mouse event, false if not
44+
*/
45+
function isMouseEvent(name) {
46+
return MOUSE_EVENTS.indexOf(name) > -1;
47+
}
48+
4149
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
4250
// check for passive event listeners
4351
var SUPPORTS_PASSIVE = false;
@@ -49,6 +57,13 @@
4957
} catch(e) {}
5058
})();
5159

60+
// decide whether to use {passive: true} for gestures listening for touch events
61+
function PASSIVE_TOUCH() {
62+
if (HAS_NATIVE_TA && SUPPORTS_PASSIVE && Polymer.Settings.passiveTouchGestures) {
63+
return {passive: true};
64+
}
65+
}
66+
5267
// Check for touch-only devices
5368
var IS_TOUCH_ONLY = navigator.userAgent.match(/iP(?:[oa]d|hone)|Android/);
5469

@@ -110,7 +125,7 @@
110125
function hasLeftMouseButton(ev) {
111126
var type = ev.type;
112127
// exit early if the event is not a mouse event
113-
if (MOUSE_EVENTS.indexOf(type) === -1) {
128+
if (!isMouseEvent(type)) {
114129
return false;
115130
}
116131
// ev.button is not reliable for mousemove (0 is overloaded as both left button and no buttons)
@@ -326,15 +341,16 @@
326341
for (var i = 0, dep, gd; i < deps.length; i++) {
327342
dep = deps[i];
328343
// don't add mouse handlers on iOS because they cause gray selection overlays
329-
if (IS_TOUCH_ONLY && MOUSE_EVENTS.indexOf(dep) > -1 && dep !== 'click') {
344+
if (IS_TOUCH_ONLY && isMouseEvent(dep) && dep !== 'click') {
330345
continue;
331346
}
332347
gd = gobj[dep];
333348
if (!gd) {
334349
gobj[dep] = gd = {_count: 0};
335350
}
336351
if (gd._count === 0) {
337-
node.addEventListener(dep, this.handleNative);
352+
var options = !isMouseEvent(dep) && PASSIVE_TOUCH();
353+
node.addEventListener(dep, this.handleNative, options);
338354
}
339355
gd[name] = (gd[name] || 0) + 1;
340356
gd._count = (gd._count || 0) + 1;
@@ -361,7 +377,8 @@
361377
gd[name] = (gd[name] || 1) - 1;
362378
gd._count = (gd._count || 1) - 1;
363379
if (gd._count === 0) {
364-
node.removeEventListener(dep, this.handleNative);
380+
var options = !isMouseEvent(dep) && PASSIVE_TOUCH();
381+
node.removeEventListener(dep, this.handleNative, options);
365382
}
366383
}
367384
}

test/smoke/passive-gestures.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8+
Code distributed by Google as part of the polymer project is also
9+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
-->
11+
<html>
12+
<head>
13+
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
14+
<script>Polymer = {passiveTouchGestures: true}</script>
15+
<link rel="import" href="../../polymer.html">
16+
<style>
17+
html, body {
18+
margin: 0;
19+
padding: 0;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<dom-module id="x-passive">
25+
<template>
26+
<style>
27+
:host {
28+
display: block;
29+
height: 2000px;
30+
background-color: -webkit-gradient(linear, left top, left bottom, from(blue), to(red));
31+
background-image: -webkit-linear-gradient(top, blue, red);
32+
background-image: -moz-linear-gradient(top, blue, red);
33+
background-image: linear-gradient(to bottom, blue, red);
34+
}
35+
</style>
36+
</template>
37+
</dom-module>
38+
<script>
39+
Polymer({
40+
is: 'x-passive',
41+
listeners: {
42+
'down': 'prevent',
43+
'move': 'prevent'
44+
},
45+
prevent: function(e) {
46+
e.preventDefault();
47+
console.log('prevented!');
48+
}
49+
});
50+
</script>
51+
<x-passive></x-passive>
52+
</body>
53+
</html>

0 commit comments

Comments
 (0)