import { EventEmitter , Injectable , NgZone } from '@angular/core' ; import { Config } from '../config/config' ; import { DomController } from './dom-controller' ; import { isTextInput } from '../util/dom' ; import { KEY_TAB } from './key' ; import { Platform } from './platform' ; name description usage @ Injectable ( ) export class Keyboard { _tmr : number ; willShow = new EventEmitter < number > ( ) ; willHide = new EventEmitter < void > ( ) ; didShow = new EventEmitter < number > ( ) ; didHide = new EventEmitter < void > ( ) ; eventsAvailable = false ; constructor ( config , private _plt , private _zone , private _dom ) { this . focusOutline ( config . get ( 'focusOutline' ) ) ; const win = < any > _plt . win ( ) ; if ( win . Ionic && win . Ionic . keyboardPlugin ) { this . listenV2 ( win ) ; } else { this . listenV1 ( win ) ; } } private listenV2 ( win ) { const platform = this . _plt ; platform . registerListener ( win , 'keyboardWillShow' , ( ev ) => { this . _zone . run ( ( ) => { this . willShow . emit ( ev . keyboardHeight ) ; } ) ; } , { zone : false , passive : true } ) ; platform . registerListener ( win , 'keyboardWillHide' , ( ) => { this . _zone . run ( ( ) => { this . willHide . emit ( ) ; } ) ; } , { zone : false , passive : true } ) ; platform . registerListener ( win , 'keyboardDidShow' , ( ev ) => { this . _zone . run ( ( ) => { this . didShow . emit ( ev . keyboardHeight ) ; } ) ; } , { zone : false , passive : true } ) ; platform . registerListener ( win , 'keyboardDidHide' , ( ) => { this . _zone . run ( ( ) => { this . didHide . emit ( ) ; } ) ; } , { zone : false , passive : true } ) ; this . eventsAvailable = true ; } private listenV1 ( win ) { const platform = this . _plt ; platform . registerListener ( win , 'native.keyboardhide' , ( ) => { this . blurActiveInput ( true ) ; } , { zone : false , passive : true } ) ; platform . registerListener ( win , 'native.keyboardshow' , ( ) => { this . blurActiveInput ( false ) ; } , { zone : false , passive : true } ) ; } private blurActiveInput ( shouldBlur ) { const platform = this . _plt ; platform . cancelTimeout ( this . _tmr ) ; if ( shouldBlur ) { this . _tmr = platform . timeout ( ( ) => { if ( this . isOpen ( ) ) { platform . focusOutActiveElement ( ) ; } } , 80 ) ; } } return { boolean } isOpen ( ) { return this . hasFocusedTextInput ( ) ; } param { function } callback return { function } onClose ( callback , pollingInternval = KEYBOARD_CLOSE_POLLING , pollingChecksMax = KEYBOARD_POLLING_CHECKS_MAX ) { console . debug ( `keyboard, onClose created` ) ; const self = this ; let checks = 0 ; let promise = null ; if ( ! callback ) { promise = new Promise ( resolve => { callback = resolve ; } ) ; } function checkKeyboard ( ) { console . debug ( `keyboard, isOpen: ${ self . isOpen ( ) }` ) ; if ( ! self . isOpen ( ) || checks > pollingChecksMax ) { self . _plt . timeout ( function ( ) { self . _zone . run ( function ( ) { console . debug ( `keyboard, closed` ) ; callback ( ) ; } ) ; } , 400 ) ; } else { self . _plt . timeout ( checkKeyboard , pollingInternval ) ; } checks ++ ; } self . _plt . timeout ( checkKeyboard , pollingInternval ) ; return promise ; } /**
     * Programmatically close the keyboard.
     */ close ( ) { this . _dom . read ( ( ) => { if ( this . isOpen ( ) ) { console . debug ( `keyboard, close()` ) ; this . _dom . write ( ( ) => { this . _plt . focusOutActiveElement ( ) ; } ) ; } } ) ; } hidden focusOutline ( setting ) { const self = this ; const platform = self . _plt ; const doc = platform . doc ( ) ; let isKeyInputEnabled = false ; let unRegMouse ; let unRegTouch ; const evOpts = { passive : true , zone : false } ; function cssClass ( ) { self . _dom . write ( ( ) => { ( < any > platform . doc ( ) . body . classList ) [ isKeyInputEnabled ? 'add' : 'remove' ] ( 'focus-outline' ) ; } ) ; } if ( setting === true ) { isKeyInputEnabled = true ; return cssClass ( ) ; } else if ( setting === false ) { return ; } function keyDown ( ev ) { if ( ! isKeyInputEnabled && ev . keyCode === KEY_TAB ) { isKeyInputEnabled = true ; enableKeyInput ( ) ; } } function pointerDown ( ) { isKeyInputEnabled = false ; enableKeyInput ( ) ; } function enableKeyInput ( ) { cssClass ( ) ; unRegMouse && unRegMouse ( ) ; unRegTouch && unRegTouch ( ) ; if ( isKeyInputEnabled ) { unRegMouse = platform . registerListener ( doc , 'mousedown' , pointerDown , evOpts ) ; unRegTouch = platform . registerListener ( doc , 'touchstart' , pointerDown , evOpts ) ; } } platform . registerListener ( platform . doc ( ) , 'keydown' , keyDown , evOpts ) ; } hasFocusedTextInput ( ) { const activeEle = this . _plt . getActiveElement ( ) ; if ( isTextInput ( activeEle ) ) { return ( activeEle . parentElement . querySelector ( ':focus' ) === activeEle ) ; } return false ; } param hidden hideFormAccessoryBar ( hidden ) { const win = this . _plt . win ( ) as any ; if ( win && win . Keyboard && win . Keyboard . hideFormAccessoryBar ) { win . Keyboard . hideFormAccessoryBar ( hidden ) ; } } } const KEYBOARD_CLOSE_POLLING = 150 ; const KEYBOARD_POLLING_CHECKS_MAX = 100 ;