@@ -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} ) ;
0 commit comments