Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
(#144) bugfix and some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjfoerster committed May 31, 2022
1 parent 1a34401 commit 9351991
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions root/static/js/unsavedChanges.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
"use strict";

const forms = document.getElementsByClassName('confirmIfUnsavedChanges');
let hasUnsavedChanges = false;

for(const form of forms) {
const inputs = Array.prototype.slice.apply(form.getElementsByTagName('input'));
const textareas = Array.prototype.slice.apply(form.getElementsByTagName('textarea'));
const selects = Array.prototype.slice.apply(form.getElementsByTagName('select'));
const elements = inputs.concat(textareas).concat(selects);
console.log(elements)
for(const elem of elements) {
elem.addEventListener('input', () => {if(!hasUnsavedChanges) hasUnsavedChanges = true});
for( const form of forms ) {
const inputs = Array.prototype.slice.apply(form.getElementsByTagName('input'));
const textareas = Array.prototype.slice.apply(form.getElementsByTagName('textarea'));
const selects = Array.prototype.slice.apply(form.getElementsByTagName('select'));
const elements = inputs.concat(textareas).concat(selects);

for( const elem of elements ) {
elem.addEventListener('input', () => { if( !hasUnsavedChanges ) hasUnsavedChanges = true });
}
form.addEventListener('submit', () => {if(hasUnsavedChanges) hasUnsavedChanges = false});

form.addEventListener('submit', () => { if( hasUnsavedChanges ) hasUnsavedChanges = false });
}

// See documentation about 'beforeunload' event on https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
window.addEventListener('beforeunload', (e) => {
if (hasUnsavedChanges) {
(e || window.event).returnValue = '';
return '';
}
})
if( hasUnsavedChanges ) e.preventDefault(); //
});

0 comments on commit 9351991

Please sign in to comment.