User message if the browser window is refreshed unintentionally

If a post is created in the modal, not sent and the user refreshes the browser window, all content is lost. 

This change ensures that the user receives a notification if a post is unsent while the browser window is unintentionally refreshed.
This commit is contained in:
loma-one 2024-09-01 10:06:13 +02:00 committed by GitHub
parent 4dd64b1a5d
commit ca46f8d1e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,6 +12,7 @@
<script type="text/javascript"> <script type="text/javascript">
var editor = false; var editor = false;
var textlen = 0; var textlen = 0;
var formModified = false;
function initEditor(callback) { function initEditor(callback) {
if (editor == false) { if (editor == false) {
@ -29,8 +30,9 @@
}); });
$(".jothidden").show(); $(".jothidden").show();
$("#profile-jot-text").keyup(function(){ $("#profile-jot-text").keyup(function(){
var textlen = $(this).val().length; textlen = $(this).val().length;
$('#character-counter').text(textlen); $('#character-counter').text(textlen);
formModified = true; // Mark the form as modified when the user types
}); });
editor = true; editor = true;
@ -43,6 +45,21 @@
function enableOnUser(){ function enableOnUser(){
initEditor(); initEditor();
} }
// Warn user before leaving the page if the form is modified
window.addEventListener('beforeunload', function (e) {
if (formModified) {
var confirmationMessage = 'There are unsaved changes. Are you sure you want to leave this page?';
e.returnValue = confirmationMessage; // Gecko, Trident, Chrome 34+
return confirmationMessage; // Gecko, WebKit, Chrome <34
}
});
// Reset formModified flag after successful submission
function resetFormModifiedFlag() {
formModified = false;
}
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -98,9 +115,10 @@
type: 'POST', type: 'POST',
}) })
.then(function () { .then(function () {
// Reset to form for jot reuse in the same page // Reset the form for jot reuse in the same page
e.target.reset(); e.target.reset();
$('#jot-modal').modal('hide'); $('#jot-modal').modal('hide');
resetFormModifiedFlag(); // Reset formModified after successful submission
}) })
.always(function() { .always(function() {
// Reset the post_id_random to avoid duplicate post errors // Reset the post_id_random to avoid duplicate post errors
@ -118,7 +136,7 @@
} }
NavUpdate(); NavUpdate();
}) });
}); });
$('#wall-image-upload').on('click', function(){ $('#wall-image-upload').on('click', function(){
@ -195,6 +213,7 @@
reply = prompt("{{$vidurl}}"); reply = prompt("{{$vidurl}}");
if(reply && reply.length) { if(reply && reply.length) {
addeditortext('[video]' + reply + '[/video]'); addeditortext('[video]' + reply + '[/video]');
formModified = true; // Mark the form as modified
} }
} }
@ -202,6 +221,7 @@
reply = prompt("{{$audurl}}"); reply = prompt("{{$audurl}}");
if(reply && reply.length) { if(reply && reply.length) {
addeditortext('[audio]' + reply + '[/audio]'); addeditortext('[audio]' + reply + '[/audio]');
formModified = true; // Mark the form as modified
} }
} }
@ -209,6 +229,7 @@
reply = prompt("{{$whereareu}}", $('#jot-location').val()); reply = prompt("{{$whereareu}}", $('#jot-location').val());
if(reply && reply.length) { if(reply && reply.length) {
$('#jot-location').val(reply); $('#jot-location').val(reply);
formModified = true; // Mark the form as modified
} }
} }
@ -219,6 +240,7 @@
initEditor(function(){ initEditor(function(){
addeditortext(data); addeditortext(data);
}); });
formModified = true; // Mark the form as modified
}); });
jotShow(); jotShow();
@ -248,6 +270,7 @@
initEditor(function(){ initEditor(function(){
addeditortext(data); addeditortext(data);
$('#profile-rotator').hide(); $('#profile-rotator').hide();
formModified = true; // Mark the form as modified
}); });
}); });
autosize.update($("#profile-jot-text")); autosize.update($("#profile-jot-text"));
@ -267,6 +290,7 @@
if(timer) clearTimeout(timer); if(timer) clearTimeout(timer);
timer = setTimeout(NavUpdate,3000); timer = setTimeout(NavUpdate,3000);
liking = 1; liking = 1;
formModified = true; // Mark the form as modified
} }
} }
} }
@ -295,6 +319,7 @@
liking = 1; liking = 1;
force_update = true; force_update = true;
$.colorbox.close(); $.colorbox.close();
formModified = true; // Mark the form as modified
} else { } else {
$("#id_term").css("border-color","#FF0000"); $("#id_term").css("border-color","#FF0000");
} }
@ -306,6 +331,7 @@
function jotClearLocation() { function jotClearLocation() {
$('#jot-coord').val(''); $('#jot-coord').val('');
$('#profile-nolocation-wrapper').hide(); $('#profile-nolocation-wrapper').hide();
formModified = true; // Mark the form as modified
} }
function addeditortext(data) { function addeditortext(data) {
@ -318,6 +344,7 @@
//insert the data as new value //insert the data as new value
textfield.value = currentText + data; textfield.value = currentText + data;
autosize.update($("#profile-jot-text")); autosize.update($("#profile-jot-text"));
formModified = true; // Mark the form as modified
} }
{{$geotag nofilter}} {{$geotag nofilter}}
@ -351,3 +378,4 @@
toggleJotNav(elemMobile[0]); toggleJotNav(elemMobile[0]);
} }
</script> </script>