2
2
3
3
/* global ngTouch: false */
4
4
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
+
5
40
/**
6
41
* @ngdoc service
7
42
* @name $swipe
@@ -25,20 +60,6 @@ ngTouch.factory('$swipe', [function() {
25
60
// The total distance in any direction before we make the call on swipe vs. scroll.
26
61
var MOVE_BUFFER_RADIUS = 10 ;
27
62
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
-
42
63
function getCoordinates ( event ) {
43
64
var touches = event . touches && event . touches . length ? event . touches : [ event ] ;
44
65
var e = ( event . changedTouches && event . changedTouches [ 0 ] ) ||
@@ -52,17 +73,6 @@ ngTouch.factory('$swipe', [function() {
52
73
} ;
53
74
}
54
75
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
-
66
76
return {
67
77
/**
68
78
* @ngdoc method
@@ -106,24 +116,23 @@ ngTouch.factory('$swipe', [function() {
106
116
// Whether a swipe is active.
107
117
var active = false ;
108
118
109
- pointerTypes = pointerTypes || [ 'mouse' , 'touch' ] ;
110
- element . on ( getEvents ( pointerTypes , 'start' ) , function ( event ) {
119
+ element . on ( ngTouchPointerEventStuff ( 'start' , pointerTypes ) , function ( event ) {
111
120
startCoords = getCoordinates ( event ) ;
112
121
active = true ;
113
122
totalX = 0 ;
114
123
totalY = 0 ;
115
124
lastPos = startCoords ;
116
125
eventHandlers [ 'start' ] && eventHandlers [ 'start' ] ( startCoords , event ) ;
117
126
} ) ;
118
- var events = getEvents ( pointerTypes , 'cancel' ) ;
127
+ var events = ngTouchPointerEventStuff ( 'cancel' , pointerTypes ) ;
119
128
if ( events ) {
120
129
element . on ( events , function ( event ) {
121
130
active = false ;
122
131
eventHandlers [ 'cancel' ] && eventHandlers [ 'cancel' ] ( event ) ;
123
132
} ) ;
124
133
}
125
134
126
- element . on ( getEvents ( pointerTypes , 'move' ) , function ( event ) {
135
+ element . on ( ngTouchPointerEventStuff ( 'move' , pointerTypes ) , function ( event ) {
127
136
if ( ! active ) return ;
128
137
129
138
// Android will send a touchcancel if it thinks we're starting to scroll.
@@ -157,7 +166,7 @@ ngTouch.factory('$swipe', [function() {
157
166
}
158
167
} ) ;
159
168
160
- element . on ( getEvents ( pointerTypes , 'end' ) , function ( event ) {
169
+ element . on ( ngTouchPointerEventStuff ( 'end' , pointerTypes ) , function ( event ) {
161
170
if ( ! active ) return ;
162
171
active = false ;
163
172
eventHandlers [ 'end' ] && eventHandlers [ 'end' ] ( getCoordinates ( event ) , event ) ;
0 commit comments