-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
296 lines (250 loc) · 9.96 KB
/
index.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style/codemirror.css">
<link rel="stylesheet" type="text/css" href="style/solarized.css">
<link rel="stylesheet" type="text/css" href="style/skulptide.css">
<script src="javascript/jquery.min.js"></script>
<script src="javascript/codemirror.js"></script>
<script src="javascript/matchbrackets.js"></script>
<script src="javascript/python.js"></script>
<script src="javascript/skulpt.min.js"></script>
<script src="javascript/skulpt-stdlib.js"></script>
<script src="javascript/processing.min.js"></script>
<script src="javascript/pythonide.js"></script>
<script src="javascript/lz-string.js"></script>
<script src="javascript/FileSaver.min.js"></script>
</head>
<body>
<div class="maindiv">
<div class="IDE">
<div id="title">
<h3>Online Python development environment</h3>
<p> See <a href="http://esperanc.github.io/skulptIde/pages.html">Github site</a> for more info.</p>
</div>
<div id="menu">
<label class="button" onclick="newProgram()">New</label>
<label class="button" onclick="runProgram()">Run</label>
<label class="button" onclick="stopit()">Stop</label>
<label class="button" onclick="clearit()">Clear</label>
<label class="button" for="loadfile">Upload</label>
<input type="file" id="loadfile" name="files[]" style="visibility:hidden;display:none;" onchange="loadit(this)"></input>
<label class="button" onclick="saveit()">Download as</label>
<input id="savefilename" type="text" value="program.py"/>
<br/><br/>
</div>
</div>
<div id="centralarea">
<div id="canvas">
</div>
<div class="editor IDE">
<textarea id="code" cols="60" rows="20">
</textarea><br />
</div>
</div>
</div>
<div id="outputpanel">
<h4 class="IDE">Output</h4>
<pre id="output"></pre>
</div>
<div id="showurl" style="display:none;">
<label class="button" onclick="showUrl()">Show URL for this sketch</label><br/>
<a id="myurl" href="" title=""></a>
</div>
<script type="text/javascript">
// These control the behavior of the script
var parms = {
program : // Initial skulpt program to show in the editor
typeof(Storage) !== "undefined" && localStorage.getItem("skulptIdeProgram") ||
["from processing import *",
"",
"def draw():",
" line(0,0,100,100)",
"",
"run()"].join("\n"),
programName: typeof(Storage) !== "undefined" && localStorage.getItem("skulptIdeProgramName") ||
"saveprogram.py",
noTitle: false, // Whether to hide the title
noOutput: false, // Whether to hide the output panel
autoRun: false, // Whether to run the initial program after load
hideIde: false, // Whether to hide the IDE buttons if autoRun is true
showUrl: false, // Whether to show an interactive button for showing an url for this sketch
};
// Number of changes to the program after save
var changeCount = 0;
// Obtain the url parameter with name theParameter. Returns false
// if not specified.
function getParameter(theParameter) {
var params = window.location.search.substr(1).split('&');
for (var i = 0; i < params.length; i++) {
var p=params[i].split('=');
if (p[0] == theParameter) {
if (p.length>1)
return p[1];
else
return true;
}
}
return false;
}
// Saves the current state to the browser's store
function storeProgram () {
localStorage.setItem("skulptIdeProgram", getProgram());
localStorage.setItem("skulptIdeProgramName", document.getElementById("savefilename").value);
}
// Callback for "run"
function runProgram() {
storeProgram();
runit();
}
// Callback for "new"
function newProgram() {
if (changeCount!=0) {
var yes = confirm ("Are you sure you want to start a new sketch?");
if (!yes) return;
}
clearit();
setProgram("");
changeCount = 0;
document.getElementById("savefilename").value = "saveprogram.py";
}
// Callback for "load"
function loadit(input) {
var components = input.value.split("/");
if (components.length < 2) components = input.value.split("\\");
var filename = components [components.length-1];
var fileobj = input.files[0];
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
var text = e.target.result;
setProgram(text);
storeProgram();
// clear the input element so that a new load on the same file will work
input.value = "";
document.getElementById("savefilename").value = filename;
};
}) (fileobj);
// Read in the file as a data URL.
reader.readAsText(fileobj);
}
// Callback for "download as"
function saveit() {
var data = getProgram();
var filename = document.getElementById("savefilename").value;
var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
saveAs(blob, filename);
changeCount = 0;
}
// Setup
function setup () {
// Setup skulpt and editor
setupPythonIDE('code','output','canvas');
// Arrange for counting the total number of changes
editor.on('change', function(cm) {changeCount++;});
// Set the initial program
setProgram (parms.program);
// Reset the change Counter
changeCount = 0;
// Arrange to run it automatically if requested
if (parms.autoRun) {
setTimeout(function() { runit() }, 50)
}
}
// Loads program from a given url using XMLHttprequest (must be on the same domain)
function loadUrl (url) {
function reqListener () {
console.log(url+" loaded");
parms.program = this.responseText;
setup ();
}
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", url);
oReq.send();
}
// Includes an encoded url for this sketch at the bottom of the screen
function showUrl() {
var link = document.URL;
var i = link.indexOf("?");
if (i>=0) link = link.substr(0,i);
var pgm = getProgram();
var s1 = LZString.compressToEncodedURIComponent(pgm);
var s2 = encodeURIComponent(pgm);
if (s1.length<s2.length)
link = link+"?lzsrc="+s1;
else
link = link+"?source="+s2;
if (parms.autoRun) link = link+"&autorun";
if (parms.hideIde) link = link+"&hideide";
if (parms.noTitle) link = link+"¬itle";
if (parms.noOutput) link = link+"&nooutput";
$("#myurl").attr("href",link).text(link);
}
// Load the program name into the box
document.getElementById("savefilename").value = parms.programName;
// Whether to start the program immediately
parms.autoRun = getParameter("autorun") || getParameter("autoRun");
// Whether to hide the IDE
parms.hideIde = getParameter("hideide") || getParameter("hideIde") ||
getParameter("noide") || getParameter("noIde");
// Whether to hide the title
parms.noTitle = getParameter("notitle") || getParameter("noTitle");
// Whether to hide the output panel
parms.noOutput = getParameter("nooutput") || getParameter("noOutput");
// Whether to show the "showurl" button
parms.showUrl = getParameter("showurl") || getParameter("showUrl");
// Unhide the showurl div if requested
if (parms.showUrl) {
document.getElementById("showurl").style.display = "inline";
}
// Hide the title div if requested
if (parms.noTitle) {
document.getElementById("title").style.display = "none";
}
// Hide the outputpanel div if requested
if (parms.noOutput) {
document.getElementById("outputpanel").style.display = "none";
}
// See if there is a source program encoded in the url
var source = getParameter ("source");
if (source) {
parms.program = decodeURIComponent(source);
}
var lzsource = getParameter("lzsrc");
if (lzsource) {
parms.program = LZString.decompressFromEncodedURIComponent(lzsource);
}
// Whether to hide interface
if (parms.hideIde) {
var elements = document.getElementsByClassName('IDE');
for(var i=0; i<elements.length; i++) {
elements[i].style.display = "none";
}
}
// Load an external program if its url is passed as parameter 'program'
var programUrl = getParameter("program");
if (programUrl) {
// Load from file
programUrl = decodeURIComponent(programUrl);
var components = programUrl.split("/")
document.getElementById("savefilename").value = components[components.length-1];
loadUrl (programUrl);
}
else {
// Load the initial program
setup();
}
// Prevent silently navigating away from the IDE
window.onbeforeunload = function() {
if (changeCount==0) return null;
return "Have you saved your program?";
}
// Try to save the program to local storage before leaving
window.onunload = storeProgram;
</script>
</body>
</html>