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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* IVLE - Informatics Virtual Learning Environment
 * Copyright (C) 2007-2008 The University of Melbourne
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Module: Console (Client-side JavaScript)
 * Author: Tom Conway, Matt Giuca
 * Date: 30/1/2008
 */

digest_constant = "hello";

var server_host;
var server_port;
var server_magic;

/* Begin religious debate (tabs vs spaces) here: */
/* (This string will be inserted in the console when the user presses the Tab
 * key) */
TAB_STRING = "    ";

/* Console DOM objects */
console_body = null;
console_filler = null;

windowpane_mode = false;
server_started = false;

/* Starts the console server, if it isn't already.
 * This can be called any number of times - it only starts the one server.
 * This is a separate step from console_init, as the server is only to be
 * started once the first command is entered.
 * Does not return a value. Writes to global variables
 * server_host, server_port, server_magic.
 */
function start_server()
{
    if (server_started) return;
    var xhr = ajax_call("consoleservice", "start", {}, "POST");
    var json_text = xhr.responseText;
    var server_info = JSON.parse(json_text);
    server_host = server_info.host;
    server_port = server_info.port;
    server_magic = server_info.magic;
    server_started = true;
}

/** Initialises the console. All apps which import console are required to
 * call this function.
 * Optional "windowpane" (bool), if true, will cause the console to go into
 * "window pane" mode which will allow it to be opened and closed, and float
 * over the page.
 * (Defaults to closed).
 */
function console_init(windowpane)
{
    /* Set up the console as a floating pane */
    console_body = document.getElementById("console_body");
    /* If there is no console body, don't worry.
     * (This lets us import console.js even on pages without a console box */
    if (console_body == null) return;
    console_filler = document.getElementById("console_filler");
    if (windowpane)
    {
        windowpane_mode = true;
        console_minimize();
    }
    /* TEMP: Start the server now.
     * Ultimately we want the server to start only when a line is typed, but
     * it currently does it asynchronously and doesn't start in time for the
     * first line. */
    start_server();
}

/** Hide the main console panel, so the console minimizes to just an input box
 *  at the page bottom. */
function console_minimize()
{
    if (!windowpane_mode) return;
    console_body.setAttribute("class", "windowpane minimal");
    console_filler.setAttribute("class", "windowpane minimal");
}

/** Show the main console panel, so it enlarges out to its full size.
 */
function console_maximize()
{
    if (!windowpane_mode) return;
    console_body.setAttribute("class", "windowpane maximal");
    console_filler.setAttribute("class", "windowpane maximal");
    /* Focus the input box by default */
    document.getElementById("console_inputText").focus()
}

/* current_text is the string currently on the command line.
 * If non-empty, it will be stored at the bottom of the history.
 */
function historyUp(current_text)
{
    /* Remember the changes made to this item */
    this.edited[this.cursor] = current_text;
    if (this.cursor > 0)
    {
        this.cursor--;
    }
    this.earliestCursor = this.cursor;
}

function historyDown(current_text)
{
    /* Remember the changes made to this item */
    this.edited[this.cursor] = current_text;
    if (this.cursor < this.items.length - 1)
    {
        this.cursor++;
    }
}

function historyCurr()
{
    return this.edited[this.cursor];
}

function historySubmit(text)
{
    /* Copy the selected item's "edited" version over the permanent version of
     * the last item. */
    this.items[this.items.length-1] = text;
    /* Add a new blank item */
    this.items[this.items.length] = "";
    this.cursor = this.items.length-1;
    /* Blow away all the edited versions, replacing them with the existing
     * items set.
     * Not the whole history - just start from the earliest edited one.
     * (This avoids slowdown over extended usage time).
     */
    for (var i=this.earliestCursor; i<=this.cursor; i++)
        this.edited[i] = this.items[i];
    this.earliestCursor = this.cursor;
}

function historyShow()
{
    var res = "";
    for (var i = 0; i < this.items.length; i++)
    {
        if (i == this.cursor)
        {
            res += "["
        }
        res += this.items[i].toString();
        if (i == this.cursor)
        {
            res += "]"
        }
        res += " "
    }
    if (this.cursor == this.items.length)
    {
        res += "[]";
    }
    return res;
}

/* How history works
 * This is a fairly complex mechanism due to complications when editing
 * history items. We store two arrays. "items" is the permanent history of
 * each item. "edited" is a "volatile" version of items - the edits made to
 * the history between now and last time you hit "enter".
 * This is because the user can go back and edit any of the previous items,
 * and the edits are remembered until they hit enter.
 *
 * When hitting enter, the "edited" version of the currently selected item
 * replaces the "item" version of the last item in the list.
 * Then a new blank item is created, for the new line of input.
 * Lastly, all the "edited" versions are replaced with their stable versions.
 *
 * Cursor never points to an invalid location.
 */
function History()
{
    this.items = new Array("");
    this.edited = new Array("");
    this.cursor = 0;
    this.earliestCursor = 0;
    this.up = historyUp;
    this.down = historyDown;
    this.curr = historyCurr;
    this.submit = historySubmit;
    this.show = historyShow;
}

var hist = new History();

/** Send a line of text to the Python server, wait for its return, and react
 * to its response by writing to the output box.
 * Also maximize the console window if not already.
 */
function console_enter_line(inputline, which)
{
    /* Start the server if it hasn't already been started */
    start_server();
    var digest = hex_md5(inputline + server_magic);
    var args = {"host": server_host, "port": server_port,
                    "digest":digest, "text":inputline};
    var xmlhttp = ajax_call("consoleservice", which, args, "POST");

    var res = JSON.parse(xmlhttp.responseText);
    var output = document.getElementById("console_output");
    {
        var pre = document.createElement("pre");
        pre.setAttribute("class", "inputMsg");
        pre.appendChild(document.createTextNode(inputline + "\n"));
        output.appendChild(pre);
    }
    if (res.hasOwnProperty('okay'))
    {
        // Success!
        // print out the output (res.okay[0])
        var pre = document.createElement("pre");
        pre.setAttribute("class", "outputMsg");
        pre.appendChild(document.createTextNode(res.okay[0]));
        output.appendChild(pre);
        // print out the return value (res.okay[1])
        if (res.okay[1])
        {
            var pre = document.createElement("pre");
            pre.setAttribute("class", "outputMsg");
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
            output.appendChild(pre);
        }
        // set the prompt to >>>
        var prompt = document.getElementById("console_prompt");
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
    }
    else if (res.hasOwnProperty('exc'))
    {
        // Failure!
        // print out any output that came before the error
        if (res.exc[0].length > 0)
        {
            var pre = document.createElement("pre");
            pre.setAttribute("class", "outputMsg");
            pre.appendChild(document.createTextNode(res.exc[0]));
            output.appendChild(pre);
        }

        // print out the error message (res.exc)
        var pre = document.createElement("pre");
        pre.setAttribute("class", "errorMsg");
        pre.appendChild(document.createTextNode(res.exc[1]));
        output.appendChild(pre);
    }
    else if (res.hasOwnProperty('more'))
    {
        // Need more input, so set the prompt to ...
        var prompt = document.getElementById("console_prompt");
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
    }
    else {
        // assert res.hasOwnProperty('input')
        var prompt = document.getElementById("console_prompt");
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
    }
    /* Open up the console so we can see the output */
    console_maximize();
}

function catch_input(key)
{
    var inp = document.getElementById('console_inputText');
    switch (key)
    {
    case 9:                 /* Tab key */
        var selstart = inp.selectionStart;
        var selend = inp.selectionEnd;
        if (selstart == selend)
        {
            /* No selection, just a carat. Insert a tab here. */
            inp.value = inp.value.substr(0, selstart)
                + TAB_STRING + inp.value.substr(selstart);
        }
        else
        {
            /* Text is selected. Just indent the whole line
             * by inserting a tab at the start */
            inp.value = TAB_STRING + inp.value;
        }
        /* Update the selection so the same characters as before are selected
         */
        inp.selectionStart = selstart + TAB_STRING.length;
        inp.selectionEnd = inp.selectionStart + (selend - selstart);
        /* Cancel the event, so the TAB key doesn't move focus away from this
         * box */
        return false;
        /* Note: If it happens that some browsers don't support event
         * cancelling properly, this hack might work instead:
        setTimeout(
            "document.getElementById('console_inputText').focus()",
            0);
         */
        break;
    case 13:                /* Enter key */
        /* Send the line of text to the server */
        console_enter_line(inp.value, "chat");
        hist.submit(inp.value);
        inp.value = hist.curr();
        break;
    case 38:                /* Up arrow */
        hist.up(inp.value);
        inp.value = hist.curr();
        break;
    case 40:                /* Down arrow */
        hist.down(inp.value);
        inp.value = hist.curr();
        break;
    }
}