import SandDance = SandDanceExplorer . SandDance ; import VegaDeckGl = SandDance . VegaDeckGl ; declare var vega ; declare var deck ; declare var luma ; declare var FluentUIReact ; namespace SandDanceEmbed { interface DataWithInsight { data : DataToLoad ; insight : Partial < SandDance . specs . Insight > ; } export let sandDanceExplorer ; export const requests = [ ] ; export function load ( data , insight ? ) { return new Promise ( ( resolve ) => { const innerLoad = ( ) => { let getPartialInsight ; if ( insight ) { getPartialInsight = columns => insight ; } sandDanceExplorer . load ( data , getPartialInsight ) . then ( resolve ) ; } ; const create = ( ) => { SandDanceExplorer . use ( FluentUIReact , React , ReactDOM , vega , deck , deck , luma ) ; const explorerProps = { logoClickUrl : 'https://microsoft.github.io/SandDance/' , mounted : explorer => { sandDanceExplorer = explorer ; innerLoad ( ) ; } } ; ReactDOM . render ( React . createElement ( SandDanceExplorer . Explorer , explorerProps ) , document . getElementById ( 'app' ) ) ; } ; if ( sandDanceExplorer ) { innerLoad ( ) ; } else { create ( ) ; } } ) ; } export function respondToRequest ( requestWithSource ) { requests . push ( requestWithSource ) ; const copy = { ... requestWithSource } ; delete copy . source ; const request = { ... copy } ; let response ; switch ( request . action ) { case 'init' : { response = { request } ; break ; } case 'load' : { const request_load = request as MessageRequest_Load ; load ( request_load . data , request_load . insight ) . then ( ( ) => { response = { request } ; requestWithSource . source . postMessage ( response , '*' ) ; } ) ; delete request_load . data ; break ; } case 'getData' : { response = < MessageResponse_GetData > { request , data : sandDanceExplorer . state . dataContent . data } ; break ; } case 'getInsight' : { response = < MessageResponse_GetInsight > { request , insight : sandDanceExplorer . viewer . getInsight ( ) } ; break ; } } if ( response ) { requestWithSource . source . postMessage ( response , '*' ) ; } } window . addEventListener ( 'message' , e => { let payload = e . data ; if ( ! payload ) return ; if ( Array . isArray ( payload ) ) { const data = payload ; const requestLoadFromArray = { action : 'load' , data , insight : null } ; payload = requestLoadFromArray ; } else { const dataWithInsight = payload as DataWithInsight ; if ( Array . isArray ( dataWithInsight . data ) ) { const requestLoadFromDataWithInsight = { action : 'load' , ... dataWithInsight } ; payload = requestLoadFromDataWithInsight ; } } const request = payload as MessageRequest ; if ( ! request ) return ; const requestWithSource = { ... request , source : e . source as WindowProxy } ; respondToRequest ( requestWithSource ) ; } ) ; if ( window . opener ) { const request = { action : 'init' , ts : new Date ( ) } ; respondToRequest ( { ... request , source : window . opener } ) ; } }