Skip to content

Commit e8f385d

Browse files
author
Philip Guo
committed
more refactoring goodness
1 parent 92132da commit e8f385d

File tree

2 files changed

+86
-91
lines changed

2 files changed

+86
-91
lines changed

PyTutorGAE/js/edu-python-tutor.js

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,6 @@ function enterEditMode() {
3434
$.bbq.pushState({ mode: 'edit' }, 2 /* completely override other hash strings to keep URL clean */);
3535
}
3636

37-
function preprocessBackendResult(traceData, inputCode) {
38-
// set gross globals, then let jQuery BBQ take care of the rest
39-
curTrace = traceData;
40-
curInputCode = inputCode;
41-
42-
renderPyCodeOutput(inputCode);
43-
44-
45-
// must postprocess traceData prior to running precomputeCurTraceLayouts() ...
46-
var lastEntry = curTrace[curTrace.length - 1];
47-
48-
// GLOBAL!
49-
instrLimitReached = (lastEntry.event == 'instruction_limit_reached');
50-
51-
if (instrLimitReached) {
52-
curTrace.pop() // kill last entry
53-
var warningMsg = lastEntry.exception_msg;
54-
$("#errorOutput").html(htmlspecialchars(warningMsg));
55-
$("#errorOutput").show();
56-
}
57-
// as imran suggests, for a (non-error) one-liner, SNIP off the
58-
// first instruction so that we start after the FIRST instruction
59-
// has been executed ...
60-
else if (curTrace.length == 2) {
61-
curTrace.shift();
62-
}
63-
64-
precomputeCurTraceLayouts(); // bam!
65-
66-
$.bbq.pushState({ mode: 'visualize' }, 2 /* completely override other hash strings to keep URL clean */);
67-
}
68-
6937

7038
var pyInputCodeMirror; // CodeMirror object that contains the input text
7139

@@ -153,7 +121,7 @@ $(document).ready(function() {
153121
$('#executeBtn').attr('disabled', true);
154122
$("#pyOutputPane").hide();
155123

156-
// TODO: is GET or POST best here?
124+
157125
$.get("exec",
158126
{user_script : pyInputCodeMirror.getValue()},
159127
function(traceData) {
@@ -186,7 +154,7 @@ $(document).ready(function() {
186154
$('#executeBtn').attr('disabled', false);
187155
}
188156
else {
189-
preprocessBackendResult(traceData, pyInputCodeMirror.getValue());
157+
createVisualization(traceData, pyInputCodeMirror.getValue());
190158
}
191159
},
192160
"json");
@@ -375,24 +343,13 @@ $(document).ready(function() {
375343
}
376344

377345

346+
// log a generic AJAX error handler
347+
// TODO: too global!
348+
$(document).ajaxError(function() {
349+
alert("Server error (possibly due to memory/resource overload).");
378350

379-
$('#executionSlider').bind('slide', function(evt, ui) {
380-
// this is SUPER subtle. if this value was changed programmatically,
381-
// then evt.originalEvent will be undefined. however, if this value
382-
// was changed by a user-initiated event, then this code should be
383-
// executed ...
384-
if (evt.originalEvent) {
385-
curInstr = ui.value;
386-
updateOutput(true); // need to pass 'true' here to prevent infinite loop
387-
}
388-
});
389-
390-
391-
$('#genUrlBtn').bind('click', function() {
392-
// override mode with 'visualize' ...
393-
var urlStr = jQuery.param.fragment(window.location.href, {code: curInputCode, curInstr: curInstr}, 2);
394-
395-
$('#urlOutput').val(urlStr);
351+
$('#executeBtn').html("Visualize execution");
352+
$('#executeBtn').attr('disabled', false);
396353
});
397354

398355
});

PyTutorGAE/js/edu-python.js

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
// since the latter might exhibit funny behavior for certain reserved keywords
3232

3333

34-
/* colors - see edu-python.css */
35-
var lightYellow = '#F5F798';
36-
var lightLineColor = '#FFFFCC';
37-
var errorColor = '#F87D76';
38-
var visitedLineColor = '#3D58A2';
39-
40-
var lightGray = "#cccccc";
41-
var darkBlue = "#3D58A2";
42-
var medBlue = "#41507A";
43-
var medLightBlue = "#6F89D1";
44-
var lightBlue = "#899CD1";
45-
var pinkish = "#F15149";
46-
var lightPink = "#F89D99";
47-
var darkRed = "#9D1E18";
48-
49-
var breakpointColor = pinkish;
50-
var hoverBreakpointColor = medLightBlue;
51-
52-
5334

5435
var appMode = 'edit'; // 'edit', 'visualize', or 'grade' (only for question.html)
5536

@@ -85,6 +66,41 @@ var instrLimitReached = false;
8566

8667

8768

69+
function createVisualization(traceData, inputCode) {
70+
// set gross globals, then let jQuery BBQ take care of the rest
71+
curTrace = traceData;
72+
curInputCode = inputCode;
73+
74+
renderPyCodeOutput(inputCode);
75+
76+
77+
// must postprocess traceData prior to running precomputeCurTraceLayouts() ...
78+
var lastEntry = curTrace[curTrace.length - 1];
79+
80+
// GLOBAL!
81+
instrLimitReached = (lastEntry.event == 'instruction_limit_reached');
82+
83+
if (instrLimitReached) {
84+
curTrace.pop() // kill last entry
85+
var warningMsg = lastEntry.exception_msg;
86+
$("#errorOutput").html(htmlspecialchars(warningMsg));
87+
$("#errorOutput").show();
88+
}
89+
// as imran suggests, for a (non-error) one-liner, SNIP off the
90+
// first instruction so that we start after the FIRST instruction
91+
// has been executed ...
92+
else if (curTrace.length == 2) {
93+
curTrace.shift();
94+
}
95+
96+
precomputeCurTraceLayouts(); // bam!
97+
98+
$.bbq.pushState({ mode: 'visualize' }, 2 /* completely override other hash strings to keep URL clean */);
99+
}
100+
101+
102+
103+
88104
function enterVisualizeMode(jumpToEnd) {
89105
curInstr = 0;
90106

@@ -1443,6 +1459,27 @@ function clearSliderBreakpoints() {
14431459
// initialization function that should be called when the page is loaded
14441460
function eduPythonCommonInit() {
14451461

1462+
$('#executionSlider').bind('slide', function(evt, ui) {
1463+
// this is SUPER subtle. if this value was changed programmatically,
1464+
// then evt.originalEvent will be undefined. however, if this value
1465+
// was changed by a user-initiated event, then this code should be
1466+
// executed ...
1467+
if (evt.originalEvent) {
1468+
curInstr = ui.value;
1469+
updateOutput(true); // need to pass 'true' here to prevent infinite loop
1470+
}
1471+
});
1472+
1473+
1474+
$('#genUrlBtn').bind('click', function() {
1475+
// override mode with 'visualize' ...
1476+
var urlStr = jQuery.param.fragment(window.location.href, {code: curInputCode, curInstr: curInstr}, 2);
1477+
1478+
$('#urlOutput').val(urlStr);
1479+
});
1480+
1481+
1482+
14461483
$("#jmpFirstInstr").click(function() {
14471484
curInstr = 0;
14481485
updateOutput();
@@ -1498,6 +1535,7 @@ function eduPythonCommonInit() {
14981535

14991536

15001537
// set keyboard event listeners ...
1538+
// TODO: too global!
15011539
$(document).keydown(function(k) {
15021540
// ONLY capture keys if we're in 'visualize code' mode:
15031541
if (appMode == 'visualize' && !keyStuckDown) {
@@ -1548,6 +1586,7 @@ function eduPythonCommonInit() {
15481586
}
15491587
});
15501588

1589+
// TODO: too global!
15511590
$(document).keyup(function(k) {
15521591
keyStuckDown = false;
15531592
});
@@ -1556,6 +1595,7 @@ function eduPythonCommonInit() {
15561595
// redraw everything on window resize so that connectors are in the
15571596
// right place
15581597
// TODO: can be SLOW on older browsers!!!
1598+
// TODO: too global!
15591599
$(window).resize(function() {
15601600
if (appMode == 'visualize') {
15611601
updateOutput();
@@ -1567,27 +1607,36 @@ function eduPythonCommonInit() {
15671607
updateOutput();
15681608
}
15691609
});
1610+
}
15701611

15711612

1572-
// log a generic AJAX error handler
1573-
$(document).ajaxError(function() {
1574-
alert("Server error (possibly due to memory/resource overload).");
1575-
1576-
$('#executeBtn').html("Visualize execution");
1577-
$('#executeBtn').attr('disabled', false);
1578-
});
15791613

1580-
}
1614+
// Utilities
15811615

15821616

1617+
/* colors - see edu-python.css */
1618+
var lightYellow = '#F5F798';
1619+
var lightLineColor = '#FFFFCC';
1620+
var errorColor = '#F87D76';
1621+
var visitedLineColor = '#3D58A2';
15831622

1623+
var lightGray = "#cccccc";
1624+
var darkBlue = "#3D58A2";
1625+
var medBlue = "#41507A";
1626+
var medLightBlue = "#6F89D1";
1627+
var lightBlue = "#899CD1";
1628+
var pinkish = "#F15149";
1629+
var lightPink = "#F89D99";
1630+
var darkRed = "#9D1E18";
15841631

1632+
var breakpointColor = pinkish;
1633+
var hoverBreakpointColor = medLightBlue;
15851634

1586-
// Utilities
15871635

15881636
function assert(cond) {
1637+
// TODO: add more precision in the error message
15891638
if (!cond) {
1590-
alert("Error: ASSERTION FAILED");
1639+
alert("Error: ASSERTION FAILED!!!");
15911640
}
15921641
}
15931642

@@ -1691,14 +1740,3 @@ function getRefID(obj) {
16911740
return obj[1];
16921741
}
16931742

1694-
function getObjectID(obj) {
1695-
assert($.isArray(obj));
1696-
1697-
if ((obj[0] == 'INSTANCE') || (obj[0] == 'CLASS')) {
1698-
return obj[2];
1699-
}
1700-
else {
1701-
return obj[1];
1702-
}
1703-
}
1704-

0 commit comments

Comments
 (0)