~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 22:44:35 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:656
Added icons for "home" and "subjects" apps.
conf/apps: "home" and "subjects" apps are now tabs.
    Added a "desc" field for each app with a long description, to be used for
    tool tips and home page descriptions.
    Added "apps_on_home_page" list, similar to apps_in_tabs.
home: The apps list is now generated from apps_on_home_page and includes
    descriptions.

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
 
37
39
/* Starts the console server, if it isn't already.
38
40
 * This can be called any number of times - it only starts the one server.
 
41
 * Note that this is asynchronous. It will return after signalling to start
 
42
 * the server, but there is no guarantee that it has been started yet.
39
43
 * This is a separate step from console_init, as the server is only to be
40
44
 * started once the first command is entered.
41
45
 * Does not return a value. Writes to global variables
42
46
 * server_host, and server_port.
 
47
 *
 
48
 * \param callback Function which will be called after the server has been
 
49
 * started. No parameters are passed. May be null.
43
50
 */
44
 
function start_server()
 
51
function start_server(callback)
45
52
{
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;
 
53
    if (server_started)
 
54
    {
 
55
        callback();
 
56
        return;
 
57
    }
 
58
    var callback1 = function(xhr)
 
59
        {
 
60
            var json_text = xhr.responseText;
 
61
            server_key = JSON.parse(json_text);
 
62
            server_started = true;
 
63
            if (callback != null)
 
64
                callback();
 
65
        }
 
66
    ajax_call(callback1, "consoleservice", "start", {}, "POST");
51
67
}
52
68
 
53
69
/** Initialises the console. All apps which import console are required to
93
109
    if (!windowpane_mode) return;
94
110
    console_body.setAttribute("class", "windowpane maximal");
95
111
    console_filler.setAttribute("class", "windowpane maximal");
96
 
    /* Focus the input box by default */
97
 
    document.getElementById("console_inputText").focus()
98
112
}
99
113
 
100
114
/* current_text is the string currently on the command line.
197
211
 
198
212
var hist = new History();
199
213
 
 
214
function set_interrupt()
 
215
{
 
216
    interrupted = true;
 
217
}
 
218
 
 
219
function clear_output()
 
220
{
 
221
    var output = document.getElementById("console_output");
 
222
    while (output.firstChild)
 
223
    {
 
224
        output.removeChild(output.firstChild);
 
225
    }
 
226
}
 
227
 
200
228
/** Send a line of text to the Python server, wait for its return, and react
201
229
 * to its response by writing to the output box.
202
230
 * Also maximize the console window if not already.
203
231
 */
204
 
function console_enter_line(inputline, which)
 
232
function console_enter_line(inputbox, which)
205
233
{
206
 
    /* Start the server if it hasn't already been started */
207
 
    start_server();
 
234
    interrupted = false;
 
235
 
 
236
    if (typeof(inputbox) == "string")
 
237
    {
 
238
        var inputline = inputbox;
 
239
        inputbox = null;
 
240
        var graytimer = null;
 
241
    }
 
242
    else
 
243
    {
 
244
        GLOBAL_inputbox = inputbox;     /* For timer */
 
245
        var inputline = inputbox.value;
 
246
        var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
 
247
            + "\"disabled\");", 100);
 
248
    }
 
249
    var output = document.getElementById("console_output");
 
250
    {
 
251
        // Print ">>>" span
 
252
        var span = document.createElement("span");
 
253
        span.setAttribute("class", "inputPrompt");
 
254
        span.appendChild(document.createTextNode(">>> "));
 
255
        output.appendChild(span);
 
256
        // Print input line itself in a span
 
257
        var span = document.createElement("span");
 
258
        span.setAttribute("class", "inputMsg");
 
259
        span.appendChild(document.createTextNode(inputline + "\n"));
 
260
        output.appendChild(span);
 
261
    }
208
262
    var args = {"key": server_key, "text":inputline};
209
 
    var xmlhttp = ajax_call("consoleservice", which, args, "POST");
 
263
    var callback = function(xhr)
 
264
        {
 
265
            console_response(inputbox, graytimer, inputline, xhr.responseText);
 
266
        }
 
267
    /* Disable the text box */
 
268
    if (inputbox != null)
 
269
        inputbox.setAttribute("disabled", "disabled");
 
270
    ajax_call(callback, "consoleservice", which, args, "POST");
 
271
}
210
272
 
211
 
    var res = JSON.parse(xmlhttp.responseText);
 
273
function console_response(inputbox, graytimer, inputline, responseText)
 
274
{
 
275
    try
 
276
    {
 
277
        var res = JSON.parse(responseText);
 
278
    }
 
279
    catch (e)
 
280
    {
 
281
        alert("An internal error occurred in the python console.");
 
282
        return;
 
283
    }
212
284
    var output = document.getElementById("console_output");
213
 
    {
214
 
        var pre = document.createElement("pre");
215
 
        pre.setAttribute("class", "inputMsg");
216
 
        pre.appendChild(document.createTextNode(inputline + "\n"));
217
 
        output.appendChild(pre);
218
 
    }
219
285
    if (res.hasOwnProperty('okay'))
220
286
    {
221
287
        // Success!
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])
228
 
        if (res.okay[1])
 
288
        if (res.okay)
229
289
        {
230
 
            var pre = document.createElement("pre");
231
 
            pre.setAttribute("class", "outputMsg");
232
 
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
233
 
            output.appendChild(pre);
 
290
            output.appendChild(document.createTextNode(res.okay + "\n"));
 
291
            output.appendChild(span);
234
292
        }
235
293
        // set the prompt to >>>
236
294
        var prompt = document.getElementById("console_prompt");
239
297
    else if (res.hasOwnProperty('exc'))
240
298
    {
241
299
        // Failure!
242
 
        // print out any output that came before the error
243
 
        if (res.exc[0].length > 0)
244
 
        {
245
 
            var pre = document.createElement("pre");
246
 
            pre.setAttribute("class", "outputMsg");
247
 
            pre.appendChild(document.createTextNode(res.exc[0]));
248
 
            output.appendChild(pre);
249
 
        }
250
 
 
251
300
        // 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);
 
301
        var span = document.createElement("span");
 
302
        span.setAttribute("class", "errorMsg");
 
303
        span.appendChild(document.createTextNode(res.exc + "\n"));
 
304
        output.appendChild(span);
 
305
        // set the prompt to >>>
 
306
        var prompt = document.getElementById("console_prompt");
 
307
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
256
308
    }
257
309
    else if (res.hasOwnProperty('more'))
258
310
    {
260
312
        var prompt = document.getElementById("console_prompt");
261
313
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
262
314
    }
 
315
    else if (res.hasOwnProperty('output'))
 
316
    {
 
317
        if (res.output.length > 0)
 
318
        {
 
319
            output.appendChild(document.createTextNode(res.output));
 
320
        }
 
321
        var callback = function(xhr)
 
322
            {
 
323
                console_response(inputbox, graytimer,
 
324
                                 null, xhr.responseText);
 
325
            }
 
326
        if (interrupted)
 
327
        {
 
328
            var kind = "interrupt";
 
329
        }
 
330
        else
 
331
        {
 
332
            var kind = "chat";
 
333
        }
 
334
        var args = {"key": server_key, "text":''};
 
335
        ajax_call(callback, "consoleservice", kind, args, "POST");
 
336
 
 
337
        // Open up the console so we can see the output
 
338
        // FIXME: do we need to maximize here?
 
339
        console_maximize();
 
340
 
 
341
        /* Auto-scrolling */
 
342
        divScroll.activeScroll();
 
343
 
 
344
        // Return early, so we don't re-enable the input box.
 
345
        return;
 
346
    }
263
347
    else {
264
348
        // assert res.hasOwnProperty('input')
265
349
        var prompt = document.getElementById("console_prompt");
266
350
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
267
351
    }
 
352
 
 
353
    if (inputbox != null)
 
354
    {
 
355
        /* Re-enable the text box */
 
356
        clearTimeout(graytimer);
 
357
        inputbox.removeAttribute("disabled");
 
358
        inputbox.removeAttribute("class");
 
359
        interrupted = false;
 
360
    }
 
361
 
268
362
    /* Open up the console so we can see the output */
269
363
    console_maximize();
 
364
    /* Auto-scrolling */
 
365
    divScroll.activeScroll();
 
366
 
 
367
    // Focus the input box by default
 
368
    document.getElementById("console_output").focus()
 
369
    document.getElementById("console_inputText").focus()
270
370
}
271
371
 
272
372
function catch_input(key)
304
404
         */
305
405
        break;
306
406
    case 13:                /* Enter key */
307
 
        /* Send the line of text to the server */
308
 
        console_enter_line(inp.value, "chat");
309
 
        hist.submit(inp.value);
310
 
        inp.value = hist.curr();
 
407
        var callback = function()
 
408
        {
 
409
            /* Send the line of text to the server */
 
410
            console_enter_line(inp, "chat");
 
411
            hist.submit(inp.value);
 
412
            inp.value = hist.curr();
 
413
        }
 
414
        /* Start the server if it hasn't already been started */
 
415
        start_server(callback);
311
416
        break;
312
417
    case 38:                /* Up arrow */
313
418
        hist.up(inp.value);
319
424
        break;
320
425
    }
321
426
}
 
427
 
 
428
/**** Following Code modified from ******************************************/
 
429
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
 
430
/****************************************************************************/
 
431
var chatscroll = new Object();
 
432
 
 
433
chatscroll.Pane = function(scrollContainerId)
 
434
{
 
435
    this.scrollContainerId = scrollContainerId;
 
436
}
 
437
 
 
438
chatscroll.Pane.prototype.activeScroll = function()
 
439
{
 
440
    var scrollDiv = document.getElementById(this.scrollContainerId);
 
441
    var currentHeight = 0;
 
442
        
 
443
    if (scrollDiv.scrollHeight > 0)
 
444
        currentHeight = scrollDiv.scrollHeight;
 
445
    else 
 
446
        if (objDiv.offsetHeight > 0)
 
447
            currentHeight = scrollDiv.offsetHeight;
 
448
 
 
449
    scrollDiv.scrollTop = currentHeight;
 
450
 
 
451
    scrollDiv = null;
 
452
}
 
453
 
 
454
var divScroll = new chatscroll.Pane('console_output');