-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
505 lines (451 loc) · 14.6 KB
/
app.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
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
"use strict";
/* BASICODE in the Browser demonstration
* (c) 2017 Rob Hagemans
* Released under the Expat MIT licence
*/
var prefix = "BASICODE";
var app_id = "basicode-script";
// settings
var settings_prefix = ["BASICODE", app_id, "settings"].join(":");
function retrieveSettings() {
var settings = JSON.parse(localStorage.getItem(settings_prefix));
if (settings) {
var keys = Object.keys(settings);
for (var k in keys) {
var e = document.getElementById(keys[k]);
if (e) e.value = settings[keys[k]];
}
}
updateSettings();
}
function updateSettings() {
var app = apps[app_id];
var settings = document.getElementById(app_id).dataset;
settings["columns"] = document.getElementById("columns").value;
settings["rows"] = document.getElementById("rows").value;
settings["speed"] = document.getElementById("speed").value;
settings["font"] = document.getElementById("font").value;
for (var i=0; i <=7; ++i) {
settings["color-" + i] = document.getElementById("color-" + i).value;
}
localStorage.setItem(settings_prefix, JSON.stringify(settings));
document.getElementById("showspeed").value = document.getElementById("speed").value;
app.reset();
app.run();
}
function drawSettings() {
var selector = document.getElementById("font");
var options = Array.prototype.slice.call(selector.children);
var colours = { 0 : "black", 7: "white" };
for (var option in options) {
var fontcanvas = document.createElement("canvas");
var display = new Display(
fontcanvas, 11, 1,
options[option].value, colours);
display.write(options[option].value);
// creating an image seems necessary to make the background data URL work
var img = document.createElement("img");
img.src = fontcanvas.toDataURL();
// set image as background
options[option].style.backgroundImage = "url(" + img.src + ")";
}
}
var presets = {
"cga": { "columns": 40, "rows": 25, "font": "cga",
"color-0": "#000000", "color-7": "#ffffff",
},
"bbc": { "columns": 40, "rows": 32, "font": "bbc",
"color-0": "#000000", "color-7": "#ffffff",
},
"c64": { "columns": 40, "rows": 25, "font": "c64",
"color-0": "#40318d", "color-7": "#7869c4",
},
"spectrum": { "columns": 32, "rows": 24, "font": "spectrum",
"color-0": "#aaaaaa", "color-7": "#000000",
},
"msx": { "columns": 40, "rows": 25, "font": "msx",
"color-0": "#5955e0", "color-7": "#ffffff",
},
"cpc": { "columns": 40, "rows": 25, "font": "cpc",
"color-0": "#0000ff", "color-7": "#ffff00",
},
"pcw": { "columns": 90, "rows": 32, "font": "pcw",
"color-0": "#000000", "color-7": "#ffffff",
},
"coco": { "columns": 32, "rows": 16, "font": "coco",
"color-0": "#1bcb01", "color-7": "#000000",
},
};
function loadPreset(preset) {
var keys = Object.keys(presets[preset]);
for (var k in keys) {
document.getElementById(keys[k]).value = presets[preset][keys[k]];
}
updateSettings();
}
// listing
function setupListing() {
var app = apps[app_id];
var listing = document.getElementById("listing0");
// reload code if listing changes
listing.onblur = function() {
if (app.program && listing.value === app.program.code) {
return;
}
app.load(listing.value);
};
}
function onProgramLoad(program) {
var listing = document.getElementById("listing0");
var info = document.getElementById("info0");
if (program === null) {
listing.value = "";
info.innerHTML = "";
}
else {
listing.value = program.code;
info.innerHTML = "<h2>" + program.title + "</h2>\n\n" +program.description;
}
}
// printer
function printOutput()
{
var app = apps[app_id];
app.printOutput();
}
function clearOutput()
{
var app = apps[app_id];
app.clearOutput();
}
// storage
// this is where we keep our blobs
var blobbery = {};
var mime_type = "text/plain";
function buildList(parent, drives)
{
var app = apps[app_id];
function drop(e) {
// this === e.currentTarget
var id = e.currentTarget.dataset.drive;
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
var reader = new FileReader();
reader.onload = function() {
app.store(id, files[0].name, reader.result);
};
reader.readAsText(files[0]);
}
function playOnClick(e) {
e.stopPropagation();
e.preventDefault();
var code = localStorage.getItem(e.currentTarget.dataset.key);
app.load(code);
}
function deleteOnClick(e) {
e.stopPropagation();
e.preventDefault();
app.delete(e.target.dataset.drive, e.target.dataset.delete_target);
}
for (var flop=0; flop < drives.length; ++flop) {
var floppy_id = drives[flop];
var topli = document.createElement("li");
topli.innerHTML = " 🖬 " + floppy_id; // 💾
topli.dataset.drive = floppy_id;
// drop files to put in storage
topli.addEventListener("drop", drop, false);
var list = document.createElement("ol");
parent.appendChild( document.createElement("ul")).appendChild(topli).appendChild(list);
for (var i=0; i < localStorage.length; ++i) {
var key = localStorage.key(i);
var key_list = key.split(":");
if (key_list[0] !== prefix) continue;
if (key_list[1] !== "" + floppy_id) continue;
// create content blob from local storage, if necessary
if (!(key in blobbery)) {
blobbery[key] = new Blob([localStorage.getItem(key)], {type: mime_type});
}
// create download link to file
var a = document.createElement("a");
a.innerHTML = "▾";
a.className = "hidden download";
a.href = window.URL.createObjectURL(blobbery[key]);
a.download = key_list[2];
var play = document.createElement("a");
play.className = "run";
play.href = "run("+key_list[2]+")";
play.innerHTML = '<span class="hidden">▸</span> '+key_list[2];
play.dataset.key = key;
play.onclick = playOnClick;
var x = document.createElement("a");
x.innerHTML = "❌";
x.className = "hidden delete";
x.href = "delete("+key_list[2]+")";
x.dataset.delete_target = key_list[2];
x.dataset.drive = floppy_id;
x.onclick = deleteOnClick;
var li = list.appendChild(document.createElement("li"));
li.appendChild(x);
li.appendChild(a);
li.appendChild(play);
}
}
}
function onFileStore()
{
var element = document.getElementById("flop1");
var drives = ["tape", "floppy"];
if (!element) return;
while (element.firstChild) {
element.removeChild(element.firstChild);
}
buildList(element, drives);
}
function loadFile(element) {
var app = apps[app_id];
var url = element.href;
if (url !== undefined && url !== null && url) {
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState === 4 && request.status === 200) {
var code = request.responseText;
app.stop();
app.load(code);
}
};
request.send();
}
return false;
}
function onProgramRun() {
var button = document.getElementById("button0");
button.onclick = stop;
button.innerHTML = "■";
}
function onProgramEnd() {
var button = document.getElementById("button0");
button.onclick = run;
button.innerHTML = "▶";
}
function onPrint(text) {
document.getElementById("printer0").innerText = text;
}
// keyboard
var shift = false;
function setShift(on) {
if (shift !== on) {
// toggle shift highlights
var shiftkeys = document.getElementsByClassName("shiftkey");
for (i=0; i < shiftkeys.length; ++i) {
shiftkeys[i].classList.toggle("pressed");
}
}
shift = on;
var visible;
var invisible;
if (shift) {
visible = document.getElementsByClassName("shift");
invisible = document.getElementsByClassName("unshift");
}
else {
visible = document.getElementsByClassName("unshift");
invisible = document.getElementsByClassName("shift");
}
var i;
for (i=0; i < visible.length; ++i) {
visible[i].style["display"] = "inline";
}
for (i=0; i < invisible.length; ++i) {
invisible[i].style["display"] = "none";
}
}
var caps = false;
function setCaps(on) {
var i;
if (caps !== on) {
// toggle caps highlight
var capskeys = document.getElementsByClassName("capskey");
for (i=0; i < capskeys.length; ++i) {
capskeys[i].classList.toggle("pressed");
}
// swap shift and unshift labels on letter keys
var letters = document.getElementsByClassName("letter");
for (i=0; i < letters.length; ++i) {
if (letters[i].classList.contains("shift")) {
letters[i].classList.remove("shift");
letters[i].classList.add("unshift");
}
else if (letters[i].classList.contains("unshift")) {
letters[i].classList.remove("unshift");
letters[i].classList.add("shift");
}
}
setShift(shift);
}
caps = on;
}
function pressKey(element) {
var keys = {
"Del": 127,
"Tab": 9,
"Enter": 13,
"Esc": 27,
"Brk": -255,
"F1": -1,
"F2": -2,
"F3": -3,
"F4": -4,
"F5": -5,
"F6": -6,
"F7": -7,
"F8": -8,
"F9": -9,
"F10": -10,
"F11": -11,
"F12": -12,
"\u2190": 28,
"\u2191": 31,
"\u2192": 29,
"\u2193": 30,
"": 32,
};
var key = element.innerText;
var app = apps[app_id];
if (key === "Shift"){
setShift(!shift);
}
else if (key === "Caps"){
setCaps(!caps);
}
else if (key in keys) {
app.pressKey(keys[key]);
setShift(false);
}
else {
app.pressKey(key.charCodeAt(0));
setShift(false);
}
}
///////////////////////////////////////////////////////////////////////////
// event handlers
function setupHandlers()
{
var app = apps[app_id];
// load files on drag & drop
function nop(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
var reader = new FileReader();
reader.onload = function() {
app.load(reader.result);
};
reader.readAsText(files[0]);
}
var element = app.canvas;
element.addEventListener("dragenter", nop);
element.addEventListener("dragover", nop);
element.addEventListener("drop", drop);
var listing = document.getElementById("listing0");
listing.addEventListener("dragenter", nop);
listing.addEventListener("dragover", nop);
listing.addEventListener("drop", drop);
// run file on click
element.addEventListener("click", function click(e) {
if (!app.running) app.run();
});
var storage = document.getElementById("flop1");
storage.addEventListener("dragenter", nop);
storage.addEventListener("dragover", nop);
storage.addEventListener("drop", nop);
// application-defined events
app.on_file_store = onFileStore;
app.on_program_load = onProgramLoad;
app.on_program_run = onProgramRun;
app.on_program_end = onProgramEnd;
app.on_print = onPrint;
}
function setup() {
var app = apps[app_id];
drawSettings();
retrieveSettings();
setupListing();
setupHandlers();
onFileStore();
onProgramLoad(app.program);
// load demo program only if nothing stored
if (!app.program) {
loadFile({"href": "demo.bc3"});
}
setupPrograms();
run();
}
function run() {
var app = apps[app_id];
app.run();
}
function stop() {
var app = apps[app_id];
app.stop();
}
///////////////////////////////////////////////////////////////////////////////
// setup
function buildCollection(parent, collection)
{
var drives = Object.keys(collection).sort();
function playOnClick(e) {
e.stopPropagation();
e.preventDefault();
return loadFile(this);
}
for (var flop=0; flop < drives.length; ++flop) {
var floppy_id = drives[flop];
var files = Object.keys(collection[floppy_id]).sort();
if (!files.length) continue;
var topli = document.createElement("li");
topli.innerHTML = " ★ " + floppy_id.split("_").join(" ").split(".").join("");
var list = document.createElement("ol");
parent.appendChild( document.createElement("ul")).appendChild(topli).appendChild(list);
for (var i=0; i < files.length; ++i) {
var name = files[i].split("/").pop().split(".")[0].split("_").join(" ");
var title = collection[floppy_id][files[i]];
// create download link to file
var a = document.createElement("a");
a.innerHTML = "▾";
a.className = "hidden download";
a.href = files[i];
var play = document.createElement("a");
play.className = "run";
play.href = files[i];
play.innerHTML = '<span class="hidden">▸</span> ' + name + ' <span class="title">' + title+ "</span> ";
play.onclick = playOnClick;
var li = list.appendChild(document.createElement("li"));
li.appendChild(a);
li.appendChild(play);
}
}
}
function setupPrograms()
{
var parent = document.getElementById("program-list");
var url = "programs.json";
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState === 4 && request.status === 200) {
var collection = JSON.parse(request.responseText);
buildCollection(parent, collection);
}
};
request.send();
}
// kludge to use he #targets while keeping a top margin
window.addEventListener("hashchange", function () {
window.scrollTo(0, 0);
});