@@ -317,37 +317,48 @@ $(document).ready(function() {
317317 $ ( ".exampleLink" ) . click ( function ( ) {
318318 var myId = $ ( this ) . attr ( 'id' ) ;
319319 var exFile ;
320+ var lang ;
320321 if ( JS_EXAMPLES [ myId ] !== undefined ) {
321322 exFile = JS_EXAMPLES [ myId ] ;
322- $ ( '#pythonVersionSelector' ) . val ( ' js') ;
323+ lang = ' js';
323324 } else if ( JAVA_EXAMPLES [ myId ] !== undefined ) {
324325 exFile = JAVA_EXAMPLES [ myId ] ;
325- $ ( '#pythonVersionSelector' ) . val ( ' java') ;
326+ lang = ' java';
326327 } else if ( PY2_EXAMPLES [ myId ] !== undefined ) {
327328 exFile = PY2_EXAMPLES [ myId ] ;
328329
329330 // only switch Python mode to 2 if we're not on '2' or '3'; otherwise
330331 // leave as-is so as not to rock the boat
331332 if ( $ ( '#pythonVersionSelector' ) . val ( ) !== '2' &&
332333 $ ( '#pythonVersionSelector' ) . val ( ) !== '3' ) {
333- $ ( '#pythonVersionSelector' ) . val ( '2' ) ;
334+ lang = '2' ;
334335 }
335336 } else {
336337 exFile = PY3_EXAMPLES [ myId ] ;
337338 assert ( exFile !== undefined ) ;
338- $ ( '#pythonVersionSelector' ) . val ( '3' ) ;
339+ lang = '3' ;
339340 }
340341
341- // very subtle! for TogetherJS to sync #pythonVersionSelector
342- // properly, we must manually trigger a change event that bubbles
343- //
344- // Inspired by http://stackoverflow.com/questions/2856513/how-can-i-trigger-an-onchange-event-manually
345- // but used jQuery.Event to get cross-browser compatibility
346- $ ( "#pythonVersionSelector" ) . trigger ( jQuery . Event ( 'change' , { 'bubbles' : true } ) ) ;
342+
343+ if ( lang ) {
344+ $ ( '#pythonVersionSelector' ) . val ( lang ) ;
345+ }
347346
348347 $ . get ( exFile , function ( dat ) {
349348 pyInputSetValue ( dat ) ;
350349 initAceAndOptions ( ) ;
350+
351+ // very subtle! for TogetherJS to sync #pythonVersionSelector
352+ // properly, we must manually send a sync request event:
353+ if ( TogetherJS && TogetherJS . running ) {
354+ TogetherJS . send ( { type : "syncAppState" ,
355+ myAppState : getAppState ( ) ,
356+ codeInputScrollTop : pyInputGetScrollTop ( ) ,
357+ pyCodeOutputDivScrollTop : myVisualizer ?
358+ myVisualizer . domRoot . find ( '#pyCodeOutputDiv' ) . scrollTop ( ) :
359+ undefined } ) ;
360+ }
361+
351362 } , 'text' /* data type - set to text or else jQuery tries to EXECUTE the JS example code! */ ) ;
352363 return false ; // prevent 'a' click from going to an actual link
353364 } ) ;
0 commit comments