Revert "Prevent frame navigations when form is changed"

It was too hacky and had issues. Let's just disable Turbo for those links for now.

This reverts commit 1d1169b478.
This commit is contained in:
David Cook
2024-04-17 17:26:49 +10:00
parent 91f0a80189
commit 8f9d8b5fb8
2 changed files with 2 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
- content_for :page_title do
= t('.header.title')
- content_for :page_actions do
%div{ :class => "toolbar", 'data-turbo': true }
%div{ :class => "toolbar" }
%ul{ :class => "actions header-action-links inline-menu" }
%li#new_product_link
= button_link_to t(:new_product), "/admin/products/new", { :icon => 'icon-plus', :id => 'admin_new_product' }

View File

@@ -33,25 +33,12 @@ export default class BulkFormController extends Controller {
this.form.addEventListener("submit", this.#registerSubmit.bind(this));
window.addEventListener("beforeunload", this.preventLeavingChangedForm.bind(this));
document.addEventListener("turbo:before-visit", this.preventLeavingChangedForm.bind(this));
// Frustratingly there's no before-frame-visit, and no other events work, so we need to bind to
// the frame and listen for any link clicks (maybe other things too).
this.turboFrame = this.form.closest("turbo-frame");
if(this.turboFrame){
this.turboFrame.addEventListener("click", this.preventLeavingChangedForm.bind(this))
}
}
disconnect() {
// Make sure to clean up anything that happened outside
this.#disableOtherElements(false);
window.removeEventListener("beforeunload", this.preventLeavingChangedForm.bind(this));
document.removeEventListener("turbo:before-visit", this.preventLeavingChangedForm.bind(this));
if(this.turboFrame){
this.turboFrame.removeEventListener("click", this.preventLeavingChangedForm.bind(this));
}
}
// Register any new elements (may be called by another controller after dynamically adding fields)
@@ -92,21 +79,9 @@ export default class BulkFormController extends Controller {
}
}
// If navigating away from form, warn to prevent accidental data loss
// If form is not being submitted, warn to prevent accidental data loss
preventLeavingChangedForm(event) {
const target = event.target;
// Ignore non-navigation events (see above)
if(this.turboFrame && this.turboFrame.contains(target) && (target.tagName != "A" || target.dataset.turboFrame != "_self")){
return;
}
if (this.formChanged && !this.submitting) {
// If fired by a custom event (eg Turbo), we need to explicitly ask.
// This is skipped on beforeunload.
if(confirm(I18n.t("are_you_sure"))){
return;
}
// Cancel the event
event.preventDefault();
// Chrome requires returnValue to be set, but ignores the value. Other browsers may display