MDL-31660 Rename form change detection javascript file
authorAndrew Robert Nicols <andrew.nicols@luns.net.uk>
Thu, 16 Feb 2012 10:25:54 +0000 (10:25 +0000)
committerAndrew Robert Nicols <andrew.nicols@luns.net.uk>
Wed, 29 Feb 2012 14:50:52 +0000 (14:50 +0000)
admin/settings.php
lib/form/filemanager.js
lib/formslib.php
lib/javascript-static.js
lib/yui/formchangechecker/formchangechecker.js [new file with mode: 0644]
lib/yui/formslib/formslib.js [deleted file]

index 3f8d2c9..3e66f24 100644 (file)
@@ -131,8 +131,8 @@ if (empty($SITE->fullname)) {
     echo '</form>';
 }
 
-$PAGE->requires->yui_module('moodle-core-formslib',
-        'M.core.init_formslib',
+$PAGE->requires->yui_module('moodle-core-formchangechecker',
+        'M.core_formchangechecker.init',
         array(array(
             'formid' => 'adminsettings'
         ))
index de89ff3..10effe4 100644 (file)
@@ -144,7 +144,7 @@ M.form_filemanager.init = function(Y, options) {
             this.filecount++;
             this.check_buttons();
             this.refresh(this.currentpath);
-            M.util.set_form_changed();
+            M.core_formchangechecker.set_form_changed();
         },
         check_buttons: function() {
             var button_addfile  = Y.one("#btnadd-"+this.client_id);
@@ -214,7 +214,7 @@ M.form_filemanager.init = function(Y, options) {
                                 scope.mkdir_dialog.hide();
                                 scope.refresh(filepath);
                                 Y.one('#fm-newname').set('value', '');
-                                M.util.set_form_changed();
+                                M.core_formchangechecker.set_form_changed();
                             }
                         });
                     }
@@ -568,7 +568,7 @@ M.form_filemanager.init = function(Y, options) {
                         callback: function(id, obj, args) {
                             scope.filecount--;
                             scope.refresh(obj.filepath);
-                            M.util.set_form_changed();
+                            M.core_formchangechecker.set_form_changed();
                             if (scope.filecount < scope.maxfiles && scope.maxfiles!=-1) {
                                 var button_addfile  = Y.one("#btnadd-"+scope.client_id);
                                 button_addfile.setStyle('display', 'inline');
@@ -616,7 +616,7 @@ M.form_filemanager.init = function(Y, options) {
                                 alert(M.str.repository.fileexists);
                             } else {
                                 scope.refresh(obj.filepath);
-                                M.util.set_form_changed();
+                                M.core_formchangechecker.set_form_changed();
                             }
                             Y.one('#fm-rename-input').set('value', '');
                             scope.rename_dialog.hide();
@@ -694,7 +694,7 @@ M.form_filemanager.init = function(Y, options) {
                             }
                             dialog.cancel();
                             scope.refresh(p);
-                            M.util.set_form_changed();
+                            M.core_formchangechecker.set_form_changed();
                         }
                     });
                 }
index 371c2b0..cd89d3c 100644 (file)
@@ -2268,8 +2268,8 @@ class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{
             $this->_hiddenHtml .= $form->_pageparams;
         }
 
-        $PAGE->requires->yui_module('moodle-core-formslib',
-                'M.core.init_formslib',
+        $PAGE->requires->yui_module('moodle-core-formchangechecker',
+                'M.core_formchangechecker.init',
                 array(array(
                     'formid' => $form->getAttribute('id')
                 ))
index 452be34..90310a9 100644 (file)
@@ -1753,71 +1753,3 @@ M.util.load_flowplayer = 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;
-};
diff --git a/lib/yui/formchangechecker/formchangechecker.js b/lib/yui/formchangechecker/formchangechecker.js
new file mode 100644 (file)
index 0000000..67b9a5e
--- /dev/null
@@ -0,0 +1,129 @@
+YUI.add('moodle-core-formchangechecker',
+    function(Y) {
+        // The CSS selectors we use
+        var CSS = {
+        };
+
+        var FORMCHANGECHECKERNAME = 'core-formchangechecker';
+
+        var FORMCHANGECHECKER = function() {
+            FORMCHANGECHECKER.superclass.constructor.apply(this, arguments);
+        }
+
+        Y.extend(FORMCHANGECHECKER, Y.Base, {
+                /**
+                 * Initialize the module
+                 */
+                initializer : function(config) {
+                    var formid = 'form#' + this.get('formid');
+
+                    // Add change events to the form elements
+                    Y.all(formid + ' input').once('change', M.core_formchangechecker.set_form_changed, this);
+                    Y.all(formid + ' textarea').once('change', M.core_formchangechecker.set_form_changed, this);
+                    Y.all(formid + ' select').once('change', M.core_formchangechecker.set_form_changed, this);
+
+                    // We need any submit buttons on the form to set the submitted flag
+                    Y.one(formid).on('submit', M.core_formchangechecker.set_form_submitted, this);
+
+                    // YUI doesn't support onbeforeunload properly so we must use the DOM to set the onbeforeunload. As
+                    // a result, the has_changed must stay in the DOM too
+                    window.onbeforeunload = M.core_formchangechecker.report_form_dirty_state;
+                },
+            },
+            {
+                NAME : FORMCHANGECHECKERNAME,
+                ATTRS : {
+                    formid : {
+                        'value' : ''
+                    }
+                }
+            }
+        );
+
+        M.core_formchangechecker = M.core_formchangechecker || {};
+
+        // We might have multiple instances of the form change protector
+        M.core_formchangechecker.instances = M.core_formchangechecker.instances || [];
+        M.core_formchangechecker.init = function(config) {
+            var formchangechecker = new FORMCHANGECHECKER(config);
+            M.core_formchangechecker.instances.push(formchangechecker);
+            return formchangechecker;
+        }
+
+        // Store state information
+        M.core_formchangechecker.stateinformation = [];
+
+        /**
+         * Set the form changed state to true
+         */
+        M.core_formchangechecker.set_form_changed = function() {
+            M.core_formchangechecker.stateinformation.formchanged = 1;
+        }
+
+        /**
+         * Set the form submitted state to true
+         */
+        M.core_formchangechecker.set_form_submitted = function() {
+            M.core_formchangechecker.stateinformation.formsubmitted = 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.core_formchangechecker.get_form_dirty_state = function() {
+            var state = M.core_formchangechecker.stateinformation;
+
+            // If the form was submitted, then return a non-dirty state
+            if (state.formsubmitted) {
+                return 0;
+            }
+
+            // If any fields have been marked dirty, return a dirty state
+            if (state.formchanged) {
+                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.core_formchangechecker.report_form_dirty_state = function(e) {
+            if (!M.core_formchangechecker.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 warningmessage = 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 = warningmessage;
+            }
+
+            // But some require it to be returned instead
+            return warningmessage;
+        };
+    },
+    '@VERSION@', {
+        requires : ['base']
+    }
+);
diff --git a/lib/yui/formslib/formslib.js b/lib/yui/formslib/formslib.js
deleted file mode 100644 (file)
index 452b9e3..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-YUI.add('moodle-core-formslib',
-    function(Y) {
-        // The CSS selectors we use
-        var CSS = {
-        };
-
-        var FORMSLIBNAME = 'core-formslib';
-
-        var FORMSLIB = function() {
-            FORMSLIB.superclass.constructor.apply(this, arguments);
-        }
-
-        Y.extend(FORMSLIB, Y.Base, {
-                /**
-                * Initialize the module
-                */
-                initializer : function(config) {
-                    var formid = 'form#' + this.get('formid');
-
-                    // Add change events to the form elements
-                    Y.all(formid + ' input').on('change', M.util.set_form_changed, this);
-                    Y.all(formid + ' textarea').on('change', M.util.set_form_changed, this);
-                    Y.all(formid + ' select').on('change', M.util.set_form_changed, this);
-
-                    // We need any submit buttons on the form to set the submitted flag
-                    Y.one(formid).on('submit', M.util.set_form_submitted, this);
-
-                    // YUI doesn't support onbeforeunload properly so we must use the DOM to set the onbeforeunload. As
-                    // a result, the has_changed must stay in the DOM too
-                    window.onbeforeunload = M.util.report_form_dirty_state;
-                },
-
-                /**
-                 * Unset the form dirty state and also set the form submitted flag to true
-                 */
-                unset_changed : function(e) {
-                    M.util.set_form_changed();
-                }
-            },
-            {
-                NAME : FORMSLIBNAME,
-                ATTRS : {
-                    formid : {
-                        'value' : ''
-                    }
-                }
-            }
-        );
-
-        M.core = M.core || {};
-        M.core.init_formslib = function(config) {
-            return new FORMSLIB(config);
-        }
-
-    },
-    '@VERSION@', {
-        requires : ['base']
-    }
-);