Skip to content

Commit 92f2d76

Browse files
mixedarschmitz
authored andcommitted
Recognizers: Fix incorrect event order
There is always one last event after the end event this should be emitted first Closes #818 Fixes #824
1 parent af32c9b commit 92f2d76

File tree

6 files changed

+85
-9
lines changed

6 files changed

+85
-9
lines changed

src/recognizer.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,24 @@ Recognizer.prototype = {
174174
var self = this;
175175
var state = this.state;
176176

177-
function emit(withState) {
178-
self.manager.emit(self.options.event + (withState ? stateStr(state) : ''), input);
177+
function emit(event) {
178+
self.manager.emit(event, input);
179179
}
180180

181181
// 'panstart' and 'panmove'
182182
if (state < STATE_ENDED) {
183-
emit(true);
183+
emit(self.options.event + stateStr(state));
184184
}
185185

186-
emit(); // simple 'eventName' events
186+
emit(self.options.event); // simple 'eventName' events
187+
188+
if (input.additionalEvent) { // additional event(panleft, panright, pinchin, pinchout...)
189+
emit(input.additionalEvent);
190+
}
187191

188192
// panend and pancancel
189193
if (state >= STATE_ENDED) {
190-
emit(true);
194+
emit(self.options.event + stateStr(state));
191195
}
192196
},
193197

src/recognizers/pan.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ inherit(PanRecognizer, AttrRecognizer, {
6565
},
6666

6767
emit: function(input) {
68+
6869
this.pX = input.deltaX;
6970
this.pY = input.deltaY;
7071

7172
var direction = directionStr(input.direction);
73+
7274
if (direction) {
73-
this.manager.emit(this.options.event + direction, input);
75+
input.additionalEvent = this.options.event + direction;
7476
}
75-
7677
this._super.emit.call(this, input);
7778
}
7879
});

src/recognizers/pinch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ inherit(PinchRecognizer, AttrRecognizer, {
2929
},
3030

3131
emit: function(input) {
32-
this._super.emit.call(this, input);
3332
if (input.scale !== 1) {
3433
var inOut = input.scale < 1 ? 'in' : 'out';
35-
this.manager.emit(this.options.event + inOut, input);
34+
input.additionalEvent = this.options.event + inOut;
3635
}
36+
this._super.emit.call(this, input);
3737
}
3838
});

tests/unit/gestures/test_pan.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,30 @@ test('`panstart` and `panmove` should be recognized', function() {
3434

3535
equal(panMoveCount, 1);
3636
});
37+
38+
asyncTest('Pan event flow should be start -> left -> end', function() {
39+
expect(1);
40+
var pan = new Hammer.Pan({threshold: 1});
41+
hammer.add(pan);
42+
43+
var eventflow = "";
44+
var isCalledPanleft = false;
45+
hammer.on('panstart', function() {
46+
eventflow += "start";
47+
});
48+
hammer.on('panleft', function() {
49+
if(!isCalledPanleft){
50+
isCalledPanleft = true;
51+
eventflow += "left";
52+
}
53+
});
54+
hammer.on('panend', function() {
55+
eventflow += "end";
56+
isCalledPanleft = true;
57+
});
58+
59+
Simulator.gestures.pan(el, { deltaX: -100, deltaY: 0 }, function() {
60+
equal(eventflow,"startleftend");
61+
start();
62+
});
63+
});

tests/unit/gestures/test_pinch.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var el,
2+
hammer;
3+
4+
module('Pinch Gesture', {
5+
setup: function() {
6+
el = document.createElement('div');
7+
document.body.appendChild(el);
8+
9+
hammer = new Hammer(el, {recognizers: []});
10+
},
11+
teardown: function() {
12+
document.body.removeChild(el);
13+
hammer.destroy();
14+
}
15+
});
16+
17+
asyncTest('Pinch event flow should be start -> in -> end', function() {
18+
expect(1);
19+
var pinch = new Hammer.Pinch({enable: true, threshold: .1});
20+
hammer.add(pinch);
21+
22+
var eventflow = "";
23+
var isFiredPinchin = false;
24+
hammer.on('pinchstart', function() {
25+
eventflow += "start";
26+
});
27+
hammer.on('pinchin', function() {
28+
if(!isFiredPinchin){
29+
isFiredPinchin = true;
30+
eventflow += "in";
31+
}
32+
});
33+
hammer.on('pinchend', function() {
34+
eventflow += "end";
35+
isFiredPinchin = false;
36+
});
37+
38+
Simulator.gestures.pinch(el, { duration: 500, scale: .5 }, function() {
39+
equal(eventflow,"startinend");
40+
start();
41+
});
42+
});
43+

tests/unit/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
<script src="test_jquery_plugin.js"></script>
3838
<script src="gestures/test_pan.js"></script>
39+
<script src="gestures/test_pinch.js"></script>
3940
<script src="gestures/test_swipe.js"></script>
4041

4142
</body>

0 commit comments

Comments
 (0)