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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-03-05 04:34:27 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:647
phpBB3 - Added svn:ignore on autoconfigged file, config.php.

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
126
86
        windowpane_mode = true;
127
87
        console_minimize();
128
88
    }
 
89
    /* TEMP: Start the server now.
 
90
     * Ultimately we want the server to start only when a line is typed, but
 
91
     * it currently does it asynchronously and doesn't start in time for the
 
92
     * first line. */
 
93
    start_server();
129
94
}
130
95
 
131
96
/** Hide the main console panel, so the console minimizes to just an input box
270
235
 
271
236
    if (typeof(inputbox) == "string")
272
237
    {
273
 
        var inputline = inputbox + "\n";
 
238
        var inputline = inputbox;
274
239
        inputbox = null;
 
240
        var graytimer = null;
275
241
    }
276
242
    else
277
243
    {
278
 
        /* Disable the text box */
279
 
        inputbox.setAttribute("disabled", "disabled");
280
 
 
281
 
        var inputline = inputbox.value + "\n";
 
244
        GLOBAL_inputbox = inputbox;     /* For timer */
 
245
        var inputline = inputbox.value;
 
246
        var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
 
247
            + "\"disabled\");", 100);
282
248
    }
283
249
    var output = document.getElementById("console_output");
284
250
    {
285
 
        // Print ">>>" span
286
 
        var span = document.createElement("span");
287
 
        span.setAttribute("class", "inputPrompt");
288
 
        span.appendChild(document.createTextNode(
289
 
              document.getElementById("console_prompt").firstChild.nodeValue)
290
 
                        );
291
 
        output.appendChild(span);
292
 
        // Print input line itself in a span
293
251
        var span = document.createElement("span");
294
252
        span.setAttribute("class", "inputMsg");
295
 
        span.appendChild(document.createTextNode(inputline));
 
253
        span.appendChild(document.createTextNode(inputline + "\n"));
296
254
        output.appendChild(span);
297
255
    }
298
 
    var args = {
299
 
        "ivle.op": "chat", "kind": which, "key": server_key,
300
 
        "text": inputline, "cwd": get_console_start_directory()
301
 
        };
 
256
    var args = {"key": server_key, "text":inputline};
302
257
    var callback = function(xhr)
303
258
        {
304
 
            console_response(inputbox, inputline, xhr.responseText);
 
259
            console_response(inputbox, graytimer, inputline, xhr.responseText);
305
260
        }
306
 
    ajax_call(callback, "console", "service", args, "POST");
 
261
    /* Disable the text box */
 
262
    if (inputbox != null)
 
263
        inputbox.setAttribute("disabled", "disabled");
 
264
    ajax_call(callback, "consoleservice", which, args, "POST");
307
265
}
308
266
 
309
 
function console_response(inputbox, inputline, responseText)
 
267
function console_response(inputbox, graytimer, inputline, responseText)
310
268
{
311
269
    try
312
270
    {
327
285
            output.appendChild(span);
328
286
        }
329
287
        // set the prompt to >>>
330
 
        set_prompt(">>>");
 
288
        var prompt = document.getElementById("console_prompt");
 
289
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
331
290
    }
332
291
    else if (res.hasOwnProperty('exc'))
333
292
    {
334
293
        // Failure!
335
294
        // print out the error message (res.exc)
336
 
        print_error(res.exc);
337
 
        
338
 
        // set the prompt to >>>
339
 
        set_prompt(">>>");
340
 
    }
341
 
    else if (res.hasOwnProperty('restart') && res.hasOwnProperty('key'))
342
 
    {
343
 
        // Server has indicated that the console should be restarted
344
 
        
345
 
        // Get the new key (host, port, magic)
346
 
        server_key = res.key;
347
 
 
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);
351
 
        
352
 
        // set the prompt to >>>
353
 
        set_prompt(">>>");
 
295
        var span = document.createElement("span");
 
296
        span.setAttribute("class", "errorMsg");
 
297
        span.appendChild(document.createTextNode(res.exc + "\n"));
 
298
        output.appendChild(span);
354
299
    }
355
300
    else if (res.hasOwnProperty('more'))
356
301
    {
357
302
        // Need more input, so set the prompt to ...
358
 
        set_prompt("...");
 
303
        var prompt = document.getElementById("console_prompt");
 
304
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
359
305
    }
360
306
    else if (res.hasOwnProperty('output'))
361
307
    {
365
311
        }
366
312
        var callback = function(xhr)
367
313
            {
368
 
                console_response(inputbox, null, xhr.responseText);
 
314
                console_response(inputbox, graytimer,
 
315
                                 null, xhr.responseText);
369
316
            }
370
317
        if (interrupted)
371
318
        {
375
322
        {
376
323
            var kind = "chat";
377
324
        }
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");
 
325
        var args = {"key": server_key, "text":''};
 
326
        ajax_call(callback, "consoleservice", kind, args, "POST");
383
327
 
384
328
        // Open up the console so we can see the output
385
329
        // FIXME: do we need to maximize here?
391
335
        // Return early, so we don't re-enable the input box.
392
336
        return;
393
337
    }
394
 
    else
395
 
    {
 
338
    else {
396
339
        // assert res.hasOwnProperty('input')
397
 
        set_prompt("...");
 
340
        var prompt = document.getElementById("console_prompt");
 
341
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
398
342
    }
399
343
 
400
344
    if (inputbox != null)
401
345
    {
402
346
        /* Re-enable the text box */
 
347
        clearTimeout(graytimer);
403
348
        inputbox.removeAttribute("disabled");
 
349
        inputbox.removeAttribute("class");
404
350
        interrupted = false;
405
351
    }
406
352
 
410
356
    divScroll.activeScroll();
411
357
 
412
358
    // Focus the input box by default
413
 
    document.getElementById("console_output").focus();
414
 
    document.getElementById("console_inputText").focus();
 
359
    document.getElementById("console_output").focus()
 
360
    document.getElementById("console_inputText").focus()
415
361
}
416
362
 
417
363
function catch_input(key)
456
402
            hist.submit(inp.value);
457
403
            inp.value = hist.curr();
458
404
        }
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
405
        /* Start the server if it hasn't already been started */
466
406
        start_server(callback);
467
407
        break;
468
408
    case 38:                /* Up arrow */
469
409
        hist.up(inp.value);
470
410
        inp.value = hist.curr();
471
 
        /* Inhibit further responses to this event, or WebKit moves the
472
 
         * cursor to the start. */
473
 
        return false;
474
411
        break;
475
412
    case 40:                /* Down arrow */
476
413
        hist.down(inp.value);
477
414
        inp.value = hist.curr();
478
 
        return false;
479
415
        break;
480
416
    }
481
417
}
482
418
 
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
419
/**** Following Code modified from ******************************************/
526
420
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
527
421
/****************************************************************************/
539
433
        
540
434
    if (scrollDiv.scrollHeight > 0)
541
435
        currentHeight = scrollDiv.scrollHeight;
542
 
    else if (scrollDiv.offsetHeight > 0)
543
 
        currentHeight = scrollDiv.offsetHeight;
 
436
    else 
 
437
        if (objDiv.offsetHeight > 0)
 
438
            currentHeight = scrollDiv.offsetHeight;
544
439
 
545
440
    scrollDiv.scrollTop = currentHeight;
546
441