1. Matthew Turk
  2. yt_ajax

Source

yt_ajax / bottle / httprepl.html

  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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<!-- Internet Explorer has to be in quirks mode for position:fixed to work;
     this comment just does that -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>yt Terminal</title>

<style type='text/css'>

body {
    margin: 0;
    padding: 6px;
}

h1 {
    font: 700 24pt Verdana, sans-serif;
    margin-top: -6px;
    margin-right: -6px;
    margin-bottom: 6px;
    margin-left: -6px;
    padding: 0.5em;
    background-color: #993333;
    color: white;
    border-bottom: 1px solid #330000;
}

#output pre {
    font: 10pt monospace;
    margin: 0;
    padding: 1px;
    border: 1px solid #E0E0FF;
    background-color: #F8F8FF;
    
    white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */
    white-space: -pre-wrap; /* Opera 4 - 6 */
    white-space: -o-pre-wrap; /* Opera 7 */
    white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation)
                            http://www.w3.org/TR/css3-text/#white-space */
    word-wrap: break-word; /* IE 5.5+ */
}

#output pre.stdin {
    border: none;
    background-color: transparent;
}

form {
    font: 10pt monospace;
    margin: 0;
}

#input {
    font: 10pt monospace;
    width: 95%;
    border-top: none;
    border-right: 1px dotted #E0E0FF;
    border-bottom: none;
    border-left: 1px dotted #E0E0FF;
}

#tipbar {
    position: fixed;
    top: 10px;
    
    /* See http://annevankesteren.nl/test/examples/ie/position-fixed.html */
    _position:absolute;
    _top:expression(eval(document.body.scrollTop) + 10);
    
    right: 10px;
    
    max-width: 50%;
    max-height: 80%;
    /* See http://www.svendtofte.com/code/max_width_in_ie/ */
    width: expression(document.body.clientWidth / 2);
    height: expression(document.body.clientHeight * 0.8);
    
    border: 2px outset #DDCCBB;
    background-color: #FFEEDD;
    font: 9pt monospace;
    
    margin: 0;
    padding: 4px;
    
    white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */
    white-space: -pre-wrap; /* Opera 4 - 6 */
    white-space: -o-pre-wrap; /* Opera 7 */
    white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation)
                            http://www.w3.org/TR/css3-text/#white-space */
    word-wrap: break-word; /* IE 5.5+ */
    
    display: none;
    overflow: auto;
}

#tipbar a {
    padding: 4px;
    text-decoration: none;
    color: #666666;
}

</style>

<script type='text/javascript'>

function http() {
    var h;
    if (typeof(XMLHttpRequest) != "undefined") {
        h = new XMLHttpRequest();
    } else {
        try { h = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {
            try { h = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (E) { alert("Your browser is not supported."); }
        }
    }
    return h
}


//                           HISTORY NAVIGATION                           //

function use_history(elem) {
    var content = get_text(elem);
    // Strip the leading prompt
    if (elem.className == 'stdin') content = content.substr(4);
    input.value = content;
    input.focus();
}

function history_hook() {
    use_history(this);
}

var current_history = undefined;

function prev_history() {
    if (current_history == undefined) {
        var prev = document.getElementById("output").lastChild;
    } else {
        var prev = current_history.previousSibling;
    }
    while (prev != null && prev.className != 'stdin') {
        prev = prev.previousSibling;
    }
    if (prev != null) {
        current_history = prev;
        use_history(prev);
    }
}

function next_history() {
    if (current_history == undefined) return;
    
    var next = current_history.nextSibling;
    while (next != null && next.className != 'stdin') {
        next = next.nextSibling;
    }
    if (next != null) {
        current_history = next;
        use_history(next);
    }
}


//                            CODE COMPLETION                            //

current_range = undefined;

function partial_line() {
    if (document.selection) {
        // Internet Explorer
        current_range = document.selection.createRange();
        var end = current_range.getBookmark().charCodeAt(2) - 2;
    } else {
        var end = input.selectionEnd;
    }
    return input.value.substring(0, end);
}

function insert_text(newtext) {
    if (document.selection) {
        // Internet Explorer
        current_range.text = newtext;
        current_range.select();
    } else {
        var start = input.selectionStart;
        input.value = input.value.substring(0, start) + newtext + input.value.substr(input.selectionEnd);
        var end = start + newtext.length;
        input.setSelectionRange(end, end);
    }
}

function clear_tips() {
    tipbar.style.display = '';
    while (tipbar.hasChildNodes()) {
        tipbar.removeChild(tipbar.lastChild);
    }
}

function show_tips() {
    tipbar.style.display = 'block';
}

var intip = false;

function use_tip() {
    insert_text(get_text(this));
    clear_tips();
    intip = true;
    input.focus();
}

function dir() {
    var data = partial_line();
    
    clear_tips();
    
    var h = http();
    h.onreadystatechange = function() {
        if (h.readyState == 4) {
            try {
                var status = h.status;
            } catch(e) {
                var status = "NO HTTP RESPONSE";
            }
            switch (status) {
                case 200:
                    var tips = eval(h.responseText);
                    for (var i=0; i < tips.length; i++) {
                        var tiplink = document.createElement("a");
                        tiplink.onclick = use_tip;
                        tiplink.href = "javascript:void(0)";
                        set_text(tiplink, tips[i]);
                        tipbar.appendChild(tiplink);
                        tipbar.appendChild(document.createElement("br"));
                    }
                    show_tips()
                    input.focus();
                    break;
                // Internet Explorer might return 1223 for 204
                case 1223:
                case 204:
                    // No tips available
                    break;
                case 12029:
                    // Internet Explorer client could not connect to server
                    status = "NO HTTP RESPONSE";
                default:
                    stdout_write(status + "\n" + h.responseText, false);
            }
        }
    }
    h.open("GET", "dir?line=" + escape(data), true);
    h.send(null);
}

function doc() {
    var data = partial_line();
    
    clear_tips();
    
    var h = http();
    h.onreadystatechange = function() {
        if (h.readyState == 4) {
            try {
                var status = h.status;
            } catch(e) {
                var status = "NO HTTP RESPONSE";
            }
            switch (status) {
                case 200:
                    tipbar.appendChild(document.createTextNode(h.responseText));
                    show_tips();
                    input.focus();
                    break;
                // Internet Explorer might return 1223 for 204
                case 1223:
                case 204:
                    // No tips available
                    break;
                case 12029:
                    // Internet Explorer client could not connect to server
                    status = "NO HTTP RESPONSE";
                default:
                    stdout_write(status + "\n" + h.responseText, false);
            }
        }
    }
    h.open("GET", "doc?line=" + escape(data), true);
    h.send(null);
}

//                           COMMAND EXECUTION                           //

function get_text(elem) {
    if (elem.innerText != undefined) {
        // Internet Explorer
        return elem.innerText;
    } else {
        // Mozilla
        return elem.textContent;
    }
}

function set_text(elem, newvalue) {
    if (elem.innerText != undefined) {
        // Internet Explorer
        elem.innerText = newvalue;
    } else {
        // Mozilla
        elem.textContent = newvalue;
    }
}

function get_prompt() {
    return get_text(document.getElementById("prompt"));
}

function set_prompt(newvalue) {
    var p = document.getElementById("prompt");
    set_text(p, newvalue);
}

function stdout_write(content, stdin) {
    if ( typeof stdout_write.counter == 'undefined' ) {
        stdout_write.counter = 0;
    }
    current_history = undefined;
    if (content == "") return;
    
    var block = document.createElement("pre");
    block.ondblclick = history_hook;
    if (stdin) {
        content = get_prompt() + content;
        block.className = 'stdin';
    }
    if (block.innerText != undefined) {
        // Internet Explorer quirkiness
        block.innerText = content;
    } else {
        block.appendChild(document.createTextNode(content));
    }
    block.id = "output_" + (++stdout_write.counter);
    output.appendChild(block);
}

function update_arbitrary_html(new_values) {
    var output_id = "output_" + stdout_write.counter;
    var elem = document.getElementById(output_id);
    var nblock = document.createElement("div");
    nblock.innerHTML = new_values;
    elem.appendChild(nblock);
}

function image_update(image_data, element_id) {
    var elem = document.getElementById(element_id);
    elem.src = "data:image/png;base64," + image_data;
}

whitespace = /^\s+$/;

function panner_widget_action(widget_id, widget_action) {
    var data;
    var h = http();
    h.onreadystatechange = function() {
        if (h.readyState == 4) {
            try {
                var status = h.status;
            } catch(e) {
                var status = "NO HTTP RESPONSE";
            }
            switch (status) {
                case 200:
                    var resp = eval('(' + h.responseText + ')');
                    image_update(resp.image_data, "pan_image_" + widget_id);
                    break;
                // Internet Explorer might return 1223 for 204
                case 1223:
                case 204:
                    break;
                case 12029:
                    // Internet Explorer client could not connect to server
                    status = "NO HTTP RESPONSE";
                default:
                    stdout_write(status + "\n" + h.responseText, false);
            }
        }
    }
    h.open("POST", "panner_widgets", true);
    h.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    h.send("widget_id=" + escape(widget_id) + "&widget_action=" + escape(widget_action));
}

function push() {
    var data = input.value;
    // Uncomment to treat a line with only whitespace like a blank line
    // if (whitespace.test(data)) data = "";
    
    input.value = "";
    
    var h = http();
    h.onreadystatechange = function() {
        if (h.readyState == 4) {
            stdout_write(data, true);
            try {
                var status = h.status;
            } catch(e) {
                var status = "NO HTTP RESPONSE";
            }
            switch (status) {
                case 200:
                    set_prompt(">>> ");
                    var resp = eval('(' + h.responseText + ')');
                    stdout_write(resp.text, false);
                    update_arbitrary_html(resp.new_values);
                    //image_update(resp.image_data, "output_image");
                    break;
                // Internet Explorer might return 1223 for 204
                case 1223:
                case 204:
                    set_prompt("... ");
                    // input.value *should* be blank, but just in case...
                    input.value = "    " + input.value;
                    break;
                case 12029:
                    // Internet Explorer client could not connect to server
                    status = "NO HTTP RESPONSE";
                default:
                    stdout_write(status + "\n" + h.responseText, false);
            }
            input.focus();
        }
    }
    h.open("POST", "push", true);
    h.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    h.send("line=" + escape(data));
}

function escape(data) {
    data = data.replace(/%/gi, "%25");
    data = data.replace(/&/gi, "%26");
    data = data.replace(/[+]/gi, "%2B");
    data = data.replace(/;/gi, "%3B");
    return data;
}


//                                 SETUP                                 //

function mapKeys(key_code) {
    if (intip) {
        intip = false;
        // Trap the bubbling keyup event from a tiplink. This means that,
        // if you actually click a tiplink instead of tab and hit 'enter',
        // you have to hit 'enter' twice to push the line. :(
        if (key_code == 13) return;
    }
    switch(key_code) {
        case 8:    // backspace
            clear_tips();
            break;
        case 13:   // enter
            clear_tips();
            push();
            break;
        case 38:   // up arrow
            prev_history();
            break;
        case 40:   // down arrow
            next_history();
            break;
        case 48:  // close paren "("
            clear_tips();
            break;
        case 57:  // open paren "("
            doc();
            break;
        case 190:  // period "."
            dir();
            break;
    }
}

function trapKeys(e) {
    if (!e) var e = window.event;
    mapKeys(e.keyCode);
}

input = undefined;
output = undefined;
tipbar = undefined;

function init() {
    input = document.getElementById("input");
    input.onkeyup = trapKeys;
    output = document.getElementById("output");
    tipbar = document.getElementById("tipbar");
    input.focus();
}

</script>
</head>

<body onload="init();">
<h1>yt Terminal</h1>

<div id="output">
</div>

<form action="" id="terminal" onsubmit="return false;">
<div id='tipbar'></div>
<nobr><span id='prompt'>&gt;&gt;&gt; </span><input type="text" id="input"
    size="60" /></nobr>
</form>

</body>
</html>