Skip to content

Commit 9604140

Browse files
committed
handled copy-and-paste of URLs over existing pages
1 parent a221c30 commit 9604140

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

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

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,17 @@ function genericOptFrontendReady() {
497497
// be friendly to the browser's forward and back buttons
498498
// thanks to http://benalman.com/projects/jquery-bbq-plugin/
499499
$(window).bind("hashchange", function(e) {
500-
var newMode = $.bbq.getState('mode');
501-
console.log('hashchange:', newMode);
502-
updateAppDisplay(newMode);
500+
// if you've got some preseeded code, then parse the entire query
501+
// string from scratch just like a page reload
502+
if ($.bbq.getState('code')) {
503+
parseQueryString();
504+
}
505+
// otherwise just do an incremental update
506+
else {
507+
var newMode = $.bbq.getState('mode');
508+
console.log('hashchange:', newMode, window.location.hash);
509+
updateAppDisplay(newMode);
510+
}
503511

504512
if (TogetherJS.running && !isExecutingCode) {
505513
TogetherJS.send({type: "hashchange",
@@ -577,30 +585,7 @@ function genericOptFrontendReady() {
577585
});
578586
}
579587

580-
// initialize from query string
581-
var queryStrOptions = getQueryStringOptions();
582-
setToggleOptions(queryStrOptions);
583-
if (queryStrOptions.preseededCode) {
584-
setCodeMirrorVal(queryStrOptions.preseededCode);
585-
}
586-
if (queryStrOptions.rawInputLst) {
587-
rawInputLst = queryStrOptions.rawInputLst; // global
588-
}
589-
else {
590-
rawInputLst = [];
591-
}
592-
593-
// ugh tricky -- always start in edit mode by default, and then
594-
// simulate a click to get it to switch to display mode ONLY after the
595-
// code successfully executes
596-
appMode = 'edit';
597-
if ((queryStrOptions.appMode == 'display' ||
598-
queryStrOptions.appMode == 'visualize' /* 'visualize' is deprecated */) &&
599-
queryStrOptions.preseededCode /* jump to display only with pre-seeded code */) {
600-
console.log('seeded in display mode');
601-
executeCode(queryStrOptions.preseededCurInstr);
602-
}
603-
$.bbq.removeState(); // clean up the URL no matter what
588+
parseQueryString();
604589

605590
$(window).resize(redrawConnectors);
606591

@@ -633,6 +618,32 @@ function genericOptFrontendReady() {
633618
$("#executeBtn").click(executeCodeFromScratch);
634619
}
635620

621+
622+
// sets globals such as rawInputLst, CodeMirror input box, and toggle options
623+
function parseQueryString() {
624+
var queryStrOptions = getQueryStringOptions();
625+
setToggleOptions(queryStrOptions);
626+
if (queryStrOptions.preseededCode) {
627+
setCodeMirrorVal(queryStrOptions.preseededCode);
628+
}
629+
if (queryStrOptions.rawInputLst) {
630+
rawInputLst = queryStrOptions.rawInputLst; // global
631+
}
632+
else {
633+
rawInputLst = [];
634+
}
635+
636+
// ugh tricky -- always start in edit mode by default, and then
637+
// switch to display mode only after the code successfully executes
638+
appMode = 'edit';
639+
if ((queryStrOptions.appMode == 'display' ||
640+
queryStrOptions.appMode == 'visualize' /* 'visualize' is deprecated */) &&
641+
queryStrOptions.preseededCode /* jump to display only with pre-seeded code */) {
642+
executeCode(queryStrOptions.preseededCurInstr); // will switch to 'display' mode
643+
}
644+
$.bbq.removeState(); // clean up the URL no matter what
645+
}
646+
636647
// parsing the URL query string hash
637648
function getQueryStringOptions() {
638649
var ril = $.bbq.getState('rawInputLstJSON');
@@ -822,7 +833,6 @@ function updateAppDisplay(newAppMode) {
822833
});
823834
});
824835

825-
826836
$.bbq.pushState({ mode: 'display' }, 2 /* completely override other hash strings to keep URL clean */);
827837
}
828838
else {

0 commit comments

Comments
 (0)