37
37
/* Starts the console server, if it isn't already.
38
38
* 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.
39
41
* This is a separate step from console_init, as the server is only to be
40
42
* started once the first command is entered.
41
43
* Does not return a value. Writes to global variables
42
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.
44
function start_server()
49
function start_server(callback)
46
if (server_started) return;
47
var xhr = ajax_call("consoleservice", "start", {}, "POST");
48
var json_text = xhr.responseText;
49
server_key = JSON.parse(json_text);
50
server_started = true;
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");
53
67
/** Initialises the console. All apps which import console are required to
201
215
* to its response by writing to the output box.
202
216
* Also maximize the console window if not already.
204
function console_enter_line(inputline, which)
218
function console_enter_line(inputbox, which)
206
/* Start the server if it hasn't already been started */
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);
208
240
var args = {"key": server_key, "text":inputline};
209
var xmlhttp = ajax_call("consoleservice", which, args, "POST");
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");
211
var res = JSON.parse(xmlhttp.responseText);
251
function console_response(inputbox, graytimer, inputline, responseText)
253
var res = JSON.parse(responseText);
212
254
var output = document.getElementById("console_output");
214
var pre = document.createElement("pre");
215
pre.setAttribute("class", "inputMsg");
216
pre.appendChild(document.createTextNode(inputline + "\n"));
217
output.appendChild(pre);
219
255
if (res.hasOwnProperty('okay'))
222
// print out the output (res.okay[0])
223
var pre = document.createElement("pre");
224
pre.setAttribute("class", "outputMsg");
225
pre.appendChild(document.createTextNode(res.okay[0]));
226
output.appendChild(pre);
227
// print out the return value (res.okay[1])
230
var pre = document.createElement("pre");
231
pre.setAttribute("class", "outputMsg");
232
pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
233
output.appendChild(pre);
260
output.appendChild(document.createTextNode(res.okay + "\n"));
261
output.appendChild(span);
235
263
// set the prompt to >>>
236
264
var prompt = document.getElementById("console_prompt");
239
267
else if (res.hasOwnProperty('exc'))
242
// print out any output that came before the error
243
if (res.exc[0].length > 0)
245
var pre = document.createElement("pre");
246
pre.setAttribute("class", "outputMsg");
247
pre.appendChild(document.createTextNode(res.exc[0]));
248
output.appendChild(pre);
251
270
// print out the error message (res.exc)
252
var pre = document.createElement("pre");
253
pre.setAttribute("class", "errorMsg");
254
pre.appendChild(document.createTextNode(res.exc[1]));
255
output.appendChild(pre);
271
var span = document.createElement("span");
272
span.setAttribute("class", "errorMsg");
273
span.appendChild(document.createTextNode(res.exc + "\n"));
274
output.appendChild(span);
257
276
else if (res.hasOwnProperty('more'))
260
279
var prompt = document.getElementById("console_prompt");
261
280
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.
264
305
// assert res.hasOwnProperty('input')
265
306
var prompt = document.getElementById("console_prompt");
266
307
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");
268
318
/* Open up the console so we can see the output */
269
319
console_maximize();
321
divScroll.activeScroll();
272
324
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');