import { ComponentRef , ElementRef , EventEmitter , Output , Renderer } from '@angular/core' ; import { assert , isPresent } from '../util/util' ; import { NavController } from './nav-controller' ; import { NavOptions , STATE_ATTACHED , STATE_DESTROYED , STATE_INITIALIZED , STATE_NEW } from './nav-util' ; import { NavParams } from './nav-params' ; import { Content , Footer , Header , Navbar } from './nav-interfaces' ; name description usage Component export class ViewController { private _cntDir : any ; private _cntRef : ElementRef ; private _ionCntDir : Content ; private _ionCntRef : ElementRef ; private _hdrDir : Header ; private _ftrDir : Footer ; private _isHidden : boolean = false ; private _leavingOpts : NavOptions ; private _nb : Navbar ; private _onDidDismiss : ( data : any , role : string ) => void ; private _onWillDismiss : ( data : any , role : string ) => void ; private _dismissData : any ; private _dismissRole : string ; private _detached : boolean ; _cmp : ComponentRef < any > ; _nav : NavController ; _zIndex : number ; _state : number = STATE_NEW ; _cssClass : string ; _ts : number ; _bindHandler : any ; returns { Observable } willEnter : EventEmitter < any > = new EventEmitter ( ) ; returns { Observable } didEnter : EventEmitter < any > = new EventEmitter ( ) ; returns { Observable } willLeave : EventEmitter < any > = new EventEmitter ( ) ; returns { Observable } didLeave : EventEmitter < any > = new EventEmitter ( ) ; returns { Observable } willUnload : EventEmitter < any > = new EventEmitter ( ) ; hidden readReady : EventEmitter < any > = new EventEmitter < any > ( ) ; hidden writeReady : EventEmitter < any > = new EventEmitter < any > ( ) ; hidden data : any ; hidden instance : any ; hidden id : string ; hidden isOverlay : boolean = false ; hidden @ Output ( ) private _emitter : EventEmitter < any > = new EventEmitter ( ) ; constructor ( public component ? , data ? , rootCssClass = DEFAULT_CSS_CLASS ) { this . data = ( data instanceof NavParams ? data . data : ( isPresent ( data ) ? data : { } ) ) ; this . _cssClass = rootCssClass ; this . _ts = Date . now ( ) ; this . _bindHandler = this . handleOrientationChange . bind ( this ) ; window . addEventListener ( 'orientationchange' , this . _bindHandler ) ; } handleOrientationChange ( ) { if ( this . getContent ( ) ) { this . getContent ( ) . resize ( ) ; } } hidden init ( componentRef ) { assert ( componentRef , 'componentRef can not be null' ) ; this . _ts = Date . now ( ) ; this . _cmp = componentRef ; this . instance = this . instance || componentRef . instance ; this . _detached = false ; } _setNav ( navCtrl ) { this . _nav = navCtrl ; } _setInstance ( instance ) { this . instance = instance ; } hidden subscribe ( generatorOrNext ? ) { return this . _emitter . subscribe ( generatorOrNext ) ; } hidden emit ( data ? ) { this . _emitter . emit ( data ) ; } /**
     * Called when the current viewController has be successfully dismissed
     */ onDidDismiss ( callback ) { this . _onDidDismiss = callback ; } /**
     * Called when the current viewController will be dismissed
     */ onWillDismiss ( callback ) { this . _onWillDismiss = callback ; } param { any } data param { any } role param { NavOptions } navOptions returns { any } dismiss ( data ? , role ? , navOptions = { } ) { if ( ! this . _nav ) { assert ( this . _state === STATE_DESTROYED , 'ViewController does not have a valid _nav' ) ; return Promise . resolve ( false ) ; } if ( this . isOverlay && ! navOptions . minClickBlockDuration ) { navOptions . minClickBlockDuration = 400 ; } this . _dismissData = data ; this . _dismissRole = role ; const options = Object . assign ( { } , this . _leavingOpts , navOptions ) ; return this . _nav . removeView ( this , options ) . then ( ( ) => data ) ; } hidden getNav ( ) { return this . _nav ; } hidden getTransitionName ( _direction ) { return this . _nav && this . _nav . config . get ( 'pageTransition' ) ; } hidden getNavParams ( ) { return new NavParams ( this . data ) ; } hidden setLeavingOpts ( opts ) { this . _leavingOpts = opts ; } returns { boolean } enableBack ( ) { if ( ! this . _nav ) { return false ; } const previousItem = this . _nav . getPrevious ( this ) ; return ! ! ( previousItem ) ; } hidden get name ( ) : string { return ( this . component ? this . component . name : '' ) ; } returns { number } get index ( ) : number { return ( this . _nav ? this . _nav . indexOf ( this ) : - 1 ) ; } returns { boolean } isFirst ( ) { return ( this . _nav ? this . _nav . first ( ) === this : false ) ; } returns { boolean } isLast ( ) { return ( this . _nav ? this . _nav . last ( ) === this : false ) ; } hidden _domShow ( shouldShow , renderer ) { if ( this . _cmp && shouldShow === this . _isHidden ) { this . _isHidden = ! shouldShow ; let value = ( shouldShow ? null : '' ) ; renderer . setElementAttribute ( this . pageRef ( ) . nativeElement , 'hidden' , value ) ; } } hidden getZIndex ( ) { return this . _zIndex ; } hidden _setZIndex ( zIndex , renderer ) { if ( zIndex !== this . _zIndex ) { this . _zIndex = zIndex ; const pageRef = this . pageRef ( ) ; if ( pageRef ) { renderer . setElementStyle ( pageRef . nativeElement , 'z-index' , ( < any > zIndex ) ) ; } } } returns { ElementRef } pageRef ( ) { return this . _cmp && this . _cmp . location ; } _setContent ( directive ) { this . _cntDir = directive ; } returns { component } getContent ( ) { return this . _cntDir ; } _setContentRef ( elementRef ) { this . _cntRef = elementRef ; } returns { ElementRef } contentRef ( ) { return this . _cntRef ; } _setIONContent ( content ) { this . _setContent ( content ) ; this . _ionCntDir = content ; } hidden getIONContent ( ) { return this . _ionCntDir ; } _setIONContentRef ( elementRef ) { this . _setContentRef ( elementRef ) ; this . _ionCntRef = elementRef ; } hidden getIONContentRef ( ) { return this . _ionCntRef ; } _setHeader ( directive ) { this . _hdrDir = directive ; } hidden getHeader ( ) { return this . _hdrDir ; } _setFooter ( directive ) { this . _ftrDir = directive ; } hidden getFooter ( ) { return this . _ftrDir ; } _setNavbar ( directive ) { this . _nb = directive ; } hidden getNavbar ( ) { return this . _nb ; } returns { boolean } hasNavbar ( ) { return ! ! this . _nb ; } param { string } val setBackButtonText ( val ) { this . _nb && this . _nb . setBackButtonText ( val ) ; } param { boolean } Set showBackButton ( shouldShow ) { if ( this . _nb ) { this . _nb . hideBackButton = ! shouldShow ; } } _preLoad ( ) { assert ( this . _state === STATE_INITIALIZED , 'view state must be INITIALIZED' ) ; this . _lifecycle ( 'PreLoad' ) ; } hidden _willLoad ( ) { assert ( this . _state === STATE_INITIALIZED , 'view state must be INITIALIZED' ) ; this . _lifecycle ( 'WillLoad' ) ; } hidden _didLoad ( ) { assert ( this . _state === STATE_ATTACHED , 'view state must be ATTACHED' ) ; this . _lifecycle ( 'DidLoad' ) ; } hidden _willEnter ( ) { this . handleOrientationChange ( ) ; assert ( this . _state === STATE_ATTACHED , 'view state must be ATTACHED' ) ; if ( this . _detached && this . _cmp ) { this . _cmp . changeDetectorRef . reattach ( ) ; this . _detached = false ; } this . willEnter . emit ( null ) ; this . _lifecycle ( 'WillEnter' ) ; } hidden _didEnter ( ) { assert ( this . _state === STATE_ATTACHED , 'view state must be ATTACHED' ) ; this . _nb && this . _nb . didEnter ( ) ; this . didEnter . emit ( null ) ; this . _lifecycle ( 'DidEnter' ) ; } hidden _willLeave ( willUnload ) { this . willLeave . emit ( null ) ; this . _lifecycle ( 'WillLeave' ) ; if ( willUnload && this . _onWillDismiss ) { this . _onWillDismiss ( this . _dismissData , this . _dismissRole ) ; this . _onWillDismiss = null ; } } hidden _didLeave ( ) { this . didLeave . emit ( null ) ; this . _lifecycle ( 'DidLeave' ) ; if ( ! this . _detached && this . _cmp ) { this . _cmp . changeDetectorRef . detach ( ) ; this . _detached = true ; } } hidden _willUnload ( ) { this . willUnload . emit ( null ) ; this . _lifecycle ( 'WillUnload' ) ; this . _onDidDismiss && this . _onDidDismiss ( this . _dismissData , this . _dismissRole ) ; this . _onDidDismiss = null ; this . _dismissData = null ; this . _dismissRole = null ; } hidden _destroy ( renderer ) { assert ( this . _state !== STATE_DESTROYED , 'view state must be ATTACHED' ) ; if ( this . _cmp ) { if ( renderer ) { var cmpEle = this . _cmp . location . nativeElement ; renderer . setElementAttribute ( cmpEle , 'class' , null ) ; renderer . setElementAttribute ( cmpEle , 'style' , null ) ; } window . removeEventListener ( 'orientationchange' , this . _bindHandler ) ; this . _cmp . destroy ( ) ; } this . _nav = this . _cmp = this . instance = this . _cntDir = this . _cntRef = this . _leavingOpts = this . _hdrDir = this . _ftrDir = this . _nb = this . _onDidDismiss = this . _onWillDismiss = null ; this . _state = STATE_DESTROYED ; } hidden _lifecycleTest ( lifecycle ) { const instance = this . instance ; const methodName = 'ionViewCan' + lifecycle ; if ( instance && instance [ methodName ] ) { try { var result = instance [ methodName ] ( ) ; if ( result instanceof Promise ) { return result ; } else { return Promise . resolve ( result !== false ) ; } } catch ( e ) { return Promise . reject ( `${ this . name } ${ methodName } error: ${ e . message }` ) ; } } return Promise . resolve ( true ) ; } hidden _lifecycle ( lifecycle ) { const instance = this . instance ; const methodName = 'ionView' + lifecycle ; if ( instance && instance [ methodName ] ) { instance [ methodName ] ( ) ; } } } export function isViewController ( viewCtrl ) { return ! ! ( viewCtrl && ( < ViewController > viewCtrl ) . _didLoad && ( < ViewController > viewCtrl ) . _willUnload ) ; } const DEFAULT_CSS_CLASS = 'ion-page' ;