-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectPath.js
90 lines (78 loc) · 1.81 KB
/
SelectPath.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
var SelectPath = (function() {
var selected;
var infoBox = $('#pathInfo');
var fromState = $('#fromState');
var toState = $('#toState');
var label = $('#label');
var remove = $('#removePath');
return {
Setup: Setup,
Teardown: Teardown,
Select: Select
};
function DrawEvent(action) {
return function() {
action.apply(this, arguments);
Application.Draw();
}
}
function Setup() {
Events.On('StateChange', RefreshStateText);
Events.On('StateRemove', StateRemove);
label.on('keyup', DrawEvent(LabelChanged));
remove.on('click', DrawEvent(RemoveSelected));
}
function Teardown() {
Events.Off('StateChange', RefreshStateText);
Events.Off('StateRemove', StateRemove);
label.off('keyup');
remove.off('click');
}
function RefreshStateText() {
if (selected) {
fromState.text(selected.source.label());
toState.text(selected.destination.label());
}
}
function LabelChanged() {
selected.label = label.val();
}
function RemoveSelected() {
for (var i=0; i < DFA.states.length; i++) {
var state = DFA.states[i];
for (var j=0; j < state.paths.length; j++) {
if (state.paths[j] == selected) {
state.paths.splice(j, 1);
j--;
}
}
}
Select(LastPath());
}
function LastPath() {
for (var i=DFA.states.length-1; i >= 0; i--) {
var state = DFA.states[i];
for (var j=state.paths.length-1; j >= 0; j--) {
return state.paths[j];
}
}
return null;
}
function Select(path) {
selected = path;
if (path) {
infoBox.show();
label.val(path.label);
RefreshStateText();
} else {
infoBox.hide();
}
}
function StateRemove(state) {
if (selected) {
if (selected.source == state || selected.destination == state) {
Select(null);
}
}
}
})();