import React from 'react' ; import { EventSubscription } from 'react-native' ; import pushRoute , { overwrite } from './push-route' ; import { AppConfigType , DrawerConfig , NavLayout , NavLayoutStackChildren , NavModal , NavOptions } from '../types' ; export interface GenericNavProp { appConfig : AppConfigType ; history : any ; modals : NavModal [ ] ; toggleDrawerFn ? : ( config : DrawerConfig ) => void ; updateModals : ( modals : NavModal [ ] ) => void ; } export default class Navigator { props : GenericNavProp ; constructor ( props ) { this . props = props ; } async push ( layout , alternateId ? ) { pushRoute ( layout , this . props . history , this . props . appConfig , alternateId ) ; } async pop ( options ? , alternateId ? ) { this . props . history . goBack ( ) ; } async popToRoot ( options ? , alternateId ? ) { if ( this . props . appConfig . screen && this . props . appConfig . screens [ this . props . appConfig . screen . name ] ) { this . props . history . push ( this . props . appConfig . screens [ this . props . appConfig . screen . name ] . path || '/' ) ; } else { this . props . history . push ( '/' ) ; } } async popTo ( options ? , alternateId ? ) { if ( alternateId ) { if ( this . props . appConfig . screens [ alternateId ] ) { this . props . history . push ( this . props . appConfig . screens [ alternateId ] . path || '/' ) ; } else { console . error ( 'Unknown screen: ' + alternateId ) ; } } else { console . warn ( 'Specify a screen to pop to.' ) ; } } async setStackRoot ( layout , alternateId ? ) { pushRoute ( layout , this . props . history , this . props . appConfig ) ; } async showStackedModal ( layout ) { if ( layout . component ) { this . props . modals . push ( { layout } ) ; } } async showModal ( layout ) { if ( layout . stack && layout . stack . children ) { for ( let loop = 0 ; loop < layout . stack . children . length ; loop ++ ) { await this . showModal ( layout . stack . children [ loop ] ) ; } } await this . showStackedModal ( layout ) ; this . props . updateModals ( this . props . modals ) ; } async dismissModal ( options ? , alternateId ? ) { this . props . modals . pop ( ) ; this . props . updateModals ( this . props . modals ) ; } async dismissAllModals ( options ? ) { this . props . modals = [ ] ; this . props . updateModals ( this . props . modals ) ; } async updateProps ( newProps , alternateId ? ) { overwrite ( newProps , this . props . history , this . props . appConfig ) ; } mergeOptions ( options , alternateId ? ) { if ( options . sideMenu && this . props . toggleDrawerFn ) { if ( options . sideMenu . left && options . sideMenu . left . visible !== undefined ) { this . props . toggleDrawerFn ( { side : 'left' , to : options . sideMenu . left . visible ? 'open' : 'closed' } ) ; } else if ( options . sideMenu . right && options . sideMenu . right . visible !== undefined ) { this . props . toggleDrawerFn ( { side : 'right' , to : options . sideMenu . right . visible ? 'open' : 'closed' } ) ; } } return ; } bindNavigation ( bindee , alternateId ? ) { console . warn ( 'binding to navigation events is not supported in web' ) ; return null ; } handleDeepLink ( options ) { console . error ( 'handleDeepLink is no longer part of react-native-navigation' ) ; } setOnNavigatorEvent ( ) { console . error ( 'setOnNavigatorEvent is no longer part of react-native-navigation. ' + 'Please use navigator.bindNavigation(this) to bind events, then reference ' + 'https://wix.github.io/react-native-navigation/#/docs/events' ) ; } resetTo ( options ) { console . warn ( 'resetTo has been deprecated. ' + 'Please use setStackRoot' ) ; this . setStackRoot ( { component : { name : options . screen , options : { topBar : { title : { text : options . title } } } } } ) . catch ( e => { console . error ( e ) ; } ) ; } setStyle ( options ) { console . warn ( 'setStyle has been deprecated. ' + 'Please use mergeOptions({\n  topBar: {\n    alignment: \'center\'\n  }\n}) instead' ) ; } setTabBadge ( options ) { console . warn ( 'setTabBadge has been deprecated. ' + 'Please use mergeOptions({\n  bottomTab: {\n    badge: \'1\',\n    ' + 'badgeColor: \'rgb(255, 255, 255)\',\n    ' + 'icon: iconImageSource\n  }\n}, componentIdOfTab) instead' ) ; } setTitle ( options ) { console . warn ( 'setTitle has been deprecated. ' + 'Please use mergeOptions({\n  topBar: {\n    title: \'title\\n  }\n}) instead' ) ; } switchToTab ( options ) { console . warn ( 'switchToTab has been deprecated. ' + 'Please use mergeOptions({\n  bottomTabs: {\n    currentTabIndex: 0\n  }\n}) instead' ) ; } }