diff --git a/themes/grav/templates/pages.html.twig b/themes/grav/templates/pages.html.twig index 8a1ad313..081a4039 100644 --- a/themes/grav/templates/pages.html.twig +++ b/themes/grav/templates/pages.html.twig @@ -78,16 +78,6 @@ - {% endif %} @@ -149,15 +139,77 @@ }; $(function(){ - var currentValues = getState(); + var currentValues = getState(), + clickedLink; + + $('#admin-main button').on('click', function(){ + $(window).off('beforeunload'); + }); + + $("#admin-mode-toggle input[name=mode-switch]").on('change', function(e){ + var value = $(this).val(); + + if (currentValues == getState()) { + setTimeout(function(){ + window.location.href = '{{ uri.route(true) }}' + ((value == 'expert') ? '/expert:1' : ''); + }, 200) + + return true; + } + + e.preventDefault(); + + var confirm = $.remodal.lookup[$('[data-remodal-id=changes]').data('remodal')], + buttons = $('[data-remodal-id=changes] a.button'), + action; + + buttons.on('click', function(e){ + e.preventDefault(); + action = $(this).data('leave-action'); + + buttons.off('click'); + confirm.close(); + + if (action == 'continue') { + $(window).off('beforeunload'); + window.location.href = '{{ uri.route(true) }}' + ((value == 'expert') ? '/expert:1' : ''); + } else { + $('input[name=mode-switch][checked]').prop('checked', true); + } + }); + + confirm.open(); + }); + + $('a[href]:not([href^=#])').on('click', function(e){ + if (currentValues != getState()){ + e.preventDefault(); + + clickedLink = $(this).attr('href'); + + var confirm = $.remodal.lookup[$('[data-remodal-id=changes]').data('remodal')], + buttons = $('[data-remodal-id=changes] a.button'), + action; + + buttons.on('click', function(e){ + e.preventDefault(); + action = $(this).data('leave-action'); + + buttons.off('click'); + confirm.close(); + + if (action == 'continue') { + $(window).off('beforeunload'); + window.location.href = clickedLink; + } + }); + + confirm.open(); + } + }); $(window).on('beforeunload', function(){ if (currentValues != getState()){ - setTimeout(function() { - setTimeout(function() { - $('input[name=mode-switch][checked]').prop('checked', true); - }, 10); - },1); return "You have made changes on this page that you have not yet confirmed. If you navigate away from this page you will lose your unsaved changes"; } }); @@ -176,8 +228,8 @@


- Cancel - Continue + Cancel + Continue