Merge pull request #84 from medialab-prado/alert-when-leaving-form

Ask for confirmation when changing the page and the forms watched do have changes
This commit is contained in:
Amaia Castro
2017-01-18 16:48:27 +01:00
committed by GitHub
8 changed files with 41 additions and 4 deletions

View File

@@ -54,6 +54,7 @@
//= require legislation
//= require legislation_allegations
//= require legislation_annotatable
//= require watch_form_changes
//= require custom
var initialize_modules = function() {
@@ -83,6 +84,7 @@ var initialize_modules = function() {
App.Legislation.initialize();
if ( $(".legislation-annotatable").length )
App.LegislationAnnotatable.initialize();
App.WatchFormChanges.initialize();
};
$(function(){

View File

@@ -0,0 +1,31 @@
App.WatchFormChanges =
forms: ->
return $('form[data-watch-changes]')
msg: ->
if($('[data-watch-form-message]').length)
return $('[data-watch-form-message]').data('watch-form-message')
checkChanges: (event) ->
console.log(event)
changes = false
App.WatchFormChanges.forms().each ->
form = $(this)
if form.serialize() != form.data('watchChanges')
changes = true
if changes
return confirm(App.WatchFormChanges.msg())
else
return true
initialize: ->
if App.WatchFormChanges.forms().length == 0 || App.WatchFormChanges.msg() == undefined
return
$(document).off('page:before-change').on('page:before-change', (e) -> App.WatchFormChanges.checkChanges(e))
App.WatchFormChanges.forms().each ->
form = $(this)
form.data('watchChanges', form.serialize())
false