MDL-31315 Ask before moving away from a modified form

This commit is contained in:
Andrew Robert Nicols
2012-02-09 14:55:18 +00:00
parent 216f6d8e9d
commit 00e8d08dba
6 changed files with 149 additions and 3 deletions
+68
View File
@@ -1752,4 +1752,72 @@ M.util.load_flowplayer = function() {
fileref.onreadystatechange = embed_function;
document.getElementsByTagName('head')[0].appendChild(fileref);
}
};
/**
* Set the form changed state to true
*/
M.util.set_form_changed = function() {
M.cfg.form_changed = 1;
};
/**
* Set the form submitted state to true
*/
M.util.set_form_submitted = function() {
M.cfg.form_submitted = 1;
}
/**
* Attempt to determine whether the form has been modified in any way and
* is thus 'dirty'
*
* @return Integer 1 is the form is dirty; 0 if not
*/
M.util.get_form_dirty_state = function() {
// If the form was submitted, then return a non-dirty state
if (M.cfg.form_submitted) {
return 0;
}
// If any fields have been marked dirty, return a dirty state
if (M.cfg.form_changed) {
return 1;
}
// Handle TinyMCE editor instances
// We can't add a listener in the initializer as the editors may not have been created by that point
// so we do so here instead
if (typeof tinyMCE != 'undefined') {
for (var editor in tinyMCE.editors) {
if (tinyMCE.editors[editor].isDirty()) {
return 1;
}
}
}
// If we reached here, then the form hasn't met any of the dirty conditions
return 0;
};
/**
* Return a suitable message if changes have been made to a form
*/
M.util.report_form_dirty_state = function(e) {
if (!M.util.get_form_dirty_state()) {
// the form is not dirty, so don't display any message
return;
}
// This is the error message that we'll show to browsers which support it
var returnValue = M.util.get_string('changesmadereallygoaway', 'moodle');
// Most browsers are happy with the returnValue being set on the event
// But some browsers do not consistently pass the event
if (e) {
e.returnValue = returnValue;
}
// But some require it to be returned instead
return returnValue;
};