From 3350475e36c5858fec55c5ad9b833182158046de Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 7 Aug 2012 12:57:51 +0100 Subject: [PATCH] MDL-34728 forms: woy for other JS to trigger disableIf update. This was discovered while working on MDL-32705. If some JavaScript (for example a select all/none link) changes the state of some form fields, then the disabledIf state of other form elements does not automatically update. The existing form JS was so well encapsulated that this was impossible. This change pokes a hole in the encapsulation, and provides an API M.form.updateFormState(formid); that other bits of JS code can call when necessary. --- lib/form/form.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/form/form.js b/lib/form/form.js index b49d76a0437..1f7856b7c24 100644 --- a/lib/form/form.js +++ b/lib/form/form.js @@ -58,6 +58,11 @@ M.form.initShowAdvanced = function(Y, config) { return M.form.showAdvanced; }; +/** + * Stores a list of the dependencyManager for each form on the page. + */ +M.form.dependencyManagers = {}; + /** * Initialises a manager for a forms dependencies. * This should happen once per form. @@ -128,7 +133,7 @@ M.form.initFormDependencies = function(Y, formid, dependencies) { return this.checkDependencies(null); }, /** - * Gets all elements in the form by thier name and returns + * Gets all elements in the form by their name and returns * a YUI NodeList * @return Y.NodeList */ @@ -352,5 +357,17 @@ M.form.initFormDependencies = function(Y, formid, dependencies) { return dependencyManager; })(); - return new M.form.dependencyManager(); -}; \ No newline at end of file + M.form.dependencyManagers[formid] = new M.form.dependencyManager(); + return M.form.dependencyManagers[formid]; +}; + +/** + * Update the state of a form. You need to call this after, for example, changing + * the state of some of the form input elements in your own code, in order that + * things like the disableIf state of elements can be updated. + */ +M.form.updateFormState = function(formid) { + if (formid in M.form.dependencyManagers) { + M.form.dependencyManagers[formid].checkDependencies(null); + } +};