simulator.js
4.04 KB
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
function addcss (css) {
$("head").append("<style type='text/css'>" + css + "</style>");
}
function addjs (js) {
$("head").append("<script type='text/javascript'>" + js + "</script>");
}
function dialog (content) {
$("#dialog").html(content);
$("#dialog").append("<a href='#' id='dialog-close'>close</a>");
$("#dialog-close").click(function() {
$("#dialog-bg").hide();
$("#dialog").hide();
});
$("#dialog").fadeTo(50, 1);
$("#dialog-bg").fadeTo(50, 0.5);
}
var update = {
settext : function (action) {
$(action.select).text(action.text);
},
sethtml : function (action) {
$(action.select).html(action.html);
},
clear : function (action) {
$(action.select).html("");
},
setlist : function (action) {
ul = $(action.select);
ul.html("");
$.each(action.items, function (num, item) {
ul.append("<li>" + item + "</li>");
});
}
}
function setstate (state) {
$.each(state.states, function (num, item) {
update[item.do](item);
});
$.each(state.modes, function (num, action) {
ul = $(action.select);
ul.html("");
$.each(action.items, function (pos, item) {
ul.append("<li><a href='#' data-mode='" + state.id + ":"
+ pos + "''>" + item + "</a></li>");
a = ul.children().last().children().first();
a.click(function () {
next = $(this).attr("data-mode").split(":");
text = $(this).text();
$("#trace").append("<div class='trace'><div class='state'>"
+ state.id + "</div><div class='mode'>"
+ text + "</div></div>");
$.get("succ", {state: next[0], mode: next[1]},
function (newstate) {
setstate(newstate);
});
});
});
});
}
$(document).ready(function() {
/*
* ping server every 10 seconds
*/
$("#alive .ping").text("Stayin alive!");
$.periodic({period: 10000, decay:1, max_period: 10000}, function() {
$.get("ping", function(data) {
$("#alive .ping").text(data);
});
});
/*
* setup UI and model
*/
$.get("init", function(init) {
/* bind reset button */
$("#ui-reset").click(function() {
$.get("init", {state: 0}, function(data) {
setstate(data.state);
$("#trace").html("");
});
});
/* bind quit button */
$("#ui-quit").click(function() {
$.get("quit", function(data) {
$("#alive .ui").html("").text(data);
});
});
/* dialogs */
$("body").append("<div id='dialog'>"
+ "</div><div id='dialog-bg'></div>");
/* build about */
$("#about").children().addClass("dialog");
$("#ui-about").click(function() {
dialog($("#about").html());
});
/* extend menu */
if (init.ui != undefined) {
ui = $("#alive .ui");
/* TODO: fix spacing between these buttons */
$.each(init.ui, function(num, menu) {
ui.append("<li><a id='" + menu.id
+ "'href='" + menu.href + "'>"
+ menu.label + "</a></li>");
if (menu.script != undefined) {
ui.children().last().click(function() {
eval(menu.script);
});
}
});
}
/* build help */
if (init.help != undefined) {
$.each(init.help, function(key, text) {
/* TODO: create help dic or whatever needed */
});
$("#ui-help").click(function() {
/* TODO: show/hide tooltips for help */
});
} else {
$("#ui-help").remove();
}
/* setup initial state */
setstate(init.state);
});
});