@@ -23,15 +23,71 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2323// back-end with a string representing the user's script POST['user_script']
2424// and receives a complete execution trace, which it parses and displays to HTML.
2525
26- // Pre-req: edu-python.js should be imported BEFORE this file
26+ // Pre-req: edu-python.js and jquery.ba-bbq.min.js should be imported BEFORE this file
27+
28+
29+ function enterEditMode ( ) {
30+ $ . bbq . pushState ( { mode : 'edit' } ) ;
31+ }
32+
33+ function enterVisualizeMode ( traceData ) {
34+ curTrace = traceData ; // first assign it to the global curTrace, then
35+ // let jQuery BBQ take care of the rest
36+ $ . bbq . pushState ( { mode : 'visualize' } ) ;
37+ }
2738
2839
2940$ ( document ) . ready ( function ( ) {
3041 eduPythonCommonInit ( ) ; // must call this first!
3142
3243 $ ( "#pyInput" ) . tabby ( ) ; // recognize TAB and SHIFT-TAB
3344
34- $ ( "#pyOutputPane" ) . hide ( ) ;
45+
46+ // be friendly to the browser's forward and back buttons
47+ // thanks to http://benalman.com/projects/jquery-bbq-plugin/
48+ $ ( window ) . bind ( "hashchange" , function ( e ) {
49+ appMode = $ . bbq . getState ( "mode" ) ; // assign this to the GLOBAL appMode
50+
51+ // default mode is 'edit'
52+ if ( appMode == undefined ) {
53+ appMode = 'edit' ;
54+ }
55+
56+ // if there's no curTrace, then default to edit mode since there's
57+ // nothing to visualize:
58+ if ( ! curTrace ) {
59+ appMode = 'edit' ;
60+ $ . bbq . pushState ( { mode : 'edit' } ) ;
61+ }
62+
63+
64+ if ( appMode == 'edit' ) {
65+ $ ( "#pyInputPane" ) . show ( ) ;
66+ $ ( "#pyOutputPane" ) . hide ( ) ;
67+ }
68+ else if ( appMode == 'visualize' ) {
69+ $ ( "#pyInputPane" ) . hide ( ) ;
70+ $ ( "#pyOutputPane" ) . show ( ) ;
71+
72+ $ ( '#executeBtn' ) . html ( "Visualize execution" ) ;
73+ $ ( '#executeBtn' ) . attr ( 'disabled' , false ) ;
74+
75+
76+ // do this AFTER making #pyOutputPane visible, or else
77+ // jsPlumb connectors won't render properly
78+ processTrace ( curTrace /* kinda dumb and redundant */ , true ) ;
79+ }
80+ else {
81+ assert ( false ) ;
82+ }
83+ } ) ;
84+
85+ // From: http://benalman.com/projects/jquery-bbq-plugin/
86+ // Since the event is only triggered when the hash changes, we need
87+ // to trigger the event now, to handle the hash the page may have
88+ // loaded with.
89+ $ ( window ) . trigger ( "hashchange" ) ;
90+
3591
3692 $ ( "#executeBtn" ) . attr ( 'disabled' , false ) ;
3793 $ ( "#executeBtn" ) . click ( function ( ) {
@@ -43,26 +99,14 @@ $(document).ready(function() {
4399 { user_script : $ ( "#pyInput" ) . val ( ) } ,
44100 function ( traceData ) {
45101 renderPyCodeOutput ( $ ( "#pyInput" ) . val ( ) ) ;
46-
47- $ ( "#pyInputPane" ) . hide ( ) ;
48- $ ( "#pyOutputPane" ) . show ( ) ;
49- appMode = 'visualize' ;
50-
51- $ ( '#executeBtn' ) . html ( "Visualize execution" ) ;
52- $ ( '#executeBtn' ) . attr ( 'disabled' , false ) ;
53-
54- // do this AFTER making #pyOutputPane visible, or else
55- // jsPlumb connectors won't render properly
56- processTrace ( traceData , false ) ;
102+ enterVisualizeMode ( traceData ) ;
57103 } ,
58104 "json" ) ;
59105 } ) ;
60106
61107
62108 $ ( "#editBtn" ) . click ( function ( ) {
63- $ ( "#pyInputPane" ) . show ( ) ;
64- $ ( "#pyOutputPane" ) . hide ( ) ;
65- appMode = 'edit' ;
109+ enterEditMode ( ) ;
66110 } ) ;
67111
68112
0 commit comments