@@ -27,6 +27,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727*/
2828
2929
30+ // TODO: combine and minify with https://github.com/mishoo/UglifyJS2
31+ // and add version numbering using a ?-style query string to prevent
32+ // caching snafus
33+
34+
3035// Pre-reqs:
3136// - pytutor.js
3237// - jquery.ba-bbq.min.js
@@ -59,7 +64,6 @@ var enableTogetherJS = false;
5964
6065var TogetherJSConfig_disableWebRTC = true ;
6166var TogetherJSConfig_suppressJoinConfirmation = true ;
62- var updateOutputSignalFromRemote = false ;
6367var TogetherJSConfig_dontShowClicks = true ;
6468
6569// stop popping up boring intro dialog box:
@@ -69,10 +73,13 @@ var TogetherJSConfig_seenIntroDialog = true;
6973var TogetherJSConfig_suppressInvite = true ;
7074var TogetherJSConfig_suppressJoinConfirmation = true ;
7175
76+ // only clone clicks in the input pane to keep things simple and prevent
77+ // duplicate clicks on, say, the visualizer pane
78+ var TogetherJSConfig_cloneClicks = '#pyInputPane' ;
79+
7280var TogetherJSConfig_siteName = "Online Python Tutor live help" ;
7381var TogetherJSConfig_toolName = "Online Python Tutor live help" ;
7482
75- var togetherJsHelpAvailable = false ;
7683
7784// TODO: consider deferred initialization later: "TogetherJS starts up
7885// automatically as soon as it can, especially when continuing a
@@ -83,6 +90,16 @@ var togetherJsHelpAvailable = false;
8390//};
8491
8592
93+ // nasty globals
94+ var updateOutputSignalFromRemote = false ;
95+ var hashchangeSignalFromRemote = false ;
96+
97+ // Global hook for ExecutionVisualizer.
98+ var try_hook = function ( hook_name , args ) {
99+ return [ false ] ; // just a stub
100+ }
101+
102+
86103function getAppState ( ) {
87104 return { code : pyInputCodeMirror . getValue ( ) ,
88105 mode : appMode ,
@@ -113,15 +130,67 @@ $(document).ready(function() {
113130
114131
115132 if ( enableTogetherJS ) {
116- // TODO: in CSS file, make chat window shorter to save vertical space
117- //.togetherjs .togetherjs-window #togetherjs-chat-messages
133+ $ ( "#surveyHeader" ) . remove ( ) ; // kill this to save space
118134
119135 var role = $ . bbq . getState ( 'role' ) ;
120136 var isTutor = ( role == 'tutor' ) ;
121137 if ( isTutor ) {
122138 $ ( "#togetherBtn" ) . html ( "TUTOR - Join live help session" ) ;
139+ $ ( "#togetherJSHeader" ) . append ( '<button id="syncBtn"\
140+ type="button">Sync with learner</button>' ) ;
141+
142+ $ ( "#syncBtn" ) . click ( function ( ) {
143+ console . log ( "Sync!" ) ;
144+ } ) ;
123145 }
124146
147+
148+ // Global hook for ExecutionVisualizer.
149+ // Set it here after TogetherJS gets defined, hopefully!
150+ try_hook = function ( hook_name , args ) {
151+ if ( hook_name == "end_updateOutput" ) {
152+ if ( updateOutputSignalFromRemote ) {
153+ return ;
154+ }
155+ if ( TogetherJS . running ) {
156+ TogetherJS . send ( { type : "updateOutput" , step : args . myViz . curInstr } ) ;
157+ }
158+ }
159+ return [ false ] ;
160+ }
161+
162+ TogetherJS . hub . on ( "updateOutput" , function ( msg ) {
163+ if ( ! msg . sameUrl ) {
164+ return ;
165+ }
166+ if ( myVisualizer ) {
167+ console . log ( "updateOutput:" , msg ) ;
168+ // to prevent this call to updateOutput from firing its own TogetherJS event
169+ updateOutputSignalFromRemote = true ;
170+ try {
171+ myVisualizer . renderStep ( msg . step ) ;
172+ }
173+ finally {
174+ updateOutputSignalFromRemote = false ;
175+ }
176+ }
177+ } ) ;
178+
179+ TogetherJS . hub . on ( "hashchange" , function ( msg ) {
180+ if ( ! msg . sameUrl ) {
181+ return ;
182+ }
183+ hashchangeSignalFromRemote = true ;
184+ try {
185+ appMode = msg . appMode ; // assign this to the GLOBAL appMode
186+ updateAppDisplay ( ) ;
187+ }
188+ finally {
189+ hashchangeSignalFromRemote = false ;
190+ }
191+ } ) ;
192+
193+
125194 try {
126195 var source = new EventSource ( 'http://togetherjs.pythontutor.com/learner-SSE' ) ;
127196 source . onmessage = function ( e ) {
@@ -140,16 +209,13 @@ $(document).ready(function() {
140209
141210 if ( dat . helpAvailable ) {
142211 $ ( "#togetherJSHeader" ) . fadeIn ( 750 ) ;
143- togetherJsHelpAvailable = true ;
144- $ ( "#surveyHeader" ) . hide ( ) ; // to save space
145212 }
146213 else {
147214 if ( TogetherJS . running ) {
148215 alert ( "No more live tutors are available now.\nPlease check back later." ) ;
149216 TogetherJS ( ) ; // toggle off
150217 }
151218 $ ( "#togetherJSHeader" ) . fadeOut ( 750 ) ;
152- togetherJsHelpAvailable = false ;
153219 }
154220 } ;
155221 }
@@ -196,54 +262,14 @@ $(document).ready(function() {
196262 // thanks to http://benalman.com/projects/jquery-bbq-plugin/
197263 $ ( window ) . bind ( "hashchange" , function ( e ) {
198264 appMode = $ . bbq . getState ( 'mode' ) ; // assign this to the GLOBAL appMode
199- //console.log("hashchange", appMode);
200-
201- if ( appMode === undefined || appMode == 'edit' ) {
202- $ ( "#pyInputPane" ) . show ( ) ;
203- $ ( "#pyOutputPane,#surveyHeader" ) . hide ( ) ;
204- $ ( "#embedLinkDiv" ) . hide ( ) ;
265+ updateAppDisplay ( ) ;
205266
206- $ ( ".surveyQ" ) . val ( '' ) ; // clear all survey results when user hits forward/back
207-
208- // destroy all annotation bubbles (NB: kludgy)
209- if ( myVisualizer ) {
210- myVisualizer . destroyAllAnnotationBubbles ( ) ;
267+ if ( enableTogetherJS ) {
268+ if ( TogetherJS . running &&
269+ ! hashchangeSignalFromRemote /* don't double send */ ) {
270+ TogetherJS . send ( { type : "hashchange" , appMode : appMode } ) ;
211271 }
212272 }
213- else if ( appMode == 'display' || appMode == 'visualize' /* 'visualize' is deprecated */ ) {
214- $ ( "#pyInputPane" ) . hide ( ) ;
215- $ ( "#pyOutputPane,#surveyHeader" ) . show ( ) ;
216- $ ( "#embedLinkDiv" ) . show ( ) ;
217-
218- $ ( '#executeBtn' ) . html ( "Visualize Execution" ) ;
219- $ ( '#executeBtn' ) . attr ( 'disabled' , false ) ;
220-
221-
222- // do this AFTER making #pyOutputPane visible, or else
223- // jsPlumb connectors won't render properly
224- myVisualizer . updateOutput ( ) ;
225-
226- // customize edit button click functionality AFTER rendering (NB: awkward!)
227- $ ( '#pyOutputPane #editCodeLinkDiv' ) . show ( ) ;
228- $ ( '#pyOutputPane #editBtn' ) . click ( function ( ) {
229- enterEditMode ( ) ;
230- } ) ;
231- }
232- else if ( appMode == 'display_no_frills' ) {
233- $ ( "#pyInputPane" ) . hide ( ) ;
234- $ ( "#pyOutputPane,#surveyHeader" ) . show ( ) ;
235- $ ( "#embedLinkDiv" ) . show ( ) ;
236- }
237- else {
238- assert ( false ) ;
239- }
240-
241-
242- if ( togetherJsHelpAvailable ) {
243- $ ( "#surveyHeader" ) . hide ( ) ; // to save space
244- }
245-
246- $ ( '#urlOutput,#embedCodeOutput' ) . val ( '' ) ; // clear to avoid stale values
247273 } ) ;
248274
249275
@@ -339,15 +365,19 @@ $(document).ready(function() {
339365 }
340366
341367 function executeCodeFromScratch ( ) {
342- // don't execute empty string:
343- if ( $ . trim ( pyInputCodeMirror . getValue ( ) ) == '' ) {
344- alert ( 'Type in some code to visualize.' ) ;
345- return ;
346- }
368+ // prevent the weird double-execute situation where someone (such as
369+ // a remote collaborator) "clicks" executeBtn while it's disabled
370+ if ( ! $ ( "#executeBtn" ) . attr ( 'disabled' ) ) {
371+ // don't execute empty string:
372+ if ( $ . trim ( pyInputCodeMirror . getValue ( ) ) == '' ) {
373+ alert ( 'Type in some code to visualize.' ) ;
374+ return ;
375+ }
347376
348- // reset these globals
349- rawInputLst = [ ] ;
350- executeCode ( ) ;
377+ // reset these globals
378+ rawInputLst = [ ] ;
379+ executeCode ( ) ;
380+ }
351381 }
352382
353383 function executeCodeWithRawInput ( rawInputStr , curInstr ) {
0 commit comments