Skip to content

Commit

Permalink
Merge pull request #23 from PolymerElements/ignore-right-click-on-but…
Browse files Browse the repository at this point in the history
…tons

Ignore non-primary mouse input.
  • Loading branch information
cdata committed Jul 27, 2015
2 parents 6b91744 + 4ab92b9 commit cf1a1a7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
30 changes: 29 additions & 1 deletion iron-button-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
'space:keyup': '_spaceKeyUpHandler',
},

_mouseEventRe: /^mouse/,

_tapHandler: function() {
if (this.toggles) {
// a tap is needed to toggle the active state
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions test/active-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit cf1a1a7

Please sign in to comment.