diff --git a/lib/Coocook/Controller/Dish.pm b/lib/Coocook/Controller/Dish.pm index ff83711d..6e3fdbed 100644 --- a/lib/Coocook/Controller/Dish.pm +++ b/lib/Coocook/Controller/Dish.pm @@ -81,6 +81,8 @@ sub edit : GET HEAD Chained('base') PathPart('') Args(0) RequiresCapability('vie for my $ingredient ( @{ $c->stash->{ingredients} } ) { $ingredient->{reposition_url} = $c->project_uri( '/dish/reposition', $ingredient->{id} ); } + + push @{ $c->stash->{js} }, '/js/unsavedChanges.js'; } sub delete : POST Chained('base') PathPart('delete') Args(0) RequiresCapability('edit_project') { diff --git a/lib/Coocook/Controller/Recipe.pm b/lib/Coocook/Controller/Recipe.pm index 77d223bf..98d3b564 100644 --- a/lib/Coocook/Controller/Recipe.pm +++ b/lib/Coocook/Controller/Recipe.pm @@ -107,6 +107,7 @@ sub edit : GET HEAD Chained('base') PathPart('') Args(0) RequiresCapability('vie and $c->stash( import_url => $c->uri_for_action( '/browse/recipe/import', [ $recipe->id, $recipe->url_name ] ) ); + push @{ $c->stash->{js} }, '/js/unsavedChanges.js'; } sub new_recipe : GET HEAD Chained('submenu') PathPart('recipes/new') diff --git a/root/static/js/unsavedChanges.js b/root/static/js/unsavedChanges.js new file mode 100644 index 00000000..15c0e31c --- /dev/null +++ b/root/static/js/unsavedChanges.js @@ -0,0 +1,21 @@ +const forms = document.getElementsByClassName('confirmIfUnsavedChanges'); +let hasUnsavedChanges = false; + +for(const form of forms) { + const inputs = Array.prototype.slice.apply(form.getElementsByTagName('input')); + const textareas = Array.prototype.slice.apply(form.getElementsByTagName('textarea')); + const selects = Array.prototype.slice.apply(form.getElementsByTagName('select')); + const elements = inputs.concat(textareas).concat(selects); + console.log(elements) + for(const elem of elements) { + elem.addEventListener('input', () => {if(!hasUnsavedChanges) hasUnsavedChanges = true}); + } + form.addEventListener('submit', () => {if(hasUnsavedChanges) hasUnsavedChanges = false}); +} + +window.addEventListener('beforeunload', (e) => { + if (hasUnsavedChanges) { + (e || window.event).returnValue = ''; + return ''; + } +}) \ No newline at end of file diff --git a/root/templates/dish/edit.tt b/root/templates/dish/edit.tt index ea8ed3de..e12132b4 100644 --- a/root/templates/dish/edit.tt +++ b/root/templates/dish/edit.tt @@ -10,33 +10,33 @@ -
[% INCLUDE 'includes/forms/ingredients_add.tt' %] diff --git a/root/templates/recipe/edit.tt b/root/templates/recipe/edit.tt index b99d1a46..d728a767 100644 --- a/root/templates/recipe/edit.tt +++ b/root/templates/recipe/edit.tt @@ -13,7 +13,7 @@ [% END %] -