-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.js
117 lines (111 loc) · 3.63 KB
/
Settings.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
import {
getExample
} from "./examples.js";
const LOCAL_STORAGE_PREFIX = "brainfuck_debugger_app__";
// let's still be functional even if storage is disabled
let fauxStorage = {};
export function putLocalStorage(key, value) {
try {
localStorage[LOCAL_STORAGE_PREFIX + key] = value;
} catch (e) {
fauxStorage[key] = value;
}
}
export function accessLocalStorage(key) {
let val;
try {
val = localStorage[LOCAL_STORAGE_PREFIX + key];
} catch (e) {
val = fauxStorage[key];
}
if (val === undefined) {
if (key === "source")
return getExample("hello-world");
}
return val;
}
function Option(name, default_value) {
this.name = name;
this.default_value = default_value;
if (accessLocalStorage(name) === undefined) {
this.value = default_value;
putLocalStorage(name, default_value);
} else {
let val = accessLocalStorage(name);
if (val === "true")
val = true;
if (val === "false")
val = false;
val = Number(val) || val;
this.value = val;
}
}
export class Settings {
constructor(update_callback) {
this.options = [
new Option("editor-keymap", "default"),
new Option("editor-highlighting", true),
new Option("editor-theme", "monokai"),
new Option("global-theme", "dagan"),
new Option("cell-width", 8),
new Option("step-delay", 1),
new Option("optimize", true),
new Option("line-numbers", true),
new Option("close-brackets", true),
new Option("match-brackets", true),
new Option("wrapping-allowed", true),
new Option("double-tape", false),
];
this.update_callback = update_callback;
this.options_map = {};
for (const option of this.options) {
this.options_map[option.name] = option;
}
this.loadToUI();
this.setUpEvents();
}
setUpEvents() {
const eventHandler = (option_name) => {
let value;
if (event.target.type === "checkbox") {
value = event.target.checked;
} else {
value = event.target.value;
}
if (value !== undefined) {
this.options_map[option_name].value = value;
putLocalStorage(option_name, value);
}
this.update_callback();
};
for (const option of this.options) {
const element = document.getElementById("opt-" + option.name);
const eh = eventHandler.bind(this, option.name);
element.addEventListener("input", eh);
}
}
loadToUI() {
for (const option of this.options) {
const element = document.getElementById("opt-" + option.name);
if (element.tagName === "INPUT") {
if (element.type === "number")
element.value = option.value;
else if (element.type === "checkbox") {
const val = !!(option.value);
if (typeof val !== "boolean")
throw ("Expected boolean value");
element.checked = val;
} else
throw ("Unknown type of input");
} else if (element.tagName === "SELECT") {
element.value = option.value;
}
}
}
get(option_name) {
if (this.options_map[option_name])
return this.options_map[option_name].value;
else
console.log("No such option: " + option_name);
}
}