-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
176 lines (152 loc) · 6.22 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var count = 1;
var nfm = new NFM();
var computationPlumber;
jsPlumb.bind("ready", function() {
//chrome fix
document.onselectstart = function () { return false; };
/* initialize jsPlumb */
plumbing.init();
jsPlumb.setRenderMode(jsPlumb.SVG);
/* initialize tool box */
initToolBox();
/* initialize how to box */
initHowToBox();
/* initialize computation box */
initCompBox();
/* event Listener for addState button */
initAddState();
});
;(function() {
window.plumbing = {
init :function() {
jsPlumb.Defaults.PaintStyle = { strokeStyle:"gray", lineWidth:2 };
jsPlumb.importDefaults({
Endpoint : ["Dot", {radius:2}],
HoverPaintStyle : {strokeStyle:"#42a62c", lineWidth:2 },
ConnectionOverlays : [
[ "Arrow", {
location:1,
id:"arrow",
length:14,
foldback:0.8
} ]
]
});
jsPlumb.bind("click", function(c) {
jsPlumb.detach(c);
nfm.removeTransition(c.sourceId, c.targetId);
var key = c.sourceId + " to " + c.targetId;
var delta_id = "#deltas option[value='"+key+"']";
$(delta_id).remove();
});
jsPlumb.bind("jsPlumbConnection", function(conn) {
conn.connection.setPaintStyle({strokeStyle:'rgb(54, 164, 207)'});
var key = conn.sourceId + " to " + conn.targetId;
if (!nfm.transition.get(conn.sourceId))
nfm.transition.put(conn.sourceId, new HashMap());
nfm.transition.get(conn.sourceId).put(conn.targetId, undefined);
var delta_id = "#deltas option[value='"+key+"']";
if ($(delta_id).length == 0) {
$('#deltas')
.append($('<option>', { value : key})
.text(key));
}
else
jsPlumb.detach(conn);
});
}
};
initAddState = function() {
$('#addState').bind('click', function() {
var state_id = "state_"+count;
var state = $("<div class='w_node' id='"+state_id+"'>#"+count+" <div class='ep'></div></div>");
/* each state consists of:
* - start indicator
* - accept indicator
*/
var accept = $("<div class='accept'>A</div>");
var start = $("<div class='start'>S</div>");
state.append(accept);
state.append(start);
addAcceptBtListener(accept);
addStartBtListener(start);
$('#main').append(state);
addPlumbProp(state_id, state);
/* update global state variables */
nfm.addState(state_id);
count += 1;
});
};
initToolBox = function() {
$("#toolBox").draggable();
$("input:text, input:password, input[type=email]").button().addClass("ui-textfield");
$("#evaluate").click(function(e) {
nfm.input = $("#input").val();
if (!validate(e)) {
e.stopPropagation();
return false;
}
nfm.evaluate();
});
initTransitions();
initAlphabets();
initEpsilonClick();
};
initHowToBox = function() {
$("#howTo").draggable();
$("#howTo").html("How to use this demo? " +
"<ul><li>Add multiple states using the `Add State` link. </li>" +
"<li>Transition arrows between states can be made by holding the mouse pointer on <img src='square_block.png'/> symbol of the source state " +
"and dragging it to the desitination state. Self-loops can also be made by dragging the arrow to the source state itself.</li> " +
"<li>Every transistion will have a entry in the `Transition` drop down list. By selecting the transition from the drop down list," +
" the transition symbol(s) can be entered in the `Transistion symbols` box.</li>"+
"<li>Every transistion should have at least one symbol. Multiple symbols should be separatd by commas. " +
"Example: a,c,e,Ɛ,f. For convenience, the Ɛ symbol can be entered by pressing the Ɛ link provided in the Glossary.</li> " +
"<li>The automaton should have one start state and at least one accept state.</li> " +
"<li>The input string to be computed by the automaton can be provided in the `Input String` box.</li>" +
"<li>Press the `Compute` button to see the result in the bottom section of the page.</li></ul>");
};
initCompBox = function() {
$(".computation").draggable();
computationPlumber = jsPlumb.getInstance({
Connector : [ "StateMachine", { curviness:20 } ],
DragOptions : { cursor: "pointer", zIndex:2000 },
PaintStyle : { strokeStyle:"gray", lineWidth:2 },
Endpoint : [ "Dot", {radius:2} ],
Anchor : "Continuous",
ConnectionOverlays : [
[ "Arrow", {
location:1,
id:"arrow",
length:14,
foldback:0.8
}]]
});
};
initTransitions = function() {
$("#deltas").change(function() {
var delta = $(this).val().split("to");
var source = trim(delta[0]);
var target = trim(delta[1]);
var alphabets = nfm.transition.get(source).get(target);
$("#alphabets").val(alphabets);
});
};
initAlphabets = function() {
$("#alphabets").attr("placeholder", "Transition symbols");
$("#input").attr("placeholder", "Input string");
$("#alphabets").blur(function() {
if ($("#deltas").find(":selected").text()) {
var delta = $("#deltas").find(":selected").text().split("to");
var source = trim(delta[0]);
var target = trim(delta[1]);
nfm.transition.get(source).put(target, this.value);
}
});
};
initEpsilonClick = function() {
$("#e-symbol").click(function () {
$("#alphabets").val($("#alphabets").val() + "Ɛ");
});
};
})();