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

332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
1
/* IVLE - Informatics Virtual Learning Environment
2
 * Copyright (C) 2007-2008 The University of Melbourne
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 *
18
 * Module: Console (Client-side JavaScript)
19
 * Author: Tom Conway, Matt Giuca
20
 * Date: 30/1/2008
21
 */
22
432 by drtomc
usrmgt: more work on this. Still some work to go.
23
var server_key;
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
24
339 by mattgiuca
console:
25
/* Begin religious debate (tabs vs spaces) here: */
26
/* (This string will be inserted in the console when the user presses the Tab
27
 * key) */
28
TAB_STRING = "    ";
29
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
30
/* Console DOM objects */
31
console_body = null;
32
console_filler = null;
33
339 by mattgiuca
console:
34
windowpane_mode = false;
343 by mattgiuca
console: Small refactoring of how server starts up. Currently does not affect
35
server_started = false;
339 by mattgiuca
console:
36
343 by mattgiuca
console: Small refactoring of how server starts up. Currently does not affect
37
/* Starts the console server, if it isn't already.
38
 * This can be called any number of times - it only starts the one server.
39
 * This is a separate step from console_init, as the server is only to be
40
 * started once the first command is entered.
41
 * Does not return a value. Writes to global variables
432 by drtomc
usrmgt: more work on this. Still some work to go.
42
 * server_host, and server_port.
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
43
 */
44
function start_server()
45
{
343 by mattgiuca
console: Small refactoring of how server starts up. Currently does not affect
46
    if (server_started) return;
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
47
    var xhr = ajax_call("consoleservice", "start", {}, "POST");
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
48
    var json_text = xhr.responseText;
432 by drtomc
usrmgt: more work on this. Still some work to go.
49
    server_key = JSON.parse(json_text);
343 by mattgiuca
console: Small refactoring of how server starts up. Currently does not affect
50
    server_started = true;
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
51
}
52
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
53
/** Initialises the console. All apps which import console are required to
54
 * call this function.
55
 * Optional "windowpane" (bool), if true, will cause the console to go into
56
 * "window pane" mode which will allow it to be opened and closed, and float
57
 * over the page.
58
 * (Defaults to closed).
59
 */
331 by mattgiuca
Console: Configured console to display properly as a "floating" window in the
60
function console_init(windowpane)
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
61
{
331 by mattgiuca
Console: Configured console to display properly as a "floating" window in the
62
    /* Set up the console as a floating pane */
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
63
    console_body = document.getElementById("console_body");
362 by mattgiuca
console: Automatically focus input box when starting console app, or when
64
    /* If there is no console body, don't worry.
65
     * (This lets us import console.js even on pages without a console box */
66
    if (console_body == null) return;
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
67
    console_filler = document.getElementById("console_filler");
331 by mattgiuca
Console: Configured console to display properly as a "floating" window in the
68
    if (windowpane)
339 by mattgiuca
console:
69
    {
70
        windowpane_mode = true;
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
71
        console_minimize();
339 by mattgiuca
console:
72
    }
343 by mattgiuca
console: Small refactoring of how server starts up. Currently does not affect
73
    /* TEMP: Start the server now.
74
     * Ultimately we want the server to start only when a line is typed, but
75
     * it currently does it asynchronously and doesn't start in time for the
76
     * first line. */
77
    start_server();
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
78
}
79
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
80
/** Hide the main console panel, so the console minimizes to just an input box
81
 *  at the page bottom. */
82
function console_minimize()
83
{
339 by mattgiuca
console:
84
    if (!windowpane_mode) return;
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
85
    console_body.setAttribute("class", "windowpane minimal");
86
    console_filler.setAttribute("class", "windowpane minimal");
87
}
88
89
/** Show the main console panel, so it enlarges out to its full size.
90
 */
91
function console_maximize()
92
{
339 by mattgiuca
console:
93
    if (!windowpane_mode) return;
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
94
    console_body.setAttribute("class", "windowpane maximal");
95
    console_filler.setAttribute("class", "windowpane maximal");
362 by mattgiuca
console: Automatically focus input box when starting console app, or when
96
    /* Focus the input box by default */
97
    document.getElementById("console_inputText").focus()
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
98
}
99
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
100
/* current_text is the string currently on the command line.
101
 * If non-empty, it will be stored at the bottom of the history.
102
 */
103
function historyUp(current_text)
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
104
{
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
105
    /* Remember the changes made to this item */
106
    this.edited[this.cursor] = current_text;
107
    if (this.cursor > 0)
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
108
    {
109
        this.cursor--;
110
    }
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
111
    this.earliestCursor = this.cursor;
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
112
}
113
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
114
function historyDown(current_text)
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
115
{
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
116
    /* Remember the changes made to this item */
117
    this.edited[this.cursor] = current_text;
118
    if (this.cursor < this.items.length - 1)
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
119
    {
120
        this.cursor++;
121
    }
122
}
123
124
function historyCurr()
125
{
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
126
    return this.edited[this.cursor];
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
127
}
128
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
129
function historySubmit(text)
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
130
{
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
131
    /* Copy the selected item's "edited" version over the permanent version of
132
     * the last item. */
133
    this.items[this.items.length-1] = text;
134
    /* Add a new blank item */
135
    this.items[this.items.length] = "";
136
    this.cursor = this.items.length-1;
137
    /* Blow away all the edited versions, replacing them with the existing
138
     * items set.
139
     * Not the whole history - just start from the earliest edited one.
140
     * (This avoids slowdown over extended usage time).
141
     */
142
    for (var i=this.earliestCursor; i<=this.cursor; i++)
143
        this.edited[i] = this.items[i];
144
    this.earliestCursor = this.cursor;
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
145
}
146
147
function historyShow()
148
{
149
    var res = "";
150
    for (var i = 0; i < this.items.length; i++)
151
    {
152
        if (i == this.cursor)
153
        {
154
            res += "["
155
        }
156
        res += this.items[i].toString();
157
        if (i == this.cursor)
158
        {
159
            res += "]"
160
        }
161
        res += " "
162
    }
163
    if (this.cursor == this.items.length)
164
    {
165
        res += "[]";
166
    }
167
    return res;
168
}
169
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
170
/* How history works
171
 * This is a fairly complex mechanism due to complications when editing
172
 * history items. We store two arrays. "items" is the permanent history of
173
 * each item. "edited" is a "volatile" version of items - the edits made to
174
 * the history between now and last time you hit "enter".
175
 * This is because the user can go back and edit any of the previous items,
176
 * and the edits are remembered until they hit enter.
177
 *
178
 * When hitting enter, the "edited" version of the currently selected item
179
 * replaces the "item" version of the last item in the list.
180
 * Then a new blank item is created, for the new line of input.
181
 * Lastly, all the "edited" versions are replaced with their stable versions.
182
 *
183
 * Cursor never points to an invalid location.
184
 */
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
185
function History()
186
{
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
187
    this.items = new Array("");
188
    this.edited = new Array("");
189
    this.cursor = 0;
190
    this.earliestCursor = 0;
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
191
    this.up = historyUp;
192
    this.down = historyDown;
193
    this.curr = historyCurr;
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
194
    this.submit = historySubmit;
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
195
    this.show = historyShow;
196
}
197
198
var hist = new History();
199
333 by mattgiuca
console.js: enter_line now accepts the line as an argument instead of reading
200
/** Send a line of text to the Python server, wait for its return, and react
201
 * to its response by writing to the output box.
202
 * Also maximize the console window if not already.
203
 */
365 by drtomc
Make the console accept blocks of code.
204
function console_enter_line(inputline, which)
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
205
{
343 by mattgiuca
console: Small refactoring of how server starts up. Currently does not affect
206
    /* Start the server if it hasn't already been started */
207
    start_server();
432 by drtomc
usrmgt: more work on this. Still some work to go.
208
    var args = {"key": server_key, "text":inputline};
365 by drtomc
Make the console accept blocks of code.
209
    var xmlhttp = ajax_call("consoleservice", which, args, "POST");
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
210
211
    var res = JSON.parse(xmlhttp.responseText);
328 by mattgiuca
console: Renamed HTML element IDs to prefix "console_".
212
    var output = document.getElementById("console_output");
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
213
    {
214
        var pre = document.createElement("pre");
215
        pre.setAttribute("class", "inputMsg");
333 by mattgiuca
console.js: enter_line now accepts the line as an argument instead of reading
216
        pre.appendChild(document.createTextNode(inputline + "\n"));
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
217
        output.appendChild(pre);
218
    }
219
    if (res.hasOwnProperty('okay'))
220
    {
221
        // 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])
229
        {
230
            var pre = document.createElement("pre");
231
            pre.setAttribute("class", "outputMsg");
232
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
233
            output.appendChild(pre);
234
        }
235
        // set the prompt to >>>
328 by mattgiuca
console: Renamed HTML element IDs to prefix "console_".
236
        var prompt = document.getElementById("console_prompt");
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
237
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
238
    }
239
    else if (res.hasOwnProperty('exc'))
240
    {
241
        // Failure!
341 by mattgiuca
www/media/console/console.js: Merged in Tom's changes from console/console.js.
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
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
251
        // print out the error message (res.exc)
252
        var pre = document.createElement("pre");
253
        pre.setAttribute("class", "errorMsg");
341 by mattgiuca
www/media/console/console.js: Merged in Tom's changes from console/console.js.
254
        pre.appendChild(document.createTextNode(res.exc[1]));
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
255
        output.appendChild(pre);
256
    }
257
    else if (res.hasOwnProperty('more'))
258
    {
259
        // Need more input, so set the prompt to ...
328 by mattgiuca
console: Renamed HTML element IDs to prefix "console_".
260
        var prompt = document.getElementById("console_prompt");
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
261
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
262
    }
263
    else {
264
        // assert res.hasOwnProperty('input')
328 by mattgiuca
console: Renamed HTML element IDs to prefix "console_".
265
        var prompt = document.getElementById("console_prompt");
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
266
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
267
    }
332 by mattgiuca
console plugin: Now presents minimize/maximize buttons, allowing itself to be
268
    /* Open up the console so we can see the output */
269
    console_maximize();
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
270
}
271
272
function catch_input(key)
273
{
328 by mattgiuca
console: Renamed HTML element IDs to prefix "console_".
274
    var inp = document.getElementById('console_inputText');
339 by mattgiuca
console:
275
    switch (key)
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
276
    {
339 by mattgiuca
console:
277
    case 9:                 /* Tab key */
278
        var selstart = inp.selectionStart;
279
        var selend = inp.selectionEnd;
280
        if (selstart == selend)
281
        {
282
            /* No selection, just a carat. Insert a tab here. */
283
            inp.value = inp.value.substr(0, selstart)
284
                + TAB_STRING + inp.value.substr(selstart);
285
        }
286
        else
287
        {
288
            /* Text is selected. Just indent the whole line
289
             * by inserting a tab at the start */
290
            inp.value = TAB_STRING + inp.value;
291
        }
292
        /* Update the selection so the same characters as before are selected
293
         */
294
        inp.selectionStart = selstart + TAB_STRING.length;
295
        inp.selectionEnd = inp.selectionStart + (selend - selstart);
296
        /* Cancel the event, so the TAB key doesn't move focus away from this
297
         * box */
298
        return false;
299
        /* Note: If it happens that some browsers don't support event
300
         * cancelling properly, this hack might work instead:
301
        setTimeout(
302
            "document.getElementById('console_inputText').focus()",
303
            0);
304
         */
305
        break;
306
    case 13:                /* Enter key */
307
        /* Send the line of text to the server */
365 by drtomc
Make the console accept blocks of code.
308
        console_enter_line(inp.value, "chat");
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
309
        hist.submit(inp.value);
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
310
        inp.value = hist.curr();
339 by mattgiuca
console:
311
        break;
312
    case 38:                /* Up arrow */
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
313
        hist.up(inp.value);
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
314
        inp.value = hist.curr();
339 by mattgiuca
console:
315
        break;
316
    case 40:                /* Down arrow */
350 by mattgiuca
media/console/console.js: Rewrote console history storage, browsing, and
317
        hist.down(inp.value);
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
318
        inp.value = hist.curr();
339 by mattgiuca
console:
319
        break;
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
320
    }
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
321
}