From 2f668c1d3ef891bc9fd682e89875d8030ea860c9 Mon Sep 17 00:00:00 2001 From: Johannes Eder Date: Tue, 13 Jul 2021 12:41:01 +0200 Subject: [PATCH] chore(navigate-away-prompt): implemnted destroy method --- frontend/src/utils/form/navigate-away-prompt.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/frontend/src/utils/form/navigate-away-prompt.js b/frontend/src/utils/form/navigate-away-prompt.js index 8890e198e..9c6a57eee 100644 --- a/frontend/src/utils/form/navigate-away-prompt.js +++ b/frontend/src/utils/form/navigate-away-prompt.js @@ -1,4 +1,5 @@ import { Utility } from '../../core/utility'; +import { EventManager, EventWrapper, EVENT_TYPE } from '../../lib/event-manager/event-manager'; import { AUTO_SUBMIT_BUTTON_UTIL_SELECTOR } from './auto-submit-button'; import { AUTO_SUBMIT_INPUT_UTIL_SELECTOR } from './auto-submit-input'; @@ -27,6 +28,8 @@ export class NavigateAwayPrompt { _element; + _eventManager; + _initFormData; _unloadDueToSubmit = false; @@ -36,6 +39,7 @@ export class NavigateAwayPrompt { } this._element = element; + this._eventManager = new EventManager(); if (this._element.classList.contains(NAVIGATE_AWAY_PROMPT_INITIALIZED_CLASS)) { return; @@ -65,15 +69,18 @@ export class NavigateAwayPrompt { return; this._initFormData = new FormData(this._element); - window.addEventListener('beforeunload', this._beforeUnloadHandler.bind(this)); + const beforeUnloadEvent = new EventWrapper(EVENT_TYPE.BEFOREUNLOAD, this._beforeUnloadHandler.bind(this), window); + this._eventManager.registerNewListener(beforeUnloadEvent); - this._element.addEventListener('submit', () => { + const submitEvent = new EventWrapper(EVENT_TYPE.SUBMIT, (() => { this._unloadDueToSubmit = true; defer(() => { this._unloadDueToSubmit = false; } ); // Restore state after event loop is settled - }); + }).bind(this), this._element); + this._eventManager.registerNewListener(submitEvent); } destroy() { + this._eventManager.cleanUp(); this._element.classList.remove(NAVIGATE_AWAY_PROMPT_INITIALIZED_CLASS); } @@ -98,8 +105,10 @@ export class NavigateAwayPrompt { // allow the event to happen if the form was not touched by the // user (i.e. if the current FormData is equal to the initial FormData) // or the unload event was initiated by a form submit - if (!formDataHasChanged || this._unloadDueToSubmit) + if (!formDataHasChanged || this._unloadDueToSubmit) { + this.destroy(); //prevent propmt from triggering after the form was closed and the user navigates away return; + } // cancel the unload event. This is the standard to force the prompt to appear. event.preventDefault();