@@ -25,6 +25,20 @@ ngTouch.factory('$swipe', [function() {
2525 // The total distance in any direction before we make the call on swipe vs. scroll.
2626 var MOVE_BUFFER_RADIUS = 10 ;
2727
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+
2842 function getCoordinates ( event ) {
2943 var touches = event . touches && event . touches . length ? event . touches : [ event ] ;
3044 var e = ( event . changedTouches && event . changedTouches [ 0 ] ) ||
@@ -38,6 +52,17 @@ ngTouch.factory('$swipe', [function() {
3852 } ;
3953 }
4054
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+
4166 return {
4267 /**
4368 * @ngdoc method
@@ -46,6 +71,9 @@ ngTouch.factory('$swipe', [function() {
4671 * @description
4772 * The main method of `$swipe`. It takes an element to be watched for swipe motions, and an
4873 * 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.
4977 *
5078 * The four events are `start`, `move`, `end`, and `cancel`. `start`, `move`, and `end`
5179 * receive as a parameter a coordinates object of the form `{ x: 150, y: 310 }`.
@@ -68,7 +96,7 @@ ngTouch.factory('$swipe', [function() {
6896 * as described above.
6997 *
7098 */
71- bind : function ( element , eventHandlers ) {
99+ bind : function ( element , eventHandlers , pointerTypes ) {
72100 // Absolute total movement, used to control swipe vs. scroll.
73101 var totalX , totalY ;
74102 // Coordinates of the start position.
@@ -78,21 +106,24 @@ ngTouch.factory('$swipe', [function() {
78106 // Whether a swipe is active.
79107 var active = false ;
80108
81- element . on ( 'touchstart mousedown' , function ( event ) {
109+ pointerTypes = pointerTypes || [ 'mouse' , 'touch' ] ;
110+ element . on ( getEvents ( pointerTypes , 'start' ) , function ( event ) {
82111 startCoords = getCoordinates ( event ) ;
83112 active = true ;
84113 totalX = 0 ;
85114 totalY = 0 ;
86115 lastPos = startCoords ;
87116 eventHandlers [ 'start' ] && eventHandlers [ 'start' ] ( startCoords , event ) ;
88117 } ) ;
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+ }
89125
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 ) {
96127 if ( ! active ) return ;
97128
98129 // Android will send a touchcancel if it thinks we're starting to scroll.
@@ -126,7 +157,7 @@ ngTouch.factory('$swipe', [function() {
126157 }
127158 } ) ;
128159
129- element . on ( 'touchend mouseup' , function ( event ) {
160+ element . on ( getEvents ( pointerTypes , 'end' ) , function ( event ) {
130161 if ( ! active ) return ;
131162 active = false ;
132163 eventHandlers [ 'end' ] && eventHandlers [ 'end' ] ( getCoordinates ( event ) , event ) ;
0 commit comments