-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.js
113 lines (99 loc) · 3.06 KB
/
repl.js
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
var QuackRepl = (function () {
function QuackRepl() {
this.stdout = document.querySelector('#quack-repl-stdout');
this.stdin = document.querySelector('#quack-repl-stdin');
this.input = document.querySelector('#quack-repl-stdin-input');
}
QuackRepl.prototype.configureStdin = function () {
var that = this;
document.onclick = function () {
that.stdin.click();
}
this.stdin.onclick = function () {
that.input.focus();
that.input.selectionStart = that.input.selectionEnd = that.input.value.length;
};
this.input.onkeypress = function (e) {
if (e.which === 13 || e.keyCode === 13) {
that.send(that.input.value);
}
};
this.input.onkeyup = function (e) {
var key = e.which || e.keyCode;
if (key === 38) {
var last = that.history[that.history.length - 1 - that.level];
if (last) {
that.input.value = last;
}
that.level = Math.min(that.level + 1, that.history.length);
}
}
};
QuackRepl.prototype.ajax = function (text) {
var xhr = new XMLHttpRequest();
var that = this;
xhr.open('GET', './api.php?action=' + encodeURIComponent(btoa(text)));
xhr.send(null);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
xhr.responseText.split('\n').forEach(function (line) {
that.input.value = "";
that.print(line);
});
that.scrollBottom();
}
};
};
QuackRepl.prototype.send = function (text) {
text = text.trim();
this.history = this.history || [];
this.level = 0;
switch (text) {
case ":clear":
this.stdout.innerHTML = "";
this.history.push(':clear');
break;
case 'show c':
text = ':license'
default:
this.print('<span class="quack-repl-name">Quack></span> ' + text);
if (text !== '') {
this.ajax(text);
}
this.history.push(text);
break;
}
this.input.value = "";
this.scrollBottom();
};
QuackRepl.prototype.print = function (text) {
this.stdout.appendChild((function () {
var div = document.createElement('div');
div.className = 'quack-repl-line';
div.innerHTML = text;
return div;
})());
}
QuackRepl.prototype.scrollBottom = function () {
window.scrollTo(0, document.body.scrollHeight);
};
QuackRepl.prototype.welcome = function () {
var that = this;
[ "Quack · Copyright (C) 2016 Marcelo Camargo <[email protected]>",
"This program comes with ABSOLUTELY NO WARRANTY.",
"This is free software, and you are welcome to redistribute it",
"under certain conditions; type 'show c' for details.",
"Use quack --help for more information",
].forEach(function (line) {
that.print(line);
});
that.input.focus();
that.input.selectionStart = that.input.selectionEnd = that.input.value.length;
}
return QuackRepl;
})();
(function (module) {
module.repl = new QuackRepl();
module.repl.configureStdin();
module.repl.welcome();
})(window);