You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.3 KiB
69 lines
2.3 KiB
/**
|
|
* Determines if a form is dirty by comparing the current value of each element
|
|
* with its default value.
|
|
*
|
|
* @param {Form} form the form to be checked.
|
|
* @return {Boolean} <code>true</code> if the form is dirty, <code>false</code>
|
|
* otherwise.
|
|
*
|
|
* Taken from here: http://stackoverflow.com/a/155812/641263
|
|
*/
|
|
|
|
var confirmExitIfModified = (function () {
|
|
|
|
function formIsDirty(form) {
|
|
for (var i = 0; i < form.elements.length; i++) {
|
|
var element = form.elements[i];
|
|
var type = element.type;
|
|
if (type == "checkbox" || type == "radio") {
|
|
if (element.checked != element.defaultChecked) {
|
|
return true;
|
|
}
|
|
}
|
|
else if (type == "hidden" || type == "password" ||
|
|
type == "text" || type == "textarea") {
|
|
var cls = element.getAttribute('class');
|
|
if (!cls) cls = '';
|
|
if (element.value != element.defaultValue &&
|
|
// Fix for select2 multiple
|
|
cls.indexOf('select2') == -1 &&
|
|
// Skip elements with ignore-changes class
|
|
cls.indexOf('ignore-changes') == -1
|
|
) {
|
|
return true;
|
|
}
|
|
}
|
|
else if (type == "select-one" || type == "select-multiple") {
|
|
for (var j = 0; j < element.options.length; j++) {
|
|
if (element.options[j].selected !=
|
|
element.options[j].defaultSelected) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
var submit = false;
|
|
return function (form_id, message) {
|
|
var form = document.forms[form_id]
|
|
if (form) {
|
|
form.onsubmit = function (e) {
|
|
e = e || window.event;
|
|
submit = true
|
|
};
|
|
}
|
|
window.onbeforeunload = function (e) {
|
|
e = e || window.event;
|
|
if (!submit && formIsDirty(form)) {
|
|
// For IE and Firefox
|
|
if (e) {
|
|
e.returnValue = message;
|
|
}
|
|
// For Safari
|
|
return message;
|
|
}
|
|
};
|
|
};
|
|
})();
|
|
|