import * as Cookies from 'js-cookie' ; import TestApp from './testApp' ; import { Config , getConfigFromUrl , getConfigFromStorage , flattenConfig , saveConfigToStorage } from './config' ; import { onSubmitForm , onFormData , showConfigForm } from './form' ; import { toQueryString } from './util' ; import { FormDataEvent } from './types' ; import { renderWidget , buildWidgetConfig } from './widget' ; declare global { interface Window { _testApp : TestApp ; _cookies : object ; onSubmitForm : ( event : Event ) => void ; onFormData : ( event : FormDataEvent ) => void ; bootstrapLanding : ( ) => void ; bootstrapLoginCallback : ( ) => void ; getWidgetConfig : ( ) => any ; getConfig : ( ) => any ; toQueryString : ( obj : any ) => string ; constructAppUrl : ( basePath : string ) => string ; navigateToApp : ( appPath : string , event : Event ) => void ; showConfigForm : ( event ? : Event ) => void ; } } let app ; let config ; const rootElem = document . getElementById ( 'root' ) ; window . addEventListener ( 'load' , ( ) => { rootElem && rootElem . classList . add ( 'loaded' ) ; } ) ; function mount ( ) { saveConfigToStorage ( config ) ; app = new TestApp ( config ) ; window . _testApp = app ; window . _cookies = Cookies ; app . mount ( window , rootElem ) ; return app ; } Object . assign ( window , { toQueryString : toQueryString , onSubmitForm : onSubmitForm , onFormData : onFormData , getConfig : function ( ) : Config { return window . location . search ? getConfigFromUrl ( ) : getConfigFromStorage ( ) ; } , constructAppUrl : function ( basePath = '' ) : string { const config = window . getConfig ( ) ; const queryParams = toQueryString ( flattenConfig ( config ) ) ; return basePath + queryParams ; } , navigateToApp : function ( appPath = '' , event ) : void { event . preventDefault ( ) ; const url = window . constructAppUrl ( appPath ) ; window . location . assign ( url ) ; } , showConfigForm : function ( event ? ) { if ( event ) { event . preventDefault ( ) ; } const config = window . getConfig ( ) ; showConfigForm ( config ) ; } , getWidgetConfig : function ( ) : any { const siwConfig = window . getConfig ( ) ; return buildWidgetConfig ( siwConfig ) ; } , renderWidget : function ( event ? , extraConfig ? ) : any { if ( event ) { event . preventDefault ( ) ; } const config = Object . assign ( { } , window . getConfig ( ) , extraConfig ) ; return renderWidget ( config ) ; } , bootstrapLanding : function ( ) : void { config = window . getConfig ( ) ; mount ( ) ; app . bootstrapHome ( ) ; } , bootstrapLoginCallback : function ( ) : void { config = getConfigFromStorage ( ) ; mount ( ) ; app . bootstrapLoginCallback ( ) ; } , bootstrapProtected : function ( ) : void { config = window . getConfig ( ) ; mount ( ) ; app . bootstrapProtected ( ) ; } } ) ;