Skip to content

Commit 6780c89

Browse files
committed
simplified togetherjs code greatly
1 parent f27c22f commit 6780c89

2 files changed

Lines changed: 38 additions & 108 deletions

File tree

‎v3/js/opt-frontend.js‎

Lines changed: 35 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ var TogetherJSConfig_toolName = "Online Python Tutor live help";
6363
//var TogetherJSConfig_hubBase = "http://184.173.101.176:30035/"; // online
6464
var TogetherJSConfig_hubBase = "http://localhost:30035/"; // local
6565

66-
// TODO: generalize beyond tutor to "joiner" or something, since we
67-
// can imagine other learners joining into the original session as well
68-
var isTutor = false;
69-
70-
var manualSharedSession = false; // XXX: this is uber-unreliable since
71-
// it's not set on page reload unless
72-
// there's a special url hash
66+
var tutorRequested = false;
7367

7468
var helpQueueSize = 0;
7569
var tutorAvailable = false;
@@ -85,7 +79,7 @@ var tutorAvailable = false;
8579
var origDocURL = document.URL; // capture this ASAP before TogetherJS munges the URL
8680

8781
var tutorWaitText = 'Please wait for the next available tutor.';
88-
var informedConsentText = '<p>During shared sessions, chat logs and code may be recorded and published for<br/>educational purposes. Do not reveal any private or confidential information.</p>';
82+
var informedConsentText = '<div style="color: #666;">During shared sessions, chat logs and code may be recorded and published for<br/>educational purposes. Do not reveal any private or confidential information.</div>';
8983

9084
// nasty globals
9185
var updateOutputSignalFromRemote = false;
@@ -128,35 +122,14 @@ function syncAppState(appState) {
128122
}
129123
}
130124

131-
/*
132-
var TogetherJSConfig_getUserName = function() {
133-
if (isTutor) {
134-
return 'Tutor';
135-
}
136-
else {
137-
return 'Learner';
138-
}
139-
}
140-
*/
141-
142-
143-
function getTutor() {
144-
manualSharedSession = false; // icky global!
145-
146-
$("#togetherjsStatus").html('<div style="font-size: 11pt;">Please wait for the next available tutor. <span id="helpQueueText"></span></div>');
147125

126+
function requestTutor() {
127+
tutorRequested = true;
148128
TogetherJS();
149129
}
150130

151131
function startSharedSession() {
152-
manualSharedSession = true; // icky global!
153-
154-
$("#togetherjsStatus").html('<div style="font-size: 11pt;">\
155-
Copy and send this URL to let others join your session:\
156-
</div>\
157-
<input type="text" style="font-size:\
158-
12pt; font-weight: bold; padding: 5px;" id="togetherjsURL"\
159-
size="80" readonly="readonly"/>');
132+
tutorRequested = false;
160133
TogetherJS();
161134
}
162135

@@ -165,24 +138,13 @@ function setHelpQueueSizeLabel() {
165138
$("#helpQueueText").html('There is 1 person in line.');
166139
}
167140
else if (helpQueueSize == 0 || helpQueueSize > 1) {
168-
$("#helpQueueText").html('There are ' + dat.helpQueueUrls + ' people in line.');
141+
$("#helpQueueText").html('There are ' + helpQueueSize + ' people in line.');
169142
}
170143
}
171144

172145

173146
// get this app ready for TogetherJS
174147
function initTogetherJS() {
175-
if (isTutor) {
176-
$("#getTutorBtn").html("TUTOR - Join live help session");
177-
/*
178-
$("#togetherJSHeader").append('<button id="syncBtn"\
179-
type="button">Sync with learner</button>');
180-
$("#syncBtn").click(requestSync);
181-
*/
182-
183-
$("#sharedSessionBtn").hide();
184-
}
185-
186148
// This event triggers when you first join a session and say 'hello',
187149
// and then one of your peers says hello back to you. If they have the
188150
// exact same name as you, then change your own name to avoid ambiguity.
@@ -328,14 +290,7 @@ function initTogetherJS() {
328290
}, 1000);
329291
});
330292

331-
// learner receives a sync request from tutor and responds by
332-
// sending its current app state
333293
TogetherJS.hub.on("requestSync", function(msg) {
334-
// only a learner should receive sync requests from a tutor
335-
if (isTutor) {
336-
return;
337-
}
338-
339294
if (TogetherJS.running) {
340295
TogetherJS.send({type: "myAppState",
341296
myAppState: getAppState(),
@@ -346,17 +301,7 @@ function initTogetherJS() {
346301
}
347302
});
348303

349-
// tutor receives an app state from a learner, either using a regular
350-
// myAppState or an initialAppState signal, which occurs when the
351-
// learner, say, REFRESHES THE PAGE!!!
352-
// TODO: what happens if more than one learner sends state to tutor?
353-
// i suppose the last one wins at this point :/
354-
TogetherJS.hub.on("myAppState initialAppState", function(msg) {
355-
// only a tutor should handle receiving an app state from a learner
356-
if (!isTutor) {
357-
return;
358-
}
359-
304+
TogetherJS.hub.on("myAppState", function(msg) {
360305
var learnerAppState = msg.myAppState;
361306

362307
if (learnerAppState.mode == 'display') {
@@ -439,12 +384,11 @@ function initTogetherJS() {
439384
};
440385
}
441386
catch(err) {
442-
// ugh, SSE doesn't work in Safari when testing on localhost,
443-
// but I think it works when deployed on pythontutor.com (maybe?)
387+
// ugh, SSE doesn't seem to work in Safari
444388
console.warn("Sad ... EventSource not supported :(");
445389
}
446390

447-
$("#getTutorBtn").click(getTutor);
391+
$("#getTutorBtn").click(requestTutor);
448392
$("#sharedSessionBtn").click(startSharedSession);
449393
$("#stopTogetherJSBtn").click(TogetherJS); // toggles off
450394

@@ -456,32 +400,36 @@ function initTogetherJS() {
456400
$("#surveyHeader").hide();
457401
$("#stopTogetherJSBtn").show();
458402

459-
$("#togetherjsStatus").append(informedConsentText).fadeIn(500);
403+
// without anything after the '#' in the hash
404+
var cleanUrl = $.param.fragment(location.href, {}, 2 /* override */);
405+
var urlToShare = cleanUrl + 'togetherjs=' + TogetherJS.shareId();
406+
407+
requestSync(); // immediately try to sync upon startup so that if
408+
// others are already in the session, we will be
409+
// synced up. and if nobody is here, then this is a NOP.
460410

461-
if (isTutor) {
462-
$("#togetherjsStatus").html(origDocURL);
463-
// if you're a tutor, immediately try to sync to the learner upon startup
464-
requestSync();
411+
if (tutorRequested) {
412+
$.get(TogetherJSConfig_hubBase + 'request-help',
413+
{url: TogetherJS.shareUrl(), id: TogetherJS.shareId()},
414+
null /* don't use a callback; rely on SSE */);
415+
416+
$("#togetherjsStatus").html('<div style="font-size: 11pt; margin-bottom: 5pt;">\
417+
Please wait for the next available tutor. \
418+
<span id="helpQueueText"></span></div>');
419+
setHelpQueueSizeLabel(); // run after creating span#helpQueueText
465420
}
466421
else {
467-
if (manualSharedSession) {
468-
// without anything after the '#' in the hash
469-
var cleanUrl = $.param.fragment(location.href, {}, 2 /* override */);
470-
var urlToShare = cleanUrl + 'togetherjs=' + TogetherJS.shareId();
471-
$("#togetherjsURL").val(urlToShare).attr('size', urlToShare.length + 15);
472-
}
473-
else {
474-
// if you're a learner, request help when TogetherJS is activated
475-
$.get(TogetherJSConfig_hubBase + 'request-help',
476-
{url: TogetherJS.shareUrl(), id: TogetherJS.shareId()},
477-
null /* don't use a callback; rely on SSE */);
478-
}
479-
480-
// also log the learner's initial state when they first requested help
481-
TogetherJS.send({type: "initialAppState", myAppState: getAppState()});
422+
$("#togetherjsStatus").html('<div>\
423+
Copy and send this URL to let others join your session:\
424+
</div>\
425+
<input type="text" style="font-size: 12pt; \
426+
font-weight: bold; padding: 5px;\
427+
margin-bottom: 10pt;" \
428+
id="togetherjsURL" size="80" readonly="readonly"/>');
429+
$("#togetherjsURL").val(urlToShare).attr('size', urlToShare.length + 15);
482430
}
483431

484-
setHelpQueueSizeLabel();
432+
$("#togetherjsStatus").append(informedConsentText).fadeIn(500);
485433
});
486434

487435
// emitted when TogetherJS is closed. This is not emitted when the
@@ -614,12 +562,7 @@ function optFinishSuccessfulExecution() {
614562
$(document).ready(function() {
615563
setSurveyHTML();
616564

617-
var role = $.bbq.getState('role');
618-
isTutor = (role == 'tutor'); // GLOBAL
619-
620-
manualSharedSession = ($.bbq.getState('share') == 'manual'); // GLOBAL
621-
622-
if (enableTogetherJS || isTutor) {
565+
if (enableTogetherJS) {
623566
initTogetherJS();
624567
}
625568

@@ -972,18 +915,6 @@ $(document).ready(function() {
972915

973916
genericOptFrontendReady(); // initialize at the end
974917

975-
// if blank, then select a canned example on start-up
976-
// (need to do this after pyInputCodeMirror is initialized)
977-
// TODO: generalize beyond tutor to "joiner" or something, since we
978-
// can imagine other learners joining into the original session as well
979-
if (!isTutor) { // don't pre-seed since things will go screwy when the
980-
// input box automatically syncs
981-
if (!pyInputCodeMirror.getValue()) {
982-
$("#aliasExampleLink").trigger('click');
983-
}
984-
}
985-
986-
987918
pyInputCodeMirror.on("change", function(cm, change) {
988919
// only trigger when the user explicitly typed something
989920
if (change.origin != 'setValue') {

‎v3/visualize.html‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
<tr>
9898
<td valign="top">
9999

100-
Experimental features:
100+
<div style="font-size: 8pt; color: #666; margin-left: 2px;">Experimental features</div>
101101

102102
<div>
103103
<button id="sharedSessionBtn" type="button" class="togetherjsBtn">
@@ -171,7 +171,7 @@
171171
</p>
172172

173173
<p style="margin-top: 20px; line-height: 200%;">
174-
Options:<br/> Execute code using
174+
Execute code using
175175
<select id="pythonVersionSelector">
176176
<option value="2">Python 2.7</option>
177177
<option value="3">Python 3.3</option>
@@ -201,8 +201,7 @@
201201
</p>
202202

203203

204-
205-
<p style="margin-top: 35px;">Here are some example Python code snippets to visualize:</p>
204+
<p style="margin-top: 25px;">Here are some example Python code snippets to visualize:</p>
206205

207206
<p style="margin-top: 10px;">Basic:
208207

0 commit comments

Comments
 (0)