2022-12-31 16:08:42 -06:00
|
|
|
import mermaid from 'https://unpkg.com/mermaid@9.2.2/dist/mermaid.esm.min.mjs';
|
|
|
|
import { Actor } from './actor.js';
|
|
|
|
import { Action } from './action.js';
|
|
|
|
import { debounce } from '../util.js';
|
2022-11-07 17:44:57 -06:00
|
|
|
|
|
|
|
export class Scene {
|
|
|
|
constructor(name, rootBox) {
|
|
|
|
this.name = name;
|
|
|
|
this.box = rootBox.addBox(name);
|
|
|
|
this.titleBox = this.box.addBox().setInnerHTML(name);
|
2022-12-31 16:08:42 -06:00
|
|
|
this.box.addBox('Spacer').setInnerHTML(' ');
|
2022-11-07 17:44:57 -06:00
|
|
|
this.displayValuesBox = this.box.addBox(`${this.name}-values`);
|
2022-12-31 16:08:42 -06:00
|
|
|
this.box.addBox('Spacer').setInnerHTML(' ');
|
2022-11-13 12:23:30 -06:00
|
|
|
this.actors = new Set();
|
2022-11-30 09:13:52 -06:00
|
|
|
this.seqDiagramContainer = this.box.addBox(
|
2022-12-31 16:08:42 -06:00
|
|
|
`${this.name}-seq-diagram-container`,
|
2022-11-30 09:13:52 -06:00
|
|
|
);
|
|
|
|
this.seqDiagramElement = this.box
|
|
|
|
.addBox(`${this.name}-seq-diagram-element`)
|
|
|
|
.setId();
|
2022-11-14 10:17:43 -06:00
|
|
|
this.seqDiagramBox = this.box.addBox(`${this.name}-seq-diagram`);
|
2022-12-31 16:08:42 -06:00
|
|
|
this.box.addBox('Spacer').setInnerHTML(' ');
|
2022-11-14 10:17:43 -06:00
|
|
|
this.logBox = this.box.addBox(`${this.name}-log`);
|
2023-01-03 11:59:44 -06:00
|
|
|
mermaid.mermaidAPI.initialize({
|
|
|
|
startOnLoad: false,
|
|
|
|
theme: 'base',
|
|
|
|
themeVariables: {
|
|
|
|
darkMode: true,
|
|
|
|
primaryColor: '#2a5b6c',
|
|
|
|
primaryTextColor: '#b6b6b6',
|
|
|
|
fontFamily: 'monospace',
|
|
|
|
noteBkgColor: '#516f77',
|
|
|
|
noteTextColor: '#cecece',
|
|
|
|
},
|
|
|
|
});
|
2022-11-14 10:17:43 -06:00
|
|
|
this.dateLastRender = null;
|
2022-11-07 17:44:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
addActor(name) {
|
|
|
|
const actor = new Actor(name, this);
|
|
|
|
return actor;
|
|
|
|
}
|
|
|
|
|
2022-11-13 12:23:30 -06:00
|
|
|
registerActor(actor) {
|
|
|
|
this.actors.add(actor);
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:44:57 -06:00
|
|
|
addAction(name) {
|
|
|
|
const action = new Action(name, this);
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
addDisplayValue(name) {
|
|
|
|
const dv = this.displayValuesBox.addDisplayValue(name);
|
|
|
|
return dv;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(msg) {
|
|
|
|
this.logBox.addBox().setInnerHTML(msg).monospace();
|
2022-11-14 10:17:43 -06:00
|
|
|
this.renderSequenceDiagram();
|
2022-11-07 17:44:57 -06:00
|
|
|
return this;
|
|
|
|
}
|
2022-11-11 16:52:57 -06:00
|
|
|
|
2022-11-13 12:23:30 -06:00
|
|
|
deactivateAll() {
|
|
|
|
for (const actor of this.actors.values()) {
|
|
|
|
while (actor.active) {
|
|
|
|
actor.deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 10:17:43 -06:00
|
|
|
async renderSequenceDiagram() {
|
|
|
|
const render = async () => {
|
|
|
|
const dateStart = new Date();
|
|
|
|
const graph = await mermaid.mermaidAPI.render(
|
|
|
|
this.seqDiagramElement.getId(),
|
2022-12-31 16:08:42 -06:00
|
|
|
this.logBox.getInnerText(),
|
2022-11-14 10:17:43 -06:00
|
|
|
);
|
|
|
|
this.seqDiagramBox.setInnerHTML(graph);
|
|
|
|
if (!this.dateLastRender) {
|
|
|
|
this.dateLastRender = new Date();
|
|
|
|
}
|
|
|
|
this.dateLastRender = dateStart;
|
|
|
|
};
|
|
|
|
debounce(render, 100);
|
|
|
|
}
|
2022-11-07 17:44:57 -06:00
|
|
|
}
|