]> git.mxchange.org Git - friendica.git/commitdiff
User message if the browser window is refreshed unintentionally
authorloma-one <44441246+loma-one@users.noreply.github.com>
Sun, 1 Sep 2024 08:06:13 +0000 (10:06 +0200)
committerGitHub <noreply@github.com>
Sun, 1 Sep 2024 08:06:13 +0000 (10:06 +0200)
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.

view/theme/frio/templates/jot-header.tpl

index cf74174fe6a81258fafdca6a19b1fa85f0cede12..e882fdf0c17c667369ba00acf3c5e32c401a573e 100644 (file)
@@ -12,6 +12,7 @@
 <script type="text/javascript">
        var editor = false;
        var textlen = 0;
+       var formModified = false;
 
        function initEditor(callback) {
                if (editor == false) {
@@ -29,8 +30,9 @@
                        });
                        $(".jothidden").show();
                        $("#profile-jot-text").keyup(function(){
-                               var textlen = $(this).val().length;
+                               textlen = $(this).val().length;
                                $('#character-counter').text(textlen);
+                               formModified = true; // Mark the form as modified when the user types
                        });
 
                        editor = true;
        function enableOnUser(){
                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 type="text/javascript">
                                type: 'POST',
                        })
                        .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();
                                $('#jot-modal').modal('hide');
+                               resetFormModifiedFlag(); // Reset formModified after successful submission
                        })
                        .always(function() {
                                // Reset the post_id_random to avoid duplicate post errors
                                }
 
                                NavUpdate();
-                       })
+                       });
                });
 
                $('#wall-image-upload').on('click', function(){
                reply = prompt("{{$vidurl}}");
                if(reply && reply.length) {
                        addeditortext('[video]' + reply + '[/video]');
+                       formModified = true; // Mark the form as modified
                }
        }
 
                reply = prompt("{{$audurl}}");
                if(reply && reply.length) {
                        addeditortext('[audio]' + reply + '[/audio]');
+                       formModified = true; // Mark the form as modified
                }
        }
 
                reply = prompt("{{$whereareu}}", $('#jot-location').val());
                if(reply && reply.length) {
                        $('#jot-location').val(reply);
+                       formModified = true; // Mark the form as modified
                }
        }
 
                        initEditor(function(){
                                addeditortext(data);
                        });
+                       formModified = true; // Mark the form as modified
                });
 
                jotShow();
                                initEditor(function(){
                                        addeditortext(data);
                                        $('#profile-rotator').hide();
+                                       formModified = true; // Mark the form as modified
                                });
                        });
                        autosize.update($("#profile-jot-text"));
                                if(timer) clearTimeout(timer);
                                timer = setTimeout(NavUpdate,3000);
                                liking = 1;
+                               formModified = true; // Mark the form as modified
                        }
                }
        }
                                        liking = 1;
                                        force_update = true;
                                        $.colorbox.close();
+                                       formModified = true; // Mark the form as modified
                                } else {
                                        $("#id_term").css("border-color","#FF0000");
                                }
        function jotClearLocation() {
                $('#jot-coord').val('');
                $('#profile-nolocation-wrapper').hide();
+               formModified = true; // Mark the form as modified
        }
 
        function addeditortext(data) {
                //insert the data as new value
                textfield.value = currentText + data;
                autosize.update($("#profile-jot-text"));
+               formModified = true; // Mark the form as modified
        }
 
        {{$geotag nofilter}}
                toggleJotNav(elemMobile[0]);
        }
 </script>
+