From 99e2bda2d4cb86b298b8cefe5f292434f6235eef Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Tue, 22 Oct 2019 22:04:06 +0530 Subject: [PATCH 1/2] Improved: Editor should confirm user before leaving the page (ROL-2136) Added custom code to add confimation box for text area which adds an event handler to beforeunload event if the text area is changed. --- .../WEB-INF/jsps/editor/EntryEditor.jsp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp b/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp index 05548ce930..66e773e9a8 100644 --- a/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp +++ b/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp @@ -124,6 +124,22 @@ textAreaElement.focus(); } } + // Added event listener to confirm once the editor content is changed + jQuery("#edit_content").one("change", function() { + var confirmFunction = function(event) { + // Chrome requires returnValue to be set and original event is found as originalEvent + // see https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Example + if (event.originalEvent) + event.originalEvent.returnValue = "Are you sure you want to leave?"; + return "Are you sure you want to leave?"; + } + jQuery(window).on("beforeunload", confirmFunction); + + // Remove it if it is form submit + $(this.form).on('submit', function() { + jQuery(window).off("beforeunload", confirmFunction); + }); + }); @@ -146,6 +162,22 @@ height: 400 } ); + // Added event listener to confirm once the editor content is changed + $('#edit_content').on('summernote.change', function(we, contents, $editable) { + var confirmFunction = function(event) { + // Chrome requires returnValue to be set and original event is found as originalEvent + // see https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Example + if (event.originalEvent) + event.originalEvent.returnValue = "Are you sure you want to leave?"; + return "Are you sure you want to leave?"; + } + jQuery(window).on("beforeunload", confirmFunction); + + // Remove it if it is form submit + $(this.form).on('submit', function() { + jQuery(window).off("beforeunload", confirmFunction); + }); + }); }); function insertMediaFile(toInsert) { From b170cafd0e3f0a9d62eb166c7f822ea6331b004c Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Wed, 23 Oct 2019 10:34:38 +0530 Subject: [PATCH 2/2] Improved: used jQuery alias for consistency (ROL-2136) --- .../main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp b/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp index 66e773e9a8..e58d61539b 100644 --- a/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp +++ b/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp @@ -125,7 +125,7 @@ } } // Added event listener to confirm once the editor content is changed - jQuery("#edit_content").one("change", function() { + $("#edit_content").one("change", function() { var confirmFunction = function(event) { // Chrome requires returnValue to be set and original event is found as originalEvent // see https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Example @@ -133,11 +133,11 @@ event.originalEvent.returnValue = "Are you sure you want to leave?"; return "Are you sure you want to leave?"; } - jQuery(window).on("beforeunload", confirmFunction); + $(window).on("beforeunload", confirmFunction); // Remove it if it is form submit $(this.form).on('submit', function() { - jQuery(window).off("beforeunload", confirmFunction); + $(window).off("beforeunload", confirmFunction); }); }); @@ -171,11 +171,11 @@ event.originalEvent.returnValue = "Are you sure you want to leave?"; return "Are you sure you want to leave?"; } - jQuery(window).on("beforeunload", confirmFunction); + $(window).on("beforeunload", confirmFunction); // Remove it if it is form submit $(this.form).on('submit', function() { - jQuery(window).off("beforeunload", confirmFunction); + $(window).off("beforeunload", confirmFunction); }); }); });