Repository navigation
Expand file tree
/
Copy pathGlowScript.html
More file actions
284 lines (254 loc) · 9.82 KB
/
Copy pathGlowScript.html
File metadata and controls
284 lines (254 loc) · 9.82 KB
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
<!DOCTYPE html>
<html>
<head>
<!-- Using GlowScript locally, based on the work of Vesa Lappalainen vesal@jyu.fi -->
<title>GlowScript Offline</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
.api {
height: 5000px;
border: 1px solid #ddd;
border-radius: 4px;
}
.split p {
padding: 20px;
}
.split {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
overflow-y: auto;
overflow-x: hidden;
}
.gutter {
background-color: #ccc;
background-repeat: no-repeat;
background-position: 50%;
}
.gutter.gutter-horizontal {
cursor: ew-resize;
}
.split.split-horizontal, .gutter.gutter-horizontal {
height: 100%;
float: left;
}
</style>
</head>
<body class="GlowScriptBody">
<link type="text/css" href="glowscript_libraries/jquery-ui.custom.css" rel="stylesheet" />
<link type="text/css" href="glowscript_libraries/ide.css" rel="stylesheet" />
<script type="text/javascript" src="glowscript_libraries/Roboto_Medium_ttf_sans.js"></script>
<script type="text/javascript" src="glowscript_libraries/NimbusRomNo9L_Med_otf_serif.js"></script>
<script type="text/javascript" src="glowscript_libraries/jquery.min.js"></script>
<script type="text/javascript" src="glowscript_libraries/jquery-ui.custom.min.js"></script>
<script type="text/javascript" src="glowscript_libraries/editor.js"></script>
<script type="text/javascript" src="glowscript_libraries/RSrun.2.7.min.js"></script>
<script type="text/javascript" src="glowscript_libraries/glow.2.7.min.js"></script>
<script type="text/javascript" src="glowscript_libraries/split.min.js"></script>
</body>
<script>
var gsversion = 2.7
var printpane = false
var glowscript_run = undefined
window.Jupyter_VPython = undefined
if (!navigator.onLine) window.Jupyter_VPython = 'glowscript_data/' // get textures when offline
function fontloading() { // trigger loading of fonts for 3D text
"use strict";
// override the fontloading url's in the GlowScript library
if (window.__font_sans === undefined) {
var fontrefsans = opentype_loadjs(Roboto_sans) // a sans serif font
window.__font_sans = fontrefsans // an opentype.js Font object
}
if (window.__font_serif === undefined) {
var fontrefserif = opentype_loadjs(Nimbus_serif) // a serif font
window.__font_serif = fontrefserif // an opentype.js Font object
}
}
fontloading() // get the sans and serif font files used by 3D text
// For an unknown reason, attempts to load fonts when disconnected from the internet doesn't work,
// even when a full path was specified in a fontloading function.
// localCompile is a modification of https://github.com/BruceSherwood/glowscript/blob/master/ide/ide.js
function localCompile(header, compReady, errordiv) {
errordiv.innerHTML = ""
var compiler_url
if (header.lang == 'vpython' || header.lang == 'rapydscript') {
compiler_url = "glowscript_libraries/RScompiler." + header.version + ".min.js"
} else compiler_url = "glowscript_libraries/compiler." + header.version + ".min.js"
window.glowscript_compile = undefined
$.ajax({
url: compiler_url,
dataType: "script",
cache: true,
crossDomain: true // use script tag rather than xhr
}).fail(function (xhr, err, exc) {
(xhr)
alert(err + " getting " + xhr.url + ": " + exc)
}).done(function () {
if (!window.glowscript_compile) {
alert("Failed to load compiler from " + compiler_url)
return
}
var embedScript
try {
embedScript = window.glowscript_compile(header.source, {lang: header.lang,
version: header.version.substr(0,3)})
} catch(err) {
errordiv.innerHTML = "<p>"+err+"</p>"
return
}
embedScript = ";(function() {" + embedScript + '\n'+
'glowscript_run = function() { main(__func); }\n' +
'})()'
embedScript = embedScript.replace("</", "<\\/") // escape anything that could be a close script tag... hopefully this sequence only occurs in strings!
compReady(embedScript)
})
}
function parseVersionHeader( source ) {
var sourceLines = source.split("\n")
var header = sourceLines[0]
// Remove a newline or similar character at the end of header:
if (header.charCodeAt(header.length-1) < 32)
header = header.substring(0,header.length-1)
var rest = source.substring( header.length+1 )
var ret = {
version: null,
lang: '', // 'vpython' (default) or 'rapydscript' or 'javascript' or a string that is neither (e.g. when editing header)
source: rest,
ok: false,
unpackaged: false,
isCurrent: false
}
header = header.split(" ")
if (header.length === undefined) return ret
if (header[0] == ' ') return ret
var elements = []
for (var i=0; i<header.length; i++) { // remove empty strings corresponding to spaces
if (header[i] != '') elements.push(header[i])
}
if (elements.length < 2 || elements.length > 3) return ret
if (elements[0] != 'GlowScript') return ret
ret.lang = 'javascript' // the default if no language is specified
if (elements.length == 3) {
ret.lang = elements[2].toLowerCase()
if (!(ret.lang == 'javascript' || ret.lang == 'rapydscript' || ret.lang == 'vpython')) return ret
}
var ver = elements[1]
if (ver != gsversion) alert('The version number, '+ver+', should be '+gsversion)
var okv = true
return {
version: ver,
lang: ret.lang,
source: rest,
}
}
var gsErrordiv
function runCode() {
var text = GSedit.getValue()
var end = text.indexOf('\n')
var i = text.slice(0,end).indexOf("GlowScript") // Look for "GlowScipt" in first line
if (i < 0 && text.indexOf('from vpython') === 0) text = "GlowScript "+gsversion+" VPython" + "\n" + text
var header = parseVersionHeader(text)
printpane = false
if (header.source.indexOf('print(') >= 0) { // if the program uses print() or GSprint(), expand 3rd pane
printpane = true
var end = header.source.indexOf('\n')
var insert
if (header.lang == 'vpython' || header.lang == 'rapydscript')
insert = "print_options(place=$('#printing'), width=300, height=500, clear=True)\n"
else insert = "print_options({place:$('#printing'), width:300, height:500, clear:true})\n"
header.source = header.source.slice(0,end+1) + insert + header.source.slice(end+1)
}
// Look for mention of MathJax in program and attempt to get it (need internet access; files too big to include in package)
if (header.source.indexOf('MathJax') >= 0) {
alert('Cannot currently use MathJax in GlowScript Offline.')
}
splitAdjust()
gsErrordiv = $("#gserrors")[0]
localCompile(header, ready, gsErrordiv)
}
function ready(sc) {
var w = $("#glows")
w[0].innerHTML = "" // Comment this and the next if get a solution for too many WebGL context
w[0].innerHTML = '<div id="glowscript" class="glowscript"></div>'
window.__context = { glowscript_container: $(".glowscript").removeAttr("id") }
// TODO: how to set an error handler???
window.__context.errorHandler = gsErrorHandler
runScript = new Function(sc)
runScript()
glowscript_run()
}
function gsErrorHandler(err) {
gsErrordiv.innerHTML = "<p>"+err+"</p>"
}
function getConsoleHeight() {
return 0
}
</script>
<button onclick="runCode()">Run</button>
<a href="VPythonDocs/index.html" target="_blank">Help</a>
<input type="file" id="read_local_file"/>
<script>
function readSingleFile(evt) {
var ok = true
if (GSedit.changed()) {
var s = "To continue without saving, click OK.\nTo save, click Cancel, then copy and paste into an application such as Notepad or TextEdit."
if (confirm(s)) ok = true
else ok = false
}
if (ok) {
var f, reader
f = evt.target.files[0]
if (f) {
reader = new FileReader()
reader.onload = function(e) {
var content = e.target.result
GSedit.setValue(content)
}
reader.readAsText(f)
}
}
}
document.getElementById('read_local_file').addEventListener('change', readSingleFile, false)
</script>
<div id="gserrors">
</div>
<div class='api'>
<div id='sourcetext' class="split split-horizontal"></div>
<div id='glows' class="split split-horizontal"></div>
<div id='printing' class="split split-horizontal"></div>
</div>
<script>
// Splits library: https://github.com/nathancahill/Split.js
var lastwindowwidth = window.innerWidth
var splits = Split(['#sourcetext', '#glows', "#printing"], {sizes: [50,50,0], onDrag:splitdrag})
// splits.getSizes() returns current percentage widths; splits.setSizes([w1,w2]) resets them
var source = 'from vpython import *\nbox()'
GSedit.init("#sourcetext", source, 0.5*lastwindowwidth, false) // not readonly
function splitdrag() {
var s = splits.getSizes() // returns [width1, width2, width3]
GSedit.setwidth(0.01*s[0]*window.innerWidth)
}
function splitAdjust() { // p is true if print pane shouold be open
var current = splits.getSizes()
var w1 = current[0]*lastwindowwidth/window.innerWidth
var w2 = current[1]
var w3 = current[2]
var rest = 100-w1
lastwindowwidth = window.innerWidth
if (printpane) splits.setSizes([w1,0.7*rest,0.3*rest])
else splits.setSizes([w1,rest,0])
GSresize(0.01*w1*window.innerWidth)
}
$(window).resize(function () {
splitAdjust()
})
window.onbeforeunload = undefined // execute window.onbeforeunload = Quit if GSedit.changed() is true
function Quit(e) { // Some browsers just say "Do you want to leave this site? Changes you made may not be saved."
var s = "To continue without saving, click OK.\nTo save, click Cancel, then copy and paste into an application such as Notepad or TextEdit."
e.returnValue = s
return s
}
</script>
</html>