~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/console/console.js

  • Committer: drtomc
  • Date: 2008-02-01 03:06:47 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:366
Somehow a couple of changed that address issues sb raised didn't get committed previously. Here they are.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Date: 30/1/2008
21
21
 */
22
22
 
23
 
var server_key;
 
23
digest_constant = "hello";
 
24
 
 
25
var server_host;
 
26
var server_port;
 
27
var server_magic;
24
28
 
25
29
/* Begin religious debate (tabs vs spaces) here: */
26
30
/* (This string will be inserted in the console when the user presses the Tab
36
40
 
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.
45
 
 *
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.
48
47
 */
49
 
function start_server(callback)
 
48
function start_server()
50
49
{
51
 
    if (server_started)
52
 
    {
53
 
        callback();
54
 
        return;
55
 
    }
56
 
    var callback1 = function(xhr)
57
 
        {
58
 
            var json_text = xhr.responseText;
59
 
            server_key = JSON.parse(json_text);
60
 
            server_started = true;
61
 
            if (callback != null)
62
 
                callback();
63
 
        }
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;
65
58
}
66
59
 
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.
217
210
 */
218
 
function console_enter_line(inputbox, which)
 
211
function console_enter_line(inputline, which)
219
212
{
220
 
    if (typeof(inputbox) == "string")
221
 
    {
222
 
        var inputline = inputbox;
223
 
        inputbox = null;
224
 
        var graytimer = null;
225
 
    }
226
 
    else
227
 
    {
228
 
        GLOBAL_inputbox = inputbox;     /* For timer */
229
 
        var inputline = inputbox.value;
230
 
        var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
231
 
            + "\"disabled\");", 100);
232
 
    }
233
 
    var output = document.getElementById("console_output");
234
 
    {
235
 
        var span = document.createElement("span");
236
 
        span.setAttribute("class", "inputMsg");
237
 
        span.appendChild(document.createTextNode(inputline + "\n"));
238
 
        output.appendChild(span);
239
 
    }
240
 
    var args = {"key": server_key, "text":inputline};
241
 
    var callback = function(xhr)
242
 
        {
243
 
            console_response(inputbox, graytimer, inputline, xhr.responseText);
244
 
        }
245
 
    /* Disable the text box */
246
 
    if (inputbox != null)
247
 
        inputbox.setAttribute("disabled", "disabled");
248
 
    ajax_call(callback, "consoleservice", which, args, "POST");
249
 
}
 
213
    /* Start the server if it hasn't already been started */
 
214
    start_server();
 
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");
250
219
 
251
 
function console_response(inputbox, graytimer, inputline, responseText)
252
 
{
253
 
    var res = JSON.parse(responseText);
 
220
    var res = JSON.parse(xmlhttp.responseText);
254
221
    var output = document.getElementById("console_output");
 
222
    {
 
223
        var pre = document.createElement("pre");
 
224
        pre.setAttribute("class", "inputMsg");
 
225
        pre.appendChild(document.createTextNode(inputline + "\n"));
 
226
        output.appendChild(pre);
 
227
    }
255
228
    if (res.hasOwnProperty('okay'))
256
229
    {
257
230
        // Success!
258
 
        if (res.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])
 
237
        if (res.okay[1])
259
238
        {
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);
262
243
        }
263
244
        // set the prompt to >>>
264
245
        var prompt = document.getElementById("console_prompt");
267
248
    else if (res.hasOwnProperty('exc'))
268
249
    {
269
250
        // Failure!
 
251
        // print out any output that came before the error
 
252
        if (res.exc[0].length > 0)
 
253
        {
 
254
            var pre = document.createElement("pre");
 
255
            pre.setAttribute("class", "outputMsg");
 
256
            pre.appendChild(document.createTextNode(res.exc[0]));
 
257
            output.appendChild(pre);
 
258
        }
 
259
 
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);
275
265
    }
276
266
    else if (res.hasOwnProperty('more'))
277
267
    {
279
269
        var prompt = document.getElementById("console_prompt");
280
270
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
281
271
    }
282
 
    else if (res.hasOwnProperty('output'))
283
 
    {
284
 
        if (res.output.length > 0)
285
 
        {
286
 
            output.appendChild(document.createTextNode(res.output));
287
 
        }
288
 
        var callback = function(xhr)
289
 
            {
290
 
                console_response(inputbox, graytimer,
291
 
                                 null, xhr.responseText);
292
 
            }
293
 
        var args = {"key": server_key, "text":''};
294
 
        ajax_call(callback, "consoleservice", "chat", args, "POST");
295
 
 
296
 
        /* Open up the console so we can see the output */
297
 
        console_maximize();
298
 
        /* Auto-scrolling */
299
 
        divScroll.activeScroll();
300
 
 
301
 
        // Return early, so we don't re-enable the input box.
302
 
        return;
303
 
    }
304
272
    else {
305
273
        // assert res.hasOwnProperty('input')
306
274
        var prompt = document.getElementById("console_prompt");
307
275
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
308
276
    }
309
 
 
310
 
    if (inputbox != null)
311
 
    {
312
 
        /* Re-enable the text box */
313
 
        clearTimeout(graytimer);
314
 
        inputbox.removeAttribute("disabled");
315
 
        inputbox.removeAttribute("class");
316
 
    }
317
 
 
318
277
    /* Open up the console so we can see the output */
319
278
    console_maximize();
320
 
    /* Auto-scrolling */
321
 
    divScroll.activeScroll();
322
279
}
323
280
 
324
281
function catch_input(key)
356
313
         */
357
314
        break;
358
315
    case 13:                /* Enter key */
359
 
        var callback = function()
360
 
        {
361
 
            /* Send the line of text to the server */
362
 
            console_enter_line(inp, "chat");
363
 
            hist.submit(inp.value);
364
 
            inp.value = hist.curr();
365
 
        }
366
 
        /* Start the server if it hasn't already been started */
367
 
        start_server(callback);
 
316
        /* Send the line of text to the server */
 
317
        console_enter_line(inp.value, "chat");
 
318
        hist.submit(inp.value);
 
319
        inp.value = hist.curr();
368
320
        break;
369
321
    case 38:                /* Up arrow */
370
322
        hist.up(inp.value);
376
328
        break;
377
329
    }
378
330
}
379
 
 
380
 
/**** Following Code modified from ******************************************/
381
 
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
382
 
/****************************************************************************/
383
 
var chatscroll = new Object();
384
 
 
385
 
chatscroll.Pane = function(scrollContainerId)
386
 
{
387
 
    this.scrollContainerId = scrollContainerId;
388
 
}
389
 
 
390
 
chatscroll.Pane.prototype.activeScroll = function()
391
 
{
392
 
    var scrollDiv = document.getElementById(this.scrollContainerId);
393
 
    var currentHeight = 0;
394
 
        
395
 
    if (scrollDiv.scrollHeight > 0)
396
 
        currentHeight = scrollDiv.scrollHeight;
397
 
    else 
398
 
        if (objDiv.offsetHeight > 0)
399
 
            currentHeight = scrollDiv.offsetHeight;
400
 
 
401
 
    scrollDiv.scrollTop = currentHeight;
402
 
 
403
 
    scrollDiv = null;
404
 
}
405
 
 
406
 
var divScroll = new chatscroll.Pane('console_output');