Skip to content

Commit

Permalink
Merge pull request #191 from bjc-edc/js-fix-browser-back-button
Browse files Browse the repository at this point in the history
Dynamic Page Loading Stuff
  • Loading branch information
cycomachead authored Jan 18, 2024
2 parents 1294987 + baf37ab commit c9d3be3
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions llab/script/curriculum.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* library.js
*/

// TODO: Notes on most necessary refactorings:
// * Dynamic Navigation is messy.
// * getters/setters for "current page" in a lab need refactored
// * getCurrentPageURL, nextPageURL, prevPageURL
llab.file = "";
llab.url_list = [];

Expand All @@ -30,10 +34,10 @@ llab.read_cache = key => sessionStorage[key];
// Switch to turn off ajax page loads.
llab.DISABLE_DYNAMIC_NAVIGATION = true;
// this should only be true when navigating back/forwards so we do no repopulate history.
llab.SKIP_PUSH_STATE = false;
// llab.SKIP_PUSH_STATE = false;

llab.dynamicNavigation = path => {
return event => {
llab.dynamicNavigation = (path) => {
return (event) => {
if (llab.DISABLE_DYNAMIC_NAVIGATION) {
location.href = path;
return;
Expand All @@ -43,6 +47,25 @@ llab.dynamicNavigation = path => {
}
}

if (!llab.DISABLE_DYNAMIC_NAVIGATION) {
// Handle popstate events for when users use the back button
window.addEventListener("popstate", (event) => {
const state = event.state;
console.log(event)
// debugger;

if (!state || !state.body || !state.title) {
location.reload();
return;
}

// llab.SKIP_PUSH_STATE = true;
llab.rerenderPage(state.body, state.title);
});
}

/////////////////////

// Executed on *every* page load.
llab.secondarySetUp = function (newPath) {
let t = llab.translate;
Expand Down Expand Up @@ -127,7 +150,7 @@ llab.secondarySetUp = function (newPath) {
* and creates navigation buttons.
* FIXME: This should share code with llab.topic!
*/
llab.processLinks = function (data) {
llab.processLinks = (data) => {
/* NOTE: DO NOT REMOVE THIS CONDITIONAL WITHOUT SERIOUS TESTING
* llab.file gets reset with the ajax call.
*/
Expand Down Expand Up @@ -239,6 +262,7 @@ llab.processLinks = function (data) {

// Make the current step have an arrow in the dropdown menu
if (isCurrentPage) {
console.log('isCurrentPage...')
llab.pageNum = pageCount;
itemContent = llab.spanTag(itemContent, 'current-page-arrow');
}
Expand Down Expand Up @@ -498,6 +522,8 @@ llab.setButtonURLs = function() {
};

llab.loadNewPage = (path) => {
console.log('LOAD NEW PAGE: ', path);

if (llab.PREVENT_NAVIGATIONS) {
// this seems like a poor way to debounce multiple clicks.
setTimeout((() => llab.PREVENT_NAVIGATIONS = false), 500);
Expand All @@ -518,22 +544,12 @@ llab.loadNewPage = (path) => {
});
}

// Handle popstate events for when users use the back button
window.addEventListener("popstate", (event) => {
const state = event.state;
if (!state || !state.body || !state.title) {
location.reload();
return;
}

llab.SKIP_PUSH_STATE = true;
llab.rerenderPage(state.body, state.title);
});

llab.rerenderPage = (body, title, path) => {
// Reset llab state.
llab.titleSet = false;
llab.conditional_setup_run = false;
console.log('RERENDER PAGE: ', path)

document.title = title;
$('.full').html(body);
Expand Down Expand Up @@ -561,6 +577,7 @@ llab.rebuildPageFromHTML = (html, path) => {

let title = doc.querySelector('title') ? doc.querySelector('title').text : '';
let body = doc.body.innerHTML;
console.log('REBUILD FROM HTML')
llab.rerenderPage(body, title, path);

llab.PREVENT_NAVIGATIONS = false;
Expand Down

0 comments on commit c9d3be3

Please sign in to comment.