MDL-53848 form: hide entire group if hideIf test is applied to the group
authorDavo Smith <davo.smith@synergy-learning.com>
Fri, 14 Jul 2017 11:02:13 +0000 (12:02 +0100)
committerDavo Smith <davo.smith@synergy-learning.com>
Thu, 17 Aug 2017 13:52:10 +0000 (14:52 +0100)
lib/form/form.js
lib/form/group.php
lib/form/templates/element-template.mustache
lib/formslib.php
theme/boost/templates/core_form/element-template-inline.mustache
theme/boost/templates/core_form/element-template.mustache

index 3ed59b1..79a3844 100644 (file)
@@ -85,6 +85,13 @@ if (typeof M.form.dependencyManager === 'undefined') {
                     names[name].push(node);
                 }
             });
+            // Locate any groups with the given name.
+            this.get('form').all('.fitem').each(function(node) {
+                var name = node.getData('groupname');
+                if (name && ({}).hasOwnProperty.call(names, name)) {
+                    names[name].push(node);
+                }
+            });
             this._nameCollections = names;
         },
 
@@ -264,7 +271,7 @@ if (typeof M.form.dependencyManager === 'undefined') {
         _hideElement: function(name, hidden) {
             var els = this.elementsByName(name);
             els.each(function(node) {
-                var e = node.ancestor('.fitem');
+                var e = node.ancestor('.fitem', true);
                 if (e) {
                     (hidden) ? e.setAttribute('hidden', 'hidden') : e.removeAttribute('hidden');
                     e.setStyles({
index da9359d..f4f241f 100644 (file)
@@ -213,6 +213,7 @@ class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable
             $i++;
         }
 
+        $context['groupname'] = $name;
         $context['elements'] = $elements;
         return $context;
     }
index 9463064..cc1195a 100644 (file)
@@ -46,7 +46,7 @@
     }
 
 }}
-<div id="fitem_{{ element.id }}" class="fitem fitem_{{ element.type }}">
+<div id="fitem_{{ element.id }}" class="fitem fitem_{{ element.type }}" {{#element.groupname}}data-groupname="{{.}}"{{/element.groupname}}>
     <div class="fitemtitle">
       <label for="{{element.id}}">{{{ label }}}</label>
         {{{ helpbutton }}}
index b8a50a5..66527b6 100644 (file)
@@ -2466,9 +2466,9 @@ require(["core/event", "jquery"], function(Event, $) {
                     $result[$dependenton][$condition][$value][self::DEP_HIDE] = array();
                     foreach ($dependents as $dependent) {
                         $elements = $this->_getElNamesRecursive($dependent);
-                        if (empty($elements)) {
-                            // Probably element inside of some group.
-                            $elements = array($dependent);
+                        if (!in_array($dependent, $elements)) {
+                            // Always want to hide the main element, even if it contains sub-elements as well.
+                            $elements[] = $dependent;
                         }
                         foreach ($elements as $element) {
                             if ($element == $dependenton) {
index 88593fc..95f1fa7 100644 (file)
@@ -1,4 +1,4 @@
-<div class="form-group {{#error}}has-danger{{/error}} fitem {{#advanced}}advanced{{/advanced}} {{{element.extraclasses}}}">
+<div class="form-group {{#error}}has-danger{{/error}} fitem {{#advanced}}advanced{{/advanced}} {{{element.extraclasses}}}" {{#element.groupname}}data-groupname="{{.}}"{{/element.groupname}}>
     <label class="col-form-label {{#element.hiddenlabel}}sr-only{{/element.hiddenlabel}}" for="{{element.id}}">
         {{{label}}} {{{helpbutton}}}
         {{#required}}<abbr class="initialism text-danger" title="{{#str}}required{{/str}}">{{#pix}}req, core, {{#str}}required{{/str}}{{/pix}}</abbr>{{/required}}
index 082a3d2..aa077ba 100644 (file)
@@ -41,7 +41,7 @@
         }
     }
 }}
-<div class="form-group row {{#error}}has-danger{{/error}} fitem {{#advanced}}advanced{{/advanced}} {{{element.extraclasses}}}">
+<div class="form-group row {{#error}}has-danger{{/error}} fitem {{#advanced}}advanced{{/advanced}} {{{element.extraclasses}}}" {{#element.groupname}}data-groupname="{{.}}"{{/element.groupname}}>
     <div class="col-md-3">
         <span class="pull-xs-right text-nowrap">
             {{#required}}<abbr class="initialism text-danger" title="{{#str}}required{{/str}}">{{#pix}}req, core, {{#str}}required{{/str}}{{/pix}}</abbr>{{/required}}