diff --git a/src/ngTouch/directive/ngClick.js b/src/ngTouch/directive/ngClick.js index 3ac1e3f24a10..6075207e9bf3 100644 --- a/src/ngTouch/directive/ngClick.js +++ b/src/ngTouch/directive/ngClick.js @@ -157,8 +157,15 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', event.stopPropagation(); event.preventDefault(); - // Blur focused form elements - event.target && event.target.blur(); + // Blur focused form elements only if the user did not specify to keep it. + // Specified by adding the attribute 'ng-touch' with the value 'keep-focus'. + if (!event.target.attributes['ng-touch']) { + event.target && event.target.blur(); + } else { + if (event.target.attributes['ng-touch'].nodeValue !== 'keep-focus') { + event.target && event.target.blur(); + } + } } diff --git a/test/ngTouch/directive/ngClickSpec.js b/test/ngTouch/directive/ngClickSpec.js index 921c64578b2b..a8e22efa717e 100644 --- a/test/ngTouch/directive/ngClickSpec.js +++ b/test/ngTouch/directive/ngClickSpec.js @@ -600,5 +600,91 @@ describe('ngClick (touch)', function() { expect(called).toEqual(true); })); + + it('should remove focus by default', inject(function($rootScope, $compile, $rootElement, $document) { + $document.find('body').append($rootElement); + $rootElement.append( + '' + ); + $compile($rootElement)($rootScope); + + element = $rootElement.find('input').eq(0); + + $rootScope.count = 0; + + $rootScope.$digest(); + + expect($rootScope.count).toBe(0); + + time = 10; + browserTrigger(element, 'touchstart',{ + keys: [], + x: 10, + y: 10 + }); + + time = 50; + browserTrigger(element, 'touchend',{ + keys: [], + x: 10, + y: 10 + }); + + expect($rootScope.count).toBe(1); + + time = 90; + // Verify that it is blured so we don't get soft-keyboard + element[0].blur = jasmine.createSpy('blur'); + browserTrigger(element, 'click',{ + keys: [], + x: 10, + y: 10 + }); + expect(element[0].blur).toHaveBeenCalled(); + $document.find('body').empty(); + })); + + it('should keep focus if it is specified', inject(function($rootScope, $compile, $rootElement, $document) { + $document.find('body').append($rootElement); + $rootElement.append( + '' + ); + $compile($rootElement)($rootScope); + + element = $rootElement.find('input').eq(0); + + $rootScope.count = 0; + + $rootScope.$digest(); + + expect($rootScope.count).toBe(0); + + time = 10; + browserTrigger(element, 'touchstart',{ + keys: [], + x: 10, + y: 10 + }); + + time = 50; + browserTrigger(element, 'touchend',{ + keys: [], + x: 10, + y: 10 + }); + + expect($rootScope.count).toBe(1); + + time = 90; + // Verify that it is not blured so we do get soft-keyboard + element[0].blur = jasmine.createSpy('blur'); + browserTrigger(element, 'click',{ + keys: [], + x: 10, + y: 10 + }); + expect(element[0].blur).not.toHaveBeenCalled(); + $document.find('body').empty(); + })); }); });