chore(navigate-away-prompt): implemnted destroy method

This commit is contained in:
Johannes Eder 2021-07-13 12:41:01 +02:00 committed by Sarah Vaupel
parent 4c2c68327e
commit 2f668c1d3e

View File

@ -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();