-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.js
380 lines (316 loc) · 10.3 KB
/
ui.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// This code was lifted from http://www.squarefree.com/shell/shell.html
// Probably originally by Jesse Ruderman.
var
histList = [""],
histPos = 0,
_win, // a top-level context
question,
_in,
_out,
tooManyMatches = null,
lastError = null;
function refocus() {
_in.blur(); // Needed for Mozilla to scroll correctly.
_in.focus();
}
var mode = 'calculator';
function init() {
_in = document.getElementById("input");
_out = document.getElementById("output");
_win = window;
_win.Shell = window;
recalculateInputHeight();
refocus();
var sourceCode = document.getElementById("sourcecode");
function setMode(newMode) {
mode = newMode;
while (sourceCode.lastChild)
sourceCode.removeChild(sourceCode.lastChild);
sourceCode.appendChild(document.createTextNode(parseModes[mode].toString()));
}
setMode('calc');
var radios = document.getElementsByTagName("input");
for (var i = 0; i < radios.length; i++) {
var e = radios[i];
if (e.type == "radio") {
e.addEventListener("click", function () {
setMode(this.value);
setTimeout(refocus, 0);
return true;
});
if (e.value == 'calc')
e.checked = true;
}
}
}
// Unless the user is selected something, refocus the textbox.
// (requested by caillon, brendan, asa)
function keepFocusInTextbox(e) {
var g = e.srcElement ? e.srcElement : e.target; // IE vs. standard
while (!g.tagName)
g = g.parentNode;
var t = g.tagName.toUpperCase();
if (t=="A" || t=="INPUT")
return;
if (window.getSelection) {
// Mozilla
if (String(window.getSelection()))
return;
} else if (document.getSelection) {
// Opera? Netscape 4?
if (document.getSelection())
return;
} else {
// IE
if ( document.selection.createRange().text )
return;
}
refocus();
}
function inputKeydown(e) {
// Use onkeydown because IE doesn't support onkeypress for arrow keys
//alert(e.keyCode + " ^ " + e.keycode);
if (e.shiftKey && e.keyCode == 13) { // shift-enter
// don't do anything; allow the shift-enter to insert a line break as normal
} else if (e.keyCode == 13) { // enter
// execute the input on enter
try { go(); } catch(er) { alert(er); };
setTimeout(function() { _in.value = ""; }, 0); // can't preventDefault on input, so clear it later
} else if (e.keyCode == 38) { // up
// go up in history if at top or ctrl-up
if (e.ctrlKey || caretInFirstLine(_in))
hist(true);
} else if (e.keyCode == 40) { // down
// go down in history if at end or ctrl-down
if (e.ctrlKey || caretInLastLine(_in))
hist(false);
} else if (e.keyCode == 9) { // tab
setTimeout(function() { refocus(); }, 0); // refocus because tab was hit
} else { }
setTimeout(recalculateInputHeight, 0);
//return true;
}
function caretInFirstLine(textbox) {
// IE doesn't support selectionStart/selectionEnd
if (textbox.selectionStart == undefined)
return true;
var firstLineBreak = textbox.value.indexOf("\n");
return ((firstLineBreak == -1) || (textbox.selectionStart <= firstLineBreak));
}
function caretInLastLine(textbox) {
// IE doesn't support selectionStart/selectionEnd
if (textbox.selectionEnd == undefined)
return true;
var lastLineBreak = textbox.value.lastIndexOf("\n");
return (textbox.selectionEnd > lastLineBreak);
}
function recalculateInputHeight() {
var rows = _in.value.split(/\n/).length
+ 1 // prevent scrollbar flickering in Mozilla
+ (window.opera ? 1 : 0); // leave room for scrollbar in Opera
if (_in.rows != rows) // without this check, it is impossible to select text in Opera 7.60 or Opera 8.0.
_in.rows = rows;
}
function writeNode(type, node) {
var newdiv = document.createElement("div");
newdiv.className = type;
newdiv.appendChild(node);
_out.appendChild(newdiv);
return newdiv;
}
function println(s, type) {
s = String(s);
if (s)
return writeNode(type, document.createTextNode(s));
}
function printWithRunin(h, s, type) {
var div = println(s, type);
var head = document.createElement("strong");
head.appendChild(document.createTextNode(h + ": "));
div.insertBefore(head, div.firstChild);
}
function hist(up) {
// histList[0] = first command entered, [1] = second, etc.
// type something, press up --> thing typed is now in "limbo"
// (last item in histList) and should be reachable by pressing
// down again.
var L = histList.length;
if (L == 1)
return;
if (up) {
if (histPos == L-1) {
// Save this entry in case the user hits the down key.
histList[histPos] = _in.value;
}
if (histPos > 0) {
histPos--;
// Use a timeout to prevent up from moving cursor within new text
// Set to nothing first for the same reason
setTimeout(
function() {
_in.value = '';
_in.value = histList[histPos];
var caretPos = _in.value.length;
if (_in.setSelectionRange)
_in.setSelectionRange(caretPos, caretPos);
},
0
);
}
} else { // down
if (histPos < L-1) {
histPos++;
_in.value = histList[histPos];
} else if (histPos == L-1) {
// Already on the current entry: clear but save
if (_in.value) {
histList[histPos] = _in.value;
++histPos;
_in.value = "";
}
}
}
}
function printQuestion(q) {
println(q, "input");
}
function printAnswer(a) {
if (a !== undefined)
println(a, "normalOutput");
}
function printError(er) {
var lineNumberString;
lastError = er; // for debugging the shell
if (er.name) {
// lineNumberString should not be "", to avoid a very wacky bug in IE 6.
lineNumberString = (er.lineNumber != undefined) ? (" on line " + er.lineNumber + ": ") : ": ";
println(er.name + lineNumberString + er.message, "error"); // Because IE doesn't have error.toString.
} else {
println(er, "error"); // Because security errors in Moz /only/ have toString.
}
}
// Example: Taylor series for sin:
// x * (1 - x*x/(2*3) * (1 - x*x/(4*5) * (1 - x*x/(6*7) * (1 - x*x/(8*9) * (1 - x*x/(10*11) * (1 - x*x/(12*13)))))))
function showPlot(fn) {
var c = document.createElement('canvas');
c.width = 600;
c.height = 400;
writeNode("normalOutput", c);
var ctx = c.getContext("2d");
var rawData = [], labels = [];
for (var i = 0; i <= 100; i++) {
var x = (i - 50) / 20;
rawData[i] = fn(x);
if (i % 10 == 0)
labels[i] = x;
else
labels[i] = "";
}
var chart = new Chart(ctx).Line({
labels: labels,
datasets: [
{
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
data : rawData
}
]
}, {
bezierCurve: false,
pointDot: false,
animation: false
});
}
function showComplexPlot(fn) {
var WIDTH = 401, HEIGHT = 401;
var c = document.createElement('canvas');
c.width = WIDTH;
c.height = HEIGHT;
writeNode("normalOutput", c);
var ctx = c.getContext('2d');
var imageData = ctx.getImageData(0, 0, WIDTH, HEIGHT);
var arr = imageData.data;
var i = 0;
for (var yi = 0; yi < HEIGHT; yi++) {
var z_im = ((HEIGHT - 1) / 2 - yi) * (6 / HEIGHT);
for (var xi = 0; xi < WIDTH; xi++) {
var z_re = (xi - (WIDTH - 1) / 2) * (6 / WIDTH);
var result = fn(z_re, z_im);
var rabs = Math.sqrt(result.re*result.re + result.im*result.im);
var h = Math.atan2(result.im, result.re) / (2*Math.PI);
var s = 1.0;
var l;
if (result.re !== result.re || result.im !== result.im) {
l = 1;
} else {
l = 0.5 + Math.log(rabs + 0.5) / 3;
if (l > 1)
l = 1;
else if (l < 0)
l = 0;
}
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
var m1 = l * 2 - m2;
function hue(h) {
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
else if (h * 2 < 1) return m2;
else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
else return m1;
}
arr[i] = Math.round(255 * hue(h + 1/3));
arr[i + 1] = Math.round(255 * hue(h));
arr[i + 2] = Math.round(255 * hue(h - 1/3));
arr[i + 3] = 255;
i += 4;
}
}
ctx.putImageData(imageData, 0, 0);
}
var mode = 'calc';
function go() {
question = _in.value;
if (question == "")
return;
histList[histList.length-1] = question;
histList[histList.length] = "";
histPos = histList.length - 1;
_in.value='';
recalculateInputHeight();
printQuestion(question);
if (_win.closed) {
printError("Target window has been closed.");
return;
}
if (!("Shell" in _win))
initTarget(); // silent
try {
var result = parseModes[mode](question);
switch (mode) {
case 'json':
Shell.printAnswer(JSON.stringify(result, undefined, " "));
break;
case 'blocks':
writeNode("normalOutput", result);
break;
case 'mathml':
writeNode("normalOutput", result.element);
break;
case 'calc':
Shell.printAnswer(result);
break;
case 'fraction':
Shell.printAnswer(result);
break;
case 'graph':
showPlot(result);
break;
case 'complex':
showComplexPlot(result);
break;
}
} catch (exc) {
Shell.printError(exc);
}
setTimeout(Shell.refocus, 0);
}