Commit c19c367a authored by Curtis Conard's avatar Curtis Conard Committed by Johan Cwiklinski
Browse files

Warn if there are unsaved form changes (#6433)

* Prompt on unsaved form changes
* Support select, select2, textarea, and TinyMCE
* Update changelog
* force-clear change tracking on form submit
parent e995f5fa
......@@ -21,6 +21,7 @@ The present file will list all changes made to the project; according to the
- Add lightbox with PhotoSwipe to timeline images
- Ability to copy tasks while merging tickets
- the API gives the ID of the user who logs in with initSession
- Add warning when there are unsaved changes in forms
### Changed
......
......@@ -3657,6 +3657,11 @@ class Html {
editor.on('SaveContent', function (contentEvent) {
contentEvent.content = contentEvent.content.replace(/\\r?\\n/g, '');
});
editor.on('Change', function (e) {
// Nothing fancy here. Since this is only used for tracking unsaved changes,
// we want to keep the logic in common.js with the other form input events.
onTinyMCEChange(e);
});
// ctrl + enter submit the parent form
editor.addShortcut('ctrl+13', 'submit', function() {
......
......@@ -1021,3 +1021,33 @@ var getTextWithoutDiacriticalMarks = function (text) {
// They are removed to keep only chars without their diacritical mark.
return text.replace(/[\u0300-\u036f]/g, '');
};
/** Track input changes and warn the user of unsaved changes if they try to navigate away */
window.glpiUnsavedFormChanges = false;
$(document).ready(function() {
// Try to limit tracking to item forms by binding to inputs under glpi_tabs only.
var glpiTabs = $('#page .glpi_tabs');
glpiTabs.on('input', 'form input, form textarea', function() {
window.glpiUnsavedFormChanges = true;
});
glpiTabs.on('change', 'form select', function() {
window.glpiUnsavedFormChanges = true;
});
glpiTabs.on('select2:select', 'form select', function() {
window.glpiUnsavedFormChanges = true;
});
$(window).on('beforeunload', function() {
if (window.glpiUnsavedFormChanges) {
// Only used for older browsers. Newer ones will display a localized message that is unique to the browser.
return "Do you want to leave this site? Changes you made may not be saved.";
}
});
glpiTabs.on('submit', 'form', function() {
window.glpiUnsavedFormChanges = false;
});
});
function onTinyMCEChange() {
window.glpiUnsavedFormChanges = true;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment