@@ -62,13 +62,13 @@ var TogetherJSConfig_seenIntroDialog = true;
6262var TogetherJSConfig_suppressInvite = true ;
6363var TogetherJSConfig_suppressJoinConfirmation = true ;
6464
65- // only clone clicks in the input pane to keep things simple and prevent
66- // duplicate clicks on, say, the visualizer pane
67- var TogetherJSConfig_cloneClicks = '#pyInputPane' ;
65+ // clone clicks ONLY in certain elements to keep things simple:
66+ var TogetherJSConfig_cloneClicks = '#pyInputPane select,#pyInputPane #executeBtn' ;
6867
6968var TogetherJSConfig_siteName = "Online Python Tutor live help" ;
7069var TogetherJSConfig_toolName = "Online Python Tutor live help" ;
7170
71+ var isTutor = false ;
7272
7373// TODO: consider deferred initialization later: "TogetherJS starts up
7474// automatically as soon as it can, especially when continuing a
@@ -82,6 +82,7 @@ var TogetherJSConfig_toolName = "Online Python Tutor live help";
8282// nasty globals
8383var updateOutputSignalFromRemote = false ;
8484var hashchangeSignalFromRemote = false ;
85+ var nowSyncing = false ;
8586
8687// Global hook for ExecutionVisualizer.
8788var try_hook = function ( hook_name , args ) {
@@ -99,10 +100,25 @@ function getAppState() {
99100 textReferences : $ ( '#textualMemoryLabelsSelector' ) . val ( ) ,
100101 showOnlyOutputs : $ ( '#showOnlyOutputsSelector' ) . val ( ) ,
101102 py : $ ( '#pythonVersionSelector' ) . val ( ) ,
103+ curInstr : myVisualizer ? myVisualizer . curInstr : undefined ,
102104 // the app state that led to the CURRENT visualization
103105 visualizedAppState : visualizedAppState } ;
104106}
105107
108+ // sets the proper GUI features to match the given appState object
109+ function setVisibleAppState ( appState ) {
110+ setCodeMirrorVal ( appState . code ) ;
111+
112+ $ ( '#cumulativeModeSelector' ) . val ( appState . cumulative ) ;
113+ $ ( '#heapPrimitivesSelector' ) . val ( appState . heapPrimitives ) ;
114+ $ ( '#drawParentPointerSelector' ) . val ( appState . drawParentPointers ) ;
115+ $ ( '#textualMemoryLabelsSelector' ) . val ( appState . textReferences ) ;
116+ $ ( '#showOnlyOutputsSelector' ) . val ( appState . showOnlyOutputs ) ;
117+ $ ( '#pythonVersionSelector' ) . val ( appState . py ) ;
118+
119+ // TODO: what to do about mode and curInstr?
120+ }
121+
106122
107123$ ( document ) . ready ( function ( ) {
108124 setSurveyHTML ( ) ;
@@ -125,14 +141,20 @@ $(document).ready(function() {
125141 $ ( "#surveyHeader" ) . remove ( ) ; // kill this to save space
126142
127143 var role = $ . bbq . getState ( 'role' ) ;
128- var isTutor = ( role == 'tutor' ) ;
144+ isTutor = ( role == 'tutor' ) ; // GLOBAL
145+
129146 if ( isTutor ) {
130147 $ ( "#togetherBtn" ) . html ( "TUTOR - Join live help session" ) ;
131148 $ ( "#togetherJSHeader" ) . append ( '<button id="syncBtn"\
132149 type="button">Sync with learner</button>' ) ;
133150
134151 $ ( "#syncBtn" ) . click ( function ( ) {
135152 console . log ( "Sync!" ) ;
153+
154+ if ( TogetherJS . running ) {
155+ nowSyncing = true ;
156+ TogetherJS . send ( { type : "requestSync" } ) ;
157+ }
136158 } ) ;
137159 }
138160
@@ -144,7 +166,7 @@ $(document).ready(function() {
144166 if ( updateOutputSignalFromRemote ) {
145167 return ;
146168 }
147- if ( TogetherJS . running ) {
169+ if ( TogetherJS . running && ! nowSyncing /* don't perterb when syncing */ ) {
148170 TogetherJS . send ( { type : "updateOutput" , step : args . myViz . curInstr } ) ;
149171 }
150172 }
@@ -174,6 +196,7 @@ $(document).ready(function() {
174196 }
175197 hashchangeSignalFromRemote = true ;
176198 try {
199+ console . log ( "TogetherJS RECEIVE hashchange" , msg . appMode ) ;
177200 appMode = msg . appMode ; // assign this to the GLOBAL appMode
178201 updateAppDisplay ( ) ;
179202 }
@@ -182,6 +205,52 @@ $(document).ready(function() {
182205 }
183206 } ) ;
184207
208+ // learner receives a sync request from tutor and responds by
209+ // sending its current app state
210+ TogetherJS . hub . on ( "requestSync" , function ( msg ) {
211+ // only a learner should receive sync requests from a tutor
212+ if ( isTutor ) {
213+ return ;
214+ }
215+
216+ if ( TogetherJS . running ) {
217+ TogetherJS . send ( { type : "myAppState" , myAppState : getAppState ( ) } ) ;
218+ }
219+ } ) ;
220+
221+ // tutor receives an app state from a learner
222+ // TODO: what happens if more than one learner sends state to tutor?
223+ // i suppose the last one wins at this point :/
224+ TogetherJS . hub . on ( "myAppState" , function ( msg ) {
225+ // only a tutor should handle receiving an app state from a learner
226+ if ( ! isTutor ) {
227+ return ;
228+ }
229+
230+ try {
231+ var learnerAppState = msg . myAppState ;
232+
233+ // if available, first set my own app state to visualizedAppState
234+ // and then EXECUTE that code with the given options to create the
235+ // proper myVisualizer object. then adjust app state to the rest
236+ // of learnerAppState
237+
238+ if ( learnerAppState . visualizedAppState ) {
239+ setVisibleAppState ( learnerAppState . visualizedAppState ) ;
240+ // execute code and jump to the learner's curInstr
241+ executeCode ( learnerAppState . curInstr ) ;
242+ }
243+
244+ setVisibleAppState ( learnerAppState ) ;
245+
246+ appMode = learnerAppState . mode ;
247+ updateAppDisplay ( ) ;
248+ }
249+ finally {
250+ nowSyncing = false ; // done with a successful sync round trip
251+ }
252+ } ) ;
253+
185254
186255 try {
187256 var source = new EventSource ( 'http://togetherjs.pythontutor.com/learner-SSE' ) ;
@@ -227,6 +296,8 @@ $(document).ready(function() {
227296 console . log ( "TogetherJS ready" ) ;
228297 $ ( "#togetherBtn" ) . html ( "Stop live help session" ) ;
229298
299+ $ ( "#togetherJSHeader" ) . fadeIn ( 750 , redrawConnectors ) ; // always show when ready
300+
230301 if ( ! isTutor ) {
231302 $ . get ( "http://togetherjs.pythontutor.com/request-help" ,
232303 { url : TogetherJS . shareUrl ( ) } ,
@@ -259,6 +330,7 @@ $(document).ready(function() {
259330 if ( enableTogetherJS ) {
260331 if ( TogetherJS . running &&
261332 ! hashchangeSignalFromRemote /* don't double send */ ) {
333+ console . log ( "TogetherJS SEND hashchange" , appMode ) ;
262334 TogetherJS . send ( { type : "hashchange" , appMode : appMode } ) ;
263335 }
264336 }
@@ -354,19 +426,15 @@ $(document).ready(function() {
354426 }
355427
356428 function executeCodeFromScratch ( ) {
357- // prevent the weird double-execute situation where someone (such as
358- // a remote collaborator) "clicks" executeBtn while it's disabled
359- if ( ! $ ( "#executeBtn" ) . attr ( 'disabled' ) ) {
360- // don't execute empty string:
361- if ( $ . trim ( pyInputCodeMirror . getValue ( ) ) == '' ) {
362- alert ( 'Type in some code to visualize.' ) ;
363- return ;
364- }
365-
366- // reset these globals
367- rawInputLst = [ ] ;
368- executeCode ( ) ;
429+ // don't execute empty string:
430+ if ( $ . trim ( pyInputCodeMirror . getValue ( ) ) == '' ) {
431+ alert ( 'Type in some code to visualize.' ) ;
432+ return ;
369433 }
434+
435+ // reset these globals
436+ rawInputLst = [ ] ;
437+ executeCode ( ) ;
370438 }
371439
372440 function executeCodeWithRawInput ( rawInputStr , curInstr ) {
0 commit comments