Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SwipeRecognizer): related to Issue #815 #948

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/recognizers/pan.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ function PanRecognizer() {

this.pX = null;
this.pY = null;
this.firstSrcEvent = null;
this.firstTarget = null;
}

inherit(PanRecognizer, AttrRecognizer, {
Expand Down Expand Up @@ -60,6 +62,12 @@ inherit(PanRecognizer, AttrRecognizer, {
},

attrTest: function(input) {
// Save the initial target element. If the threshold is greater than
// the element itself, the pointer will land on a different element.
if (input.isFirst) {
this.firstSrcEvent = input.srcEvent;
this.firstTarget = input.target;
}
return AttrRecognizer.prototype.attrTest.call(this, input) &&
(this.state & STATE_BEGAN || (!(this.state & STATE_BEGAN) && this.directionTest(input)));
},
Expand All @@ -69,6 +77,9 @@ inherit(PanRecognizer, AttrRecognizer, {
this.pX = input.deltaX;
this.pY = input.deltaY;

input.srcEvent = this.firstSrcEvent;
input.target = this.firstTarget;

var direction = directionStr(input.direction);

if (direction) {
Expand Down
11 changes: 11 additions & 0 deletions src/recognizers/swipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
function SwipeRecognizer() {
AttrRecognizer.apply(this, arguments);

this.firstSrcEvent = null;
this.firstTarget = null;
}

inherit(SwipeRecognizer, AttrRecognizer, {
Expand Down Expand Up @@ -36,6 +39,12 @@ inherit(SwipeRecognizer, AttrRecognizer, {
} else if (direction & DIRECTION_VERTICAL) {
velocity = input.overallVelocityY;
}
// Save the initial target element. If the threshold is greater than
// the element itself, the pointer will land on a different element.
if (input.isFirst) {
this.firstSrcEvent = input.srcEvent;
this.firstTarget = input.target;
}

return this._super.attrTest.call(this, input) &&
direction & input.offsetDirection &&
Expand All @@ -49,6 +58,8 @@ inherit(SwipeRecognizer, AttrRecognizer, {
if (direction) {
this.manager.emit(this.options.event + direction, input);
}
input.srcEvent = this.firstSrcEvent;
input.target = this.firstTarget;

this.manager.emit(this.options.event, input);
}
Expand Down
61 changes: 61 additions & 0 deletions tests/manual/panstart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<title>Hammer.js</title>
<style>
body, .noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
#out {
white-space:pre;
border: 1px solid #666;
margin-top: 20px;
background: #ddd;
height: 110px;
text-align: center;
font: 20px/25px Helvetica, Arial, sans-serif;
padding:30px;
}
#wrapper {background:#ccf; height:100px; position:relative;}
.dragger {width:10px; height:10px; position:absolute; top:30px; left: 30px; background:#c00;}
.dragger.big {width:30px; height:30px;}
</style>
</head>
<body>

<div id="stage">
<div id="wrapper">
<div id="dragger_1" class="dragger"></div>
<div id="dragger_2" class="dragger" style="left:60px;"></div>
<div id="dragger_3" class="dragger big" style="left:90px;"></div>
</div>
</div>

<div id="out"></div>

<script src="../../hammer.js"></script>
<script>
var stage = document.getElementById('stage');
var out = document.getElementById('out');

var mc = new Hammer(stage);
mc.get('pan').set({ direction: Hammer.DIRECTION_ALL, threshold:12 });
mc.on('panstart', function(e) {
out.textContent = e.type +'\n Event.Target ID="' + e.target.id + '"\n Event.srcEvent.target ID="' + e.srcEvent.target.id + '"\n(Expected: ID="' + getActualDragStartElement(e).id + '")';
});
mc.on('panend', function() {
out.textContent = '';
});

function getActualDragStartElement(e) {
var pointer = e.pointers[0] || e.srcEvent;
return document.elementFromPoint(pointer.clientX - e.deltaX, pointer.clientY - e.deltaY);
}
</script>
</body>
</html>