Skip to content

Commit feae491

Browse files
committed
feat: highlight active node
work on #9
1 parent e942aef commit feae491

File tree

4 files changed

+98
-44
lines changed

4 files changed

+98
-44
lines changed

jetty/src/main/js/src/lg4j-executor.js

+54-38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import TWStyles from './twlit';
22

33
import { html, css, LitElement } from 'lit';
44

5+
/**
6+
* Asynchronously waits for a specified number of milliseconds.
7+
*
8+
* @param {number} ms - The number of milliseconds to wait.
9+
* @returns {Promise<void>} A promise that resolves after the specified delay.
10+
*/
11+
const delay = async (ms) => (new Promise(resolve => setTimeout(resolve, ms)));
12+
513
/**
614
* Asynchronously fetches data from a given fetch call and yields the data in chunks.
715
*
@@ -75,36 +83,15 @@ export class LG4JExecutorElement extends LitElement {
7583
super.connectedCallback();
7684

7785
if(this.test ) {
78-
79-
setTimeout( () => {
80-
81-
this.dispatchEvent( new CustomEvent( 'graph', {
82-
detail: `
83-
flowchart TD
84-
Start --> Stop
85-
`,
86-
bubbles: true,
87-
composed: true,
88-
cancelable: true
89-
}));
90-
91-
this.formMetaData = {
92-
input: { type: 'string', required: true }
93-
}
94-
95-
this.requestUpdate()
96-
97-
}, 1000 );
98-
86+
this.#init_test();
87+
return
9988
}
100-
else {
10189

102-
this.#init()
103-
104-
}
90+
this.#init()
10591

10692
}
10793

94+
10895
async #init() {
10996

11097
const initResponse = await fetch( `${this.url}/init` )
@@ -124,6 +111,29 @@ export class LG4JExecutorElement extends LitElement {
124111
this.requestUpdate()
125112
}
126113

114+
async #init_test() {
115+
116+
await delay( 1000 );
117+
this.dispatchEvent( new CustomEvent( 'graph', {
118+
detail: `
119+
flowchart TD
120+
Start --> node1:::node1
121+
node1:::node1 --> node2:::node2
122+
node2:::node2 --> Stop
123+
`,
124+
bubbles: true,
125+
composed: true,
126+
cancelable: true
127+
}));
128+
129+
this.formMetaData = {
130+
input: { type: 'string', required: true }
131+
}
132+
133+
this.requestUpdate()
134+
135+
}
136+
127137
/**
128138
* Renders the HTML template for the component.
129139
*
@@ -141,22 +151,11 @@ export class LG4JExecutorElement extends LitElement {
141151
`;
142152
}
143153

154+
144155
async #submit() {
145-
// console.debug( 'test', this.test )
146156

147157
if(this.test ) {
148-
149-
setTimeout( () => {
150-
151-
this.dispatchEvent( new CustomEvent( 'result', {
152-
detail: { node: 'node1', state: { property1: "value1", property2: "value2" }},
153-
bubbles: true,
154-
composed: true,
155-
cancelable: true
156-
} ) );
157-
158-
}, 1000 );
159-
158+
await this.#submit_test();
160159
return
161160
}
162161

@@ -186,6 +185,23 @@ export class LG4JExecutorElement extends LitElement {
186185
}
187186
}
188187

188+
async #submit_test() {
189+
190+
const send = async ( nodeId ) => {
191+
await delay( 1000 );
192+
this.dispatchEvent( new CustomEvent( 'result', {
193+
detail: { node: nodeId, state: { property1: "value1", property2: "value2" }},
194+
bubbles: true,
195+
composed: true,
196+
cancelable: true
197+
}));
198+
}
199+
200+
await send( 'node1' );
201+
await send( 'node2');
202+
203+
}
204+
189205
}
190206

191207

jetty/src/main/js/src/lg4j-graph.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class LG4jMermaid extends LitElement {
2626
});
2727

2828
this._content = null
29+
this._activeClass = null
2930
}
3031

3132
#mermaidTask = new Task(this, {
@@ -52,18 +53,41 @@ export class LG4jMermaid extends LitElement {
5253
* @private
5354
*/
5455
get #textContent() {
55-
return this._content ?? this.#textNodes.map(node => node.textContent?.trim()).join('');
56+
if( this._content ) {
57+
58+
if( this._activeClass ) {
59+
return `
60+
${this._content}
61+
classDef ${this._activeClass} fill:#f96
62+
`
63+
}
64+
65+
return this._content
66+
}
67+
68+
return this.#textNodes.map(node => node.textContent?.trim()).join('');
5669
}
5770

5871
#onContent(e) {
59-
this._content = e.detail
72+
const { detail: newContent } = e
73+
74+
this._content = newContent
75+
this.requestUpdate()
76+
}
77+
78+
#onActive(e) {
79+
80+
const { detail: activeClass } = e
81+
82+
this._activeClass = activeClass;
6083
this.requestUpdate()
6184
}
6285

6386
connectedCallback() {
6487
super.connectedCallback()
6588

6689
this.addEventListener('graph', this.#onContent)
90+
this.addEventListener('graph-active', this.#onActive)
6791
// this.__observer = new MutationObserver(() => {
6892
// this.__observeTextNodes();
6993
// this.__renderGraph();
@@ -77,6 +101,7 @@ export class LG4jMermaid extends LitElement {
77101
super.disconnectedCallback()
78102

79103
this.removeEventListener('graph', this.#onContent)
104+
this.removeEventListener('graph-active', this.#onActive)
80105
// this.__cleanTextNodeObservers();
81106

82107
// if (this.__observer) {

jetty/src/main/js/src/lg4j-result.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,19 @@ export class LG4JResultElement extends LitElement {
6969
*/
7070
#onResult = (e) => {
7171

72+
const { detail: result } = e
7273
console.debug( "onResult", e )
7374

7475
// TODO: validate e.detail
75-
this.results.push( e.detail )
76+
this.results.push( result )
77+
78+
this.dispatchEvent( new CustomEvent( 'graph-active', {
79+
detail: result.node,
80+
bubbles: true,
81+
composed: true,
82+
cancelable: true
83+
}));
84+
7685
this.requestUpdate()
7786

7887
}

jetty/src/main/js/src/lg4j-workbench.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ export class LG4JWorkbenchElement extends LitElement {
3838

3939
const { type, detail } = e
4040

41-
console.log( 'routeEvent', type )
41+
const slot = type.split('-')[0]
4242

43-
const elem = this.querySelector(`[slot="${type}"]`)
43+
console.debug( 'routeEvent', type, slot )
44+
45+
const elem = this.querySelector(`[slot="${slot}"]`)
4446
if( !elem ) {
45-
console.error( `slot "${type}" not found!` )
47+
console.error( `slot "${slot}" not found!` )
4648
return
4749
}
4850
elem.dispatchEvent( new CustomEvent( type, { detail } ) )
@@ -53,13 +55,15 @@ export class LG4JWorkbenchElement extends LitElement {
5355

5456
this.addEventListener( "result", this.#routeEvent );
5557
this.addEventListener( "graph", this.#routeEvent );
58+
this.addEventListener( "graph-active", this.#routeEvent );
5659
}
5760

5861
disconnectedCallback() {
5962
super.disconnectedCallback()
6063

6164
this.removeEventListener( "result", this.#routeEvent );
6265
this.removeEventListener( "graph", this.#routeEvent );
66+
this.removeEventListener( "graph-active", this.#routeEvent );
6367
}
6468

6569
// firstUpdated() {

0 commit comments

Comments
 (0)