-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
203 lines (167 loc) · 5.08 KB
/
main.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
// Decorate console.log function to also print it in in the on-screen console.
let _console = console.log
console.log = function(somethingToLog) {
let resultTextArea = document.getElementById("executionresult")
resultTextArea.value = resultTextArea.value + somethingToLog + "\n"
_console(somethingToLog)
}
function clearConsole() {
let resultTextArea = document.getElementById("executionresult")
resultTextArea.value = ""
}
function printExecutionHeaderOnConsole() {
console.log("Starting execution (" + currentFormattedTime() + ")...")
}
function currentFormattedTime() {
return new Date().toLocaleTimeString(
'en-US', {
hour12: false,
hour: "numeric",
minute: "numeric",
second: "numeric"}
)
}
function disableAutoClose() {
editor.setBehavioursEnabled(false)
}
function logWarningEditorLibraryNotLoaded() {
console.log("\nERROR:\nThe Editor library is not loaded yet. Please try again in a few seconds.")
}
function isRubyLibraryLoaded() {
return typeof Opal !== 'undefined'
}
function isEditorLoaded() {
return (typeof editor !== 'undefined')
}
function isPythonLibraryLoaded() {
return typeof brython !== 'undefined'
}
function autoCompleteChanged(el) {
if (isEditorLoaded() === false) {
logWarningEditorLibraryNotLoaded()
el.checked = false
return
}
editor.setOptions({
enableBasicAutocompletion: el.checked
})
}
function autoPairingChanged(el) {
if (isEditorLoaded() === false) {
logWarningEditorLibraryNotLoaded()
el.checked = false
return
}
editor.setBehavioursEnabled(el.checked)
}
function languageSelectChanged(el) {
if (isEditorLoaded() === false) {
logWarningEditorLibraryNotLoaded()
el.options.selectedIndex = 0
return
}
let selectedLanguage = el.value
switch(selectedLanguage) {
case "javascript":
editor.getSession().setMode("ace/mode/javascript")
disableAceEditorMissingSemiColonWarningMessage()
break
case "ruby":
editor.getSession().setMode("ace/mode/ruby")
break
case "python":
editor.getSession().setMode("ace/mode/python")
break
default:
console.log("\nERROR:\nLanguage not supported: " + selectedLanguage)
}
}
function themeSelectChanged(el) {
if (isEditorLoaded() === false) {
logWarningEditorLibraryNotLoaded()
el.options.selectedIndex = 0
return
}
let selectedTheme = el.value
switch(selectedTheme) {
case "bright":
editor.setTheme("ace/theme/tomorrow")
break
case "dark":
editor.setTheme("ace/theme/twilight")
break
default:
console.log("\nERROR:\nTheme not supported: " + selectedTheme)
}
}
function getSelectedLanguage() {
return document.getElementById("selectLanguage").value
}
function disableAceEditorMissingSemiColonWarningMessage() {
editor.session.$worker.call("changeOptions", [{asi: true}])
}
function executeCode() {
clearConsole()
printExecutionHeaderOnConsole()
if (isEditorLoaded() === false) {
logWarningEditorLibraryNotLoaded()
return
}
let codeToEval = editor.getValue()
let selectedLanguage = getSelectedLanguage()
try {
let result
switch(selectedLanguage) {
case "javascript":
result = executeJavaScriptCode(codeToEval)
break
case "python":
result = executePythonCode(codeToEval)
break
case "ruby":
result = executeRubyCode(codeToEval)
break
default:
console.log("\nERROR:\nLanguage not supported: " + selectedLanguage)
}
if (result != null) {
console.log(result)
}
console.log("Execution finished successfully.")
} catch(exception) {
console.log("\nERROR:\n" + exception.message + "\n")
}
}
function executeRubyCode(codeToEval) {
if (isRubyLibraryLoaded() === false) {
console.log("\nERROR:\nThe Ruby library is not loaded yet. Please try again in a few seconds.")
return
}
let transpiledCodeFromRubyToJS = Opal.compile(codeToEval)
return eval(transpiledCodeFromRubyToJS)
}
function executePythonCode(codeToEval) {
if (isPythonLibraryLoaded() === false) {
console.log("\nERROR:\nThe Python library is not loaded yet. Please try again in a few seconds.")
return
}
return brython.$eval(codeToEval)
}
function executeJavaScriptCode(codeToEval) {
return eval(codeToEval)
}
window.onload = function() {
disableAceEditorMissingSemiColonWarningMessage()
disableAutoClose()
}
// Adding keyboard shortcut for "Run" button.
document.onkeydown = function(event) {
// For IEs window event-object.
event = event || window.event
if (pressedAltR(event)) {
executeCode()
}
}
function pressedAltR(event) {
return event.altKey && event.key === 'r'
}