-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathtryclojure.js
More file actions
194 lines (172 loc) · 4.4 KB
/
tryclojure.js
File metadata and controls
194 lines (172 loc) · 4.4 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var currentPage = -1;
var pages = [
"page1",
"page2",
"page3",
"page4",
"page5",
"page6",
"page7",
"page8",
"page9",
"page10",
"page11",
"end"
];
var pageExitConditions = [
{
verify: function(data) { return false; }
},
{
verify: function(data) { return data.expr == "(+ 3 3)"; }
},
{
verify: function(data) { return data.expr == "(/ 10 3)"; }
},
{
verify: function(data) { return data.expr == "(/ 10 3.0)"; }
},
{
verify: function(data) { return data.expr == "(+ 1 2 3 4 5 6)"; }
},
{
verify: function (data) { return data.expr == "(defn square [x] (* x x))"; }
},
{
verify: function (data) { return data.expr == "(square 10)"; }
},
{
verify: function (data) { return data.expr == "((fn [x] (* x x)) 10)"; }
},
{
verify: function (data) { return data.expr == "(def square (fn [x] (* x x)))"; }
},
{
verify: function (data) { return data.expr == "(map inc [1 2 3 4])"; }
},
{
verify: function (data) { return false; }
},
{
verify: function (data) { return false; }
}
];
function goToPage(pageNumber) {
if (pageNumber == currentPage || pageNumber < 0 || pageNumber >= pages.length) {
return;
}
currentPage = pageNumber;
var block = $("#changer");
block.fadeTo("fast", 0, function(e) {
block.load("/tutorial", { 'page' : pages[pageNumber] }, function() {
block.fadeTo("fast", 100);
changerUpdated();
});
});
}
function setupLink(url) {
return function(e) { $("#changer").load(url, function(data) { $("#changer").html(data); }); }
}
function setupExamples(controller) {
$(".code").click(function(e) {
controller.promptText($(this).text());
});
}
function getStep(n, controller) {
$("#tuttext").load("tutorial", { step: n }, function() { setupExamples(controller); });
}
function eval_clojure(code) {
var data;
$.ajax({
url: "eval.json",
data: { expr : code },
async: false,
success: function(res) { data = res; }
});
return data;
}
function html_escape(val) {
var result = val;
result = result.replace(/\n/g, "<br/>");
result = result.replace(/[<]/g, "<");
result = result.replace(/[>]/g, ">");
return result;
}
function doCommand(input) {
if (input.match(/^gopage /)) {
goToPage(parseInt(input.substring("gopage ".length)));
return true;
}
switch (input) {
case 'next':
case 'forward':
goToPage(currentPage + 1);
return true;
case 'previous':
case 'prev':
case 'back':
goToPage(currentPage - 1);
return true;
case 'restart':
case 'reset':
case 'home':
case 'quit':
goToPage(0);
return true;
default:
return false;
}
}
function onValidate(input) {
return (input != "");
}
function onHandle(line, report) {
var input = $.trim(line);
// handle commands
if (doCommand(input)) {
report();
return;
}
// perform evaluation
var data = eval_clojure(input);
// handle error
if (data.error) {
return [{msg: data.message, className: "jquery-console-message-error"}];
}
// handle page
if (currentPage >= 0 && pageExitConditions[currentPage].verify(data)) {
goToPage(currentPage + 1);
}
// display expr results
return [{msg: data.result, className: "jquery-console-message-value"}];
}
/**
* This should be called anytime the changer div is updated so it can rebind event listeners.
* Currently this is just to make the code elements clickable.
*/
function changerUpdated() {
$("#changer code.expr").each(function() {
$(this).css("cursor", "pointer");
$(this).attr("title", "Click to insert '" + $(this).text() + "' into the console.");
$(this).click(function(e) {
controller.promptText($(this).text());
controller.inner.click();
});
});
}
var controller;
$(document).ready(function() {
controller = $("#console").console({
welcomeMessage:'Give me some Clojure:',
promptLabel: '> ',
commandValidate: onValidate,
commandHandle: onHandle,
autofocus:true,
animateScroll:true,
promptHistory:true
});
$("#about").click(setupLink("about"));
$("#links").click(setupLink("links"));
$("#home").click(setupLink("home"));
changerUpdated();
});