@@ -120,187 +120,188 @@ function setVisibleAppState(appState) {
120120}
121121
122122
123+ // get this app ready for TogetherJS
123124function initTogetherJS ( ) {
124- $ ( "#surveyHeader" ) . remove ( ) ; // kill this to save space
125+ $ ( "#surveyHeader" ) . remove ( ) ; // kill this to save space
125126
126- var role = $ . bbq . getState ( 'role' ) ;
127- isTutor = ( role == 'tutor' ) ; // GLOBAL
127+ var role = $ . bbq . getState ( 'role' ) ;
128+ isTutor = ( role == 'tutor' ) ; // GLOBAL
128129
129- if ( isTutor ) {
130- $ ( "#togetherBtn" ) . html ( "TUTOR - Join live help session" ) ;
131- $ ( "#togetherJSHeader" ) . append ( '<button id="syncBtn"\
132- type="button">Sync with learner</button>' ) ;
133-
134- $ ( "#syncBtn" ) . click ( function ( ) {
135- console . log ( "Sync!" ) ;
136-
137- if ( TogetherJS . running ) {
138- nowSyncing = true ;
139- TogetherJS . send ( { type : "requestSync" } ) ;
140- }
141- } ) ;
142- }
130+ if ( isTutor ) {
131+ $ ( "#togetherBtn" ) . html ( "TUTOR - Join live help session" ) ;
132+ $ ( "#togetherJSHeader" ) . append ( '<button id="syncBtn"\
133+ type="button">Sync with learner</button>' ) ;
143134
135+ $ ( "#syncBtn" ) . click ( function ( ) {
136+ console . log ( "Sync!" ) ;
144137
145- // Global hook for ExecutionVisualizer.
146- // Set it here after TogetherJS gets defined, hopefully!
147- try_hook = function ( hook_name , args ) {
148- if ( hook_name == "end_updateOutput" ) {
149- if ( updateOutputSignalFromRemote ) {
150- return ;
151- }
152- if ( TogetherJS . running && ! nowSyncing /* don't perterb when syncing */ ) {
153- TogetherJS . send ( { type : "updateOutput" , step : args . myViz . curInstr } ) ;
154- }
138+ if ( TogetherJS . running ) {
139+ nowSyncing = true ;
140+ TogetherJS . send ( { type : "requestSync" } ) ;
155141 }
156- return [ false ] ;
157- }
142+ } ) ;
143+ }
144+
158145
159- TogetherJS . hub . on ( "updateOutput" , function ( msg ) {
160- if ( ! msg . sameUrl ) {
146+ // Global hook for ExecutionVisualizer.
147+ // Set it here after TogetherJS gets defined, hopefully!
148+ try_hook = function ( hook_name , args ) {
149+ if ( hook_name == "end_updateOutput" ) {
150+ if ( updateOutputSignalFromRemote ) {
161151 return ;
162152 }
163- if ( myVisualizer ) {
164- console . log ( "updateOutput:" , msg ) ;
165- // to prevent this call to updateOutput from firing its own TogetherJS event
166- updateOutputSignalFromRemote = true ;
167- try {
168- myVisualizer . renderStep ( msg . step ) ;
169- }
170- finally {
171- updateOutputSignalFromRemote = false ;
172- }
153+ if ( TogetherJS . running && ! nowSyncing /* don't perterb when syncing */ ) {
154+ TogetherJS . send ( { type : "updateOutput" , step : args . myViz . curInstr } ) ;
173155 }
174- } ) ;
156+ }
157+ return [ false ] ;
158+ }
175159
176- TogetherJS . hub . on ( "hashchange" , function ( msg ) {
177- if ( ! msg . sameUrl ) {
178- return ;
179- }
180- hashchangeSignalFromRemote = true ;
160+ TogetherJS . hub . on ( "updateOutput" , function ( msg ) {
161+ if ( ! msg . sameUrl ) {
162+ return ;
163+ }
164+ if ( myVisualizer ) {
165+ console . log ( "updateOutput:" , msg ) ;
166+ // to prevent this call to updateOutput from firing its own TogetherJS event
167+ updateOutputSignalFromRemote = true ;
181168 try {
182- console . log ( "TogetherJS RECEIVE hashchange" , msg . appMode ) ;
183- appMode = msg . appMode ; // assign this to the GLOBAL appMode
184- updateAppDisplay ( ) ;
169+ myVisualizer . renderStep ( msg . step ) ;
185170 }
186171 finally {
187- hashchangeSignalFromRemote = false ;
188- }
189- } ) ;
190-
191- // learner receives a sync request from tutor and responds by
192- // sending its current app state
193- TogetherJS . hub . on ( "requestSync" , function ( msg ) {
194- // only a learner should receive sync requests from a tutor
195- if ( isTutor ) {
196- return ;
172+ updateOutputSignalFromRemote = false ;
197173 }
174+ }
175+ } ) ;
198176
199- if ( TogetherJS . running ) {
200- TogetherJS . send ( { type : "myAppState" , myAppState : getAppState ( ) } ) ;
201- }
202- } ) ;
177+ TogetherJS . hub . on ( "hashchange" , function ( msg ) {
178+ if ( ! msg . sameUrl ) {
179+ return ;
180+ }
181+ hashchangeSignalFromRemote = true ;
182+ try {
183+ console . log ( "TogetherJS RECEIVE hashchange" , msg . appMode ) ;
184+ appMode = msg . appMode ; // assign this to the GLOBAL appMode
185+ updateAppDisplay ( ) ;
186+ }
187+ finally {
188+ hashchangeSignalFromRemote = false ;
189+ }
190+ } ) ;
203191
204- // tutor receives an app state from a learner
205- // TODO: what happens if more than one learner sends state to tutor?
206- // i suppose the last one wins at this point :/
207- TogetherJS . hub . on ( "myAppState" , function ( msg ) {
208- // only a tutor should handle receiving an app state from a learner
209- if ( ! isTutor ) {
210- return ;
211- }
192+ // learner receives a sync request from tutor and responds by
193+ // sending its current app state
194+ TogetherJS . hub . on ( "requestSync" , function ( msg ) {
195+ // only a learner should receive sync requests from a tutor
196+ if ( isTutor ) {
197+ return ;
198+ }
212199
213- try {
214- var learnerAppState = msg . myAppState ;
200+ if ( TogetherJS . running ) {
201+ TogetherJS . send ( { type : "myAppState" , myAppState : getAppState ( ) } ) ;
202+ }
203+ } ) ;
215204
216- // if available, first set my own app state to visualizedAppState
217- // and then EXECUTE that code with the given options to create the
218- // proper myVisualizer object. then adjust app state to the rest
219- // of learnerAppState
205+ // tutor receives an app state from a learner
206+ // TODO: what happens if more than one learner sends state to tutor?
207+ // i suppose the last one wins at this point :/
208+ TogetherJS . hub . on ( "myAppState" , function ( msg ) {
209+ // only a tutor should handle receiving an app state from a learner
210+ if ( ! isTutor ) {
211+ return ;
212+ }
220213
221- if ( learnerAppState . visualizedAppState ) {
222- setVisibleAppState ( learnerAppState . visualizedAppState ) ;
223- // execute code and jump to the learner's curInstr
224- executeCode ( learnerAppState . curInstr ) ;
225- }
214+ try {
215+ var learnerAppState = msg . myAppState ;
226216
227- setVisibleAppState ( learnerAppState ) ;
217+ // if available, first set my own app state to visualizedAppState
218+ // and then EXECUTE that code with the given options to create the
219+ // proper myVisualizer object. then adjust app state to the rest
220+ // of learnerAppState
228221
229- appMode = learnerAppState . mode ;
230- updateAppDisplay ( ) ;
231- }
232- finally {
233- nowSyncing = false ; // done with a successful sync round trip
222+ if ( learnerAppState . visualizedAppState ) {
223+ setVisibleAppState ( learnerAppState . visualizedAppState ) ;
224+ // execute code and jump to the learner's curInstr
225+ executeCode ( learnerAppState . curInstr ) ;
234226 }
235- } ) ;
236-
237227
238- try {
239- var source = new EventSource ( 'http://togetherjs.pythontutor.com/learner-SSE' ) ;
240- source . onmessage = function ( e ) {
241- var dat = JSON . parse ( e . data ) ;
228+ setVisibleAppState ( learnerAppState ) ;
242229
243- if ( dat . helpQueueUrls == 1 ) {
244- $ ( "#helpQueueUrls" ) . html ( 'There is 1 person waiting in the queue.' ) ;
245- }
246- else if ( dat . helpQueueUrls == 0 || dat . helpQueueUrls > 1 ) {
247- $ ( "#helpQueueUrls" ) . html ( 'There are ' + dat . helpQueueUrls +
248- ' people waiting in the queue.' ) ;
249- }
250- else {
251- $ ( "#helpQueueUrls" ) . html ( '' ) ;
252- }
253-
254- if ( dat . helpAvailable ) {
255- $ ( "#togetherJSHeader" ) . fadeIn ( 750 , redrawConnectors ) ;
256- }
257- else {
258- if ( TogetherJS . running ) {
259- alert ( "No more live tutors are available now.\nPlease check back later." ) ;
260- TogetherJS ( ) ; // toggle off
261- }
262- $ ( "#togetherJSHeader" ) . fadeOut ( 750 , redrawConnectors ) ;
263- }
264- } ;
230+ appMode = learnerAppState . mode ;
231+ updateAppDisplay ( ) ;
265232 }
266- catch ( err ) {
267- // ugh, SSE doesn't work in Safari when testing on localhost,
268- // but I think it works when deployed on pythontutor.com
269- console . warn ( "Sad ... EventSource not supported :(" ) ;
233+ finally {
234+ nowSyncing = false ; // done with a successful sync round trip
270235 }
236+ } ) ;
271237
272- $ ( "#togetherBtn" ) . click ( function ( ) {
273- TogetherJS ( ) ; // toggles on and off
274- } ) ;
275-
276- // fired when TogetherJS is activated. might fire on page load if there's
277- // already an open session from a prior page load in the recent past.
278- TogetherJS . on ( "ready" , function ( ) {
279- console . log ( "TogetherJS ready" ) ;
280- $ ( "#togetherBtn" ) . html ( "Stop live help session" ) ;
281238
282- $ ( "#togetherJSHeader" ) . fadeIn ( 750 , redrawConnectors ) ; // always show when ready
239+ try {
240+ var source = new EventSource ( 'http://togetherjs.pythontutor.com/learner-SSE' ) ;
241+ source . onmessage = function ( e ) {
242+ var dat = JSON . parse ( e . data ) ;
283243
284- if ( ! isTutor ) {
285- $ . get ( "http://togetherjs.pythontutor.com/request-help" ,
286- { url : TogetherJS . shareUrl ( ) } ,
287- null /* don't use a callback; rely on SSE */ ) ;
244+ if ( dat . helpQueueUrls == 1 ) {
245+ $ ( "#helpQueueUrls" ) . html ( 'There is 1 person waiting in the queue.' ) ;
246+ }
247+ else if ( dat . helpQueueUrls == 0 || dat . helpQueueUrls > 1 ) {
248+ $ ( "#helpQueueUrls" ) . html ( 'There are ' + dat . helpQueueUrls +
249+ ' people waiting in the queue.' ) ;
250+ }
251+ else {
252+ $ ( "#helpQueueUrls" ) . html ( '' ) ;
288253 }
289- } ) ;
290-
291- // emitted when TogetherJS is closed. This is not emitted when the
292- // page simply closes or navigates elsewhere. It is only closed when
293- // TogetherJS is specifically stopped.
294- TogetherJS . on ( "close" , function ( ) {
295- console . log ( "TogetherJS close" ) ;
296254
297- if ( isTutor ) {
298- $ ( "#togetherBtn " ) . html ( "TUTOR - Join live help session" ) ;
255+ if ( dat . helpAvailable ) {
256+ $ ( "#togetherJSHeader " ) . fadeIn ( 750 , redrawConnectors ) ;
299257 }
300258 else {
301- $ ( "#togetherBtn" ) . html ( "Get live help now" ) ;
259+ if ( TogetherJS . running ) {
260+ alert ( "No more live tutors are available now.\nPlease check back later." ) ;
261+ TogetherJS ( ) ; // toggle off
262+ }
263+ $ ( "#togetherJSHeader" ) . fadeOut ( 750 , redrawConnectors ) ;
302264 }
303- } ) ;
265+ } ;
266+ }
267+ catch ( err ) {
268+ // ugh, SSE doesn't work in Safari when testing on localhost,
269+ // but I think it works when deployed on pythontutor.com
270+ console . warn ( "Sad ... EventSource not supported :(" ) ;
271+ }
272+
273+ $ ( "#togetherBtn" ) . click ( function ( ) {
274+ TogetherJS ( ) ; // toggles on and off
275+ } ) ;
276+
277+ // fired when TogetherJS is activated. might fire on page load if there's
278+ // already an open session from a prior page load in the recent past.
279+ TogetherJS . on ( "ready" , function ( ) {
280+ console . log ( "TogetherJS ready" ) ;
281+ $ ( "#togetherBtn" ) . html ( "Stop live help session" ) ;
282+
283+ $ ( "#togetherJSHeader" ) . fadeIn ( 750 , redrawConnectors ) ; // always show when ready
284+
285+ if ( ! isTutor ) {
286+ $ . get ( "http://togetherjs.pythontutor.com/request-help" ,
287+ { url : TogetherJS . shareUrl ( ) } ,
288+ null /* don't use a callback; rely on SSE */ ) ;
289+ }
290+ } ) ;
291+
292+ // emitted when TogetherJS is closed. This is not emitted when the
293+ // page simply closes or navigates elsewhere. It is only closed when
294+ // TogetherJS is specifically stopped.
295+ TogetherJS . on ( "close" , function ( ) {
296+ console . log ( "TogetherJS close" ) ;
297+
298+ if ( isTutor ) {
299+ $ ( "#togetherBtn" ) . html ( "TUTOR - Join live help session" ) ;
300+ }
301+ else {
302+ $ ( "#togetherBtn" ) . html ( "Get live help now" ) ;
303+ }
304+ } ) ;
304305}
305306
306307
0 commit comments