@@ -19,9 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
2020*/
2121
22- // The Online Python Tutor front-end, which calls the cgi-bin/web_exec.py
23- // back-end with a string representing the user's script POST['user_script']
24- // and receives a complete execution trace, which it parses and displays to HTML.
22+ // code that is common to all Online Python Tutor pages
2523
2624var appMode = 'edit' ; // 'edit' or 'visualize'
2725
@@ -1093,57 +1091,10 @@ function renderPyCodeOutput(codeStr) {
10931091
10941092}
10951093
1096- $ ( document ) . ready ( function ( ) {
1097-
1098- $ ( "#pyOutputPane" ) . hide ( ) ;
1099-
1100- $ ( "#executeBtn" ) . attr ( 'disabled' , false ) ;
1101-
1094+ // initialization function that should be called when the page is loaded
1095+ function eduPythonCommonInit ( ) {
11021096 $ ( "#pyInput" ) . tabby ( ) ; // recognize TAB and SHIFT-TAB
11031097
1104- // disable autogrow for simplicity
1105- //$("#pyInput").autogrow();
1106-
1107- $ ( "#executeBtn" ) . click ( function ( ) {
1108- if ( localTesting ) {
1109- renderPyCodeOutput ( $ ( "#pyInput" ) . val ( ) ) ;
1110-
1111- processTrace ( data_test_trace ) ;
1112-
1113- $ ( "#pyInputPane" ) . hide ( ) ;
1114- $ ( "#pyOutputPane" ) . show ( ) ;
1115- appMode = 'visualize' ;
1116- }
1117- else {
1118- $ ( '#executeBtn' ) . html ( "Please wait ... processing your code" ) ;
1119- $ ( '#executeBtn' ) . attr ( 'disabled' , true ) ;
1120- $ ( "#pyOutputPane" ) . hide ( ) ;
1121-
1122- $ . post ( "cgi-bin/web_exec.py" ,
1123- { user_script : $ ( "#pyInput" ) . val ( ) } ,
1124- function ( traceData ) {
1125- renderPyCodeOutput ( $ ( "#pyInput" ) . val ( ) ) ;
1126- processTrace ( traceData ) ;
1127-
1128- $ ( "#pyInputPane" ) . hide ( ) ;
1129- $ ( "#pyOutputPane" ) . show ( ) ;
1130- appMode = 'visualize' ;
1131-
1132- $ ( '#executeBtn' ) . html ( "Visualize execution" ) ;
1133- $ ( '#executeBtn' ) . attr ( 'disabled' , false ) ;
1134- } ,
1135- "json" ) ;
1136- }
1137- } ) ;
1138-
1139-
1140- $ ( "#editBtn" ) . click ( function ( ) {
1141- $ ( "#pyInputPane" ) . show ( ) ;
1142- $ ( "#pyOutputPane" ) . hide ( ) ;
1143- appMode = 'edit' ;
1144- } ) ;
1145-
1146-
11471098 $ ( "#jmpFirstInstr" ) . click ( function ( ) {
11481099 curInstr = 0 ;
11491100 updateOutput ( ) ;
@@ -1168,103 +1119,11 @@ $(document).ready(function() {
11681119 }
11691120 } ) ;
11701121
1171-
1172- // canned examples
1173-
1174- $ ( "#tutorialExampleLink" ) . click ( function ( ) {
1175- $ . get ( "example-code/py_tutorial.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1176- return false ;
1177- } ) ;
1178-
1179- $ ( "#strtokExampleLink" ) . click ( function ( ) {
1180- $ . get ( "example-code/strtok.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1181- return false ;
1182- } ) ;
1183-
1184- $ ( "#fibonacciExampleLink" ) . click ( function ( ) {
1185- $ . get ( "example-code/fib.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1186- return false ;
1187- } ) ;
1188-
1189- $ ( "#memoFibExampleLink" ) . click ( function ( ) {
1190- $ . get ( "example-code/memo_fib.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1191- return false ;
1192- } ) ;
1193-
1194- $ ( "#factExampleLink" ) . click ( function ( ) {
1195- $ . get ( "example-code/fact.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1196- return false ;
1197- } ) ;
1198-
1199- $ ( "#filterExampleLink" ) . click ( function ( ) {
1200- $ . get ( "example-code/filter.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1201- return false ;
1202- } ) ;
1203-
1204- $ ( "#insSortExampleLink" ) . click ( function ( ) {
1205- $ . get ( "example-code/ins_sort.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1206- return false ;
1207- } ) ;
1208-
1209- $ ( "#aliasExampleLink" ) . click ( function ( ) {
1210- $ . get ( "example-code/aliasing.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1211- return false ;
1212- } ) ;
1213-
1214- $ ( "#newtonExampleLink" ) . click ( function ( ) {
1215- $ . get ( "example-code/sqrt.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1216- return false ;
1217- } ) ;
1218-
1219- $ ( "#oopSmallExampleLink" ) . click ( function ( ) {
1220- $ . get ( "example-code/oop_small.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1221- return false ;
1222- } ) ;
1223-
1224- $ ( "#mapExampleLink" ) . click ( function ( ) {
1225- $ . get ( "example-code/map.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1226- return false ;
1227- } ) ;
1228-
1229- $ ( "#oop1ExampleLink" ) . click ( function ( ) {
1230- $ . get ( "example-code/oop_1.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1231- return false ;
1232- } ) ;
1233-
1234- $ ( "#oop2ExampleLink" ) . click ( function ( ) {
1235- $ . get ( "example-code/oop_2.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1236- return false ;
1237- } ) ;
1238-
1239- $ ( "#inheritanceExampleLink" ) . click ( function ( ) {
1240- $ . get ( "example-code/oop_inherit.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1241- return false ;
1242- } ) ;
1243-
1244- $ ( "#sumExampleLink" ) . click ( function ( ) {
1245- $ . get ( "example-code/sum.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1246- return false ;
1247- } ) ;
1248-
1249- $ ( "#pwGcdLink" ) . click ( function ( ) {
1250- $ . get ( "example-code/wentworth_gcd.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1251- return false ;
1252- } ) ;
1253-
1254- $ ( "#pwSumListLink" ) . click ( function ( ) {
1255- $ . get ( "example-code/wentworth_sumList.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1256- return false ;
1257- } ) ;
1258-
1259- $ ( "#towersOfHanoiLink" ) . click ( function ( ) {
1260- $ . get ( "example-code/towers_of_hanoi.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1261- return false ;
1262- } ) ;
1263-
1264- $ ( "#pwTryFinallyLink" ) . click ( function ( ) {
1265- $ . get ( "example-code/wentworth_try_finally.txt" , function ( dat ) { $ ( "#pyInput" ) . val ( dat ) ; } ) ;
1266- return false ;
1267- } ) ;
1122+ // disable controls initially ...
1123+ $ ( "#vcrControls #jmpFirstInstr" ) . attr ( "disabled" , true ) ;
1124+ $ ( "#vcrControls #jmpStepBack" ) . attr ( "disabled" , true ) ;
1125+ $ ( "#vcrControls #jmpStepFwd" ) . attr ( "disabled" , true ) ;
1126+ $ ( "#vcrControls #jmpLastInstr" ) . attr ( "disabled" , true ) ;
12681127
12691128
12701129 // set some sensible jsPlumb defaults
@@ -1279,17 +1138,6 @@ $(document).ready(function() {
12791138 jsPlumb . Defaults . HoverPaintStyle = { lineWidth :2 , strokeStyle : pinkish } ;
12801139
12811140
1282- // select an example on start-up:
1283- $ ( "#aliasExampleLink" ) . trigger ( 'click' ) ;
1284-
1285-
1286- // disable controls initially ...
1287- $ ( "#vcrControls #jmpFirstInstr" ) . attr ( "disabled" , true ) ;
1288- $ ( "#vcrControls #jmpStepBack" ) . attr ( "disabled" , true ) ;
1289- $ ( "#vcrControls #jmpStepFwd" ) . attr ( "disabled" , true ) ;
1290- $ ( "#vcrControls #jmpLastInstr" ) . attr ( "disabled" , true ) ;
1291-
1292-
12931141 // set keyboard event listeners ...
12941142 $ ( document ) . keydown ( function ( k ) {
12951143 // ONLY capture keys if we're in 'visualize code' mode:
@@ -1311,9 +1159,11 @@ $(document).ready(function() {
13111159
13121160 // redraw everything on window resize so that connectors are in the
13131161 // right place
1162+ // TODO: can be SLOW on older browsers!!!
13141163 $ ( window ) . resize ( function ( ) {
1315- updateOutput ( ) ;
1164+ if ( appMode == 'visualize' ) {
1165+ updateOutput ( ) ;
1166+ }
13161167 } ) ;
1317-
1318- } ) ;
1168+ }
13191169
0 commit comments