Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a86ec11

Browse files
committedApr 27, 2015
fix(keyboard): check if input is in scroll view
Closes #3586.
1 parent 08956b2 commit a86ec11

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed
 

‎js/utils/keyboard.js

+36-23
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ function keyboardFocusIn(e) {
297297
e.target.readOnly ||
298298
!ionic.tap.isKeyboardElement(e.target) ||
299299
!(scrollView = inputScrollView(e.target))) {
300+
keyboardActiveElement = null;
300301
return;
301302
}
302303

@@ -527,9 +528,11 @@ function keyboardHide() {
527528
ionic.keyboard.isOpen = false;
528529
ionic.keyboard.isClosing = false;
529530

530-
ionic.trigger('resetScrollView', {
531-
target: keyboardActiveElement
532-
}, true);
531+
if (keyboardActiveElement) {
532+
ionic.trigger('resetScrollView', {
533+
target: keyboardActiveElement
534+
}, true);
535+
}
533536

534537
ionic.requestAnimationFrame(function(){
535538
document.body.classList.remove(KEYBOARD_OPEN_CSS);
@@ -549,6 +552,8 @@ function keyboardHide() {
549552
if (keyboardHasPlugin()) cordova.plugins.Keyboard.close();
550553
keyboardActiveElement && keyboardActiveElement.blur();
551554
}
555+
556+
keyboardActiveElement = null;
552557
}
553558

554559
/**
@@ -557,36 +562,42 @@ function keyboardHide() {
557562
* the currently focused input into view if necessary.
558563
*/
559564
function keyboardShow() {
560-
var elementBounds = keyboardActiveElement.getBoundingClientRect();
565+
566+
ionic.keyboard.isOpen = true;
567+
ionic.keyboard.isOpening = false;
568+
561569
var details = {
562-
target: keyboardActiveElement,
563-
elementTop: Math.round(elementBounds.top),
564-
elementBottom: Math.round(elementBounds.bottom),
565570
keyboardHeight: keyboardGetHeight(),
566571
viewportHeight: keyboardCurrentViewportHeight
567572
};
568573

569-
details.windowHeight = details.viewportHeight - details.keyboardHeight;
570-
//console.log("keyboardShow viewportHeight: " + details.viewportHeight +
571-
//", windowHeight: " + details.windowHeight +
572-
//", keyboardHeight: " + details.keyboardHeight);
574+
if (keyboardActiveElement) {
575+
details.target = keyboardActiveElement;
573576

574-
// figure out if the element is under the keyboard
575-
details.isElementUnderKeyboard = (details.elementBottom > details.windowHeight);
576-
//console.log("isUnderKeyboard: " + details.isElementUnderKeyboard);
577-
//console.log("elementBottom: " + details.elementBottom);
577+
var elementBounds = keyboardActiveElement.getBoundingClientRect();
578578

579-
ionic.keyboard.isOpen = true;
580-
ionic.keyboard.isOpening = false;
579+
details.elementTop = Math.round(elementBounds.top);
580+
details.elementBottom = Math.round(elementBounds.bottom);
581+
582+
details.windowHeight = details.viewportHeight - details.keyboardHeight;
583+
//console.log("keyboardShow viewportHeight: " + details.viewportHeight +
584+
//", windowHeight: " + details.windowHeight +
585+
//", keyboardHeight: " + details.keyboardHeight);
581586

582-
// send event so the scroll view adjusts
583-
ionic.trigger('scrollChildIntoView', details, true);
587+
// figure out if the element is under the keyboard
588+
details.isElementUnderKeyboard = (details.elementBottom > details.windowHeight);
589+
//console.log("isUnderKeyboard: " + details.isElementUnderKeyboard);
590+
//console.log("elementBottom: " + details.elementBottom);
591+
592+
// send event so the scroll view adjusts
593+
ionic.trigger('scrollChildIntoView', details, true);
594+
}
584595

585596
setTimeout(function(){
586597
document.body.classList.add(KEYBOARD_OPEN_CSS);
587598
}, 400);
588599

589-
return details;
600+
return details; //for testing
590601
}
591602

592603
/* eslint no-unused-vars:0 */
@@ -654,9 +665,11 @@ function keyboardUpdateViewportHeight() {
654665
keyboardPortraitViewportHeight = keyboardCurrentViewportHeight;
655666
}
656667

657-
ionic.trigger('resetScrollView', {
658-
target: keyboardActiveElement
659-
}, true);
668+
if (keyboardActiveElement) {
669+
ionic.trigger('resetScrollView', {
670+
target: keyboardActiveElement
671+
}, true);
672+
}
660673

661674
if (ionic.keyboard.isOpen && ionic.tap.isTextInput(keyboardActiveElement)) {
662675
keyboardShow();

0 commit comments

Comments
 (0)
Please sign in to comment.