Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Return the correct "buttons" value for touch
Browse files Browse the repository at this point in the history
Fixes #130

Clear out `ev` variable to release MouseEvent test
  • Loading branch information
dfreedm committed Mar 1, 2014
1 parent a601355 commit b74b52c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/PointerEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
var ev = new MouseEvent('click', {buttons: 1});
NEW_MOUSE_EVENT = true;
HAS_BUTTONS = ev.buttons === 1;
ev = null;
} catch(e) {
}

Expand Down Expand Up @@ -86,10 +87,9 @@
// is to call initMouseEvent with a buttonArg value of -1.
//
// This is fixed with DOM Level 4's use of buttons
var buttons;
if (inDict.buttons || HAS_BUTTONS) {
buttons = inDict.buttons;
} else {
var buttons = inDict.buttons;
// touch has two possible buttons state: 0 and 1, rely on being told the right one
if (!HAS_BUTTONS && inType !== 'touch') {
switch (inDict.which) {
case 1: buttons = 1; break;
case 2: buttons = 4; break;
Expand Down
10 changes: 9 additions & 1 deletion src/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@
clearTimeout(this.resetId);
}
},
typeToButtons: function(type) {
var ret = 0;
if (type === 'touchstart' || type === 'touchmove') {
ret = 1;
}
return ret;
},
touchToPointer: function(inTouch) {
var e = dispatcher.cloneEvent(inTouch);
// Spec specifies that pointerId 1 is reserved for Mouse.
Expand All @@ -148,7 +155,7 @@
e.cancelable = true;
e.detail = this.clickCount;
e.button = 0;
e.buttons = 1;
e.buttons = this.typeToButtons(this.currentTouchEvent);
e.width = inTouch.webkitRadiusX || inTouch.radiusX || 0;
e.height = inTouch.webkitRadiusY || inTouch.radiusY || 0;
e.pressure = inTouch.webkitForce || inTouch.force || 0.5;
Expand All @@ -158,6 +165,7 @@
},
processTouches: function(inEvent, inFunction) {
var tl = inEvent.changedTouches;
this.currentTouchEvent = inEvent.type;
var pointers = touchMap(tl, this.touchToPointer, this);
// forward touch preventDefaults
pointers.forEach(function(p) {
Expand Down

0 comments on commit b74b52c

Please sign in to comment.