@@ -20,22 +20,53 @@ import './StateGraph.scss';
2020// - zoom and pan state graph
2121
2222// TODO move to service.ts?
23- function createGraph ( definition ) {
23+ function createGraph ( props ) {
2424 const g = new dagreD3 . graphlib . Graph ( ) . setGraph ( { } ) ;
25- const states = definition . states ;
25+ const states = props . definition . states ;
2626 // create nodes
2727 for ( let state of states ) {
2828 g . setNode ( state . id , { label : state . id , class : `node-${ state . type } ` } ) ;
2929 // Round the corners of the nodes
3030 const node = g . node ( state . id ) ;
3131 node . rx = node . ry = 5 ;
32+
33+ //if instance is provided, highlight with the .active
34+ //
35+ if ( props . instance && props . instance . state === state . id ) {
36+ node . class += ' current-state' ;
37+
38+ }
39+
40+ }
41+
42+ function hasNavigatedState ( fromState , toState , instance ) {
43+ //reverse iterate through the actions,
44+ //when we have two actions in sequence that match the from state and the to state we return true
45+ const actions = instance . actions ;
46+
47+ for ( let i = 0 ; i < actions . length - 1 ; i ++ ) {
48+ // Check if the current action state matches the toState (since it's reversed)
49+ if ( actions [ i ] . state === toState && actions [ i + 1 ] . state === fromState ) {
50+ return true ;
51+ }
52+ }
53+ if ( actions [ 0 ] . state == fromState && toState == instance . state ) {
54+ //this is the final state where there is no action showing
55+ return true ;
56+ }
57+
58+ return false ;
3259 }
3360
3461 // create edges between nodes
3562 for ( let state of states ) {
3663 for ( let transition of state . transitions || [ ] ) {
64+ var hasNavigated = false ;
65+ if ( props . instance ) {
66+ hasNavigated = hasNavigatedState ( state . id , transition , props . instance ) ;
67+ }
3768 g . setEdge ( state . id , transition , {
38- class : 'edge-normal' ,
69+ class : 'edge-normal' + ( hasNavigated ? ' active' : '' ) ,
3970 curve : d3 . curveBasis ,
4071 arrowhead : 'normal'
4172 } ) ;
@@ -46,8 +77,8 @@ function createGraph(definition) {
4677 curve : d3 . curveBasis ,
4778 arrowhead : 'normal'
4879 } ) ;
49- } else if ( definition . onError ) {
50- g . setEdge ( state . id , definition . onError , {
80+ } else if ( props . definition . onError ) {
81+ g . setEdge ( state . id , props . definition . onError , {
5182 class : 'edge-error' ,
5283 curve : d3 . curveBasis ,
5384 arrowhead : 'normal'
@@ -84,8 +115,8 @@ function render(g, selector) {
84115
85116function StateGraph ( props ) {
86117 useEffect ( ( ) => {
87- console . info ( 'StateGraph' , props . definition ) ;
88- const g = createGraph ( props . definition ) ;
118+ console . info ( 'StateGraph' , props ) ;
119+ const g = createGraph ( props ) ;
89120 render ( g , 'svg#stategraph' ) ;
90121 return ( ) => {
91122 // Remove svg element which is created in render(), since it is not managed by React
0 commit comments