forked from mmattozzi/webrepl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
193 lines (173 loc) · 6.9 KB
/
index.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
<html>
<head>
<title>Node REPL</title>
<link rel="stylesheet" href="webrepl.css" TYPE="text/css"/>
<script src="jquery-1.5.min.js" type="text/javascript"></script>
<script type="text/javascript">
var blurbHidden = false;
var cmdHistory = [];
var historyIndex = -1;
function historyUp() {
if (historyIndex + 1 < cmdHistory.length) historyIndex++;
if (historyIndex < cmdHistory.length) {
return cmdHistory[historyIndex];
}
}
function historyDown() {
if (historyIndex > -1) historyIndex--;
if (historyIndex == -1) return "";
if (historyIndex < cmdHistory.length) {
return cmdHistory[historyIndex];
}
}
function getCommonPrefix(words) {
if (words.length == 0) return '';
if (words.length == 1) return words[0];
var prefix = words[0];
for (var i = 1; i < words.length; ++i) {
var _p = '';
var min_len = Math.min(prefix.length, words[i].length);
for (var j = 0; j < min_len && prefix[j] == words[i][j]; ++j) {
_p += prefix[j];
}
prefix = _p;
}
return prefix;
}
$(document).ready(function() {
$('#out').ajaxError(function(e, req, opts, err) {
$('#out').prepend($("<div class='replErr'>[ Error communicating with node process ]</div>"));
});
$('#in').keypress(function(event) {
var keyCode = event.keyCode || event.which;
if (keyCode === 13) {
event.preventDefault();
commandEntered();
}
});
$('#in').keydown(function(event) {
var keyCode = event.keyCode || event.which;
if (keyCode === 9) {
event.preventDefault();
complete();
} else if (keyCode === 38) {
event.preventDefault();
$('#in').val(historyUp());
} else if (keyCode === 40) {
event.preventDefault();
$('#in').val(historyDown());
}
});
$('#clear').width('100px');
$('#clear').click(function() {
clear();
});
$.get('info', function(data) {
$('#header').prepend('[' + data.pid + '] ' + data.name);
}, 'json');
$('#in').focus();
});
function clear() {
$.post('clear', function(data) {
getResponse();
});
$('#out').prepend($("<div class='replErr'>[ Reset ]</div>"));
}
function commandEntered() {
if (! blurbHidden) {
$('#blurb').hide();
blurbHidden = true;
}
var command = $('#in').val();
cmdHistory.unshift(command);
historyIndex = -1;
$('#out').prepend($("<div class='replIn'>" + command + "</div>"));
$.post('repl', command, function(data) {
$('#in').val('');
getResponse();
}, "text");
}
function getResponse() {
$.get('repl', function(resData) {
var type = 'replOut';
if (resData.match(/.*Error:/)) {
type = 'replErr';
}
$('#out').prepend($("<div class='" + type + "'>" + resData + "</div>"));
});
}
function complete() {
var str = $('#in').val();
var i = str.lastIndexOf(' ');
var word = str.substr(i + 1);
$.get('complete/' + word, function(data) {
if (data.completions.length == 1) {
var sWord = data.completions[0];
$('#in').val($('#in').val() + lettersToAppend(word, sWord));
} else {
var possibles = [];
for (j = 0; j < data.completions.length; j++) {
if (data.completions[j] != '') {
possibles.push(data.completions[j]);
}
}
var expansion = getCommonPrefix(possibles);
if (expansion.length > 0) {
$('#in').val($('#in').val() + lettersToAppend(word, expansion));
}
var suggestString = '';
for (j = 0; j < possibles.length; j++) {
suggestString += possibles[j] + ' ';
}
if (! blurbHidden) {
$('#blurb').hide();
blurbHidden = true;
}
$('#out').prepend($("<div class='replCompl'>" + suggestString + "</div>"));
}
}, "json");
}
function lettersToAppend(word1, word2) {
var word1Matched = 0;
for (i = 0; i < word1.length + word2.length; i++) {
if (word1[i] === word2[word1Matched]) {
word1Matched++;
if ((i+1) == word1.length) {
return word2.substring(word1Matched);
}
} else {
word1Matched = 0;
}
}
}
function longestInCommon(candidates, index) {
var i, ch, memo
do {
memo = null
for (i=0; i < candidates.length; i++) {
ch = candidates[i].charAt(index)
if (!ch) break
if (!memo) memo = ch
else if (ch != memo) break
}
} while (i == candidates.length && ++index)
return candidates[0].slice(0, index)
}
</script>
</head>
<body>
<div id="header">
<!-- <button type='button' style='margin-left:20px;font-size:20px' onclick='window.history.back()'>Exit</button>
-->
</div>
<table id="input-bar">
<tr>
<td style="width:95%"><input id="in" type="text" /></td>
<td style="width:5%"><span id="clear">^C</span></td>
</tr>
</table>
<div id="main-container">
<div id="out"><div id="blurb">web-repl: Type commands in the above box to execute in this javascript process. Use tab for completion.</div></div>
</div>
</body>
</html>