Skip to content

Commit 68e1d2f

Browse files
committed
bam
1 parent 247bb29 commit 68e1d2f

4 files changed

Lines changed: 296 additions & 27 deletions

File tree

‎v3/csc108h.html‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@
5959
<script type="text/javascript" src="js/csc108h.js"></script>
6060
<link rel="stylesheet" href="css/opt-frontend.css"/>
6161

62-
<!-- include this AFTER opt-frontend.js so that configuration
63-
global variables are already defined at load time -->
64-
<script type="text/javascript" src="js/togetherjs/togetherjs-min.js"></script>
65-
6662
</head>
6763

6864
<body>

‎v3/js/opt-frontend-common.js‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ TogetherJSConfig_ignoreForms = ['.togetherjsIgnore']; // do NOT sync these eleme
358358

359359

360360
function requestSync() {
361-
if (TogetherJS.running) {
361+
if (typeof TogetherJS !== 'undefined' && TogetherJS.running) {
362362
togetherjsSyncRequested = true;
363363
TogetherJS.send({type: "requestSync"});
364364
}
@@ -387,6 +387,10 @@ function syncAppState(appState) {
387387

388388
// get OPT ready for integration with TogetherJS
389389
function initTogetherJS() {
390+
if (typeof TogetherJS === "undefined") {
391+
return;
392+
}
393+
390394
if (togetherjsInUrl) {
391395
$("#ssDiv").hide(); // hide ASAP!
392396
$("#togetherjsStatus").html("Please wait ... loading shared session");
@@ -828,7 +832,7 @@ function genericOptFrontendReady() {
828832
updateAppDisplay(newMode);
829833
}
830834

831-
if (TogetherJS.running && !isExecutingCode) {
835+
if (typeof TogetherJS !== 'undefined' && TogetherJS.running && !isExecutingCode) {
832836
TogetherJS.send({type: "hashchange",
833837
appMode: appMode,
834838
codeInputScrollTop: pyInputGetScrollTop(),
@@ -859,7 +863,7 @@ function genericOptFrontendReady() {
859863
pyInputCodeMirror.on("change", function(cm, change) {
860864
// only trigger when the user explicitly typed something
861865
if (change.origin != 'setValue') {
862-
if (TogetherJS.running) {
866+
if (typeof TogetherJS !== 'undefined' && TogetherJS.running) {
863867
TogetherJS.send({type: "codemirror-edit"});
864868
}
865869
}
@@ -869,7 +873,7 @@ function genericOptFrontendReady() {
869873
pyInputAceEditor.getSession().on("change", function(e) {
870874
// unfortunately, Ace doesn't detect whether a change was caused
871875
// by a setValue call
872-
if (TogetherJS.running) {
876+
if (typeof TogetherJS !== 'undefined' && TogetherJS.running) {
873877
TogetherJS.send({type: "codemirror-edit"});
874878
}
875879
});
@@ -878,7 +882,7 @@ function genericOptFrontendReady() {
878882

879883
if (useCodeMirror) {
880884
$(codeMirrorScroller).scroll(function(e) {
881-
if (TogetherJS.running) {
885+
if (typeof TogetherJS !== 'undefined' && TogetherJS.running) {
882886
var elt = $(this);
883887
$.doTimeout('codeInputScroll', 100, function() { // debounce
884888
// note that this will send a signal back and forth both ways
@@ -895,7 +899,7 @@ function genericOptFrontendReady() {
895899
// don't sync for Ace since I can't get it working properly yet
896900
/*
897901
pyInputAceEditor.getSession().on('changeScrollTop', function() {
898-
if (TogetherJS.running) {
902+
if (typeof TogetherJS !== 'undefined' && TogetherJS.running) {
899903
$.doTimeout('codeInputScroll', 100, function() { // debounce
900904
// note that this will send a signal back and forth both ways
901905
// (there's no easy way to prevent this), but it shouldn't keep
@@ -1053,7 +1057,7 @@ function genericOptFrontendReady() {
10531057
if (myVisualizer) {
10541058
var uh = myVisualizer.updateHistory;
10551059
// don't submit identical entries repeatedly since that's redundant
1056-
if (uh.length != lastSubmittedUpdateHistoryLength) {
1060+
if (uh && (uh.length != lastSubmittedUpdateHistoryLength)) {
10571061
lastSubmittedUpdateHistoryLength = uh.length;
10581062
submitUpdateHistory('periodic');
10591063
}
@@ -1697,27 +1701,22 @@ function submitUpdateHistory(why) {
16971701
// entry except for the first be a delta from the FIRST entry.
16981702
var uh = myVisualizer.updateHistory;
16991703
var encodedUh = [];
1700-
encodedUh.push(uh[0]);
1704+
if (uh) {
1705+
encodedUh.push(uh[0]);
17011706

1702-
var firstTs = uh[0][1];
1703-
for (var i = 1; i < uh.length; i++) {
1704-
var e = uh[i];
1705-
encodedUh.push([e[0], e[1] - firstTs]);
1706-
}
1707+
var firstTs = uh[0][1];
1708+
for (var i = 1; i < uh.length; i++) {
1709+
var e = uh[i];
1710+
encodedUh.push([e[0], e[1] - firstTs]);
1711+
}
17071712

1708-
// finally push a final entry with the current timestamp delta
1709-
var curTs = new Date().getTime();
1710-
encodedUh.push([myVisualizer.curInstr, curTs - firstTs]);
1713+
// finally push a final entry with the current timestamp delta
1714+
var curTs = new Date().getTime();
1715+
encodedUh.push([myVisualizer.curInstr, curTs - firstTs]);
1716+
}
17111717

1712-
var uhJSON = JSON.stringify(uh);
17131718
var encodedUhJSON = JSON.stringify(encodedUh);
17141719

1715-
//console.log(uhJSON);
1716-
//console.log(encodedUhJSON);
1717-
1718-
//console.log(uhJSON.length);
1719-
//console.log(encodedUhJSON.length);
1720-
17211720
var myArgs = {session_uuid: sessionUUID,
17221721
updateHistoryJSON: encodedUhJSON};
17231722
if (why) {

‎v3/js/opt-live.js‎

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
3+
Online Python Tutor
4+
https://github.com/pgbovine/OnlinePythonTutor/
5+
6+
Copyright (C) Philip J. Guo (philip@pgbovine.net)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a
9+
copy of this software and associated documentation files (the
10+
"Software"), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included
17+
in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
*/
28+
29+
// OPT live programming prototype started on 2016-05-30
30+
31+
/* TODOs:
32+
33+
- display run-time exceptions properly -- right now display-mode code is
34+
hidden, so no exceptions displayed either
35+
36+
- when there's a syntax error, the ace code editor snaps cursor back to
37+
beginning of column at the marked line, which is annoying
38+
39+
*/
40+
41+
// Pre-reqs:
42+
// - pytutor.js
43+
// - jquery.ba-bbq.min.js
44+
// - jquery.ba-dotimeout.min.js // for event debouncing: http://benalman.com/code/projects/jquery-dotimeout/examples/debouncing/
45+
// - opt-frontend-common.js
46+
// should all be imported BEFORE this file
47+
48+
var originFrontendJsFile = 'opt-live.js';
49+
50+
function optliveFinishSuccessfulExecution() {
51+
assert(myVisualizer);
52+
$("#pyOutputPane").show();
53+
54+
doneExecutingCode();
55+
56+
// do this AFTER making #pyOutputPane visible, or else
57+
// jsPlumb connectors won't render properly
58+
myVisualizer.updateOutput();
59+
}
60+
61+
// override the one in opt-frontend-common.js
62+
function initAceEditor(height) {
63+
pyInputAceEditor = ace.edit('codeInputPane');
64+
65+
var s = pyInputAceEditor.getSession();
66+
s.setMode("ace/mode/python");
67+
// tab -> 4 spaces
68+
s.setTabSize(4);
69+
s.setUseSoftTabs(true);
70+
71+
// disable extraneous indicators:
72+
s.setFoldStyle('manual'); // no code folding indicators
73+
pyInputAceEditor.setHighlightActiveLine(false);
74+
pyInputAceEditor.setShowPrintMargin(false);
75+
pyInputAceEditor.setBehavioursEnabled(false);
76+
pyInputAceEditor.$blockScrolling = Infinity; // kludgy to shut up weird warnings
77+
78+
// auto-grow height as fit
79+
pyInputAceEditor.setOptions({minLines: 18, maxLines: 1000});
80+
81+
$('#codeInputPane').css('width', '500px');
82+
$('#codeInputPane').css('height', height + 'px'); // VERY IMPORTANT so that it works on I.E., ugh!
83+
84+
pyInputAceEditor.on('change', function(e) {
85+
$.doTimeout('pyInputAceEditorChange', 500, function() {
86+
executeCodeFromScratch();
87+
}); // debounce
88+
clearFrontendError();
89+
s.clearAnnotations();
90+
});
91+
92+
// don't do real-time syntax checks:
93+
// https://github.com/ajaxorg/ace/wiki/Syntax-validation
94+
s.setOption("useWorker", false);
95+
pyInputAceEditor.focus();
96+
}
97+
98+
// based on executeCodeAndCreateViz
99+
function optliveExecuteCodeAndCreateViz(codeToExec,
100+
backendScript, backendOptionsObj,
101+
frontendOptionsObj,
102+
outputDiv,
103+
handleSuccessFunc, handleUncaughtExceptionFunc) {
104+
105+
function execCallback(dataFromBackend) {
106+
var trace = dataFromBackend.trace;
107+
108+
// don't enter visualize mode if there are killer errors:
109+
if (!trace ||
110+
(trace.length == 0) ||
111+
(trace[trace.length - 1].event == 'uncaught_exception')) {
112+
113+
handleUncaughtExceptionFunc(trace);
114+
115+
if (trace.length == 1) {
116+
setFronendError([trace[0].exception_msg]);
117+
}
118+
else if (trace.length > 0 && trace[trace.length - 1].exception_msg) {
119+
setFronendError([trace[trace.length - 1].exception_msg]);
120+
}
121+
else {
122+
setFronendError(["Unknown error. Reload the page and try again. Or report a bug to",
123+
"philip@pgbovine.net by clicking on the 'Generate permanent link'",
124+
"button at the bottom and including a URL in your email."]);
125+
}
126+
}
127+
else {
128+
myVisualizer = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
129+
handleSuccessFunc();
130+
}
131+
132+
doneExecutingCode(); // rain or shine, we're done executing!
133+
// run this at the VERY END after all the dust has settled
134+
}
135+
136+
clearFrontendError();
137+
startExecutingCode();
138+
139+
if (backendScript === python2_backend_script) {
140+
frontendOptionsObj.lang = 'py2';
141+
} else if (backendScript === python3_backend_script) {
142+
frontendOptionsObj.lang = 'py3';
143+
} else {
144+
assert(false);
145+
}
146+
147+
$.get(backendScript,
148+
{user_script : codeToExec,
149+
raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '',
150+
options_json: JSON.stringify(backendOptionsObj),
151+
user_uuid: supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined,
152+
session_uuid: sessionUUID},
153+
execCallback, "json");
154+
}
155+
156+
function executeCode(forceStartingInstr, forceRawInputLst) {
157+
if (forceRawInputLst !== undefined) {
158+
rawInputLst = forceRawInputLst; // UGLY global across modules, FIXME
159+
}
160+
161+
backend_script = python2_backend_script;
162+
163+
var backendOptionsObj = {cumulative_mode: false,
164+
heap_primitives: false,
165+
show_only_outputs: false,
166+
py_crazy_mode: false,
167+
origin: originFrontendJsFile};
168+
169+
var startingInstruction = forceStartingInstr ? forceStartingInstr : 0;
170+
var frontendOptionsObj = {startingInstruction: startingInstruction,
171+
executeCodeWithRawInputFunc: executeCodeWithRawInput,
172+
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
173+
hideCode: true,
174+
jumpToEnd: true,
175+
}
176+
177+
optliveExecuteCodeAndCreateViz(pyInputGetValue(),
178+
backend_script, backendOptionsObj,
179+
frontendOptionsObj,
180+
'pyOutputPane',
181+
optliveFinishSuccessfulExecution,
182+
handleUncaughtExceptionFunc);
183+
}
184+
185+
$(document).ready(function() {
186+
genericOptFrontendReady(); // initialize at the very end
187+
$("#pyOutputPane").show();
188+
});

‎v3/opt-live.html‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
4+
<!-- OPT live programming prototype started on 2016-05-30 -->
5+
6+
<!--
7+
8+
Online Python Tutor
9+
https://github.com/pgbovine/OnlinePythonTutor/
10+
11+
Copyright (C) Philip J. Guo (philip@pgbovine.net)
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a
14+
copy of this software and associated documentation files (the
15+
"Software"), to deal in the Software without restriction, including
16+
without limitation the rights to use, copy, modify, merge, publish,
17+
distribute, sublicense, and/or sell copies of the Software, and to
18+
permit persons to whom the Software is furnished to do so, subject to
19+
the following conditions:
20+
21+
The above copyright notice and this permission notice shall be included
22+
in all copies or substantial portions of the Software.
23+
24+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31+
32+
-->
33+
34+
<head>
35+
<title>OPT Live Programming Prototype</title>
36+
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
37+
38+
<!-- requirements for pytutor.js -->
39+
<script type="text/javascript" src="js/d3.v2.min.js"></script>
40+
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
41+
<script type="text/javascript" src="js/jquery.ba-bbq.min.js"></script> <!-- for handling back button and URL hashes -->
42+
<script type="text/javascript" src="js/jquery.ba-dotimeout.min.js"></script> <!-- for event debouncing -->
43+
<script type="text/javascript" src="js/jquery.jsPlumb-1.3.10-all-min.js "></script> <!-- for rendering SVG connectors
44+
DO NOT UPGRADE ABOVE 1.3.10 OR ELSE BREAKAGE WILL OCCUR -->
45+
<script type="text/javascript" src="js/diff_match_patch.js"></script>
46+
47+
<script type="text/javascript" src="js/jquery-ui-1.8.24.custom.min.js"></script> <!-- for sliders and other UI elements -->
48+
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.24.custom.css" rel="stylesheet" />
49+
50+
<script type="text/javascript" src="js/pytutor.js"></script>
51+
<link rel="stylesheet" href="css/pytutor.css"/>
52+
53+
<script type="text/javascript" src="js/ace/src-min-noconflict/ace.js" charset="utf-8"></script>
54+
<script type="text/javascript" src="js/opt-frontend-common.js"></script>
55+
<script type="text/javascript" src="js/opt-live.js"></script>
56+
<link rel="stylesheet" href="css/opt-frontend.css"/>
57+
58+
</head>
59+
<body>
60+
61+
<table>
62+
<tr>
63+
<td valign="top">
64+
<div id="pyInputPane">
65+
66+
<div id="codeInputWarnings">Write Python code here:</div>
67+
68+
<div id="codeInputPane"></div>
69+
<div id="frontendErrorOutput"></div>
70+
<div id="surveyPane"></div>
71+
72+
<p>
73+
<button id="executeBtn" class="bigBtn" type="button">Visualize Execution</button>
74+
</p>
75+
76+
</div>
77+
</td>
78+
<td valign="top">
79+
<div id="pyOutputPane">
80+
</div>
81+
</td>
82+
</tr>
83+
</table>
84+
85+
</body>
86+
</html>

0 commit comments

Comments
 (0)