From 4ab92b99adf17f54deddda07fb8820491f4dddca Mon Sep 17 00:00:00 2001 From: Chris Joel Date: Thu, 23 Jul 2015 15:49:01 -0700 Subject: [PATCH] Ignore non-primary mouse input. --- iron-button-state.html | 30 +++++++++++++++++++++++++++++- test/active-state.html | 9 +++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/iron-button-state.html b/iron-button-state.html index fc52e17..11c03d7 100644 --- a/iron-button-state.html +++ b/iron-button-state.html @@ -91,6 +91,8 @@ 'space:keyup': '_spaceKeyUpHandler', }, + _mouseEventRe: /^mouse/, + _tapHandler: function() { if (this.toggles) { // a tap is needed to toggle the active state @@ -111,7 +113,33 @@ this.fire('change'); }, - _downHandler: function() { + _eventSourceIsPrimaryInput: function(event) { + event = event.detail.sourceEvent || event; + + // Always true for non-mouse events.... + if (!this._mouseEventRe.test(event.type)) { + return true; + } + + // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons + if ('buttons' in event) { + return event.buttons === 1; + } + + // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/which + if (typeof event.which === 'number') { + return event.which < 2; + } + + // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button + return event.button < 1; + }, + + _downHandler: function(event) { + if (!this._eventSourceIsPrimaryInput(event)) { + return; + } + this._setPointerDown(true); this._setPressed(true); this._setReceivedFocusFromKeyboard(false); diff --git a/test/active-state.html b/test/active-state.html index bffa727..983df0e 100644 --- a/test/active-state.html +++ b/test/active-state.html @@ -41,6 +41,15 @@ activeTarget = fixture('TrivialActiveState'); }); + suite('non-primary pointer input source', function() { + test('does not cause state to change', function() { + var rightClickMouseEvent = new CustomEvent('mousedown'); + rightClickMouseEvent.buttons = 2; + activeTarget.dispatchEvent(rightClickMouseEvent); + expect(activeTarget.pressed).to.be.equal(false); + }); + }); + suite('active state with toggles attribute', function() { setup(function() { activeTarget = fixture('ToggleActiveState');