]> git.mjollnir.org Git - moodle.git/commitdiff
javascript MDL-20400 Addition of disabledif hide option, thanks Jonathan for the...
authorsamhemelryk <samhemelryk>
Thu, 1 Oct 2009 06:41:33 +0000 (06:41 +0000)
committersamhemelryk <samhemelryk>
Thu, 1 Oct 2009 06:41:33 +0000 (06:41 +0000)
lib/javascript-static.js

index 39674c2710f8642789175503842dc0854cf1d824..a6de9fbffde8375efeb0e6740568794f060bcf5b 100644 (file)
@@ -116,10 +116,43 @@ function set_form_element_disabled(form, name, disabled) {
     }
 }
 
+/**
+ * Set the hidden state of the 'virtual form element' with a particular name.
+ * This abstracts away the difference between a normal form element, like a select
+ * which is a single HTML element with a .value property, and a set of radio
+ * buttons, which is several HTML elements.
+ *
+ * @param form a HTML form.
+ * @param master the name of an element in that form.
+ * @param hidden the hidden state to set.
+ */
+function set_form_element_hidden(form, name, hidden) {
+    var element = form[name];
+    if (!element) {
+        return;
+    }
+    if (element.tagName) {
+        var el = findParentNode(element, 'DIV', 'fitem', false);
+        if (el!=null) {
+            el.style.display = hidden ? 'none' : '';
+            el.style.visibility = hidden ? 'hidden' : '';
+        }
+    }
+    // Array of things, like radio buttons.
+    for (var j = 0; j < element.length; j++) {
+        var el = findParentNode(element[j], 'DIV', 'fitem', false);
+        if (el!=null) {
+            el.style.display = hidden ? 'none' : '';
+            el.style.visibility = hidden ? 'hidden' : '';
+        }
+    }
+}
+
 function lockoptionsall(formid) {
     var form = document.forms[formid];
     var dependons = eval(formid + 'items');
     var tolock = [];
+    var tohide = [];
     for (var dependon in dependons) {
         // change for MooTools compatibility
         if (!dependons.propertyIsEnumerable(dependon)) {
@@ -131,6 +164,7 @@ function lockoptionsall(formid) {
         for (var condition in dependons[dependon]) {
             for (var value in dependons[dependon][condition]) {
                 var lock;
+                var hide = false;
                 switch (condition) {
                   case 'notchecked':
                       lock = !form[dependon].checked; break;
@@ -140,15 +174,17 @@ function lockoptionsall(formid) {
                       lock = form[dependon].selectedIndex == -1; break;
                   case 'eq':
                       lock = get_form_element_value(form, dependon) == value; break;
+                  case 'hide':
+                      // hide as well as disable
+                      hide = true; break;
                   default:
                       lock = get_form_element_value(form, dependon) != value; break;
                 }
                 for (var ei in dependons[dependon][condition][value]) {
-                    // change for MooTools compatibility
-                    if (!window.webkit && (!dependons[dependon][condition][value].propertyIsEnumerable(ei))) {
-                        continue;
-                    }
                     var eltolock = dependons[dependon][condition][value][ei];
+                    if (hide) {
+                        tohide[eltolock] = true;
+                    }
                     if (tolock[eltolock] != null) {
                         tolock[eltolock] = lock || tolock[eltolock];
                     } else {
@@ -164,6 +200,9 @@ function lockoptionsall(formid) {
             continue;
         }
         set_form_element_disabled(form, el, tolock[el]);
+        if (tohide.propertyIsEnumerable(el)) {
+            set_form_element_hidden(form, el, tolock[el]);
+    }
     }
     return true;
 }
@@ -1344,4 +1383,4 @@ function update_progress_bar (id, width, pt, msg, es){
 
 function frame_breakout(e, properties) {
     this.setAttribute('target', properties.framename);
-}
\ No newline at end of file
+}