Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember checkbox state following page navigation #764

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions airlock/static/assets/file_browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,22 @@ function addCheckboxClickListener() {

// On first load of the page we need to wire up the event listener
// so that we can respond to checkbox changes.
document.addEventListener("DOMContentLoaded", addCheckboxClickListener);
if (document.readyState !== "loading") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this because of the htmx loads? So the document itself is already loaded but the checkbox listener and status have to be called again for htmx-refreshed content?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's actually just for the back button case. The event listener is put on an element far enough up the DOM so that any htmx changes within the file browser don't touch it. Once its enabled initially you therefore don't need to redo it. However, when the back button is clicked you get a whole page refresh so event listeners are disregarded - but you also don't get a DOMContentLoaded event (or at least it has already triggered before this code runs). I think this is because the page is cached by the browser in some way, but I'm not 100%.

// If the document is already loaded then we can add the event listener now
// and also ensure the checkboxes are in sync with the stored values
addCheckboxClickListener();
renderCheckboxStatus();
} else {
// If the document is not yet loaded we wait for the loaded event
document.addEventListener("DOMContentLoaded", () => {
addCheckboxClickListener();
// I'm pretty sure we never need to call renderCheckboxStatus here
// because in this scenario the "datatable-ready" event (see below)
// will fire. But calling it twice isn't an issue, so just in case...
renderCheckboxStatus();
});
}

// Every time a datatable is rendered we need to update the checkboxes
// so they match the saved state
document.body.addEventListener("datatable-ready", renderCheckboxStatus)
document.body.addEventListener("datatable-ready", renderCheckboxStatus);