~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;
263 by drtomc
A few little cleanups.
31
    this.cursor = this.items.length;
126 by drtomc
A basic version of the console going!
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
126 by drtomc
A basic version of the console going!
74
function make_post_body(args)
75
{
76
    var qs = '';
77
    for (key in args)
78
    {
79
        vals = args[key];
80
        // vals can be an array, to make multiple args with the same name
81
        // To handle this, make non-array objects into an array, then loop
82
        if (!(vals instanceof Array))
83
            vals = [vals];
274 by mattgiuca
console/console.js: Removed make_query_string (not being used, and mostly
84
        var i;
85
        for (i=0; i<vals.length; i++)
126 by drtomc
A basic version of the console going!
86
        {
279 by drtomc
Fix the fix to the for loop. :-)
87
            if (i > 0)
126 by drtomc
A basic version of the console going!
88
            {
89
                qs += "&";
90
            }
274 by mattgiuca
console/console.js: Removed make_query_string (not being used, and mostly
91
            qs += encodeURIComponent(key) + "=" + encodeURIComponent(vals[i]);
126 by drtomc
A basic version of the console going!
92
        }
93
    }
94
    return qs;
95
}
96
97
function enter_line()
98
{
99
    var inp = document.getElementById('inputText');
100
    var digest = hex_md5(inp.value + magic);
101
    var xmlhttp = new XMLHttpRequest();
102
    xmlhttp.open("POST", "chat", false);
103
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
104
    xmlhttp.send(make_post_body({"digest":digest, "text":inp.value}))
105
    var res = JSON.parse(xmlhttp.responseText);
263 by drtomc
A few little cleanups.
106
    var output = document.getElementById("output");
127 by drtomc
A bit of simplification - unify the output areas and use colours to
107
    {
108
        var pre = document.createElement("pre");
109
        pre.setAttribute("class", "inputMsg");
110
        pre.appendChild(document.createTextNode(inp.value + "\n"));
111
        output.appendChild(pre);
112
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
113
    if (res.hasOwnProperty('okay'))
111 by drtomc
Checkpoint work on the console.
114
    {
115
        // Success!
212 by drtomc
Gosh, it all looks pretty simple in the end!
116
        // print out the output (res.okay[0])
127 by drtomc
A bit of simplification - unify the output areas and use colours to
117
        var pre = document.createElement("pre");
118
        pre.setAttribute("class", "outputMsg");
212 by drtomc
Gosh, it all looks pretty simple in the end!
119
        pre.appendChild(document.createTextNode(res.okay[0]));
127 by drtomc
A bit of simplification - unify the output areas and use colours to
120
        output.appendChild(pre);
212 by drtomc
Gosh, it all looks pretty simple in the end!
121
        // print out the return value (res.okay[1])
122
        if (res.okay[1])
126 by drtomc
A basic version of the console going!
123
        {
127 by drtomc
A bit of simplification - unify the output areas and use colours to
124
            var pre = document.createElement("pre");
125
            pre.setAttribute("class", "outputMsg");
212 by drtomc
Gosh, it all looks pretty simple in the end!
126
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
127 by drtomc
A bit of simplification - unify the output areas and use colours to
127
            output.appendChild(pre);
126 by drtomc
A basic version of the console going!
128
        }
111 by drtomc
Checkpoint work on the console.
129
        // set the prompt to >>>
126 by drtomc
A basic version of the console going!
130
        var prompt = document.getElementById("prompt");
131
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
111 by drtomc
Checkpoint work on the console.
132
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
133
    else if (res.hasOwnProperty('exc'))
111 by drtomc
Checkpoint work on the console.
134
    {
135
        // Failure!
212 by drtomc
Gosh, it all looks pretty simple in the end!
136
        // print out the error message (res.exc)
127 by drtomc
A bit of simplification - unify the output areas and use colours to
137
        var pre = document.createElement("pre");
138
        pre.setAttribute("class", "errorMsg");
212 by drtomc
Gosh, it all looks pretty simple in the end!
139
        pre.appendChild(document.createTextNode(res.exc));
127 by drtomc
A bit of simplification - unify the output areas and use colours to
140
        output.appendChild(pre);
111 by drtomc
Checkpoint work on the console.
141
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
142
    else if (res.hasOwnProperty('more'))
111 by drtomc
Checkpoint work on the console.
143
    {
144
        // Need more input, so set the prompt to ...
126 by drtomc
A basic version of the console going!
145
        var prompt = document.getElementById("prompt");
146
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
147
    }
212 by drtomc
Gosh, it all looks pretty simple in the end!
148
    else {
149
        // assert res.hasOwnProperty('input')
150
        var prompt = document.getElementById("prompt");
151
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
152
    }
126 by drtomc
A basic version of the console going!
153
}
154
155
function catch_input(key)
156
{
157
    var inp = document.getElementById('inputText');
158
    if (key == 13)
159
    {
160
        enter_line();
161
        hist.add(inp.value);
127 by drtomc
A bit of simplification - unify the output areas and use colours to
162
        inp.value = hist.curr();
126 by drtomc
A basic version of the console going!
163
    }
164
    if (key == 38)
165
    {
166
        hist.up();
127 by drtomc
A bit of simplification - unify the output areas and use colours to
167
        inp.value = hist.curr();
126 by drtomc
A basic version of the console going!
168
    }
169
    if (key == 40)
170
    {
171
        hist.down();
127 by drtomc
A bit of simplification - unify the output areas and use colours to
172
        inp.value = hist.curr();
126 by drtomc
A basic version of the console going!
173
    }
111 by drtomc
Checkpoint work on the console.
174
}