Skip to content

Commit dde3dd3

Browse files
committed
feat(ngTouch): override mouse event directives
The mouse directives handle the touch events too. Closes angular#5334
1 parent 89c57a8 commit dde3dd3

File tree

3 files changed

+126
-30
lines changed

3 files changed

+126
-30
lines changed

angularFiles.js

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ var angularFiles = {
106106
'src/ngTouch/touch.js',
107107
'src/ngTouch/swipe.js',
108108
'src/ngTouch/directive/ngClick.js',
109+
'src/ngTouch/directive/ngEventDirs.js',
109110
'src/ngTouch/directive/ngSwipe.js'
110111
],
111112
'ngAria': [

src/ngTouch/swipe.js

+39-30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,41 @@
22

33
/* global ngTouch: false */
44

5+
// The pointer event matching map
6+
var POINTER_EVENTS = {
7+
'mouse': {
8+
start: 'mousedown',
9+
move: 'mousemove',
10+
end: 'mouseup'
11+
},
12+
'touch': {
13+
start: 'touchstart',
14+
move: 'touchmove',
15+
end: 'touchend',
16+
cancel: 'touchcancel'
17+
}
18+
};
19+
20+
var ngTouchPointerEventStuff = (function pointerEventHandler(){
21+
22+
23+
var POINTER_TYPES = ['mouse', 'touch'];
24+
25+
return function getEvents(eventType, pointerTypes) {
26+
pointerTypes = pointerTypes || POINTER_TYPES;
27+
var res = [];
28+
angular.forEach(pointerTypes, function(pointerType) {
29+
var eventName = POINTER_EVENTS[pointerType][eventType];
30+
if (eventName) {
31+
res.push(eventName);
32+
}
33+
});
34+
return res.join(' ');
35+
}
36+
37+
})();
38+
39+
540
/**
641
* @ngdoc service
742
* @name $swipe
@@ -25,20 +60,6 @@ ngTouch.factory('$swipe', [function() {
2560
// The total distance in any direction before we make the call on swipe vs. scroll.
2661
var MOVE_BUFFER_RADIUS = 10;
2762

28-
var POINTER_EVENTS = {
29-
'mouse': {
30-
start: 'mousedown',
31-
move: 'mousemove',
32-
end: 'mouseup'
33-
},
34-
'touch': {
35-
start: 'touchstart',
36-
move: 'touchmove',
37-
end: 'touchend',
38-
cancel: 'touchcancel'
39-
}
40-
};
41-
4263
function getCoordinates(event) {
4364
var touches = event.touches && event.touches.length ? event.touches : [event];
4465
var e = (event.changedTouches && event.changedTouches[0]) ||
@@ -52,17 +73,6 @@ ngTouch.factory('$swipe', [function() {
5273
};
5374
}
5475

55-
function getEvents(pointerTypes, eventType) {
56-
var res = [];
57-
angular.forEach(pointerTypes, function(pointerType) {
58-
var eventName = POINTER_EVENTS[pointerType][eventType];
59-
if (eventName) {
60-
res.push(eventName);
61-
}
62-
});
63-
return res.join(' ');
64-
}
65-
6676
return {
6777
/**
6878
* @ngdoc method
@@ -106,24 +116,23 @@ ngTouch.factory('$swipe', [function() {
106116
// Whether a swipe is active.
107117
var active = false;
108118

109-
pointerTypes = pointerTypes || ['mouse', 'touch'];
110-
element.on(getEvents(pointerTypes, 'start'), function(event) {
119+
element.on(ngTouchPointerEventStuff('start', pointerTypes), function(event) {
111120
startCoords = getCoordinates(event);
112121
active = true;
113122
totalX = 0;
114123
totalY = 0;
115124
lastPos = startCoords;
116125
eventHandlers['start'] && eventHandlers['start'](startCoords, event);
117126
});
118-
var events = getEvents(pointerTypes, 'cancel');
127+
var events = ngTouchPointerEventStuff( 'cancel', pointerTypes);
119128
if (events) {
120129
element.on(events, function(event) {
121130
active = false;
122131
eventHandlers['cancel'] && eventHandlers['cancel'](event);
123132
});
124133
}
125134

126-
element.on(getEvents(pointerTypes, 'move'), function(event) {
135+
element.on(ngTouchPointerEventStuff('move', pointerTypes), function(event) {
127136
if (!active) return;
128137

129138
// Android will send a touchcancel if it thinks we're starting to scroll.
@@ -157,7 +166,7 @@ ngTouch.factory('$swipe', [function() {
157166
}
158167
});
159168

160-
element.on(getEvents(pointerTypes, 'end'), function(event) {
169+
element.on(ngTouchPointerEventStuff('end', pointerTypes), function(event) {
161170
if (!active) return;
162171
active = false;
163172
eventHandlers['end'] && eventHandlers['end'](getCoordinates(event), event);
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
describe('ngMousedown (touch)', function() {
4+
var element;
5+
6+
beforeEach(function() {
7+
module('ngTouch');
8+
});
9+
10+
afterEach(function() {
11+
dealoc(element);
12+
});
13+
14+
it('should pass event object on mousedown', inject(function($rootScope, $compile) {
15+
element = $compile('<div ng-mousedown="event = $event"></div>')($rootScope);
16+
$rootScope.$digest();
17+
18+
browserTrigger(element, 'mousedown');
19+
expect($rootScope.event).toBeDefined();
20+
}));
21+
22+
it('should pass event object on touchstart too', inject(function($rootScope, $compile) {
23+
element = $compile('<div ng-mousedown="event = $event"></div>')($rootScope);
24+
$rootScope.$digest();
25+
26+
browserTrigger(element, 'touchstart');
27+
expect($rootScope.event).toBeDefined();
28+
}));
29+
});
30+
31+
32+
describe('ngMousemove (touch)', function() {
33+
var element;
34+
35+
beforeEach(function() {
36+
module('ngTouch');
37+
});
38+
39+
afterEach(function() {
40+
dealoc(element);
41+
});
42+
43+
it('should pass event object on mousemove', inject(function($rootScope, $compile) {
44+
element = $compile('<div ng-mousemove="event = $event"></div>')($rootScope);
45+
$rootScope.$digest();
46+
47+
browserTrigger(element, 'mousemove');
48+
expect($rootScope.event).toBeDefined();
49+
}));
50+
51+
it('should pass event object on touchstart too', inject(function($rootScope, $compile) {
52+
element = $compile('<div ng-mousemove="event = $event"></div>')($rootScope);
53+
$rootScope.$digest();
54+
55+
browserTrigger(element, 'mousemove');
56+
expect($rootScope.event).toBeDefined();
57+
}));
58+
});
59+
60+
describe('ngMouseup (touch)', function() {
61+
var element;
62+
63+
beforeEach(function() {
64+
module('ngTouch');
65+
});
66+
67+
afterEach(function() {
68+
dealoc(element);
69+
});
70+
71+
it('should pass event object on mouseup', inject(function($rootScope, $compile) {
72+
element = $compile('<div ng-mouseup="event = $event"></div>')($rootScope);
73+
$rootScope.$digest();
74+
75+
browserTrigger(element, 'mouseup');
76+
expect($rootScope.event).toBeDefined();
77+
}));
78+
79+
it('should pass event object on touchstart too', inject(function($rootScope, $compile) {
80+
element = $compile('<div ng-mouseup="event = $event"></div>')($rootScope);
81+
$rootScope.$digest();
82+
83+
browserTrigger(element, 'mouseup');
84+
expect($rootScope.event).toBeDefined();
85+
}));
86+
});

0 commit comments

Comments
 (0)