MDL-68932 core: showhidesettings to work with multiple hideIf()s
authorShamim Rezaie <shamim@moodle.com>
Fri, 12 Jun 2020 07:40:10 +0000 (17:40 +1000)
committerShamim Rezaie <shamim@moodle.com>
Wed, 5 Aug 2020 09:06:08 +0000 (19:06 +1000)
lib/amd/build/showhidesettings.min.js
lib/amd/build/showhidesettings.min.js.map
lib/amd/src/showhidesettings.js

index 08c3954..1f99ffc 100644 (file)
Binary files a/lib/amd/build/showhidesettings.min.js and b/lib/amd/build/showhidesettings.min.js differ
index e8e2cfb..2d9f178 100644 (file)
Binary files a/lib/amd/build/showhidesettings.min.js.map and b/lib/amd/build/showhidesettings.min.js.map differ
index d06db8f..ba5731b 100644 (file)
@@ -249,15 +249,6 @@ define(['jquery'], function($) {
         return $('[name="' + name + '"],[name="' + name + '[]"]');
     }
 
-    /**
-     * Find the name of the given element
-     * @param {EventTarget} el
-     * @returns {String}
-     */
-    function getElementName(el) {
-        return $(el).attr('name').replace(/\[]/, '');
-    }
-
     /**
      * Check to see whether a particular condition is met
      * @param {*|jQuery|HTMLElement} $dependon
@@ -273,29 +264,23 @@ define(['jquery'], function($) {
     }
 
     /**
-     * Show / hide the elements that depend on the element(s) with the given name
-     * OR (if no dependonname given) the element(s) with the same name as the element that
-     * triggered the event e.
-     * @param {Event} e
-     * @param {String} dependonname (optional)
+     * Show / hide the elements that depend on some elements.
      */
-    function updateDependencies(e, dependonname) {
-        dependonname = dependonname || getElementName(e.currentTarget);
-        var $dependon = getElementsByName(dependonname);
-        if (!dependencies.hasOwnProperty(dependonname)) {
-            return;
-        }
-        // Process all dependency conditions related to the updated element.
+    function updateDependencies() {
+        // Process all dependency conditions.
         var toHide = {};
-        $.each(dependencies[dependonname], function(condition, values) {
-            $.each(values, function(value, elements) {
-                var hide = checkDependency($dependon, condition, value);
-                $.each(elements, function(idx, elToHide) {
-                    if (toHide.hasOwnProperty(elToHide)) {
-                        toHide[elToHide] = toHide[elToHide] || hide;
-                    } else {
-                        toHide[elToHide] = hide;
-                    }
+        $.each(dependencies, function(dependonname) {
+            var dependon = getElementsByName(dependonname);
+            $.each(dependencies[dependonname], function(condition, values) {
+                $.each(values, function(value, elements) {
+                    var hide = checkDependency(dependon, condition, value);
+                    $.each(elements, function(idx, elToHide) {
+                        if (toHide.hasOwnProperty(elToHide)) {
+                            toHide[elToHide] = toHide[elToHide] || hide;
+                        } else {
+                            toHide[elToHide] = hide;
+                        }
+                    });
                 });
             });
         });
@@ -323,9 +308,9 @@ define(['jquery'], function($) {
             var $el = getElementsByName(depname);
             if ($el.length) {
                 $el.on('change', updateDependencies);
-                updateDependencies(null, depname);
             }
         });
+        updateDependencies();
     }
 
     /**