From 43627f77c12f21b454ae4e3cbad4aa1e7816fe57 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Thu, 17 Apr 2014 09:49:08 -0500 Subject: [PATCH] created containsOrIsTextInput --- js/utils/tap.js | 12 ++-- js/views/scrollView.js | 4 +- test/unit/angular/service/tap.unit.js | 82 ++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/js/utils/tap.js b/js/utils/tap.js index 2e81c84105..e45a7b416e 100644 --- a/js/utils/tap.js +++ b/js/utils/tap.js @@ -95,13 +95,17 @@ ionic.tap = { (ele.tagName == 'INPUT' && !(/radio|checkbox|range|file|submit|reset/i).test(ele.type))); }, - isLabelWithInput: function(ele) { + isLabelWithTextInput: function(ele) { var container = tapContainingElement(ele, false); - return container && + return !!container && ionic.tap.isTextInput( tapTargetElement( container ) ); }, + containsOrIsTextInput: function(ele) { + return ionic.tap.isTextInput(ele) || ionic.tap.isLabelWithTextInput(ele); + }, + cloneFocusedInput: function(container, instance) { if(ionic.tap.hasCheckedClone) return; ionic.tap.hasCheckedClone = true; @@ -183,7 +187,7 @@ function tapClickGateKeeper(e) { console.debug('clickPrevent', e.target.tagName); e.stopPropagation(); - if( !ionic.tap.isLabelWithInput(e.target) ) { + if( !ionic.tap.isLabelWithTextInput(e.target) ) { // labels clicks from native should not preventDefault othersize keyboard will not show on input focus e.preventDefault(); } @@ -311,7 +315,7 @@ function tapIgnoreEvent(e) { if(e.isTapHandled) return true; e.isTapHandled = true; - if( ionic.scroll.isScrolling ) { + if( ionic.scroll.isScrolling && ionic.tap.containsOrIsTextInput(e.target) ) { e.preventDefault(); return true; } diff --git a/js/views/scrollView.js b/js/views/scrollView.js index 24545b0440..087b692825 100644 --- a/js/views/scrollView.js +++ b/js/views/scrollView.js @@ -686,7 +686,7 @@ ionic.views.Scroll = ionic.views.View.inherit({ return; } - if( ionic.tap.isTextInput(e.target) || ionic.tap.isLabelWithInput(e.target) ) { + if( ionic.tap.containsOrIsTextInput(e.target) ) { // do not start if the target is a text input // if there is a touchmove on this input, then we can start the scroll self.__hasStarted = false; @@ -705,7 +705,7 @@ ionic.views.Scroll = ionic.views.View.inherit({ return; } - if( !self.__hasStarted && ( (ionic.tap.isTextInput(e.target) || ionic.tap.isLabelWithInput(e.target)) ) ) { + if( !self.__hasStarted && ionic.tap.containsOrIsTextInput(e.target) ) { // the target is a text input and scroll has started // since the text input doesn't start on touchStart, do it here self.__hasStarted = true; diff --git a/test/unit/angular/service/tap.unit.js b/test/unit/angular/service/tap.unit.js index 45a6bae119..c4f2e1e84b 100644 --- a/test/unit/angular/service/tap.unit.js +++ b/test/unit/angular/service/tap.unit.js @@ -928,6 +928,40 @@ describe('Ionic Tap', function() { expect( ionic.tap.isTextInput(ele) ).toEqual(false); }); + it('Should isLabelWithTextInput', function() { + var label = document.createElement('label'); + expect( ionic.tap.isLabelWithTextInput(label) ).toEqual(false); + + var span = document.createElement('span'); + expect( ionic.tap.isLabelWithTextInput(span) ).toEqual(false); + + label.appendChild(span); + expect( ionic.tap.isLabelWithTextInput(span) ).toEqual(false); + + var textarea = document.createElement('textarea'); + expect( ionic.tap.isLabelWithTextInput(textarea) ).toEqual(false); + + label.appendChild(textarea); + expect( ionic.tap.isLabelWithTextInput(textarea) ).toEqual(true); + }); + + it('Should containsOrIsTextInput', function() { + var label = document.createElement('label'); + expect( ionic.tap.containsOrIsTextInput(label) ).toEqual(false); + + var span = document.createElement('span'); + expect( ionic.tap.containsOrIsTextInput(span) ).toEqual(false); + + label.appendChild(span); + expect( ionic.tap.containsOrIsTextInput(span) ).toEqual(false); + + var textarea = document.createElement('textarea'); + expect( ionic.tap.containsOrIsTextInput(textarea) ).toEqual(true); + + label.appendChild(textarea); + expect( ionic.tap.containsOrIsTextInput(textarea) ).toEqual(true); + }); + it('Should reset focus to tapTouchFocusedInput if the active element changed from mousedown', function() { tapEnabledTouchEvents = true; tapActiveElement(document.createElement('textarea')); @@ -960,8 +994,10 @@ describe('Ionic Tap', function() { expect( tapIgnoreEvent(e) ).toEqual(true); }); - it('Should tapIgnoreEvent true because ionic.scroll.isScrolling', function(){ + it('Should tapIgnoreEvent true because ionic.scroll.isScrolling and target is a text input', function(){ + var target = document.createElement('textarea'); var e = { + target: target, preventDefault: function(){ this.preventedDefault=true; } }; ionic.scroll.isScrolling = true; @@ -969,4 +1005,48 @@ describe('Ionic Tap', function() { expect( e.preventedDefault ).toEqual(true); }); + it('Should tapIgnoreEvent true because ionic.scroll.isScrolling and target is span in a label containing a text input', function(){ + var label = document.createElement('label'); + var span = document.createElement('span'); + var textarea = document.createElement('textarea'); + + label.appendChild(span); + label.appendChild(textarea); + + var e = { + target: label, + preventDefault: function(){ this.preventedDefault=true; } + }; + ionic.scroll.isScrolling = true; + expect( tapIgnoreEvent(e) ).toEqual(true); + expect( e.preventedDefault ).toEqual(true); + }); + + it('Should tapIgnoreEvent false because ionic.scroll.isScrolling but target is not a text input', function(){ + var target = document.createElement('span'); + var e = { + target: target, + preventDefault: function(){ this.preventedDefault=true; } + }; + ionic.scroll.isScrolling = true; + expect( tapIgnoreEvent(e) ).toBeUndefined(); + }); + + it('Should tapIgnoreEvent false because ionic.scroll.isScrolling but target is a input[checkbox]', function(){ + var label = document.createElement('label'); + var span = document.createElement('span'); + var checkbox = document.createElement('input'); + checkbox.type = 'checkbox'; + + label.appendChild(span); + label.appendChild(checkbox); + + var e = { + target: span, + preventDefault: function(){ this.preventedDefault=true; } + }; + ionic.scroll.isScrolling = true; + expect( tapIgnoreEvent(e) ).toBeUndefined(); + }); + });