37
41
/* Starts the console server, if it isn't already.
38
42
* This can be called any number of times - it only starts the one server.
39
* Note that this is asynchronous. It will return after signalling to start
40
* the server, but there is no guarantee that it has been started yet.
41
43
* This is a separate step from console_init, as the server is only to be
42
44
* started once the first command is entered.
43
45
* Does not return a value. Writes to global variables
44
* server_host, and server_port.
46
* \param callback Function which will be called after the server has been
47
* started. No parameters are passed. May be null.
46
* server_host, server_port, server_magic.
49
function start_server(callback)
48
function start_server()
56
var callback1 = function(xhr)
58
var json_text = xhr.responseText;
59
server_key = JSON.parse(json_text);
60
server_started = true;
64
ajax_call(callback1, "consoleservice", "start", {}, "POST");
50
if (server_started) return;
51
var xhr = ajax_call("consoleservice", "start", {}, "POST");
52
var json_text = xhr.responseText;
53
var server_info = JSON.parse(json_text);
54
server_host = server_info.host;
55
server_port = server_info.port;
56
server_magic = server_info.magic;
57
server_started = true;
67
60
/** Initialises the console. All apps which import console are required to
215
208
* to its response by writing to the output box.
216
209
* Also maximize the console window if not already.
218
function console_enter_line(inputbox, which)
211
function console_enter_line(inputline, which)
220
if (typeof(inputbox) == "string")
222
var inputline = inputbox;
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);
233
var output = document.getElementById("console_output");
235
var span = document.createElement("span");
236
span.setAttribute("class", "inputMsg");
237
span.appendChild(document.createTextNode(inputline + "\n"));
238
output.appendChild(span);
240
var args = {"key": server_key, "text":inputline};
241
var callback = function(xhr)
243
console_response(inputbox, graytimer, 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");
213
/* Start the server if it hasn't already been started */
215
var digest = hex_md5(inputline + server_magic);
216
var args = {"host": server_host, "port": server_port,
217
"digest":digest, "text":inputline};
218
var xmlhttp = ajax_call("consoleservice", which, args, "POST");
251
function console_response(inputbox, graytimer, inputline, responseText)
253
var res = JSON.parse(responseText);
220
var res = JSON.parse(xmlhttp.responseText);
254
221
var output = document.getElementById("console_output");
223
var pre = document.createElement("pre");
224
pre.setAttribute("class", "inputMsg");
225
pre.appendChild(document.createTextNode(inputline + "\n"));
226
output.appendChild(pre);
255
228
if (res.hasOwnProperty('okay'))
231
// print out the output (res.okay[0])
232
var pre = document.createElement("pre");
233
pre.setAttribute("class", "outputMsg");
234
pre.appendChild(document.createTextNode(res.okay[0]));
235
output.appendChild(pre);
236
// print out the return value (res.okay[1])
260
output.appendChild(document.createTextNode(res.okay + "\n"));
261
output.appendChild(span);
239
var pre = document.createElement("pre");
240
pre.setAttribute("class", "outputMsg");
241
pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
242
output.appendChild(pre);
263
244
// set the prompt to >>>
264
245
var prompt = document.getElementById("console_prompt");
267
248
else if (res.hasOwnProperty('exc'))
251
// print out any output that came before the error
252
if (res.exc[0].length > 0)
254
var pre = document.createElement("pre");
255
pre.setAttribute("class", "outputMsg");
256
pre.appendChild(document.createTextNode(res.exc[0]));
257
output.appendChild(pre);
270
260
// 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);
261
var pre = document.createElement("pre");
262
pre.setAttribute("class", "errorMsg");
263
pre.appendChild(document.createTextNode(res.exc[1]));
264
output.appendChild(pre);
276
266
else if (res.hasOwnProperty('more'))
279
269
var prompt = document.getElementById("console_prompt");
280
270
prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
282
else if (res.hasOwnProperty('output'))
284
if (res.output.length > 0)
286
output.appendChild(document.createTextNode(res.output));
288
var callback = function(xhr)
290
console_response(inputbox, graytimer,
291
null, xhr.responseText);
293
var args = {"key": server_key, "text":''};
294
ajax_call(callback, "consoleservice", "chat", args, "POST");
296
/* Open up the console so we can see the output */
299
divScroll.activeScroll();
301
// Return early, so we don't re-enable the input box.
305
273
// assert res.hasOwnProperty('input')
306
274
var prompt = document.getElementById("console_prompt");
307
275
prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
310
if (inputbox != null)
312
/* Re-enable the text box */
313
clearTimeout(graytimer);
314
inputbox.removeAttribute("disabled");
315
inputbox.removeAttribute("class");
318
277
/* Open up the console so we can see the output */
319
278
console_maximize();
321
divScroll.activeScroll();
324
281
function catch_input(key)
380
/**** Following Code modified from ******************************************/
381
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
382
/****************************************************************************/
383
var chatscroll = new Object();
385
chatscroll.Pane = function(scrollContainerId)
387
this.scrollContainerId = scrollContainerId;
390
chatscroll.Pane.prototype.activeScroll = function()
392
var scrollDiv = document.getElementById(this.scrollContainerId);
393
var currentHeight = 0;
395
if (scrollDiv.scrollHeight > 0)
396
currentHeight = scrollDiv.scrollHeight;
398
if (objDiv.offsetHeight > 0)
399
currentHeight = scrollDiv.offsetHeight;
401
scrollDiv.scrollTop = currentHeight;
406
var divScroll = new chatscroll.Pane('console_output');