MDL-68463 core: Add helper functions to toggle checkboxes
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 13 May 2020 07:59:23 +0000 (15:59 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 18 May 2020 12:03:38 +0000 (20:03 +0800)
lib/amd/build/checkbox-toggleall.min.js
lib/amd/build/checkbox-toggleall.min.js.map
lib/amd/src/checkbox-toggleall.js

index be9c0ae..4fd1ed4 100644 (file)
Binary files a/lib/amd/build/checkbox-toggleall.min.js and b/lib/amd/build/checkbox-toggleall.min.js differ
index 8468421..e7a5c40 100644 (file)
Binary files a/lib/amd/build/checkbox-toggleall.min.js.map and b/lib/amd/build/checkbox-toggleall.min.js.map differ
index 345db8f..473a514 100644 (file)
@@ -113,6 +113,38 @@ define(['jquery', 'core/pubsub'], function($, PubSub) {
             targetState = target.data('checkall') === 1;
         }
 
+        toggleSlavesToState(root, toggleGroupName, targetState);
+    };
+
+    /**
+     * Toggles the slave checkboxes from the masters.
+     *
+     * @param {HTMLElement} root
+     * @param {String} toggleGroupName
+     */
+    var updateSlavesFromMasterState = function(root, toggleGroupName) {
+        // Normalise to jQuery Object.
+        root = $(root);
+
+        var target = getControlCheckboxes(root, toggleGroupName, false);
+        var targetState;
+        if (target.is(':checkbox')) {
+            targetState = target.is(':checked');
+        } else {
+            targetState = target.data('checkall') === 1;
+        }
+
+        toggleSlavesToState(root, toggleGroupName, targetState);
+    };
+
+    /**
+     * Toggles the slave checkboxes to a specific state.
+     *
+     * @param {HTMLElement} root
+     * @param {String} toggleGroupName
+     * @param {Bool} targetState
+     */
+    var toggleSlavesToState = function(root, toggleGroupName, targetState) {
         var slaves = getAllSlaveCheckboxes(root, toggleGroupName);
         var checkedSlaves = slaves.filter(':checked');
 
@@ -132,6 +164,22 @@ define(['jquery', 'core/pubsub'], function($, PubSub) {
         });
     };
 
+    /**
+     * Set the state for an entire group of checkboxes.
+     *
+     * @param {HTMLElement} root
+     * @param {String} toggleGroupName
+     * @param {Bool} targetState
+     */
+    var setGroupState = function(root, toggleGroupName, targetState) {
+        // Normalise to jQuery Object.
+        root = $(root);
+
+        // Set the master and slaves.
+        setMasterStates(root, toggleGroupName, targetState, true);
+        toggleSlavesToState(root, toggleGroupName, targetState);
+    };
+
     /**
      * Toggles the master checkboxes in a given toggle group when all or none of the slave checkboxes in the same toggle group
      * have been selected.
@@ -242,5 +290,7 @@ define(['jquery', 'core/pubsub'], function($, PubSub) {
             registerListeners();
         },
         events: events,
+        setGroupState: setGroupState,
+        updateSlavesFromMasterState: updateSlavesFromMasterState,
     };
 });