Skip to content

Commit

Permalink
feat(keyboard): easily disable/re-enable keyboard
Browse files Browse the repository at this point in the history
Closes #2285.
  • Loading branch information
tlancina committed Apr 20, 2015
1 parent 464223c commit f7db8c3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
56 changes: 53 additions & 3 deletions js/utils/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ var KEYBOARD_OPEN_CSS = 'keyboard-open';
*/
var SCROLL_CONTAINER_CSS = 'scroll-content';

/**
* Debounced keyboardFocusIn function
*/
var debouncedKeyboardFocusIn = ionic.debounce(keyboardFocusIn, 200, true);

/**
* Debounced keyboardNativeShow function
*/
var debouncedKeyboardNativeShow = ionic.debounce(keyboardNativeShow, 100, true);

/**
* Ionic keyboard namespace.
* @namespace keyboard
Expand Down Expand Up @@ -150,6 +160,11 @@ ionic.keyboard = {
*/
isLandscape: false,

/**
* Whether the keyboard event listeners have been added or not
*/
isInitialized: false,

/**
* Hide the keyboard, if it is open.
*/
Expand All @@ -168,6 +183,38 @@ ionic.keyboard = {
if (keyboardHasPlugin()) {
cordova.plugins.Keyboard.show();
}
},

/**
* Remove all keyboard related event listeners, effectively disabling Ionic's
* keyboard adjustments.
*/
disable: function() {
if (keyboardHasPlugin()) {
window.removeEventListener('native.keyboardshow', debouncedKeyboardNativeShow );
window.removeEventListener('native.keyboardhide', keyboardFocusOut);
} else {
document.body.removeEventListener('focusout', keyboardFocusOut);
}

document.body.removeEventListener('ionic.focusin', debouncedKeyboardFocusIn);
document.body.removeEventListener('focusin', debouncedKeyboardFocusIn);

window.removeEventListener('orientationchange', keyboardOrientationChange);

if ( window.navigator.msPointerEnabled ) {
document.removeEventListener("MSPointerDown", keyboardInit);
} else {
document.removeEventListener('touchstart', keyboardInit);
}
ionic.keyboard.isInitialized = false;
},

/**
* Alias for keyboardInit, initialize all keyboard related event listeners.
*/
enable: function() {
keyboardInit();
}
};

Expand All @@ -181,13 +228,14 @@ keyboardCurrentViewportHeight = getViewportHeight();

/**
* Event handler for first touch event, initializes all event listeners
* for keyboard related events.
* for keyboard related events. Also aliased by ionic.keyboard.enable.
*/
function keyboardInit() {
var debouncedKeyboardFocusIn = ionic.debounce(keyboardFocusIn, 200, true);

if (ionic.keyboard.isInitialized) return;

if (keyboardHasPlugin()) {
window.addEventListener('native.keyboardshow', ionic.debounce(keyboardNativeShow, 100, true));
window.addEventListener('native.keyboardshow', debouncedKeyboardNativeShow);
window.addEventListener('native.keyboardhide', keyboardFocusOut);
} else {
document.body.addEventListener('focusout', keyboardFocusOut);
Expand All @@ -201,6 +249,8 @@ function keyboardInit() {
} else {
document.removeEventListener('touchstart', keyboardInit);
}

ionic.keyboard.isInitialized = true;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion test/unit/utils/keyboard.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,5 +502,17 @@ describe('Ionic Keyboard', function() {

ionic.Platform.isFullScreen = true;
expect(getViewportHeight()).toBe(268);
})
});

it('enable() should set isInitialized to true', function(){
expect(ionic.keyboard.isInitialized).toBe(false);
ionic.keyboard.enable();
expect(ionic.keyboard.isInitialized).toBe(true);
});

it('disable() should set isInitialized to false', function(){
expect(ionic.keyboard.isInitialized).toBe(true);
ionic.keyboard.disable();
expect(ionic.keyboard.isInitialized).toBe(false);
});
});

0 comments on commit f7db8c3

Please sign in to comment.