import { customEvent } from '../helper/app.helper' ; import { MOOA_EVENT } from '../model/constants' ; declare const window ; window . mooa = window . mooa || { } ; export class MooaPlatform { name : string = '' ; router : any ; mount ( name , router ? ) { this . name = name ; this . router = router ; return new Promise ( ( resolve , reject ) => { if ( this . isSingleSpaApp ( ) ) { customEvent ( MOOA_EVENT . CHILD_MOUNT , { name : this . name } ) ; window . mooa [ this . name ] = window . mooa [ this . name ] || { } ; window . mooa [ this . name ] . mount = ( props ) => { resolve ( { props , attachUnmount : this . unmount . bind ( this ) } ) ; } ; } else { resolve ( { props : { } , attachUnmount : this . unmount . bind ( this ) } ) ; } } ) ; } unmount ( module ) { if ( this . isSingleSpaApp ( ) ) { customEvent ( MOOA_EVENT . CHILD_UNMOUNT , { name : this . name } ) ; window . mooa [ this . name ] . unmount = ( ) => { if ( module ) { module . destroy ( ) ; if ( this . router ) { module . injector . get ( this . router ) . dispose ( ) ; } } } ; } } appBase ( ) { if ( this . isSingleSpaApp ( ) ) { const pathNames = window . location . pathname . split ( '/' ) ; if ( pathNames . length < 2 ) { return '/' ; } const parentRouter = pathNames [ 1 ] ; const appName = pathNames [ 2 ] ; const locationPath = '/' + parentRouter + '/' + appName ; window . mooa . basePath = locationPath ; return locationPath ; } else { return '/' ; } } navigateTo ( opts ) { customEvent ( MOOA_EVENT . ROUTING_NAVIGATE , opts ) ; } handleRouterUpdate ( router , appName ) { window . addEventListener ( MOOA_EVENT . ROUTING_CHANGE , ( event ) => { if ( event . detail . app . name === appName ) { let urlPrefix = 'app' ; if ( urlPrefix ) { urlPrefix = `/${ window . mooa . option . urlPrefix }/` ; } router . navigate ( [ event . detail . url . replace ( urlPrefix + appName , '' ) ] ) ; } } ) ; } private isSingleSpaApp ( ) { return window . mooa . isSingleSpa ; } } export default MooaPlatform ;