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

« back to all changes in this revision

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

  • Committer: dcoles
  • Date: 2008-07-08 02:23:05 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:825
Setup: Added missing modules and fixed bad module location

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
interrupted = false;
38
38
 
39
 
 
40
 
function get_console_start_directory()
41
 
{
42
 
    if((typeof(current_path) != 'undefined') && current_file)
43
 
    {
44
 
        // We have a current_path - give a suggestion to the server
45
 
        var path;
46
 
        if (current_file.isdir)
47
 
        {
48
 
            // Browser
49
 
            return path_join("/home", current_path);
50
 
        }
51
 
        else
52
 
        {
53
 
            // Editor - need to chop off filename
54
 
            var tmp_path = current_path.split('/');
55
 
            tmp_path.pop();
56
 
            return path_join("/home", tmp_path.join('/'));
57
 
        }
58
 
    }
59
 
    else
60
 
    {
61
 
        // No current_path - let the server decide
62
 
        return '';
63
 
    }
64
 
}
65
 
 
66
39
/* Starts the console server, if it isn't already.
67
40
 * This can be called any number of times - it only starts the one server.
68
41
 * Note that this is asynchronous. It will return after signalling to start
85
58
    var callback1 = function(xhr)
86
59
        {
87
60
            var json_text = xhr.responseText;
88
 
            try
89
 
            {
90
 
                server_key = JSON.parse(json_text).key;
91
 
                server_started = true;
92
 
                if (callback != null)
93
 
                    callback();
94
 
            }
95
 
            catch (e)
96
 
            {
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.")
100
 
            }
 
61
            server_key = JSON.parse(json_text);
 
62
            server_started = true;
 
63
            if (callback != null)
 
64
                callback();
101
65
        }
102
 
 
103
 
    ajax_call(
104
 
        callback1, "console", "service",
105
 
        {"ivle.op": "start", "cwd": get_console_start_directory()}, 
106
 
        "POST");
 
66
    ajax_call(callback1, "consoleservice", "start", {}, "POST");
107
67
}
108
68
 
109
69
/** Initialises the console. All apps which import console are required to
270
230
 
271
231
    if (typeof(inputbox) == "string")
272
232
    {
273
 
        var inputline = inputbox + "\n";
 
233
        var inputline = inputbox;
274
234
        inputbox = null;
 
235
        var graytimer = null;
275
236
    }
276
237
    else
277
238
    {
278
 
        /* Disable the text box */
279
 
        inputbox.setAttribute("disabled", "disabled");
280
 
 
281
 
        var inputline = inputbox.value + "\n";
 
239
        GLOBAL_inputbox = inputbox;     /* For timer */
 
240
        var inputline = inputbox.value;
 
241
        var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
 
242
            + "\"disabled\");", 100);
282
243
    }
283
244
    var output = document.getElementById("console_output");
284
245
    {
285
246
        // Print ">>>" span
286
247
        var span = document.createElement("span");
287
248
        span.setAttribute("class", "inputPrompt");
288
 
        span.appendChild(document.createTextNode(
289
 
              document.getElementById("console_prompt").firstChild.nodeValue)
290
 
                        );
 
249
        span.appendChild(document.createTextNode(">>> "));
291
250
        output.appendChild(span);
292
251
        // Print input line itself in a span
293
252
        var span = document.createElement("span");
294
253
        span.setAttribute("class", "inputMsg");
295
 
        span.appendChild(document.createTextNode(inputline));
 
254
        span.appendChild(document.createTextNode(inputline + "\n"));
296
255
        output.appendChild(span);
297
256
    }
298
 
    var args = {
299
 
        "ivle.op": "chat", "kind": which, "key": server_key,
300
 
        "text": inputline, "cwd": get_console_start_directory()
301
 
        };
 
257
    var args = {"key": server_key, "text":inputline};
302
258
    var callback = function(xhr)
303
259
        {
304
 
            console_response(inputbox, inputline, xhr.responseText);
 
260
            console_response(inputbox, graytimer, inputline, xhr.responseText);
305
261
        }
306
 
    ajax_call(callback, "console", "service", args, "POST");
 
262
    /* Disable the text box */
 
263
    if (inputbox != null)
 
264
        inputbox.setAttribute("disabled", "disabled");
 
265
    ajax_call(callback, "consoleservice", which, args, "POST");
307
266
}
308
267
 
309
 
function console_response(inputbox, inputline, responseText)
 
268
function console_response(inputbox, graytimer, inputline, responseText)
310
269
{
311
270
    try
312
271
    {
327
286
            output.appendChild(span);
328
287
        }
329
288
        // set the prompt to >>>
330
 
        set_prompt(">>>");
 
289
        var prompt = document.getElementById("console_prompt");
 
290
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
331
291
    }
332
292
    else if (res.hasOwnProperty('exc'))
333
293
    {
334
294
        // Failure!
335
295
        // print out the error message (res.exc)
336
 
        print_error(res.exc);
337
 
        
 
296
        var span = document.createElement("span");
 
297
        span.setAttribute("class", "errorMsg");
 
298
        span.appendChild(document.createTextNode(res.exc + "\n"));
 
299
        output.appendChild(span);
338
300
        // set the prompt to >>>
339
 
        set_prompt(">>>");
 
301
        var prompt = document.getElementById("console_prompt");
 
302
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
340
303
    }
341
304
    else if (res.hasOwnProperty('restart') && res.hasOwnProperty('key'))
342
305
    {
347
310
 
348
311
        // Print a reason to explain why we'd do such a horrible thing
349
312
        // (console timeout, server error etc.)
350
 
        print_error("Console Restart: " + res.restart);
351
 
        
 
313
        var span = document.createElement("span");
 
314
        span.setAttribute("class", "errorMsg");
 
315
        span.appendChild(document.createTextNode("Console Restart: " + res.restart + "\n"));
 
316
        output.appendChild(span);
352
317
        // set the prompt to >>>
353
 
        set_prompt(">>>");
 
318
        var prompt = document.getElementById("console_prompt");
 
319
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
 
320
 
354
321
    }
355
322
    else if (res.hasOwnProperty('more'))
356
323
    {
357
324
        // Need more input, so set the prompt to ...
358
 
        set_prompt("...");
 
325
        var prompt = document.getElementById("console_prompt");
 
326
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
359
327
    }
360
328
    else if (res.hasOwnProperty('output'))
361
329
    {
365
333
        }
366
334
        var callback = function(xhr)
367
335
            {
368
 
                console_response(inputbox, null, xhr.responseText);
 
336
                console_response(inputbox, graytimer,
 
337
                                 null, xhr.responseText);
369
338
            }
370
339
        if (interrupted)
371
340
        {
375
344
        {
376
345
            var kind = "chat";
377
346
        }
378
 
        var args = {
379
 
            "ivle.op": "chat", "kind": kind, "key": server_key,
380
 
            "text": '', "cwd": get_console_start_directory()
381
 
            };
382
 
        ajax_call(callback, "console", "service", args, "POST");
 
347
        var args = {"key": server_key, "text":''};
 
348
        ajax_call(callback, "consoleservice", kind, args, "POST");
383
349
 
384
350
        // Open up the console so we can see the output
385
351
        // FIXME: do we need to maximize here?
391
357
        // Return early, so we don't re-enable the input box.
392
358
        return;
393
359
    }
394
 
    else
395
 
    {
 
360
    else {
396
361
        // assert res.hasOwnProperty('input')
397
 
        set_prompt("...");
 
362
        var prompt = document.getElementById("console_prompt");
 
363
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
398
364
    }
399
365
 
400
366
    if (inputbox != null)
401
367
    {
402
368
        /* Re-enable the text box */
 
369
        clearTimeout(graytimer);
403
370
        inputbox.removeAttribute("disabled");
 
371
        inputbox.removeAttribute("class");
404
372
        interrupted = false;
405
373
    }
406
374
 
410
378
    divScroll.activeScroll();
411
379
 
412
380
    // Focus the input box by default
413
 
    document.getElementById("console_output").focus();
414
 
    document.getElementById("console_inputText").focus();
 
381
    document.getElementById("console_output").focus()
 
382
    document.getElementById("console_inputText").focus()
415
383
}
416
384
 
417
385
function catch_input(key)
456
424
            hist.submit(inp.value);
457
425
            inp.value = hist.curr();
458
426
        }
459
 
 
460
 
        /* Disable the text box. This will be redone by
461
 
         * console_enter_line, but we do it here too in case start_server
462
 
         * takes a while.
463
 
         */
464
 
        inp.setAttribute("disabled", "disabled");
465
427
        /* Start the server if it hasn't already been started */
466
428
        start_server(callback);
467
429
        break;
468
430
    case 38:                /* Up arrow */
469
431
        hist.up(inp.value);
470
432
        inp.value = hist.curr();
471
 
        /* Inhibit further responses to this event, or WebKit moves the
472
 
         * cursor to the start. */
473
 
        return false;
474
433
        break;
475
434
    case 40:                /* Down arrow */
476
435
        hist.down(inp.value);
477
436
        inp.value = hist.curr();
478
 
        return false;
479
437
        break;
480
438
    }
481
439
}
482
440
 
483
 
/** Resets the console by signalling the old console to expire and starting a 
484
 
 * new one.
485
 
 */
486
 
function console_reset()
487
 
{
488
 
    // FIXME: We show some feedback here - either disable input or at very 
489
 
    // least the reset button.
490
 
 
491
 
    // Restart the console
492
 
    if(!server_started)
493
 
    {
494
 
        start_server(null);
495
 
    }
496
 
    else
497
 
    {
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);
500
 
    }
501
 
}
502
 
 
503
 
/** Prints an error line in the console **/
504
 
function print_error(error)
505
 
506
 
    var output = document.getElementById("console_output");
507
 
  
508
 
    // Create text block
509
 
    var span = document.createElement("span");
510
 
    span.setAttribute("class", "errorMsg");
511
 
    span.appendChild(document.createTextNode(error + "\n"));
512
 
    output.appendChild(span);
513
 
 
514
 
    // Autoscroll
515
 
    divScroll.activeScroll();
516
 
}
517
 
 
518
 
/** Sets the prompt text **/
519
 
function set_prompt(prompt_text)
520
 
{
521
 
    var prompt = document.getElementById("console_prompt");
522
 
    prompt.replaceChild(document.createTextNode(prompt_text + " "), prompt.firstChild);
523
 
}
524
 
 
525
441
/**** Following Code modified from ******************************************/
526
442
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
527
443
/****************************************************************************/
539
455
        
540
456
    if (scrollDiv.scrollHeight > 0)
541
457
        currentHeight = scrollDiv.scrollHeight;
542
 
    else if (scrollDiv.offsetHeight > 0)
543
 
        currentHeight = scrollDiv.offsetHeight;
 
458
    else 
 
459
        if (objDiv.offsetHeight > 0)
 
460
            currentHeight = scrollDiv.offsetHeight;
544
461
 
545
462
    scrollDiv.scrollTop = currentHeight;
546
463