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

« back to all changes in this revision

Viewing changes to ivle/webapp/console/media/console.js

  • Committer: Matt Giuca
  • Date: 2010-02-23 05:27:07 UTC
  • Revision ID: matt.giuca@gmail.com-20100223052707-3a76wo23r2z503t8
browser.js: Adjusted condition for enabling "Commit" action; now allowed if
    no files are selected AND current directory is versioned (as well as if
    all selected files are versioned). Committing with 0 files selected will
    commit the current directory.
ivle.fileservice_lib.action: Fixed to allow commit to contain 0 paths. This
    will commit the current directory instead.
This fixes Launchpad bug #526161.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
windowpane_mode = false;
35
35
server_started = false;
36
36
 
 
37
interrupted = false;
 
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
 
37
66
/* Starts the console server, if it isn't already.
38
67
 * This can be called any number of times - it only starts the one server.
39
68
 * Note that this is asynchronous. It will return after signalling to start
56
85
    var callback1 = function(xhr)
57
86
        {
58
87
            var json_text = xhr.responseText;
59
 
            server_key = JSON.parse(json_text);
60
 
            server_started = true;
61
 
            if (callback != null)
62
 
                callback();
 
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
            }
63
101
        }
64
 
    ajax_call(callback1, "consoleservice", "start", {}, "POST");
 
102
 
 
103
    ajax_call(
 
104
        callback1, "console", "service",
 
105
        {"ivle.op": "start", "cwd": get_console_start_directory()}, 
 
106
        "POST");
65
107
}
66
108
 
67
109
/** Initialises the console. All apps which import console are required to
84
126
        windowpane_mode = true;
85
127
        console_minimize();
86
128
    }
87
 
    /* TEMP: Start the server now.
88
 
     * Ultimately we want the server to start only when a line is typed, but
89
 
     * it currently does it asynchronously and doesn't start in time for the
90
 
     * first line. */
91
 
    start_server();
92
129
}
93
130
 
94
131
/** Hide the main console panel, so the console minimizes to just an input box
107
144
    if (!windowpane_mode) return;
108
145
    console_body.setAttribute("class", "windowpane maximal");
109
146
    console_filler.setAttribute("class", "windowpane maximal");
110
 
    /* Focus the input box by default */
111
 
    document.getElementById("console_inputText").focus()
112
147
}
113
148
 
114
149
/* current_text is the string currently on the command line.
211
246
 
212
247
var hist = new History();
213
248
 
 
249
function set_interrupt()
 
250
{
 
251
    interrupted = true;
 
252
}
 
253
 
 
254
function clear_output()
 
255
{
 
256
    var output = document.getElementById("console_output");
 
257
    while (output.firstChild)
 
258
    {
 
259
        output.removeChild(output.firstChild);
 
260
    }
 
261
}
 
262
 
214
263
/** Send a line of text to the Python server, wait for its return, and react
215
264
 * to its response by writing to the output box.
216
265
 * Also maximize the console window if not already.
217
266
 */
218
267
function console_enter_line(inputbox, which)
219
268
{
 
269
    interrupted = false;
 
270
 
220
271
    if (typeof(inputbox) == "string")
221
272
    {
222
 
        var inputline = inputbox;
 
273
        var inputline = inputbox + "\n";
223
274
        inputbox = null;
224
 
        var graytimer = null;
225
275
    }
226
276
    else
227
277
    {
228
 
        GLOBAL_inputbox = inputbox;     /* For timer */
229
 
        var inputline = inputbox.value;
230
 
        var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
231
 
            + "\"disabled\");", 100);
 
278
        /* Disable the text box */
 
279
        inputbox.setAttribute("disabled", "disabled");
 
280
 
 
281
        var inputline = inputbox.value + "\n";
232
282
    }
233
283
    var output = document.getElementById("console_output");
234
284
    {
 
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
235
293
        var span = document.createElement("span");
236
294
        span.setAttribute("class", "inputMsg");
237
 
        span.appendChild(document.createTextNode(inputline + "\n"));
 
295
        span.appendChild(document.createTextNode(inputline));
238
296
        output.appendChild(span);
239
297
    }
240
 
    var args = {"key": server_key, "text":inputline};
 
298
    var args = {
 
299
        "ivle.op": "chat", "kind": which, "key": server_key,
 
300
        "text": inputline, "cwd": get_console_start_directory()
 
301
        };
241
302
    var callback = function(xhr)
242
303
        {
243
 
            console_response(inputbox, graytimer, inputline, xhr.responseText);
 
304
            console_response(inputbox, inputline, xhr.responseText);
244
305
        }
245
 
    /* Disable the text box */
246
 
    if (inputbox != null)
247
 
        inputbox.setAttribute("disabled", "disabled");
248
 
    ajax_call(callback, "consoleservice", which, args, "POST");
 
306
    ajax_call(callback, "console", "service", args, "POST");
249
307
}
250
308
 
251
 
function console_response(inputbox, graytimer, inputline, responseText)
 
309
function console_response(inputbox, inputline, responseText)
252
310
{
253
 
    var res = JSON.parse(responseText);
 
311
    try
 
312
    {
 
313
        var res = JSON.parse(responseText);
 
314
    }
 
315
    catch (e)
 
316
    {
 
317
        alert("An internal error occurred in the python console.");
 
318
        return;
 
319
    }
254
320
    var output = document.getElementById("console_output");
255
321
    if (res.hasOwnProperty('okay'))
256
322
    {
261
327
            output.appendChild(span);
262
328
        }
263
329
        // set the prompt to >>>
264
 
        var prompt = document.getElementById("console_prompt");
265
 
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
 
330
        set_prompt(">>>");
266
331
    }
267
332
    else if (res.hasOwnProperty('exc'))
268
333
    {
269
334
        // Failure!
270
335
        // 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);
 
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(">>>");
275
354
    }
276
355
    else if (res.hasOwnProperty('more'))
277
356
    {
278
357
        // Need more input, so set the prompt to ...
279
 
        var prompt = document.getElementById("console_prompt");
280
 
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
 
358
        set_prompt("...");
281
359
    }
282
360
    else if (res.hasOwnProperty('output'))
283
361
    {
287
365
        }
288
366
        var callback = function(xhr)
289
367
            {
290
 
                console_response(inputbox, graytimer,
291
 
                                 null, xhr.responseText);
 
368
                console_response(inputbox, null, xhr.responseText);
292
369
            }
293
 
        var args = {"key": server_key, "text":''};
294
 
        ajax_call(callback, "consoleservice", "chat", args, "POST");
 
370
        if (interrupted)
 
371
        {
 
372
            var kind = "interrupt";
 
373
        }
 
374
        else
 
375
        {
 
376
            var kind = "chat";
 
377
        }
 
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");
295
383
 
296
 
        /* Open up the console so we can see the output */
 
384
        // Open up the console so we can see the output
 
385
        // FIXME: do we need to maximize here?
297
386
        console_maximize();
 
387
 
298
388
        /* Auto-scrolling */
299
389
        divScroll.activeScroll();
300
390
 
301
391
        // Return early, so we don't re-enable the input box.
302
392
        return;
303
393
    }
304
 
    else {
 
394
    else
 
395
    {
305
396
        // assert res.hasOwnProperty('input')
306
 
        var prompt = document.getElementById("console_prompt");
307
 
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
 
397
        set_prompt("...");
308
398
    }
309
399
 
310
400
    if (inputbox != null)
311
401
    {
312
402
        /* Re-enable the text box */
313
 
        clearTimeout(graytimer);
314
403
        inputbox.removeAttribute("disabled");
315
 
        inputbox.removeAttribute("class");
 
404
        interrupted = false;
316
405
    }
317
406
 
318
407
    /* Open up the console so we can see the output */
319
408
    console_maximize();
320
409
    /* Auto-scrolling */
321
410
    divScroll.activeScroll();
 
411
 
 
412
    // Focus the input box by default
 
413
    document.getElementById("console_output").focus();
 
414
    document.getElementById("console_inputText").focus();
322
415
}
323
416
 
324
417
function catch_input(key)
363
456
            hist.submit(inp.value);
364
457
            inp.value = hist.curr();
365
458
        }
 
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");
366
465
        /* Start the server if it hasn't already been started */
367
466
        start_server(callback);
368
467
        break;
369
468
    case 38:                /* Up arrow */
370
469
        hist.up(inp.value);
371
470
        inp.value = hist.curr();
 
471
        /* Inhibit further responses to this event, or WebKit moves the
 
472
         * cursor to the start. */
 
473
        return false;
372
474
        break;
373
475
    case 40:                /* Down arrow */
374
476
        hist.down(inp.value);
375
477
        inp.value = hist.curr();
 
478
        return false;
376
479
        break;
377
480
    }
378
481
}
379
482
 
 
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
 
380
525
/**** Following Code modified from ******************************************/
381
526
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
382
527
/****************************************************************************/
394
539
        
395
540
    if (scrollDiv.scrollHeight > 0)
396
541
        currentHeight = scrollDiv.scrollHeight;
397
 
    else 
398
 
        if (objDiv.offsetHeight > 0)
399
 
            currentHeight = scrollDiv.offsetHeight;
 
542
    else if (scrollDiv.offsetHeight > 0)
 
543
        currentHeight = scrollDiv.offsetHeight;
400
544
 
401
545
    scrollDiv.scrollTop = currentHeight;
402
546