Skip to content

Commit 2c1ba8f

Browse files
committed
try to get togetherjs working again when user clicks on examples
1 parent 6b2e3b0 commit 2c1ba8f

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎v3/js/opt-frontend-common.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ function initTogetherJS() {
477477
}
478478
});
479479

480+
TogetherJS.hub.on("syncAppState", function(msg) {
481+
syncAppState(msg.myAppState);
482+
});
483+
480484
TogetherJS.hub.on("codeInputScroll", function(msg) {
481485
// do NOT use a msg.sameUrl guard since that will miss some signals
482486
// due to our funky URLs

‎v3/js/opt-frontend.js‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)