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

Commit

Permalink
feat(ngMobile): emit 'swipeleft' and 'swiperight' events
Browse files Browse the repository at this point in the history
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', ...).
  • Loading branch information
bshepherdson committed Jul 25, 2013
1 parent f9ea69f commit ab18914
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ngMobile/directive/ngSwipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</doc:example>
*/

function makeSwipeDirective(directiveName, direction) {
function makeSwipeDirective(directiveName, direction, eventName) {
ngMobile.directive(directiveName, ['$parse', '$swipe', function($parse, $swipe) {
// The maximum vertical delta for a swipe should be less than 75px.
var MAX_VERTICAL_DISTANCE = 75;
Expand Down Expand Up @@ -98,6 +98,7 @@ function makeSwipeDirective(directiveName, direction) {
'end': function(coords) {
if (validSwipe(coords)) {
scope.$apply(function() {
element.triggerHandler(eventName);
swipeHandler(scope);
});
}
Expand All @@ -108,6 +109,6 @@ function makeSwipeDirective(directiveName, direction) {
}

// Left is negative X-coordinate, right is positive.
makeSwipeDirective('ngSwipeLeft', -1);
makeSwipeDirective('ngSwipeRight', 1);
makeSwipeDirective('ngSwipeLeft', -1, 'swipeleft');
makeSwipeDirective('ngSwipeRight', 1, 'swiperight');

32 changes: 32 additions & 0 deletions test/ngMobile/directive/ngSwipeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,38 @@ var swipeTests = function(description, restrictBrowsers, startEvent, moveEvent,

expect($rootScope.swiped).toBeUndefined();
}));

it('should emit "swipeleft" events for left swipes', inject(function($rootScope, $compile, $rootElement) {
element = $compile('<div ng-swipe-left="swiped = true"></div>')($rootScope);
$rootElement.append(element);
$rootScope.$digest();

expect($rootScope.swiped).toBeUndefined();
var eventFired = false;
element.on('swipeleft', function() {
eventFired = true;
});

browserTrigger(element, startEvent, [], 100, 20);
browserTrigger(element, endEvent, [], 20, 20);
expect(eventFired).toEqual(true);
}));

it('should emit "swiperight" events for right swipes', inject(function($rootScope, $compile, $rootElement) {
element = $compile('<div ng-swipe-right="swiped = true"></div>')($rootScope);
$rootElement.append(element);
$rootScope.$digest();

expect($rootScope.swiped).toBeUndefined();
var eventFired = false;
element.on('swiperight', function() {
eventFired = true;
});

browserTrigger(element, startEvent, [], 20, 20);
browserTrigger(element, endEvent, [], 100, 20);
expect(eventFired).toEqual(true);
}));
});
}

Expand Down

0 comments on commit ab18914

Please sign in to comment.