-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathide.html
410 lines (361 loc) · 12.6 KB
/
ide.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
<!doctype html>
<html>
<head>
<title>IDE</title>
<script src="lib/jquery.js"></script>
<script src="lib/facebox.js"></script>
<link rel="stylesheet" href="lib/facebox.css">
<script src="https://pagecdn.io/lib/ace/1.4.13/ace.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
html, body {
font-family: Verdana, Arial, sans-serif;
overflow: hidden;
}
#ioDiv {
position: absolute;
right: 50%;
left: 0;
bottom: 0;
top: 0;
}
#code {
position: absolute;
left: 50%;
right: 0;
bottom: 0;
top: 0;
}
#inputDiv, #outputDiv, #consoleDiv, #commandsDiv {
position: absolute;
}
#consoleDiv {
right: 30%;
left: 0;
top: 50%;
bottom: 0;
}
#commandsDiv {
width: 30%;
right: 0;
top: 50%;
}
#commandsDiv input {
width: 100%;
/*margin-bottom: 5px;*/
}
#outputDiv {
right: 0;
left: 50%;
top: 0;
bottom: 50%;
}
#inputDiv {
left: 0;
right: 50%;
top: 0;
bottom: 50%;
}
#inputDiv form, #outputDiv form, #consoleDiv form {
display: block;
position: absolute;
top: 20px;
bottom: 10px;
left: 10px;
right: 10px;
}
#inputDiv textarea, #outputDiv textarea, #consoleDiv textarea {
width: 100%;
height: 100%;
}
.title {
font-weight: bold;
margin-left: 10px;
}
.title span {
font-size: 80%;
font-weight: normal;
margin-left: 10px;
}
#help, #arrayhelp, #stringhelp {
display: none;
}
#facebox h2 {
text-align: center;
margin-bottom: 15px;
margin-right: 16px;
}
#facebox p {
margin-top: 3px;
margin-bottom: 15px;
margin-right: 16px;
}
</style>
</head>
<body>
<div id="ioDiv">
<div id="inputDiv">
<div class="title">Input <span><a href="#" onclick="return openFile();">Open file</a></span> <span><a href="#" onclick="clearInput(); return false;">Clear</a></span></div>
<form>
<input type="file" id="inputfile" accept="*" style="display:none" onchange="handleInputFiles(this.files)">
<textarea id="input" name="input"></textarea></form>
</div>
<div id="outputDiv">
<div class="title">Output <span><a href="#" onclick="$('#output').val(''); return false;">Clear</a></span></div>
<form><textarea id="output" name="output" readonly></textarea></form>
</div>
<div id="consoleDiv">
<div class="title">Console <span><a href="#" onclick="$('#console').val(''); return false;">Clear</a></span></div>
<form><textarea id="console" name="console" readonly></textarea></form>
</div>
<div id="commandsDiv">
<input type="button" value="Run" onclick="runProgram(false);"/>
<input type="button" value="Stop" onclick="stopProgram();"/>
<input type="button" value="Clear program" onclick="if (confirm('Are you sure?')) { editor.getSession().setValue(''); editor.focus(); } return false;" style="margin-bottom: 20px;"/>
<input type="button" value="Special functions" onclick="jQuery.facebox({ div: '#help' });" />
<input type="button" value="Array functions" onclick="jQuery.facebox({ div: '#arrayhelp' });" />
<input type="button" value="String functions" onclick="jQuery.facebox({ div: '#stringhelp' });" />
</div>
</div>
<div id="code">
</div>
<div id="help">
<h2>Special functions</h2>
<div><b>cache</b></div>
<p>
Object to store data between iterations; it will be reset after each complete run. Use it as any other JS object to store data in its properties.
</p>
<div><b>print(arg1<em>[, arg2, arg3, ...]</em>)</b></div>
<p>
Print on output for the current case; will separate each arguments with a single space character; if an argument is an array, its elements are also printed separatedly.
</p>
<p>
If <em>return</em> is used in the program, the returned value (if any) will be printed.
</p>
<div><b>println(arg1<em>[, arg2, arg3, ...]</em>)</b></div>
<p>
Works like <em>print</em>, but appends a new line character at the end of the printing sequence.
</p>
<div><b>log(arg1<em>[, arg2, arg3, ...]</em>)</b></div>
<p>
Logs a message to the console; objects passed to this function are converted to JSON.
</p>
<div><b>readLine()</b></div>
<p>
Reads the next line as a string.
</p>
<p>
<div><b>readInt()</b></div>
Reads the next line, returning its integer value.
</p>
<div><b>readInts()</b></div>
<p>
Reads the next line, and interprets it as a list of integers, returning an array with them.
</p>
<div><b>readFloats()</b></div>
<p>
Reads the next line, and interprets it as a list of floats, returning an array with them.
</p>
<div><b>readStrings()</b></div>
<p>
Reads the next line assuming is a list of space-separated strings, and returns them as an array.
</p>
<div><b>smartRead(<em>[firstIsCount]</em>)</b></div>
<p>
Reads the next line and tries to guess what it is made of; the parameter indicates if the first number is the number of subgroups (if it's not present it will be autodetected), and detects strings, numbers and floats.
</p>
</div>
<div id="arrayhelp">
<h2>Array functions</h2>
<div><b><em>array</em>.shift()</b></div>
<p>Removes the first element of an array, and returns that element.</p>
<div><b><em>array</em>.pop()</b></div>
<p>Removes the last element of an array, and returns that element.</p>
<div><b><em>array</em>.unshift(arg1<em>[, arg2, arg3, ...]</em>)</b></div>
<p>Adds the specified elements (in order) to the top of array.</p>
<div><b><em>array</em>.push(arg1<em>[, arg2, arg3, ...]</em>)</b></div>
<p>Adds the specified elements (in order) to the bottom of array.</p>
<div><b><em>array</em>.splice(index, howmanytoremove<em>[, arg1, arg2, arg3, ...]</em>)</b></div>
<p>The splice() method adds and/or removes elements to/from an array, and returns the removed element(s).</p>
<div><b><em>array</em>.sort(<em>[compareFunction]</em>)</b></div>
<p>Sorts the elements of an array in place. <em>compareFunction</em> takes 2 parameters, and compares them; if the result is <0, a is before b; if is 0, keeps the order between a and b, and >0 puts a after b.</p>
<div><b><em>array</em>.join(<em>[separator]</em>)</b></div>
<p>Joins all the elements of an array into a string, placing <em>separator</em> between them (or "," if unespecified).</p>
</div>
<div id="stringhelp">
<h2>String functions</h2>
<div><b><em>string</em>.toLowerCase(), <em>string</em>.toUpperCase()</b></div>
<p>Converts an string to all lowercase or all uppercase respectively.</p>
<div><b><em>string</em>.indexOf(arg<em>[, start]</em>), <em>string</em>.lastIndexOf(arg<em>[, start]</em>)</b></div>
<p>Find the next instance of <em>arg</em> in a string starting from position <em>start</em>, from the start or from the end respectively. Returns -1 if not found.</p>
<div><b><em>string</em>.substr(start<em>[, length]</em>)</b></div>
<p>Extracts a substring from a string, starting from <em>start</em>; if <em>length</em> is not specified, it will extract to the end.</p>
<div><b><em>string</em>.substring(start<em>[, end]</em>)</b></div>
<p>Extracts a substring from a string between two specified indices; if only one is specified, it will extract to the end.</p>
<div><b><em>string</em>.split(separator<em>[, limit]</em>)</b></div>
<p>Splits the strings into substrings; if <em>limit</em> is used, it will stop splitting when the number is reached. Use empty string as the separator to return an array with the individual characters.</p>
<div><b><em>string</em>.charAt(index)</b></div>
<p>Returns the character at certain position of a string.</p>
<div><b><em>string</em>.charCodeAt(index)</b></div>
<p>Returns the Unicode value of a character at certain position of a string.</p>
</div>
<script>
var bigFile = false;
var bigFileContent = null;
function clearInput() {
bigFile = false;
bigFileContent = null;
$('#input').val('').removeAttr('readonly').focus();
}
function openFile() {
$('#inputfile').trigger('click');
return false;
}
function handleInputFiles(files) {
if (!files.length) {
alert("No files selected!");
} else {
var reader = new FileReader();
reader.onload = function() {
if (reader.result.length > 300000) {
bigFile = true;
bigFileContent = reader.result;
$('#input').attr('readonly','readonly').val("File too big to show. The program will still run.\n\nPress the Clear link to remove.");
} else {
bigFile = false;
bigFileContent = null;
$('#input').removeAttr('readonly').val(reader.result);
}
};
reader.onerror = function(stuff) {
alert('File could not be opened');
};
reader.readAsText(files[0]);
}
}
function dragenter(e) {
e.stopPropagation();
e.preventDefault();
}
function dragover(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
var dt = e.dataTransfer;
var files = dt.files;
handleInputFiles(files);
}
var consoleCache = null;
function logMessage(message) {
if (consoleCache == null) {
consoleCache = document.getElementById('console');
}
message += "\n------------\n" + consoleCache.value;
consoleCache.value = message;
}
var worker = null;
function runProgram(withLogs, param) {
// Store data
if (!bigFile) {
localStorage["input"] = $('#input').val();
}
localStorage["code"] = editor.getSession().getValue();
if (worker != null) worker.terminate();
if (!window.BlobBuilder) {
if (window.WebKitBlobBuilder) {
window.BlobBuilder = window.WebKitBlobBuilder;
} else if (window.MozBlobBuilder) {
window.BlobBuilder = window.MozBlobBuilder;
} else if (window.MSBlobBuilder) {
window.BlobBuilder = window.MSBlobBuilder;
}
}
if (!window.URL && window.webkitURL)
window.URL = window.webkitURL;
var currentLocation = window.location.href;
var lastIndex = currentLocation.lastIndexOf('/');
currentLocation = currentLocation.substring(0, lastIndex);
var blob;
var blobContents = "var cache = {}; function nullifyCache() { cache = {}; } function program() {\n" + editor.getSession().getValue() + "}\n";
if (!window.BlobBuilder) {
blob = new Blob([blobContents]);
} else {
var bb = new BlobBuilder();
bb.append(blobContents);
blob = bb.getBlob();
}
var blobURL = window.URL.createObjectURL(blob);
worker = new Worker("worker.js");
worker.onerror = function(event) {
logMessage("Error on worker, line " + (event.lineno-1) + ": " + event.message);
};
worker.onmessage = function(event) {
if (event.data[0] == "result") {
$('#output').val(event.data[1]);
} else if (event.data[0] == "log") {
logMessage(event.data[1]);
}
};
var numCases = -1;
if (typeof param != "undefined") {
numCases = param;
}
var input;
if (bigFile) {
input = bigFileContent;
} else {
input = $('#input').val();
}
worker.postMessage([input, blobURL, numCases]);
}
function stopProgram() {
if (worker != null) {
worker.terminate();
worker = null;
}
}
var editor;
// Document ready
window.onload = function() {
$('#output').focus(function() {
window.setTimeout(function() {
var output = document.getElementById('output');
output.select();
}, 0);
});
var dropbox = document.getElementById("inputDiv");
dropbox.addEventListener("dragenter", dragenter, false);
dropbox.addEventListener("dragover", dragover, false);
dropbox.addEventListener("drop", drop, false);
var codeNode = document.getElementById("code");
codeNode.appendChild(document.createTextNode(localStorage["code"]));
ace.config.set('basePath', 'https://pagecdn.io/lib/ace/1.4.13');
editor = ace.edit("code");
editor.setTheme("ace/theme/solarized_light");
editor.getSession().setMode("ace/mode/javascript");
editor.getSession().setTabSize(2);
editor.getSession().setUseSoftTabs(true);
// Restore data
$('#input').val(localStorage["input"]);
};
window.onbeforeunload = function (e) {
var e = e || window.event;
// Store data
localStorage["input"] = $('#input').val();
localStorage["code"] = editor.getSession().getValue();
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Are you sure you want to move from this page?';
}
// For Safari
return 'Are you sure you want to move from this page?';
};
</script>
</body>
</html>