From d72c6c5d6ec7bbb157ea228bfbbeb5e6c0768694 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 16 Sep 2014 17:29:11 +0200 Subject: [PATCH] Fixed page exit confirmation --- public_html/js/Router.js | 13 +++++++++++++ public_html/js/Util.js | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/public_html/js/Router.js b/public_html/js/Router.js index 3c34e931..1115ae6e 100644 --- a/public_html/js/Router.js +++ b/public_html/js/Router.js @@ -3,6 +3,7 @@ var App = App || {}; App.Router = function(pathJs, _, jQuery, util, appState, presenterManager) { var root = '#/'; + var previousLocation = window.location.href; injectRoutes(); @@ -57,6 +58,18 @@ App.Router = function(pathJs, _, jQuery, util, appState, presenterManager) { {previousRoute: pathJs.routes.previous}); presenterManager.switchContentPresenter( presenterName, finalParams); + }).enter(function(e) { + if (util.isExitConfirmationEnabled()) { + if (window.location.href !== previousLocation) { + if (!window.confirm('Are you sure you want to leave this page? Data will be lost.')) { + window.location.href = previousLocation; + return false; + } else { + util.disableExitConfirmation(); + } + } + } + previousLocation = window.location.href; }); } diff --git a/public_html/js/Util.js b/public_html/js/Util.js index 88c48949..2f71dfcd 100644 --- a/public_html/js/Util.js +++ b/public_html/js/Util.js @@ -3,17 +3,24 @@ var App = App || {}; App.Util = function(_, jQuery, promise) { var templateCache = {}; + var exitConfirmationEnabled = false; function enableExitConfirmation() { + exitConfirmationEnabled = true; jQuery(window).bind('beforeunload', function(e) { return 'There are unsaved changes.'; }); } function disableExitConfirmation() { + exitConfirmationEnabled = false; jQuery(window).unbind('beforeunload'); } + function isExitConfirmationEnabled() { + return exitConfirmationEnabled; + } + function parseComplexRouteArgs(args) { var result = {}; args = (args || '').split(/;/); @@ -148,6 +155,7 @@ App.Util = function(_, jQuery, promise) { formatRelativeTime: formatRelativeTime, enableExitConfirmation: enableExitConfirmation, disableExitConfirmation: disableExitConfirmation, + isExitConfirmationEnabled: isExitConfirmationEnabled, }; };