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

Commit 507d802

Browse files
akentpetebacondarwin
authored andcommitted
fix(ngTouch): add $event to ng-swipe
Existing documentation implies that an Event object should be available as `$event` on swipe directives, which previously was only working for `ng-click`. Closes #4071 Closes #4321
1 parent bed08c9 commit 507d802

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/ngTouch/directive/ngSwipe.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,18 @@ function makeSwipeDirective(directiveName, direction, eventName) {
9292
}
9393

9494
$swipe.bind(element, {
95-
'start': function(coords) {
95+
'start': function(coords, event) {
9696
startCoords = coords;
9797
valid = true;
9898
},
99-
'cancel': function() {
99+
'cancel': function(event) {
100100
valid = false;
101101
},
102-
'end': function(coords) {
102+
'end': function(coords, event) {
103103
if (validSwipe(coords)) {
104104
scope.$apply(function() {
105105
element.triggerHandler(eventName);
106-
swipeHandler(scope);
106+
swipeHandler(scope, {$event: event});
107107
});
108108
}
109109
}

src/ngTouch/swipe.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ ngTouch.factory('$swipe', [function() {
8383
totalX = 0;
8484
totalY = 0;
8585
lastPos = startCoords;
86-
eventHandlers['start'] && eventHandlers['start'](startCoords);
86+
eventHandlers['start'] && eventHandlers['start'](startCoords, event);
8787
});
8888

8989
element.on('touchcancel', function(event) {
9090
active = false;
91-
eventHandlers['cancel'] && eventHandlers['cancel']();
91+
eventHandlers['cancel'] && eventHandlers['cancel'](event);
9292
});
9393

9494
element.on('touchmove mousemove', function(event) {
@@ -116,20 +116,19 @@ ngTouch.factory('$swipe', [function() {
116116
if (totalY > totalX) {
117117
// Allow native scrolling to take over.
118118
active = false;
119-
eventHandlers['cancel'] && eventHandlers['cancel']();
119+
eventHandlers['cancel'] && eventHandlers['cancel'](event);
120120
return;
121121
} else {
122122
// Prevent the browser from scrolling.
123123
event.preventDefault();
124-
125-
eventHandlers['move'] && eventHandlers['move'](coords);
124+
eventHandlers['move'] && eventHandlers['move'](coords, event);
126125
}
127126
});
128127

129128
element.on('touchend mouseup', function(event) {
130129
if (!active) return;
131130
active = false;
132-
eventHandlers['end'] && eventHandlers['end'](getCoordinates(event));
131+
eventHandlers['end'] && eventHandlers['end'](getCoordinates(event), event);
133132
});
134133
}
135134
};

test/ngTouch/directive/ngSwipeSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ var swipeTests = function(description, restrictBrowsers, startEvent, moveEvent,
6666
expect($rootScope.swiped).toBe(true);
6767
}));
6868

69+
it('should pass event object', inject(function($rootScope, $compile) {
70+
element = $compile('<div ng-swipe-left="event = $event"></div>')($rootScope);
71+
$rootScope.$digest();
72+
73+
browserTrigger(element, startEvent, {
74+
keys : [],
75+
x : 100,
76+
y : 20
77+
});
78+
browserTrigger(element, endEvent,{
79+
keys: [],
80+
x: 20,
81+
y: 20
82+
});
83+
expect($rootScope.event).toBeDefined();
84+
}));
85+
6986
it('should not swipe if you move too far vertically', inject(function($rootScope, $compile, $rootElement) {
7087
element = $compile('<div ng-swipe-left="swiped = true"></div>')($rootScope);
7188
$rootElement.append(element);

0 commit comments

Comments
 (0)