Skip to content

Commit 75decf9

Browse files
committed
more TogetherJS prep
1 parent 0ff917b commit 75decf9

3 files changed

Lines changed: 158 additions & 63 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,53 @@ function setToggleOptions(dat) {
9797
}
9898

9999

100+
// update the app display based on current state of the appMode global
101+
// TODO: refactor all frontend clients to call this unified function
102+
function updateAppDisplay() {
103+
if (appMode === undefined || appMode == 'edit') {
104+
$("#pyInputPane").show();
105+
$("#pyOutputPane,#surveyHeader").hide();
106+
$("#embedLinkDiv").hide();
107+
108+
$(".surveyQ").val(''); // clear all survey results when user hits forward/back
109+
110+
// destroy all annotation bubbles (NB: kludgy)
111+
if (myVisualizer) {
112+
myVisualizer.destroyAllAnnotationBubbles();
113+
}
114+
}
115+
else if (appMode == 'display' || appMode == 'visualize' /* 'visualize' is deprecated */) {
116+
$("#pyInputPane").hide();
117+
$("#pyOutputPane,#surveyHeader").show();
118+
$("#embedLinkDiv").show();
119+
120+
$('#executeBtn').html("Visualize Execution");
121+
$('#executeBtn').attr('disabled', false);
122+
123+
124+
// do this AFTER making #pyOutputPane visible, or else
125+
// jsPlumb connectors won't render properly
126+
myVisualizer.updateOutput();
127+
128+
// customize edit button click functionality AFTER rendering (NB: awkward!)
129+
$('#pyOutputPane #editCodeLinkDiv').show();
130+
$('#pyOutputPane #editBtn').click(function() {
131+
enterEditMode();
132+
});
133+
}
134+
else if (appMode == 'display_no_frills') {
135+
$("#pyInputPane").hide();
136+
$("#pyOutputPane,#surveyHeader").show();
137+
$("#embedLinkDiv").show();
138+
}
139+
else {
140+
assert(false);
141+
}
142+
143+
$('#urlOutput,#embedCodeOutput').val(''); // clear to avoid stale values
144+
}
145+
146+
100147
function enterDisplayMode() {
101148
$(document).scrollTop(0); // scroll to top to make UX better on small monitors
102149
$.bbq.pushState({ mode: 'display' }, 2 /* completely override other hash strings to keep URL clean */);

‎v3/js/opt-frontend.js‎

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

6065
var TogetherJSConfig_disableWebRTC = true;
6166
var TogetherJSConfig_suppressJoinConfirmation = true;
62-
var updateOutputSignalFromRemote = false;
6367
var TogetherJSConfig_dontShowClicks = true;
6468

6569
// stop popping up boring intro dialog box:
@@ -69,10 +73,13 @@ var TogetherJSConfig_seenIntroDialog = true;
6973
var TogetherJSConfig_suppressInvite = true;
7074
var 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+
7280
var TogetherJSConfig_siteName = "Online Python Tutor live help";
7381
var 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+
86103
function 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) {

‎v3/js/togetherjs/togetherjs/togetherjs.css‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,13 @@ input.togetherjs-upload-avatar {
638638
overflow-y: auto;
639639
/*FIXME: I'm just pulling numbers out of thin air here:*/
640640

641-
height: 260px;
641+
/* pgbovine -- make a bit smaller */
642+
643+
height: 180px;
644+
/*
645+
height: 260px;
646+
*/
647+
642648
}
643649
.togetherjs #togetherjs-feedback-form > header {
644650
font-weight: bold;
@@ -762,7 +768,13 @@ input.togetherjs-upload-avatar {
762768
overflow-y: auto;
763769
/*FIXME: I'm just pulling numbers out of thin air here:*/
764770

765-
height: 260px;
771+
/* pgbovine -- make a bit smaller */
772+
773+
height: 180px;
774+
/*
775+
height: 260px;
776+
*/
777+
766778
}
767779
.togetherjs .togetherjs-notification:hover {
768780
background: #f2f2f2;
@@ -1489,7 +1501,13 @@ section.togetherjs-buttons .togetherjs-same-url {
14891501
overflow-y: auto;
14901502
/*FIXME: I'm just pulling numbers out of thin air here:*/
14911503

1492-
height: 260px;
1504+
/* pgbovine -- make a bit smaller */
1505+
1506+
height: 180px;
1507+
/*
1508+
height: 260px;
1509+
*/
1510+
14931511
}
14941512
.togetherjs .togetherjs-modal.togetherjs-modal-wide {
14951513
width: 550px;

0 commit comments

Comments
 (0)