From 7432cd759661bdf00bf49b4b4f00b687f84e49f6 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Thu, 27 Jul 2017 21:13:28 +0300 Subject: [PATCH] fix(gesture): unable to move text cursor and tap away on mobile * Fixes not being able to move the text cursor within and input on mobile. * Fixes not being able to blur an input by tapping away on mobile. Both of the issues were due to the click hijacking from the gesture service being too aggressive and preventing focus from shifting. Fixes #10301. Fixes #5365. --- src/core/services/gesture/gesture.js | 42 +++++++++++++++++----------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/core/services/gesture/gesture.js b/src/core/services/gesture/gesture.js index 88a2182403..884cd081d1 100644 --- a/src/core/services/gesture/gesture.js +++ b/src/core/services/gesture/gesture.js @@ -104,20 +104,9 @@ function MdGesture($$MdGestureHandler, $$rAF, $timeout) { maxDistance: maxClickDistance }, onEnd: function(ev, pointer) { - if (pointer.distance < this.state.options.maxDistance) { - if (canFocus(ev.target)) { - this.dispatchEvent(ev, 'focus', pointer); - ev.target.focus(); - } - } - - function canFocus(element) { - var focusableElements = ['INPUT', 'SELECT', 'BUTTON', 'TEXTAREA', 'VIDEO', 'AUDIO']; - - return (element.getAttribute('tabindex') != '-1') && - !element.hasAttribute('DISABLED') && - (element.hasAttribute('tabindex') || element.hasAttribute('href') || element.isContentEditable || - (focusableElements.indexOf(element.nodeName) != -1)); + if (pointer.distance < this.state.options.maxDistance && canFocus(ev.target)) { + this.dispatchEvent(ev, 'focus', pointer); + ev.target.focus(); } } }); @@ -550,8 +539,14 @@ function attachToDocument( $mdGesture, $$MdGestureHandler ) { function mouseInputHijacker(ev) { var isKeyClick = !ev.clientX && !ev.clientY; - if (!isKeyClick && !ev.$material && !ev.isIonicTap - && !isInputEventFromLabelClick(ev)) { + + if ( + !isKeyClick && + !ev.$material && + !ev.isIonicTap && + !isInputEventFromLabelClick(ev) && + (ev.type !== 'mousedown' || (!canFocus(ev.target) && !canFocus(document.activeElement))) + ) { ev.preventDefault(); ev.stopPropagation(); } @@ -745,3 +740,18 @@ function getEventPoint(ev) { (ev.changedTouches && ev.changedTouches[0]) || ev; } + +/** Checks whether an element can be focused. */ +function canFocus(element) { + return ( + !!element && + element.getAttribute('tabindex') != '-1' && + !element.hasAttribute('disabled') && + ( + element.hasAttribute('tabindex') || + element.hasAttribute('href') || + element.isContentEditable || + ['INPUT', 'SELECT', 'BUTTON', 'TEXTAREA', 'VIDEO', 'AUDIO'].indexOf(element.nodeName) != -1 + ) + ); +}