1- import React from 'react' ;
1+ import React from 'react'
22
3- import { Box , Grid } from '@material-ui/core' ;
4- import { withStyles } from '@material-ui/core/styles' ;
3+ import { Box , Grid } from '@material-ui/core'
4+ import { withStyles } from '@material-ui/core/styles'
55import {
66 ErrorDialog ,
77 Topbar ,
88 LayoutManager ,
99 Drawer ,
1010 Dialog ,
1111 ConfirmationDialog ,
12- LaunchDialog ,
13- } from 'netpyne/components' ;
14-
12+ LaunchDialog
13+ } from 'netpyne/components'
14+ import { loadModel } from '../redux/actions/general'
1515
1616const styles = ( { zIndex } ) => ( {
1717 root : {
1818 height : '100%' ,
1919 overflow : 'hidden' ,
2020 display : 'flex' ,
21- flex : 1 ,
21+ flex : 1
2222 } ,
2323 container : {
2424 display : 'flex' ,
2525 flex : 1 ,
2626 alignItems : 'stretch' ,
27- flexDirection : 'column' ,
27+ flexDirection : 'column'
2828 } ,
2929 topbar : {
3030 position : 'relative' ,
31- zIndex : zIndex . drawer ,
31+ zIndex : zIndex . drawer
3232 } ,
3333 content : {
3434 flexGrow : 1 ,
3535 display : 'flex' ,
3636 flexDirection : 'row' ,
37- position : 'relative' ,
37+ position : 'relative'
3838 } ,
39- noGrow : { flexGrow : 0 } ,
40- } ) ;
41-
42-
39+ noGrow : { flexGrow : 0 }
40+ } )
4341
4442class NetPyNE extends React . Component {
4543 constructor ( props ) {
46- super ( props ) ;
47- this . openPythonCallDialog = this . openPythonCallDialog . bind ( this ) ;
44+ super ( props )
45+ this . openPythonCallDialog = this . openPythonCallDialog . bind ( this )
46+ this . loaded = false
4847 }
4948
5049 componentDidMount ( ) {
51- GEPPETTO . on ( GEPPETTO . Events . Error_while_exec_python_command , this . openPythonCallDialog , this ) ;
50+ GEPPETTO . on ( GEPPETTO . Events . Error_while_exec_python_command , this . openPythonCallDialog , this )
5251
5352 const {
54- setDefaultWidgets,
55- } = this . props ;
53+ setDefaultWidgets
54+ } = this . props
5655
57- setDefaultWidgets ( ) ;
56+ setDefaultWidgets ( )
57+
58+ // Listen messages
59+ const loadFromEvent = event => {
60+ console . log ( 'Parent frame message received:' , event )
61+
62+ // Here we would expect some cross-origin check, but we don't do anything more than load a model here
63+ switch ( event . data . type ) {
64+ case 'INIT_INSTANCE' :
65+ if ( this . loaded ) {
66+ return
67+ }
68+ this . loaded = true
69+ console . log ( 'Netpyne is ready' )
70+ if ( window !== window . parent ) {
71+ window . parent . postMessage ( {
72+ type : 'APP_READY'
73+ } , '*' )
74+ }
75+ break
76+ case 'LOAD_RESOURCE' : {
77+ const resource = event . data . payload
78+ this . props . dispatchAction ( loadModel ( resource ) )
79+ }
80+ }
81+ }
82+ // A message from the parent frame can specify the file to load
83+ window . addEventListener ( 'message' , loadFromEvent )
84+ // window.load = loadFromEvent
5885 }
5986
6087 componentWillUnmount ( ) {
61- GEPPETTO . off ( GEPPETTO . Events . Error_while_exec_python_command , this . openPythonCallDialog , this ) ;
88+ GEPPETTO . off ( GEPPETTO . Events . Error_while_exec_python_command , this . openPythonCallDialog , this )
6289 }
6390
6491 openPythonCallDialog ( event ) {
6592 if ( event ?. evalue && event ?. traceback ) {
6693 this . props . pythonCallErrorDialogBox ( {
6794 errorMessage : event . evalue ,
68- errorDetails : event . traceback . join ( '\n' ) ,
69- } ) ;
95+ errorDetails : event . traceback . join ( '\n' )
96+ } )
7097 } else {
7198 this . props . pythonCallErrorDialogBox ( {
7299 errorMessage : event . data . response . evalue ,
73- errorDetails : event . data . response . traceback . join ( '\n' ) ,
74- } ) ;
100+ errorDetails : event . data . response . traceback . join ( '\n' )
101+ } )
75102 }
76103 }
77104
78-
79-
80105 render ( ) {
81- const { classes } = this . props ;
106+ const { classes } = this . props
82107
83- const Layout = LayoutManager ( ) ;
108+ const Layout = LayoutManager ( )
84109 return (
85110 < div className = { classes . root } >
86111 < div className = { classes . container } >
87112 < div className = { classes . topbar } >
88113 < Topbar />
89114 </ div >
90- < Box p = { 1 } flex = { 1 } display = " flex" alignItems = " stretch" >
91- < Grid container spacing = { 1 } className = { classes . content } alignItems = " stretch" >
115+ < Box p = { 1 } flex = { 1 } display = ' flex' alignItems = ' stretch' >
116+ < Grid container spacing = { 1 } className = { classes . content } alignItems = ' stretch' >
92117 < Grid item className = { classes . noGrow } >
93118 < Drawer />
94119 </ Grid >
@@ -103,8 +128,8 @@ class NetPyNE extends React.Component {
103128 < ErrorDialog />
104129 < LaunchDialog />
105130 </ div >
106- ) ;
131+ )
107132 }
108133}
109134
110- export default withStyles ( styles ) ( NetPyNE ) ;
135+ export default withStyles ( styles ) ( NetPyNE )
0 commit comments