11import Split from "split.js" ;
22import { verifyAFD } from "./afd.js" ;
3- import { renderOut , renderOutString } from "./animateNode.js" ;
4- import { createAutomata } from "./automata.js" ;
3+ import { renderError , renderOut , renderOutString } from "./animateNode.js" ;
4+ import { createAutomata , clearAutomata } from "./automata.js" ;
55import { startDragTools } from "./dragTools.js" ;
66import { CANVAS_HEIGHT , initGraph } from "./graph.js" ;
77import { CircleShape , FILL_NODE_FINAL , NODE_WIDTH } from "./shapes.js" ;
@@ -10,6 +10,11 @@ import MicroModal from "micromodal"; // es6 module
1010const { graph, paper } = initGraph ( ) ;
1111const inputString = document . querySelector ( "#input-string" ) ;
1212const inputEl = document . querySelector ( "#input-label-name" ) ;
13+ const inputLabel = document . querySelector ( "#input-label-name" ) ;
14+ const inputState = document . querySelector ( "#input-state-name" ) ;
15+ const btnClearAll = document . querySelector ( "#btn-clear-all" ) ;
16+
17+ const automata = createAutomata ( ) ;
1318
1419function run ( ) {
1520 const data = graph . toJSON ( ) ;
@@ -21,7 +26,8 @@ function run() {
2126 const statesArr = [ ] ;
2227 const transitions = [ ] ;
2328
24- const automata = createAutomata ( ) ;
29+ // clear errors
30+ renderError ( null ) ;
2531
2632 elements . forEach ( ( el ) => {
2733 if ( el . attributes . type === "Circle" ) {
@@ -51,11 +57,14 @@ function run() {
5157 Object . values ( states ) . forEach ( ( state ) => statesArr . push ( state . text ) ) ;
5258
5359 if ( statesArr . length <= 0 ) {
54- alert ( "No states" ) ;
60+ renderError ( "No states" ) ;
5561 return ;
5662 }
5763
58- console . log ( transitions ) ;
64+ if ( ! statesArr . includes ( "q0" ) ) {
65+ renderError ( "Initial state not found: q0" ) ;
66+ return ;
67+ }
5968
6069 automata . alphabet = alphabet ;
6170 automata . initialState = "q0" ;
@@ -70,6 +79,34 @@ function run() {
7079 verifyAFD ( paper , graph , automata , string ) ;
7180}
7281
82+ function changeLabelName ( ) {
83+ const id = inputLabel . getAttribute ( "link-id" ) ;
84+ const currentLink = graph . getLinks ( ) . find ( ( link ) => link . id === id ) ;
85+
86+ currentLink . label ( 0 , {
87+ attrs : {
88+ text : {
89+ text : inputLabel . value || "λ" ,
90+ } ,
91+ } ,
92+ } ) ;
93+
94+ inputLabel . value = "" ;
95+ MicroModal . close ( "modal-label-name" ) ;
96+ }
97+
98+ function changeStateName ( ) {
99+ const id = inputState . getAttribute ( "state-id" ) ;
100+ const data = paper . model . getElements ( ) . find ( ( el ) => {
101+ return el . id === id ;
102+ } ) ;
103+
104+ if ( data ) data . attr ( "label/text" , inputState . value ) ;
105+
106+ inputState . value = "" ;
107+ MicroModal . close ( "modal-state-name" ) ;
108+ }
109+
73110window . addEventListener ( "DOMContentLoaded" , ( ) => {
74111 MicroModal . init ( ) ;
75112 Split ( [ "#paper" , "#split-1" ] , { sizes : [ 80 , 20 ] , minSize : 300 } ) ;
@@ -91,19 +128,28 @@ window.addEventListener("resize", () => {
91128 paper . setDimensions ( document . body . clientWidth ) ;
92129} ) ;
93130
94- document . querySelector ( "#btn-label-name" ) . addEventListener ( "click" , ( ) => {
95- const inputLabel = document . querySelector ( "#input-label-name" ) ;
96- const id = inputLabel . getAttribute ( "link-id" ) ;
97- const currentLink = graph . getLinks ( ) . find ( ( link ) => link . id === id ) ;
131+ // Button change label name
132+ // document.querySelector("#btn-label-name").addEventListener("click", () => {});
98133
99- currentLink . label ( 0 , {
100- attrs : {
101- text : {
102- text : inputLabel . value || "λ" ,
103- } ,
104- } ,
105- } ) ;
134+ inputLabel . addEventListener ( "keydown" , ( e ) => {
135+ if ( e . code === "Enter" ) {
136+ changeLabelName ( ) ;
137+ }
138+ } ) ;
106139
107- inputLabel . value = "" ;
108- MicroModal . close ( "modal-label-name" ) ;
140+ // Button change state name
141+ // document
142+ // .querySelector("#btn-state-name")
143+ // .addEventListener("click", changeStateName);
144+
145+ inputState . addEventListener ( "keydown" , ( e ) => {
146+ if ( e . code === "Enter" ) {
147+ changeStateName ( ) ;
148+ }
149+ } ) ;
150+
151+ // Clear all
152+ btnClearAll . addEventListener ( "click" , ( ) => {
153+ clearAutomata ( automata ) ;
154+ graph . clear ( ) ;
109155} ) ;
0 commit comments