Skip to content

Commit 7744580

Browse files
committed
added composingprograms fork
1 parent 9b2553e commit 7744580

3 files changed

Lines changed: 286 additions & 4 deletions

File tree

‎v3/composingprograms.html‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
<script type="text/javascript" src="js/pytutor.js"></script>
5151
<link rel="stylesheet" href="css/pytutor.css"/>
52-
<link rel="stylesheet" href="css/pytutor.cs61a.css"/> <!-- include AFTER pytutor.css -->
52+
<link rel="stylesheet" href="css/composingprograms.css"/> <!-- include AFTER pytutor.css -->
5353

5454

5555
<!-- requirements for opt-frontend.js -->
@@ -59,7 +59,8 @@
5959
<link type="text/css" href="css/codemirror.css" rel="stylesheet" />
6060
<script type="text/javascript" src="js/codemirror/python.js"></script>
6161

62-
<script type="text/javascript" src="js/opt-frontend-cs61a.js"></script>
62+
<script type="text/javascript" src="js/opt-frontend-common.js"></script>
63+
<script type="text/javascript" src="js/composingprograms.js"></script>
6364
<link rel="stylesheet" href="css/opt-frontend.css"/>
6465

6566
</head>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/* custom CSS for Berkeley CS61a
1+
/* custom CSS for ../composingprograms.html
22
3-
always include this AFTER pytutor.css
3+
always include this file AFTER pytutor.css
44
*/
55

66
div.ExecutionVisualizer table#pyCodeOutput {

‎v3/js/composingprograms.js‎

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
/*
2+
3+
Online Python Tutor
4+
https://github.com/pgbovine/OnlinePythonTutor/
5+
6+
Copyright (C) 2010-2013 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+
30+
// simplified version of opt-frontend.js for ../composingprograms.html
31+
32+
33+
// Pre-reqs:
34+
// - pytutor.js
35+
// - jquery.ba-bbq.min.js
36+
// - opt-frontend-common.js
37+
// should all be imported BEFORE this file
38+
39+
40+
var appMode = 'edit'; // 'edit', 'display', or 'display_no_frills'
41+
42+
var preseededCurInstr = null; // if you passed in a 'curInstr=<number>' in the URL, then set this var
43+
44+
var myVisualizer = null; // singleton ExecutionVisualizer instance
45+
46+
47+
function enterEditMode() {
48+
$.bbq.pushState({ mode: 'edit' }, 2 /* completely override other hash strings to keep URL clean */);
49+
}
50+
51+
function enterDisplayNoFrillsMode() {
52+
$.bbq.pushState({ mode: 'display_no_frills' }, 2 /* completely override other hash strings to keep URL clean */);
53+
}
54+
55+
var pyInputCodeMirror; // CodeMirror object that contains the input text
56+
57+
function setCodeMirrorVal(dat) {
58+
pyInputCodeMirror.setValue(dat.rtrim() /* kill trailing spaces */);
59+
$('#urlOutput,#embedCodeOutput').val('');
60+
61+
// also scroll to top to make the UI more usable on smaller monitors
62+
$(document).scrollTop(0);
63+
}
64+
65+
66+
$(document).ready(function() {
67+
68+
$("#embedLinkDiv").hide();
69+
70+
pyInputCodeMirror = CodeMirror(document.getElementById('codeInputPane'), {
71+
mode: 'python',
72+
lineNumbers: true,
73+
tabSize: 4,
74+
indentUnit: 4,
75+
// convert tab into four spaces:
76+
extraKeys: {Tab: function(cm) {cm.replaceSelection(" ", "end");}}
77+
});
78+
79+
pyInputCodeMirror.setSize(null, '420px');
80+
81+
82+
83+
// be friendly to the browser's forward and back buttons
84+
// thanks to http://benalman.com/projects/jquery-bbq-plugin/
85+
$(window).bind("hashchange", function(e) {
86+
appMode = $.bbq.getState('mode'); // assign this to the GLOBAL appMode
87+
88+
if (appMode === undefined || appMode == 'edit') {
89+
$("#pyInputPane").show();
90+
$("#pyOutputPane").hide();
91+
$("#embedLinkDiv").hide();
92+
93+
// destroy all annotation bubbles (NB: kludgy)
94+
if (myVisualizer) {
95+
myVisualizer.destroyAllAnnotationBubbles();
96+
}
97+
}
98+
else if (appMode == 'display') {
99+
$("#pyInputPane").hide();
100+
$("#pyOutputPane").show();
101+
102+
$("#embedLinkDiv").show();
103+
104+
$('#executeBtn').html("Visualize Execution");
105+
$('#executeBtn').attr('disabled', false);
106+
107+
108+
// do this AFTER making #pyOutputPane visible, or else
109+
// jsPlumb connectors won't render properly
110+
myVisualizer.updateOutput();
111+
112+
// customize edit button click functionality AFTER rendering (NB: awkward!)
113+
$('#pyOutputPane #editCodeLinkDiv').show();
114+
$('#pyOutputPane #editBtn').click(function() {
115+
enterEditMode();
116+
});
117+
}
118+
else if (appMode == 'display_no_frills') {
119+
$("#pyInputPane").hide();
120+
$("#pyOutputPane").show();
121+
$("#embedLinkDiv").show();
122+
}
123+
else {
124+
assert(false);
125+
}
126+
127+
$('#urlOutput,#embedCodeOutput').val(''); // clear to avoid stale values
128+
});
129+
130+
131+
function executeCode(forceStartingInstr) {
132+
backend_script = python3_backend_script;
133+
134+
$('#executeBtn').html("Please wait ... processing your code");
135+
$('#executeBtn').attr('disabled', true);
136+
$("#pyOutputPane").hide();
137+
$("#embedLinkDiv").hide();
138+
139+
var backendOptionsObj = {cumulative_mode: ($('#cumulativeModeSelector').val() == 'true'),
140+
heap_primitives: false,
141+
show_only_outputs: false,
142+
py_crazy_mode: false,
143+
origin: 'composingprograms.js'};
144+
145+
var startingInstruction = 0;
146+
147+
// only do this at most ONCE, and then clear out preseededCurInstr
148+
// NOP anyways if preseededCurInstr is 0
149+
if (preseededCurInstr) {
150+
startingInstruction = preseededCurInstr;
151+
preseededCurInstr = null;
152+
}
153+
154+
// forceStartingInstr overrides everything else
155+
if (forceStartingInstr !== undefined) {
156+
startingInstruction = forceStartingInstr;
157+
}
158+
159+
var frontendOptionsObj = {startingInstruction: startingInstruction,
160+
executeCodeWithRawInputFunc: executeCodeWithRawInput,
161+
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
162+
}
163+
164+
function handleSuccessFunc() {
165+
// also scroll to top to make the UI more usable on smaller monitors
166+
$(document).scrollTop(0);
167+
168+
$.bbq.pushState({ mode: 'display' }, 2 /* completely override other hash strings to keep URL clean */);
169+
}
170+
171+
function handleUncaughtExceptionFunc(trace) {
172+
if (trace.length == 1) {
173+
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
174+
if (errorLineNo !== undefined) {
175+
// highlight the faulting line in pyInputCodeMirror
176+
pyInputCodeMirror.focus();
177+
pyInputCodeMirror.setCursor(errorLineNo, 0);
178+
pyInputCodeMirror.setLineClass(errorLineNo, null, 'errorLine');
179+
180+
pyInputCodeMirror.setOption('onChange', function() {
181+
pyInputCodeMirror.setLineClass(errorLineNo, null, null); // reset line back to normal
182+
pyInputCodeMirror.setOption('onChange', null); // cancel
183+
});
184+
}
185+
186+
$('#executeBtn').html("Visualize Execution");
187+
$('#executeBtn').attr('disabled', false);
188+
}
189+
}
190+
191+
executePythonCode(pyInputCodeMirror.getValue(),
192+
backend_script, backendOptionsObj,
193+
frontendOptionsObj,
194+
'pyOutputPane',
195+
handleSuccessFunc, handleUncaughtExceptionFunc);
196+
}
197+
198+
function executeCodeFromScratch() {
199+
// reset these globals
200+
rawInputLst = [];
201+
executeCode();
202+
}
203+
204+
function executeCodeWithRawInput(rawInputStr, curInstr) {
205+
enterDisplayNoFrillsMode();
206+
207+
// set some globals
208+
rawInputLst.push(rawInputStr);
209+
executeCode(curInstr);
210+
}
211+
212+
$("#executeBtn").attr('disabled', false);
213+
$("#executeBtn").click(executeCodeFromScratch);
214+
215+
216+
var queryStrOptions = getQueryStringOptions();
217+
218+
if (queryStrOptions.preseededCode) {
219+
setCodeMirrorVal(queryStrOptions.preseededCode);
220+
}
221+
else {
222+
// select a canned example on start-up:
223+
$("#aliasExampleLink").trigger('click');
224+
}
225+
226+
// ugh, ugly tristate due to the possibility of each being undefined
227+
if (queryStrOptions.cumulativeState !== undefined) {
228+
$('#cumulativeModeSelector').val(queryStrOptions.cumulativeState);
229+
}
230+
231+
appMode = $.bbq.getState('mode'); // assign this to the GLOBAL appMode
232+
if ((appMode == "display") && queryStrOptions.preseededCode /* jump to display only with pre-seeded code */) {
233+
preseededCurInstr = queryStrOptions.preseededCurInstr; // ugly global
234+
$("#executeBtn").trigger('click');
235+
}
236+
else {
237+
if (appMode === undefined) {
238+
// default mode is 'edit', don't trigger a "hashchange" event
239+
appMode = 'edit';
240+
}
241+
else {
242+
// fail-soft by killing all passed-in hashes and triggering a "hashchange"
243+
// event, which will then go to 'edit' mode
244+
$.bbq.removeState();
245+
}
246+
}
247+
248+
249+
// log a generic AJAX error handler
250+
$(document).ajaxError(function() {
251+
alert("Server error (possibly due to memory/resource overload). " +
252+
"Report a bug to philip@pgbovine.net\n\n" +
253+
"(Click the 'Generate URL' button to include a unique URL in your email bug report.)");
254+
255+
$('#executeBtn').html("Visualize Execution");
256+
$('#executeBtn').attr('disabled', false);
257+
});
258+
259+
260+
// redraw connector arrows on window resize
261+
$(window).resize(function() {
262+
if (appMode == 'display') {
263+
myVisualizer.redrawConnectors();
264+
}
265+
});
266+
267+
$('#genUrlBtn').bind('click', function() {
268+
var myArgs = {code: pyInputCodeMirror.getValue(),
269+
mode: appMode,
270+
cumulative: $('#cumulativeModeSelector').val(),
271+
py: $('#pythonVersionSelector').val()};
272+
273+
if (appMode == 'display') {
274+
myArgs.curInstr = myVisualizer.curInstr;
275+
}
276+
277+
var urlStr = $.param.fragment(window.location.href, myArgs, 2 /* clobber all */);
278+
$('#urlOutput').val(urlStr);
279+
});
280+
});
281+

0 commit comments

Comments
 (0)