From 84c650ab25532ae2e05825609f7fe7e3cba7b193 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Thu, 26 Feb 2026 07:41:51 -0500 Subject: [PATCH] Fix beforeunload event listeners --- .../controllers/bridge/buttons_controller.js | 16 ++++++++-------- .../controllers/bridge/form_controller.js | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/javascript/controllers/bridge/buttons_controller.js b/app/javascript/controllers/bridge/buttons_controller.js index e0d8b4b7c..dca0b5ae1 100644 --- a/app/javascript/controllers/bridge/buttons_controller.js +++ b/app/javascript/controllers/bridge/buttons_controller.js @@ -6,13 +6,17 @@ export default class extends BridgeComponent { static targets = [ "button" ] connect() { - window.addEventListener("beforeunload", this.handleBeforeUnload.bind(this)) - window.addEventListener("turbo:before-visit", this.handleBeforeVisit.bind(this)) + if (!this.beforeUnloadHandler) { + this.beforeUnloadHandler = this.handleBeforeUnload.bind(this) + } + + window.addEventListener("beforeunload", this.beforeUnloadHandler) } disconnect() { - window.removeEventListener("beforeunload", this.handleBeforeUnload.bind(this)) - window.removeEventListener("turbo:before-visit", this.handleBeforeVisit.bind(this)) + if (this.beforeUnloadHandler) { + window.removeEventListener("beforeunload", this.beforeUnloadHandler) + } } buttonTargetConnected() { @@ -45,10 +49,6 @@ export default class extends BridgeComponent { this.notifyBridgeOfDisconnect() } - handleBeforeVisit() { - this.notifyBridgeOfDisconnect() - } - #clickButton(message) { const selectedIndex = message.data.selectedIndex this.#enabledButtonTargets[selectedIndex].click() diff --git a/app/javascript/controllers/bridge/form_controller.js b/app/javascript/controllers/bridge/form_controller.js index 175c6fe39..f5f618669 100644 --- a/app/javascript/controllers/bridge/form_controller.js +++ b/app/javascript/controllers/bridge/form_controller.js @@ -7,13 +7,17 @@ export default class extends BridgeComponent { static values = { submitTitle: String } connect() { - window.addEventListener("beforeunload", this.handleBeforeUnload.bind(this)) - window.addEventListener("turbo:before-visit", this.handleBeforeVisit.bind(this)) + if (!this.beforeUnloadHandler) { + this.beforeUnloadHandler = this.handleBeforeUnload.bind(this) + } + + window.addEventListener("beforeunload", this.beforeUnloadHandler) } disconnect() { - window.removeEventListener("beforeunload", this.handleBeforeUnload.bind(this)) - window.removeEventListener("turbo:before-visit", this.handleBeforeVisit.bind(this)) + if (this.beforeUnloadHandler) { + window.removeEventListener("beforeunload", this.beforeUnloadHandler) + } } submitTargetConnected() { @@ -63,10 +67,6 @@ export default class extends BridgeComponent { this.notifyBridgeOfDisconnect() } - handleBeforeVisit() { - this.notifyBridgeOfDisconnect() - } - #observeSubmitTarget() { this.submitObserver = new MutationObserver(() => { this.send(this.submitTarget.disabled ? "submitDisabled" : "submitEnabled")