From 1fb048469ef409c42e657d0bfb4b8c116a52a4f1 Mon Sep 17 00:00:00 2001 From: Adam Kent Date: Tue, 8 Oct 2013 14:49:56 +1100 Subject: [PATCH] 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 --- src/ngTouch/directive/ngSwipe.js | 8 ++++---- src/ngTouch/swipe.js | 11 +++++------ test/ngTouch/directive/ngSwipeSpec.js | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/ngTouch/directive/ngSwipe.js b/src/ngTouch/directive/ngSwipe.js index a5911f9ace8d..7a468374cb4e 100644 --- a/src/ngTouch/directive/ngSwipe.js +++ b/src/ngTouch/directive/ngSwipe.js @@ -92,18 +92,18 @@ function makeSwipeDirective(directiveName, direction, eventName) { } $swipe.bind(element, { - 'start': function(coords) { + 'start': function(coords, event) { startCoords = coords; valid = true; }, - 'cancel': function() { + 'cancel': function(event) { valid = false; }, - 'end': function(coords) { + 'end': function(coords, event) { if (validSwipe(coords)) { scope.$apply(function() { element.triggerHandler(eventName); - swipeHandler(scope); + swipeHandler(scope, {$event: event}); }); } } diff --git a/src/ngTouch/swipe.js b/src/ngTouch/swipe.js index 0ee4218eac48..4ad581219c63 100644 --- a/src/ngTouch/swipe.js +++ b/src/ngTouch/swipe.js @@ -83,12 +83,12 @@ ngTouch.factory('$swipe', [function() { totalX = 0; totalY = 0; lastPos = startCoords; - eventHandlers['start'] && eventHandlers['start'](startCoords); + eventHandlers['start'] && eventHandlers['start'](startCoords, event); }); element.on('touchcancel', function(event) { active = false; - eventHandlers['cancel'] && eventHandlers['cancel'](); + eventHandlers['cancel'] && eventHandlers['cancel'](event); }); element.on('touchmove mousemove', function(event) { @@ -116,20 +116,19 @@ ngTouch.factory('$swipe', [function() { if (totalY > totalX) { // Allow native scrolling to take over. active = false; - eventHandlers['cancel'] && eventHandlers['cancel'](); + eventHandlers['cancel'] && eventHandlers['cancel'](event); return; } else { // Prevent the browser from scrolling. event.preventDefault(); - - eventHandlers['move'] && eventHandlers['move'](coords); + eventHandlers['move'] && eventHandlers['move'](coords, event); } }); element.on('touchend mouseup', function(event) { if (!active) return; active = false; - eventHandlers['end'] && eventHandlers['end'](getCoordinates(event)); + eventHandlers['end'] && eventHandlers['end'](getCoordinates(event), event); }); } }; diff --git a/test/ngTouch/directive/ngSwipeSpec.js b/test/ngTouch/directive/ngSwipeSpec.js index a7869a36639f..2aa1a8fe338d 100644 --- a/test/ngTouch/directive/ngSwipeSpec.js +++ b/test/ngTouch/directive/ngSwipeSpec.js @@ -66,6 +66,23 @@ var swipeTests = function(description, restrictBrowsers, startEvent, moveEvent, expect($rootScope.swiped).toBe(true); })); + it('should pass event object', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.$digest(); + + browserTrigger(element, startEvent, { + keys : [], + x : 100, + y : 20 + }); + browserTrigger(element, endEvent,{ + keys: [], + x: 20, + y: 20 + }); + expect($rootScope.event).toBeDefined(); + })); + it('should not swipe if you move too far vertically', inject(function($rootScope, $compile, $rootElement) { element = $compile('
')($rootScope); $rootElement.append(element);