Files
grecia/app/assets/javascripts/watch_form_changes.js
Javi Martín 7b96180a76 Upgrade Turbolinks to version 5.2.1
We didn't upgrade Turbolinks when we upgraded to Rails 5 so we didn't
upgrade too many things at the same time, and postponed it... until now
:).

Note upgrading Turbolinks fixes an issue with foundation's sticky when
using the browser's back and forward buttons. We're adding tests for
these scenarios.

Co-authored-by: Senén Rodero Rodríguez <senenrodero@gmail.com>
2020-08-05 14:10:22 +02:00

33 lines
1002 B
JavaScript

(function() {
"use strict";
App.WatchFormChanges = {
forms: function() {
return $("form[data-watch-changes]");
},
msg: function() {
return $("[data-watch-form-message]").data("watch-form-message");
},
hasChanged: function() {
return App.WatchFormChanges.forms().is(function() {
return $(this).serialize() !== $(this).data("watchChanges");
});
},
checkChanges: function() {
if (App.WatchFormChanges.hasChanged()) {
return confirm(App.WatchFormChanges.msg());
} else {
return true;
}
},
initialize: function() {
if (App.WatchFormChanges.forms().length === 0 || App.WatchFormChanges.msg() === undefined) {
return;
}
$(document).off("turbolinks:before-visit").on("turbolinks:before-visit", App.WatchFormChanges.checkChanges);
App.WatchFormChanges.forms().each(function() {
$(this).data("watchChanges", $(this).serialize());
});
}
};
}).call(this);