|
40 | 40 | }
|
41 | 41 | })();
|
42 | 42 |
|
| 43 | + /** |
| 44 | + * @param {string} name Possible mouse event name |
| 45 | + * @return {boolean} true if mouse event, false if not |
| 46 | + */ |
| 47 | + function isMouseEvent(name) { |
| 48 | + return MOUSE_EVENTS.indexOf(name) > -1; |
| 49 | + } |
| 50 | + |
43 | 51 | /* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
|
44 | 52 | // check for passive event listeners
|
45 | 53 | let SUPPORTS_PASSIVE = false;
|
46 | 54 | (function() {
|
47 | 55 | try {
|
48 |
| - let opts = Object.defineProperty({}, 'passive', {get: function() {SUPPORTS_PASSIVE = true;}}); |
| 56 | + let opts = Object.defineProperty({}, 'passive', {get() {SUPPORTS_PASSIVE = true;}}); |
49 | 57 | window.addEventListener('test', null, opts);
|
50 | 58 | window.removeEventListener('test', null, opts);
|
51 | 59 | } catch(e) {}
|
52 | 60 | })();
|
53 | 61 |
|
| 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 | + } |
| 74 | + |
54 | 75 | // Check for touch-only devices
|
55 | 76 | let IS_TOUCH_ONLY = navigator.userAgent.match(/iP(?:[oa]d|hone)|Android/);
|
56 | 77 |
|
57 | 78 | let GestureRecognizer = function(){}; // eslint-disable-line no-unused-vars
|
58 |
| - GestureRecognizer.prototype.reset = function(){}; |
| 79 | + /** @type {function()} */ |
| 80 | + GestureRecognizer.prototype.reset; |
59 | 81 | /** @type {function(MouseEvent) | undefined} */
|
60 | 82 | GestureRecognizer.prototype.mousedown;
|
61 | 83 | /** @type {(function(MouseEvent) | undefined)} */
|
|
140 | 162 | function hasLeftMouseButton(ev) {
|
141 | 163 | let type = ev.type;
|
142 | 164 | // exit early if the event is not a mouse event
|
143 |
| - if (MOUSE_EVENTS.indexOf(type) === -1) { |
| 165 | + if (!isMouseEvent(type)) { |
144 | 166 | return false;
|
145 | 167 | }
|
146 | 168 | // ev.button is not reliable for mousemove (0 is overloaded as both left button and no buttons)
|
|
450 | 472 | for (let i = 0, dep, gd; i < deps.length; i++) {
|
451 | 473 | dep = deps[i];
|
452 | 474 | // don't add mouse handlers on iOS because they cause gray selection overlays
|
453 |
| - if (IS_TOUCH_ONLY && MOUSE_EVENTS.indexOf(dep) > -1 && dep !== 'click') { |
| 475 | + if (IS_TOUCH_ONLY && isMouseEvent(dep) && dep !== 'click') { |
454 | 476 | continue;
|
455 | 477 | }
|
456 | 478 | gd = gobj[dep];
|
457 | 479 | if (!gd) {
|
458 | 480 | gobj[dep] = gd = {_count: 0};
|
459 | 481 | }
|
460 | 482 | if (gd._count === 0) {
|
461 |
| - node.addEventListener(dep, this._handleNative); |
| 483 | + let options = !isMouseEvent(dep) && PASSIVE_TOUCH(); |
| 484 | + node.addEventListener(dep, this._handleNative, options); |
462 | 485 | }
|
463 | 486 | gd[name] = (gd[name] || 0) + 1;
|
464 | 487 | gd._count = (gd._count || 0) + 1;
|
|
491 | 514 | gd[name] = (gd[name] || 1) - 1;
|
492 | 515 | gd._count = (gd._count || 1) - 1;
|
493 | 516 | if (gd._count === 0) {
|
494 |
| - node.removeEventListener(dep, this._handleNative); |
| 517 | + let options = !isMouseEvent(dep) && PASSIVE_TOUCH(); |
| 518 | + node.removeEventListener(dep, this._handleNative, options); |
495 | 519 | }
|
496 | 520 | }
|
497 | 521 | }
|
|
0 commit comments