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

fix: Correctly dispatching the load event on popstate after snippets are restored by Naja #22

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
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
16 changes: 11 additions & 5 deletions src/extensions/AjaxModalExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,20 @@ export class AjaxModalExtension implements Extension {
this.modal.show(this.getElementFromString(state.pdModal.opener), state.pdModal.options, event)

// If there is some snippet cache, we might restore modal options. If not, options will be restored based on
// options after the ajax request. Same applies to dispatching load event - if cache is on, we dispatch the
// event immediately, otherwise it will be dispatched after ajax request.
// options after the ajax request. The same applies to dispatching the load event - if the cache is on, we
// dispatch the event immediately (after the restore has actually happened, see below), otherwise it is
// dispatched after the ajax request.
if (state.snippets?.storage !== 'off') {
this.modal.setOptions(state.pdModal.options)

if (this.modal.dispatchLoad) {
this.modal.dispatchLoad(this.modalOptions, event)
}
// We need to delay the load dispatch after the naja has restored the snippets. Unfortunately, there is
// no event after the snippets have been restored, only the `restore` event, which is dispatched before
// the actual restore.
setTimeout(() => {
if (this.modal.dispatchLoad) {
this.modal.dispatchLoad(this.modalOptions, event)
}
}, 0)
}
} else {
this.historyEnabled = false // Hiding modal using forward / back button, we disable the history to prevent state duplication
Expand Down