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

Commit ab18914

Browse files
committed
feat(ngMobile): emit 'swipeleft' and 'swiperight' events
Similar to ngMobile clicks, these events were not capturable by other directives. Now they emit 'swipeleft' and 'swiperight' events that can be follow with element.on('swipeleft', ...).
1 parent f9ea69f commit ab18914

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/ngMobile/directive/ngSwipe.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</doc:example>
5555
*/
5656

57-
function makeSwipeDirective(directiveName, direction) {
57+
function makeSwipeDirective(directiveName, direction, eventName) {
5858
ngMobile.directive(directiveName, ['$parse', '$swipe', function($parse, $swipe) {
5959
// The maximum vertical delta for a swipe should be less than 75px.
6060
var MAX_VERTICAL_DISTANCE = 75;
@@ -98,6 +98,7 @@ function makeSwipeDirective(directiveName, direction) {
9898
'end': function(coords) {
9999
if (validSwipe(coords)) {
100100
scope.$apply(function() {
101+
element.triggerHandler(eventName);
101102
swipeHandler(scope);
102103
});
103104
}
@@ -108,6 +109,6 @@ function makeSwipeDirective(directiveName, direction) {
108109
}
109110

110111
// Left is negative X-coordinate, right is positive.
111-
makeSwipeDirective('ngSwipeLeft', -1);
112-
makeSwipeDirective('ngSwipeRight', 1);
112+
makeSwipeDirective('ngSwipeLeft', -1, 'swipeleft');
113+
makeSwipeDirective('ngSwipeRight', 1, 'swiperight');
113114

test/ngMobile/directive/ngSwipeSpec.js

+32
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,38 @@ var swipeTests = function(description, restrictBrowsers, startEvent, moveEvent,
102102

103103
expect($rootScope.swiped).toBeUndefined();
104104
}));
105+
106+
it('should emit "swipeleft" events for left swipes', inject(function($rootScope, $compile, $rootElement) {
107+
element = $compile('<div ng-swipe-left="swiped = true"></div>')($rootScope);
108+
$rootElement.append(element);
109+
$rootScope.$digest();
110+
111+
expect($rootScope.swiped).toBeUndefined();
112+
var eventFired = false;
113+
element.on('swipeleft', function() {
114+
eventFired = true;
115+
});
116+
117+
browserTrigger(element, startEvent, [], 100, 20);
118+
browserTrigger(element, endEvent, [], 20, 20);
119+
expect(eventFired).toEqual(true);
120+
}));
121+
122+
it('should emit "swiperight" events for right swipes', inject(function($rootScope, $compile, $rootElement) {
123+
element = $compile('<div ng-swipe-right="swiped = true"></div>')($rootScope);
124+
$rootElement.append(element);
125+
$rootScope.$digest();
126+
127+
expect($rootScope.swiped).toBeUndefined();
128+
var eventFired = false;
129+
element.on('swiperight', function() {
130+
eventFired = true;
131+
});
132+
133+
browserTrigger(element, startEvent, [], 20, 20);
134+
browserTrigger(element, endEvent, [], 100, 20);
135+
expect(eventFired).toEqual(true);
136+
}));
105137
});
106138
}
107139

0 commit comments

Comments
 (0)