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

126 by drtomc
A basic version of the console going!
1
var magic = 'xyzzy';
2
3
function historyUp()
4
{
5
    if (this.cursor >= 0)
6
    {
7
        this.cursor--;
8
    }
9
}
10
11
function historyDown()
12
{
13
    if (this.cursor < this.items.length)
14
    {
15
        this.cursor++;
16
    }
17
}
18
19
function historyCurr()
20
{
21
    if (this.cursor < 0 || this.cursor >= this.items.length)
22
    {
23
        return "";
24
    }
25
    return this.items[this.cursor];
26
}
27
28
function historyAdd(text)
29
{
30
    this.items[this.items.length] = text;
31
    this.cursor = this.items.length
32
}
33
34
function historyShow()
35
{
36
    var res = "";
37
    if (this.cursor == -1)
38
    {
39
        res += "[]";
40
    }
41
    for (var i = 0; i < this.items.length; i++)
42
    {
43
        if (i == this.cursor)
44
        {
45
            res += "["
46
        }
47
        res += this.items[i].toString();
48
        if (i == this.cursor)
49
        {
50
            res += "]"
51
        }
52
        res += " "
53
    }
54
    if (this.cursor == this.items.length)
55
    {
56
        res += "[]";
57
    }
58
    return res;
59
}
60
61
function History()
62
{
63
    this.items = new Array();
64
    this.cursor = -1;
65
    this.up = historyUp;
66
    this.down = historyDown;
67
    this.curr = historyCurr;
68
    this.add = historyAdd;
69
    this.show = historyShow;
70
}
71
72
var hist = new History();
111 by drtomc
Checkpoint work on the console.
73
74
function make_query_string(pagename, args)
75
{
76
    var first = true;
77
    var qs = pagename;
126 by drtomc
A basic version of the console going!
78
    for (key in args)
111 by drtomc
Checkpoint work on the console.
79
    {
126 by drtomc
A basic version of the console going!
80
        vals = args[key];
111 by drtomc
Checkpoint work on the console.
81
        // vals can be an array, to make multiple args with the same name
82
        // To handle this, make non-array objects into an array, then loop
83
        if (!(vals instanceof Array))
84
            vals = [vals];
85
        for each (val in vals)
86
        {
126 by drtomc
A basic version of the console going!
87
            if (first)
111 by drtomc
Checkpoint work on the console.
88
            {
89
                qs += "?";
90
                first = false;
91
            }
92
            else
93
            {
94
                qs += "&";
95
            }
96
            qs += encodeURI(key) + "=" + encodeURI(val);
97
        }
98
    }
99
    return qs;
100
}
101
126 by drtomc
A basic version of the console going!
102
function make_post_body(args)
103
{
104
    var first = true;
105
    var qs = '';
106
    for (key in args)
107
    {
108
        vals = args[key];
109
        // vals can be an array, to make multiple args with the same name
110
        // To handle this, make non-array objects into an array, then loop
111
        if (!(vals instanceof Array))
112
            vals = [vals];
113
        for each (val in vals)
114
        {
115
            if (first)
116
            {
117
                first = false;
118
            }
119
            else
120
            {
121
                qs += "&";
122
            }
123
            qs += encodeURI(key) + "=" + encodeURI(val);
124
        }
125
    }
126
    return qs;
127
}
128
129
function enter_line()
130
{
131
    var inp = document.getElementById('inputText');
132
    var digest = hex_md5(inp.value + magic);
133
    var xmlhttp = new XMLHttpRequest();
134
    xmlhttp.open("POST", "chat", false);
135
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
136
    xmlhttp.send(make_post_body({"digest":digest, "text":inp.value}))
137
    var res = JSON.parse(xmlhttp.responseText);
127 by drtomc
A bit of simplification - unify the output areas and use colours to
138
    var output = document.getElementById("output")
139
    {
140
        var pre = document.createElement("pre");
141
        pre.setAttribute("class", "inputMsg");
142
        pre.appendChild(document.createTextNode(inp.value + "\n"));
143
        output.appendChild(pre);
144
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
145
    if (res.hasOwnProperty('okay'))
111 by drtomc
Checkpoint work on the console.
146
    {
147
        // Success!
212 by drtomc
Gosh, it all looks pretty simple in the end!
148
        // print out the output (res.okay[0])
127 by drtomc
A bit of simplification - unify the output areas and use colours to
149
        var pre = document.createElement("pre");
150
        pre.setAttribute("class", "outputMsg");
212 by drtomc
Gosh, it all looks pretty simple in the end!
151
        pre.appendChild(document.createTextNode(res.okay[0]));
127 by drtomc
A bit of simplification - unify the output areas and use colours to
152
        output.appendChild(pre);
212 by drtomc
Gosh, it all looks pretty simple in the end!
153
        // print out the return value (res.okay[1])
154
        if (res.okay[1])
126 by drtomc
A basic version of the console going!
155
        {
127 by drtomc
A bit of simplification - unify the output areas and use colours to
156
            var pre = document.createElement("pre");
157
            pre.setAttribute("class", "outputMsg");
212 by drtomc
Gosh, it all looks pretty simple in the end!
158
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
127 by drtomc
A bit of simplification - unify the output areas and use colours to
159
            output.appendChild(pre);
126 by drtomc
A basic version of the console going!
160
        }
111 by drtomc
Checkpoint work on the console.
161
        // set the prompt to >>>
126 by drtomc
A basic version of the console going!
162
        var prompt = document.getElementById("prompt");
163
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
111 by drtomc
Checkpoint work on the console.
164
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
165
    else if (res.hasOwnProperty('exc'))
111 by drtomc
Checkpoint work on the console.
166
    {
167
        // Failure!
212 by drtomc
Gosh, it all looks pretty simple in the end!
168
        // print out the error message (res.exc)
127 by drtomc
A bit of simplification - unify the output areas and use colours to
169
        var pre = document.createElement("pre");
170
        pre.setAttribute("class", "errorMsg");
212 by drtomc
Gosh, it all looks pretty simple in the end!
171
        pre.appendChild(document.createTextNode(res.exc));
127 by drtomc
A bit of simplification - unify the output areas and use colours to
172
        output.appendChild(pre);
111 by drtomc
Checkpoint work on the console.
173
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
174
    else if (res.hasOwnProperty('more'))
111 by drtomc
Checkpoint work on the console.
175
    {
176
        // Need more input, so set the prompt to ...
126 by drtomc
A basic version of the console going!
177
        var prompt = document.getElementById("prompt");
178
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
179
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
180
    else {
181
        // assert res.hasOwnProperty('input')
182
        var prompt = document.getElementById("prompt");
183
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
184
    }
126 by drtomc
A basic version of the console going!
185
}
186
187
function catch_input(key)
188
{
189
    var inp = document.getElementById('inputText');
190
    if (key == 13)
191
    {
192
        enter_line();
193
        hist.add(inp.value);
127 by drtomc
A bit of simplification - unify the output areas and use colours to
194
        inp.value = hist.curr();
126 by drtomc
A basic version of the console going!
195
    }
196
    if (key == 38)
197
    {
198
        hist.up();
127 by drtomc
A bit of simplification - unify the output areas and use colours to
199
        inp.value = hist.curr();
126 by drtomc
A basic version of the console going!
200
    }
201
    if (key == 40)
202
    {
203
        hist.down();
127 by drtomc
A bit of simplification - unify the output areas and use colours to
204
        inp.value = hist.curr();
126 by drtomc
A basic version of the console going!
205
    }
111 by drtomc
Checkpoint work on the console.
206
}