34
34
windowpane_mode = false;
35
35
server_started = false;
40
function get_console_start_directory()
42
if((typeof(current_path) != 'undefined') && current_file)
44
// We have a current_path - give a suggestion to the server
46
if (current_file.isdir)
49
return path_join("/home", current_path);
53
// Editor - need to chop off filename
54
var tmp_path = current_path.split('/');
56
return path_join("/home", tmp_path.join('/'));
61
// No current_path - let the server decide
37
66
/* Starts the console server, if it isn't already.
38
67
* This can be called any number of times - it only starts the one server.
39
68
* Note that this is asynchronous. It will return after signalling to start
56
85
var callback1 = function(xhr)
58
87
var json_text = xhr.responseText;
59
server_key = JSON.parse(json_text);
60
server_started = true;
90
server_key = JSON.parse(json_text).key;
91
server_started = true;
97
alert("An error occured when starting the IVLE console. " +
98
"Please refresh the page and try again.\n" +
99
"Details have been logged for further examination.")
64
ajax_call(callback1, "consoleservice", "start", {}, "POST");
104
callback1, "console", "service",
105
{"ivle.op": "start", "cwd": get_console_start_directory()},
67
109
/** Initialises the console. All apps which import console are required to
212
247
var hist = new History();
249
function set_interrupt()
254
function clear_output()
256
var output = document.getElementById("console_output");
257
while (output.firstChild)
259
output.removeChild(output.firstChild);
214
263
/** Send a line of text to the Python server, wait for its return, and react
215
264
* to its response by writing to the output box.
216
265
* Also maximize the console window if not already.
218
267
function console_enter_line(inputbox, which)
220
271
if (typeof(inputbox) == "string")
222
var inputline = inputbox;
273
var inputline = inputbox + "\n";
224
var graytimer = null;
228
GLOBAL_inputbox = inputbox; /* For timer */
229
var inputline = inputbox.value;
230
var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
231
+ "\"disabled\");", 100);
278
/* Disable the text box */
279
inputbox.setAttribute("disabled", "disabled");
281
var inputline = inputbox.value + "\n";
233
283
var output = document.getElementById("console_output");
286
var span = document.createElement("span");
287
span.setAttribute("class", "inputPrompt");
288
span.appendChild(document.createTextNode(
289
document.getElementById("console_prompt").firstChild.nodeValue)
291
output.appendChild(span);
292
// Print input line itself in a span
235
293
var span = document.createElement("span");
236
294
span.setAttribute("class", "inputMsg");
237
span.appendChild(document.createTextNode(inputline + "\n"));
295
span.appendChild(document.createTextNode(inputline));
238
296
output.appendChild(span);
240
var args = {"key": server_key, "text":inputline};
299
"ivle.op": "chat", "kind": which, "key": server_key,
300
"text": inputline, "cwd": get_console_start_directory()
241
302
var callback = function(xhr)
243
console_response(inputbox, graytimer, inputline, xhr.responseText);
304
console_response(inputbox, inputline, xhr.responseText);
245
/* Disable the text box */
246
if (inputbox != null)
247
inputbox.setAttribute("disabled", "disabled");
248
ajax_call(callback, "consoleservice", which, args, "POST");
306
ajax_call(callback, "console", "service", args, "POST");
251
function console_response(inputbox, graytimer, inputline, responseText)
309
function console_response(inputbox, inputline, responseText)
253
var res = JSON.parse(responseText);
313
var res = JSON.parse(responseText);
317
alert("An internal error occurred in the python console.");
254
320
var output = document.getElementById("console_output");
255
321
if (res.hasOwnProperty('okay'))
261
327
output.appendChild(span);
263
329
// set the prompt to >>>
264
var prompt = document.getElementById("console_prompt");
265
prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
267
332
else if (res.hasOwnProperty('exc'))
270
335
// print out the error message (res.exc)
271
var span = document.createElement("span");
272
span.setAttribute("class", "errorMsg");
273
span.appendChild(document.createTextNode(res.exc + "\n"));
274
output.appendChild(span);
336
print_error(res.exc);
338
// set the prompt to >>>
341
else if (res.hasOwnProperty('restart') && res.hasOwnProperty('key'))
343
// Server has indicated that the console should be restarted
345
// Get the new key (host, port, magic)
346
server_key = res.key;
348
// Print a reason to explain why we'd do such a horrible thing
349
// (console timeout, server error etc.)
350
print_error("Console Restart: " + res.restart);
352
// set the prompt to >>>
276
355
else if (res.hasOwnProperty('more'))
278
357
// Need more input, so set the prompt to ...
279
var prompt = document.getElementById("console_prompt");
280
prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
282
360
else if (res.hasOwnProperty('output'))
288
366
var callback = function(xhr)
290
console_response(inputbox, graytimer,
291
null, xhr.responseText);
368
console_response(inputbox, null, xhr.responseText);
293
var args = {"key": server_key, "text":''};
294
ajax_call(callback, "consoleservice", "chat", args, "POST");
372
var kind = "interrupt";
379
"ivle.op": "chat", "kind": kind, "key": server_key,
380
"text": '', "cwd": get_console_start_directory()
382
ajax_call(callback, "console", "service", args, "POST");
296
/* Open up the console so we can see the output */
384
// Open up the console so we can see the output
385
// FIXME: do we need to maximize here?
297
386
console_maximize();
298
388
/* Auto-scrolling */
299
389
divScroll.activeScroll();
301
391
// Return early, so we don't re-enable the input box.
305
396
// assert res.hasOwnProperty('input')
306
var prompt = document.getElementById("console_prompt");
307
prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
310
400
if (inputbox != null)
312
402
/* Re-enable the text box */
313
clearTimeout(graytimer);
314
403
inputbox.removeAttribute("disabled");
315
inputbox.removeAttribute("class");
318
407
/* Open up the console so we can see the output */
319
408
console_maximize();
320
409
/* Auto-scrolling */
321
410
divScroll.activeScroll();
412
// Focus the input box by default
413
document.getElementById("console_output").focus();
414
document.getElementById("console_inputText").focus();
324
417
function catch_input(key)
363
456
hist.submit(inp.value);
364
457
inp.value = hist.curr();
460
/* Disable the text box. This will be redone by
461
* console_enter_line, but we do it here too in case start_server
464
inp.setAttribute("disabled", "disabled");
366
465
/* Start the server if it hasn't already been started */
367
466
start_server(callback);
369
468
case 38: /* Up arrow */
370
469
hist.up(inp.value);
371
470
inp.value = hist.curr();
471
/* Inhibit further responses to this event, or WebKit moves the
472
* cursor to the start. */
373
475
case 40: /* Down arrow */
374
476
hist.down(inp.value);
375
477
inp.value = hist.curr();
483
/** Resets the console by signalling the old console to expire and starting a
486
function console_reset()
488
// FIXME: We show some feedback here - either disable input or at very
489
// least the reset button.
491
// Restart the console
498
xhr = ajax_call(null, "console", "service", {"ivle.op": "chat", "kind": "terminate", "key": server_key, "cwd": get_console_start_directory()}, "POST");
499
console_response(null, null, xhr.responseText);
503
/** Prints an error line in the console **/
504
function print_error(error)
506
var output = document.getElementById("console_output");
509
var span = document.createElement("span");
510
span.setAttribute("class", "errorMsg");
511
span.appendChild(document.createTextNode(error + "\n"));
512
output.appendChild(span);
515
divScroll.activeScroll();
518
/** Sets the prompt text **/
519
function set_prompt(prompt_text)
521
var prompt = document.getElementById("console_prompt");
522
prompt.replaceChild(document.createTextNode(prompt_text + " "), prompt.firstChild);
380
525
/**** Following Code modified from ******************************************/
381
526
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
382
527
/****************************************************************************/