11import React from 'react' ;
22import { connect } from 'react-redux' ;
3- import { classes } from '/common/util' ;
4- import { BaseComponent , ResizableContainer } from '/components' ;
3+ import { BaseComponent } from '/components' ;
54import { actions } from '/reducers' ;
65import styles from './stylesheet.scss' ;
7- import { Array1DData , Array2DData , ChartData , Data , GraphData , LogData , MarkdownData } from '/core/datas' ;
6+ import * as TracerClasses from '/core/tracers' ;
7+ import * as LayoutClasses from '/core/layouts' ;
8+ import { classes } from '/common/util' ;
89
910@connect ( ( { player } ) => ( { player } ) , actions )
1011class VisualizationViewer extends BaseComponent {
1112 constructor ( props ) {
1213 super ( props ) ;
1314
14- this . state = {
15- dataWeights : { } ,
16- } ;
15+ this . reset ( ) ;
16+ }
1717
18- this . datas = [ ] ;
18+ reset ( ) {
19+ this . root = null ;
20+ this . objects = { } ;
1921 }
2022
2123 componentDidMount ( ) {
@@ -36,19 +38,11 @@ class VisualizationViewer extends BaseComponent {
3638 if ( cursor > oldCursor ) {
3739 applyingChunks = chunks . slice ( oldCursor , cursor ) ;
3840 } else {
39- this . datas = [ ] ;
41+ this . reset ( ) ;
4042 applyingChunks = chunks . slice ( 0 , cursor ) ;
4143 }
4244 applyingChunks . forEach ( chunk => this . applyChunk ( chunk ) ) ;
4345
44- const dataWeights = chunks === oldChunks ? { ...this . state . dataWeights } : { } ;
45- this . datas . forEach ( data => {
46- if ( ! ( data . tracerKey in dataWeights ) ) {
47- dataWeights [ data . tracerKey ] = 1 ;
48- }
49- } ) ;
50- this . setState ( { dataWeights } ) ;
51-
5246 const lastChunk = applyingChunks [ applyingChunks . length - 1 ] ;
5347 if ( lastChunk && lastChunk . lineNumber !== undefined ) {
5448 this . props . setLineIndicator ( { lineNumber : lastChunk . lineNumber , cursor } ) ;
@@ -57,60 +51,43 @@ class VisualizationViewer extends BaseComponent {
5751 }
5852 }
5953
60- addTracer ( className , tracerKey , title ) {
61- const DataClass = {
62- Tracer : Data ,
63- MarkdownTracer : MarkdownData ,
64- LogTracer : LogData ,
65- Array2DTracer : Array2DData ,
66- Array1DTracer : Array1DData ,
67- ChartTracer : ChartData ,
68- GraphTracer : GraphData ,
69- } [ className ] ;
70- const data = new DataClass ( tracerKey , title , this . datas ) ;
71- this . datas . push ( data ) ;
72- }
73-
74- applyTrace ( trace ) {
75- const { tracerKey, method, args } = trace ;
54+ applyCommand ( command ) {
55+ const { key, method, args } = command ;
7656 try {
77- if ( method === 'construct' ) {
78- const [ className , title ] = args ;
79- this . addTracer ( className , tracerKey , title ) ;
57+ if ( key === null && method === 'setRoot' ) {
58+ const [ root ] = args ;
59+ this . root = this . objects [ root ] ;
60+ } else if ( method === 'destroy' ) {
61+ delete this . objects [ key ] ;
62+ } else if ( method in LayoutClasses ) {
63+ const [ children ] = args ;
64+ const LayoutClass = LayoutClasses [ method ] ;
65+ this . objects [ key ] = new LayoutClass ( key , key => this . objects [ key ] , children ) ;
66+ } else if ( method in TracerClasses ) {
67+ const [ title ] = args ;
68+ const TracerClass = TracerClasses [ method ] ;
69+ this . objects [ key ] = new TracerClass ( key , key => this . objects [ key ] , title ) ;
8070 } else {
81- const data = this . datas . find ( data => data . tracerKey === tracerKey ) ;
82- data [ method ] ( ...args ) ;
71+ this . objects [ key ] [ method ] ( ...args ) ;
8372 }
8473 } catch ( error ) {
8574 this . handleError ( error ) ;
8675 }
8776 }
8877
8978 applyChunk ( chunk ) {
90- chunk . traces . forEach ( trace => this . applyTrace ( trace ) ) ;
91- }
92-
93- handleChangeWeights ( weights ) {
94- const dataWeights = { } ;
95- weights . forEach ( ( weight , i ) => {
96- dataWeights [ this . datas [ i ] . tracerKey ] = weight ;
97- } ) ;
98- this . setState ( { dataWeights } ) ;
79+ chunk . commands . forEach ( command => this . applyCommand ( command ) ) ;
9980 }
10081
10182 render ( ) {
10283 const { className } = this . props ;
103- const { dataWeights } = this . state ;
10484
10585 return (
106- < ResizableContainer className = { classes ( styles . visualization_viewer , className ) }
107- weights = { this . datas . map ( data => dataWeights [ data . tracerKey ] ) }
108- visibles = { this . datas . map ( ( ) => true ) }
109- onChangeWeights = { weights => this . handleChangeWeights ( weights ) } >
86+ < div className = { classes ( styles . visualization_viewer , className ) } >
11087 {
111- this . datas . map ( data => data . render ( ) )
88+ this . root && this . root . render ( )
11289 }
113- </ ResizableContainer >
90+ </ div >
11491 ) ;
11592 }
11693}
0 commit comments