@@ -25,6 +25,20 @@ ngTouch.factory('$swipe', [function() {
25
25
// The total distance in any direction before we make the call on swipe vs. scroll.
26
26
var MOVE_BUFFER_RADIUS = 10 ;
27
27
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
+
28
42
function getCoordinates ( event ) {
29
43
var touches = event . touches && event . touches . length ? event . touches : [ event ] ;
30
44
var e = ( event . changedTouches && event . changedTouches [ 0 ] ) ||
@@ -38,6 +52,17 @@ ngTouch.factory('$swipe', [function() {
38
52
} ;
39
53
}
40
54
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
+
41
66
return {
42
67
/**
43
68
* @ngdoc method
@@ -46,6 +71,9 @@ ngTouch.factory('$swipe', [function() {
46
71
* @description
47
72
* The main method of `$swipe`. It takes an element to be watched for swipe motions, and an
48
73
* object containing event handlers.
74
+ * The pointer types that should be used can be specified via the optional
75
+ * third argument, which is an array of strings `'mouse'` and `'touch'`. By default,
76
+ * `$swipe` will listen for `mouse` and `touch` events.
49
77
*
50
78
* The four events are `start`, `move`, `end`, and `cancel`. `start`, `move`, and `end`
51
79
* receive as a parameter a coordinates object of the form `{ x: 150, y: 310 }`.
@@ -68,7 +96,7 @@ ngTouch.factory('$swipe', [function() {
68
96
* as described above.
69
97
*
70
98
*/
71
- bind : function ( element , eventHandlers ) {
99
+ bind : function ( element , eventHandlers , pointerTypes ) {
72
100
// Absolute total movement, used to control swipe vs. scroll.
73
101
var totalX , totalY ;
74
102
// Coordinates of the start position.
@@ -78,21 +106,24 @@ ngTouch.factory('$swipe', [function() {
78
106
// Whether a swipe is active.
79
107
var active = false ;
80
108
81
- element . on ( 'touchstart mousedown' , function ( event ) {
109
+ pointerTypes = pointerTypes || [ 'mouse' , 'touch' ] ;
110
+ element . on ( getEvents ( pointerTypes , 'start' ) , function ( event ) {
82
111
startCoords = getCoordinates ( event ) ;
83
112
active = true ;
84
113
totalX = 0 ;
85
114
totalY = 0 ;
86
115
lastPos = startCoords ;
87
116
eventHandlers [ 'start' ] && eventHandlers [ 'start' ] ( startCoords , event ) ;
88
117
} ) ;
118
+ var events = getEvents ( pointerTypes , 'cancel' ) ;
119
+ if ( events ) {
120
+ element . on ( events , function ( event ) {
121
+ active = false ;
122
+ eventHandlers [ 'cancel' ] && eventHandlers [ 'cancel' ] ( event ) ;
123
+ } ) ;
124
+ }
89
125
90
- element . on ( 'touchcancel' , function ( event ) {
91
- active = false ;
92
- eventHandlers [ 'cancel' ] && eventHandlers [ 'cancel' ] ( event ) ;
93
- } ) ;
94
-
95
- element . on ( 'touchmove mousemove' , function ( event ) {
126
+ element . on ( getEvents ( pointerTypes , 'move' ) , function ( event ) {
96
127
if ( ! active ) return ;
97
128
98
129
// Android will send a touchcancel if it thinks we're starting to scroll.
@@ -126,7 +157,7 @@ ngTouch.factory('$swipe', [function() {
126
157
}
127
158
} ) ;
128
159
129
- element . on ( 'touchend mouseup' , function ( event ) {
160
+ element . on ( getEvents ( pointerTypes , 'end' ) , function ( event ) {
130
161
if ( ! active ) return ;
131
162
active = false ;
132
163
eventHandlers [ 'end' ] && eventHandlers [ 'end' ] ( getCoordinates ( event ) , event ) ;
0 commit comments