Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d9b42dd

Browse files
Abdulkaderpetebacondarwin
Abdulkader
authored andcommitted
feat($swipe): add pointer support
Add pointer events to $swipe to support IE11 on touch devices Closes #14061 Closes #14791
1 parent 7ab00f4 commit d9b42dd

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/ngTouch/swipe.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ ngTouch.factory('$swipe', [function() {
3535
move: 'touchmove',
3636
end: 'touchend',
3737
cancel: 'touchcancel'
38+
},
39+
'pointer': {
40+
start: 'pointerdown',
41+
move: 'pointermove',
42+
end: 'pointerup',
43+
cancel: 'pointercancel'
3844
}
3945
};
4046

@@ -69,28 +75,28 @@ ngTouch.factory('$swipe', [function() {
6975
* The main method of `$swipe`. It takes an element to be watched for swipe motions, and an
7076
* object containing event handlers.
7177
* The pointer types that should be used can be specified via the optional
72-
* third argument, which is an array of strings `'mouse'` and `'touch'`. By default,
73-
* `$swipe` will listen for `mouse` and `touch` events.
78+
* third argument, which is an array of strings `'mouse'`, `'touch'` and `'pointer'`. By default,
79+
* `$swipe` will listen for `mouse`, `touch` and `pointer` events.
7480
*
7581
* The four events are `start`, `move`, `end`, and `cancel`. `start`, `move`, and `end`
7682
* receive as a parameter a coordinates object of the form `{ x: 150, y: 310 }` and the raw
7783
* `event`. `cancel` receives the raw `event` as its single parameter.
7884
*
79-
* `start` is called on either `mousedown` or `touchstart`. After this event, `$swipe` is
80-
* watching for `touchmove` or `mousemove` events. These events are ignored until the total
85+
* `start` is called on either `mousedown`, `touchstart` or `pointerdown`. After this event, `$swipe` is
86+
* watching for `touchmove`, `mousemove` or `pointermove` events. These events are ignored until the total
8187
* distance moved in either dimension exceeds a small threshold.
8288
*
8389
* Once this threshold is exceeded, either the horizontal or vertical delta is greater.
8490
* - If the horizontal distance is greater, this is a swipe and `move` and `end` events follow.
8591
* - If the vertical distance is greater, this is a scroll, and we let the browser take over.
8692
* A `cancel` event is sent.
8793
*
88-
* `move` is called on `mousemove` and `touchmove` after the above logic has determined that
94+
* `move` is called on `mousemove`, `touchmove` and `pointermove` after the above logic has determined that
8995
* a swipe is in progress.
9096
*
91-
* `end` is called when a swipe is successfully completed with a `touchend` or `mouseup`.
97+
* `end` is called when a swipe is successfully completed with a `touchend`, `mouseup` or `pointerup`.
9298
*
93-
* `cancel` is called either on a `touchcancel` from the browser, or when we begin scrolling
99+
* `cancel` is called either on a `touchcancel` or `pointercancel` from the browser, or when we begin scrolling
94100
* as described above.
95101
*
96102
*/
@@ -104,7 +110,7 @@ ngTouch.factory('$swipe', [function() {
104110
// Whether a swipe is active.
105111
var active = false;
106112

107-
pointerTypes = pointerTypes || ['mouse', 'touch'];
113+
pointerTypes = pointerTypes || ['mouse', 'touch', 'pointer'];
108114
element.on(getEvents(pointerTypes, 'start'), function(event) {
109115
startCoords = getCoordinates(event);
110116
active = true;

test/ngTouch/swipeSpec.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ describe('$swipe', function() {
2525
var usedEvents;
2626
var MOUSE_EVENTS = ['mousedown','mousemove','mouseup'].sort();
2727
var TOUCH_EVENTS = ['touchcancel','touchend','touchmove','touchstart'].sort();
28-
var ALL_EVENTS = MOUSE_EVENTS.concat(TOUCH_EVENTS).sort();
28+
var POINTER_EVENTS = ['pointerdown', 'pointermove', 'pointerup', 'pointercancel'].sort();
29+
var ALL_EVENTS = MOUSE_EVENTS.concat(TOUCH_EVENTS, POINTER_EVENTS).sort();
2930

3031
beforeEach(function() {
3132
usedEvents = [];
@@ -36,7 +37,7 @@ describe('$swipe', function() {
3637
});
3738
});
3839

39-
it('should use mouse and touch by default', inject(function($swipe) {
40+
it('should use mouse, touch and pointer by default', inject(function($swipe) {
4041
$swipe.bind(element, events);
4142
expect(usedEvents.sort()).toEqual(ALL_EVENTS);
4243
}));
@@ -51,14 +52,35 @@ describe('$swipe', function() {
5152
expect(usedEvents.sort()).toEqual(TOUCH_EVENTS);
5253
}));
5354

55+
it('should only use pointer events for pointerType "pointer"', inject(function($swipe) {
56+
$swipe.bind(element, events, ['pointer']);
57+
expect(usedEvents.sort()).toEqual(POINTER_EVENTS);
58+
}));
59+
5460
it('should use mouse and touch if both are specified', inject(function($swipe) {
5561
$swipe.bind(element, events, ['touch', 'mouse']);
62+
expect(usedEvents.sort()).toEqual(MOUSE_EVENTS.concat(TOUCH_EVENTS).sort());
63+
}));
64+
65+
it('should use mouse and pointer if both are specified', inject(function($swipe) {
66+
$swipe.bind(element, events, ['mouse', 'pointer']);
67+
expect(usedEvents.sort()).toEqual(MOUSE_EVENTS.concat(POINTER_EVENTS).sort());
68+
}));
69+
70+
it('should use touch and pointer if both are specified', inject(function($swipe) {
71+
$swipe.bind(element, events, ['touch', 'pointer']);
72+
expect(usedEvents.sort()).toEqual(TOUCH_EVENTS.concat(POINTER_EVENTS).sort());
73+
}));
74+
75+
it('should use mouse, touch and pointer if they are specified', inject(function($swipe) {
76+
$swipe.bind(element, events, ['mouse', 'touch', 'pointer']);
5677
expect(usedEvents.sort()).toEqual(ALL_EVENTS);
5778
}));
5879

5980
});
6081

6182
swipeTests('touch', /* restrictBrowers */ true, 'touchstart', 'touchmove', 'touchend');
83+
swipeTests('pointer', /* restrictBrowers */ true, 'pointerdown', 'pointermove', 'pointerup');
6284
swipeTests('mouse', /* restrictBrowers */ false, 'mousedown', 'mousemove', 'mouseup');
6385

6486
// Wrapper to abstract over using touch events or mouse events.

0 commit comments

Comments
 (0)