Skip to content

Commit 85dd9e0

Browse files
author
Philip Guo
committed
more gross refactoring
1 parent bf7e58c commit 85dd9e0

4 files changed

Lines changed: 105 additions & 77 deletions

File tree

‎v3/iframe-embed.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<script type="text/javascript" src="js/pytutor.js"></script>
1919
<link rel="stylesheet" href="css/pytutor.css"/>
2020

21+
<script type="text/javascript" src="js/opt-frontend-common.js"></script>
2122
<script type="text/javascript" src="js/iframe-embed.js"></script>
2223

2324
</head>

‎v3/js/iframe-embed.js‎

Lines changed: 97 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,25 @@ var myVisualizer = null; // singleton ExecutionVisualizer instance
3838

3939

4040
$(document).ready(function() {
41-
var preseededCode = $.bbq.getState('code');
41+
var queryStrOptions = getQueryStringOptions();
4242

43-
var pyState = $.bbq.getState('py');
44-
var verticalStackBool = ($.bbq.getState('verticalStack') == 'true'); // boolean
45-
var heapPrimitivesBool = ($.bbq.getState('heapPrimitives') == 'true');
46-
var drawParentPointerBool = ($.bbq.getState('drawParentPointers') == 'true');
47-
var textRefsBool = ($.bbq.getState('textReferences') == 'true');
48-
var showOnlyOutputsBool = ($.bbq.getState('showOnlyOutputs') == 'true');
43+
var preseededCode = queryStrOptions.preseededCode;
44+
var pyState = queryStrOptions.pyState;
45+
var verticalStackBool = (queryStrOptions.verticalStack == 'true');
46+
var heapPrimitivesBool = (queryStrOptions.heapPrimitives == 'true');
47+
var drawParentPointerBool = (queryStrOptions.drawParentPointers == 'true');
48+
var textRefsBool = (queryStrOptions.textRefs == 'true');
49+
var showOnlyOutputsBool = (queryStrOptions.showOnlyOutputs == 'true');
50+
var cumModeBool = (queryStrOptions.cumulativeState == 'true');
4951

5052
// set up all options in a JS object
51-
var options = {cumulative_mode: ($.bbq.getState('cumulative') == 'true'),
53+
var options = {cumulative_mode: cumModeBool,
5254
heap_primitives: heapPrimitivesBool,
5355
show_only_outputs: showOnlyOutputsBool,
54-
py_crazy_mode: ($.bbq.getState('py') == '2crazy')};
56+
py_crazy_mode: (pyState == '2crazy')};
5557

5658

57-
var preseededCurInstr = Number($.bbq.getState('curInstr'));
59+
var preseededCurInstr = queryStrOptions.preseededCurInstr;
5860
if (!preseededCurInstr) {
5961
preseededCurInstr = 0;
6062
}
@@ -95,82 +97,106 @@ $(document).ready(function() {
9597
};
9698
}
9799

98-
99-
$.get(backend_script,
100-
{user_script : preseededCode,
101-
options_json: JSON.stringify(options)},
102-
function(dataFromBackend) {
103-
var trace = dataFromBackend.trace;
104-
105-
// don't enter visualize mode if there are killer errors:
106-
if (!trace ||
107-
(trace.length == 0) ||
108-
(trace[trace.length - 1].event == 'uncaught_exception')) {
109-
110-
if (trace.length == 1) {
111-
alert(trace[0].exception_msg);
112-
}
113-
else if (trace[trace.length - 1].exception_msg) {
114-
alert(trace[trace.length - 1].exception_msg);
100+
101+
function executeCode(forceStartingInstr) {
102+
$.get(backend_script,
103+
{user_script : preseededCode,
104+
raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '',
105+
options_json: JSON.stringify(options)},
106+
function(dataFromBackend) {
107+
var trace = dataFromBackend.trace;
108+
109+
// don't enter visualize mode if there are killer errors:
110+
if (!trace ||
111+
(trace.length == 0) ||
112+
(trace[trace.length - 1].event == 'uncaught_exception')) {
113+
114+
if (trace.length == 1) {
115+
alert(trace[0].exception_msg);
116+
}
117+
else if (trace[trace.length - 1].exception_msg) {
118+
alert(trace[trace.length - 1].exception_msg);
119+
}
120+
else {
121+
alert("Whoa, unknown error! Reload to try again, or report a bug to philip@pgbovine.net");
122+
}
115123
}
116124
else {
117-
alert("Whoa, unknown error! Reload to try again, or report a bug to philip@pgbovine.net\n\n(Click the 'Generate URL' button to include a unique URL in your email bug report.)");
118-
}
119-
}
120-
else {
121-
var startingInstruction = 0;
125+
var startingInstruction = 0;
122126

123-
// only do this at most ONCE, and then clear out preseededCurInstr
124-
if (preseededCurInstr && preseededCurInstr < trace.length) { // NOP anyways if preseededCurInstr is 0
125-
startingInstruction = preseededCurInstr;
126-
}
127+
// only do this at most ONCE, and then clear out preseededCurInstr
128+
if (preseededCurInstr && preseededCurInstr < trace.length) { // NOP anyways if preseededCurInstr is 0
129+
startingInstruction = preseededCurInstr;
130+
}
127131

128-
myVisualizer = new ExecutionVisualizer('vizDiv',
129-
dataFromBackend,
130-
{startingInstruction: preseededCurInstr,
131-
embeddedMode: true,
132-
verticalStack: verticalStackBool,
133-
disableHeapNesting: heapPrimitivesBool,
134-
drawParentPointers: drawParentPointerBool,
135-
textualMemoryLabels: textRefsBool,
136-
showOnlyOutputs: showOnlyOutputsBool,
137-
highlightLines: typeof $.bbq.getState("highlightLines") !== "undefined",
138-
pyCrazyMode: ($.bbq.getState('py') == '2crazy'),
139-
updateOutputCallback: (resizeContainer ? resizeContainerNow : null)
140-
});
141-
142-
// set keyboard bindings
143-
// VERY IMPORTANT to clear and reset this every time or
144-
// else the handlers might be bound multiple times
145-
$(document).unbind('keydown');
146-
$(document).keydown(function(k) {
147-
if (k.keyCode == 37) { // left arrow
148-
if (myVisualizer.stepBack()) {
149-
k.preventDefault(); // don't horizontally scroll the display
150-
}
132+
// forceStartingInstr overrides everything else
133+
if (forceStartingInstr !== undefined) {
134+
startingInstruction = forceStartingInstr;
151135
}
152-
else if (k.keyCode == 39) { // right arrow
153-
if (myVisualizer.stepForward()) {
154-
k.preventDefault(); // don't horizontally scroll the display
136+
137+
myVisualizer = new ExecutionVisualizer('vizDiv',
138+
dataFromBackend,
139+
{startingInstruction: preseededCurInstr,
140+
embeddedMode: true,
141+
verticalStack: verticalStackBool,
142+
disableHeapNesting: heapPrimitivesBool,
143+
drawParentPointers: drawParentPointerBool,
144+
textualMemoryLabels: textRefsBool,
145+
showOnlyOutputs: showOnlyOutputsBool,
146+
highlightLines: typeof $.bbq.getState("highlightLines") !== "undefined",
147+
executeCodeWithRawInputFunc: executeCodeWithRawInput,
148+
pyCrazyMode: (pyState == '2crazy'),
149+
updateOutputCallback: (resizeContainer ? resizeContainerNow : null)
150+
});
151+
152+
// set keyboard bindings
153+
// VERY IMPORTANT to clear and reset this every time or
154+
// else the handlers might be bound multiple times
155+
$(document).unbind('keydown');
156+
$(document).keydown(function(k) {
157+
if (k.keyCode == 37) { // left arrow
158+
if (myVisualizer.stepBack()) {
159+
k.preventDefault(); // don't horizontally scroll the display
160+
}
155161
}
156-
}
157-
});
158-
}
159-
},
160-
"json");
162+
else if (k.keyCode == 39) { // right arrow
163+
if (myVisualizer.stepForward()) {
164+
k.preventDefault(); // don't horizontally scroll the display
165+
}
166+
}
167+
});
168+
}
169+
},
170+
"json");
171+
}
172+
173+
174+
function executeCodeFromScratch() {
175+
// reset these globals
176+
rawInputLst = [];
177+
178+
executeCode();
179+
}
180+
181+
function executeCodeWithRawInput(rawInputStr, curInstr) {
182+
enterDisplayNoFrillsMode();
183+
184+
// set some globals
185+
rawInputLst.push(rawInputStr);
186+
187+
executeCode(curInstr);
188+
}
161189

162190

163191
// log a generic AJAX error handler
164192
$(document).ajaxError(function() {
165-
alert("Online Python Tutor server error (possibly due to memory/resource overload).");
193+
alert("Ugh, Online Python Tutor server error :(");
166194
});
167195

168196

169197
// redraw connector arrows on window resize
170198
$(window).resize(function() {
171-
if (typeof appMode !== "undefined" && appMode == 'display') {
172-
myVisualizer.redrawConnectors();
173-
}
199+
myVisualizer.redrawConnectors();
174200
});
175201

176202
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ var python2crazy_backend_script = 'web_exec_py2-crazy.py';
5252
//var python2crazy_backend_script = 'exec';
5353

5454

55+
var rawInputLst = []; // a list of strings inputted by the user in response to raw_input or mouse_input events
56+
57+
5558
function getQueryStringOptions() {
5659
// note that any of these can be 'undefined'
5760
var preseededCode = $.bbq.getState('code');
@@ -72,5 +75,5 @@ function getQueryStringOptions() {
7275
drawParentPointers: drawParentPointers,
7376
textRefs: textRefs,
7477
showOnlyOutputs: showOnlyOutputs,
75-
cumulativeState: cumulativeState}
78+
cumulativeState: cumulativeState};
7679
}

‎v3/js/opt-frontend.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ var appMode = 'edit'; // 'edit', 'display', or 'display_no_frills'
3939
var preseededCode = null; // if you passed in a 'code=<code string>' in the URL, then set this var
4040
var preseededCurInstr = null; // if you passed in a 'curInstr=<number>' in the URL, then set this var
4141

42-
var rawInputLst = []; // a list of strings inputted by the user in response to raw_input or mouse_input events
43-
4442
var myVisualizer = null; // singleton ExecutionVisualizer instance
4543

4644

@@ -101,7 +99,7 @@ $(document).ready(function() {
10199

102100
$("#embedLinkDiv").show();
103101

104-
$('#executeBtn').html("Visualize execution");
102+
$('#executeBtn').html("Visualize Execution");
105103
$('#executeBtn').attr('disabled', false);
106104

107105

@@ -193,7 +191,7 @@ $(document).ready(function() {
193191
alert("Whoa, unknown error! Reload to try again, or report a bug to philip@pgbovine.net\n\n(Click the 'Generate URL' button to include a unique URL in your email bug report.)");
194192
}
195193

196-
$('#executeBtn').html("Visualize execution");
194+
$('#executeBtn').html("Visualize Execution");
197195
$('#executeBtn').attr('disabled', false);
198196
}
199197
else {
@@ -553,7 +551,7 @@ $(document).ready(function() {
553551
$(document).ajaxError(function() {
554552
alert("Server error (possibly due to memory/resource overload). Report a bug to philip@pgbovine.net\n\n(Click the 'Generate URL' button to include a unique URL in your email bug report.)");
555553

556-
$('#executeBtn').html("Visualize execution");
554+
$('#executeBtn').html("Visualize Execution");
557555
$('#executeBtn').attr('disabled', false);
558556
});
559557

0 commit comments

Comments
 (0)