mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
1 line
17 KiB
Plaintext
1 line
17 KiB
Plaintext
{"version":3,"file":"checkbox-toggleall.min.js","sources":["../src/checkbox-toggleall.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * A module to help with toggle select/deselect all.\n *\n * @module core/checkbox-toggleall\n * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/pubsub'], function($, PubSub) {\n\n /**\n * Whether event listeners have already been registered.\n *\n * @private\n * @type {boolean}\n */\n var registered = false;\n\n /**\n * List of custom events that this module publishes.\n *\n * @private\n * @type {{checkboxToggled: string}}\n */\n var events = {\n checkboxToggled: 'core/checkbox-toggleall:checkboxToggled',\n };\n\n /**\n * Fetches elements that are member of a given toggle group.\n *\n * @private\n * @param {jQuery} root The root jQuery element.\n * @param {string} toggleGroup The toggle group name that we're searching form.\n * @param {boolean} exactMatch Whether we want an exact match we just want to match toggle groups that start with the given\n * toggle group name.\n * @returns {jQuery} The elements matching the given toggle group.\n */\n var getToggleGroupElements = function(root, toggleGroup, exactMatch) {\n if (exactMatch) {\n return root.find('[data-action=\"toggle\"][data-togglegroup=\"' + toggleGroup + '\"]');\n } else {\n return root.find('[data-action=\"toggle\"][data-togglegroup^=\"' + toggleGroup + '\"]');\n }\n };\n\n /**\n * Fetches the slave checkboxes for a given toggle group.\n *\n * @private\n * @param {jQuery} root The root jQuery element.\n * @param {string} toggleGroup The toggle group name.\n * @returns {jQuery} The slave checkboxes belonging to the toggle group.\n */\n var getAllSlaveCheckboxes = function(root, toggleGroup) {\n return getToggleGroupElements(root, toggleGroup, false).filter('[data-toggle=\"slave\"]');\n };\n\n /**\n * Fetches the master elements (checkboxes or buttons) that control the slave checkboxes in a given toggle group.\n *\n * @private\n * @param {jQuery} root The root jQuery element.\n * @param {string} toggleGroup The toggle group name.\n * @param {boolean} exactMatch\n * @returns {jQuery} The control elements belonging to the toggle group.\n */\n var getControlCheckboxes = function(root, toggleGroup, exactMatch) {\n return getToggleGroupElements(root, toggleGroup, exactMatch).filter('[data-toggle=\"master\"]');\n };\n\n /**\n * Fetches the action elements that perform actions on the selected checkboxes in a given toggle group.\n *\n * @private\n * @param {jQuery} root The root jQuery element.\n * @param {string} toggleGroup The toggle group name.\n * @returns {jQuery} The action elements belonging to the toggle group.\n */\n var getActionElements = function(root, toggleGroup) {\n return getToggleGroupElements(root, toggleGroup, true).filter('[data-toggle=\"action\"]');\n };\n\n /**\n * Toggles the slave checkboxes in a given toggle group when a master element in that toggle group is toggled.\n *\n * @private\n * @param {Object} e The event object.\n */\n var toggleSlavesFromMasters = function(e) {\n var root = e.data.root;\n var target = $(e.target);\n\n var toggleGroupName = target.data('togglegroup');\n var targetState;\n if (target.is(':checkbox')) {\n targetState = target.is(':checked');\n } else {\n targetState = target.data('checkall') === 1;\n }\n\n toggleSlavesToState(root, toggleGroupName, targetState);\n };\n\n /**\n * Toggles the slave checkboxes from the masters.\n *\n * @param {HTMLElement} root\n * @param {String} toggleGroupName\n */\n var updateSlavesFromMasterState = function(root, toggleGroupName) {\n // Normalise to jQuery Object.\n root = $(root);\n\n var target = getControlCheckboxes(root, toggleGroupName, false);\n var targetState;\n if (target.is(':checkbox')) {\n targetState = target.is(':checked');\n } else {\n targetState = target.data('checkall') === 1;\n }\n\n toggleSlavesToState(root, toggleGroupName, targetState);\n };\n\n /**\n * Toggles the master checkboxes and action elements in a given toggle group.\n *\n * @param {jQuery} root The root jQuery element.\n * @param {String} toggleGroupName The name of the toggle group\n */\n var toggleMastersAndActionElements = function(root, toggleGroupName) {\n var toggleGroupSlaves = getAllSlaveCheckboxes(root, toggleGroupName);\n if (toggleGroupSlaves.length > 0) {\n var toggleGroupCheckedSlaves = toggleGroupSlaves.filter(':checked');\n var targetState = toggleGroupSlaves.length === toggleGroupCheckedSlaves.length;\n\n // Make sure to toggle the exact master checkbox in the given toggle group.\n setMasterStates(root, toggleGroupName, targetState, true);\n // Enable the action elements if there's at least one checkbox checked in the given toggle group.\n // Disable otherwise.\n setActionElementStates(root, toggleGroupName, !toggleGroupCheckedSlaves.length);\n }\n };\n\n /**\n * Returns an array containing every toggle group level of a given toggle group.\n *\n * @param {String} toggleGroupName The name of the toggle group\n * @return {Array} toggleGroupLevels Array that contains every toggle group level of a given toggle group\n */\n var getToggleGroupLevels = function(toggleGroupName) {\n var toggleGroups = toggleGroupName.split(' ');\n var toggleGroupLevels = [];\n var toggleGroupLevel = '';\n\n toggleGroups.forEach(function(toggleGroupName) {\n toggleGroupLevel += ' ' + toggleGroupName;\n toggleGroupLevels.push(toggleGroupLevel.trim());\n });\n\n return toggleGroupLevels;\n };\n\n /**\n * Toggles the slave checkboxes to a specific state.\n *\n * @param {HTMLElement} root\n * @param {String} toggleGroupName\n * @param {Bool} targetState\n */\n var toggleSlavesToState = function(root, toggleGroupName, targetState) {\n var slaves = getAllSlaveCheckboxes(root, toggleGroupName);\n // Set the slave checkboxes from the masters and manually trigger the native 'change' event.\n slaves.prop('checked', targetState).trigger('change');\n // Get all checked slaves after the change of state.\n var checkedSlaves = slaves.filter(':checked');\n\n // Toggle the master checkbox in the given toggle group.\n setMasterStates(root, toggleGroupName, targetState, false);\n // Enable the action elements if there's at least one checkbox checked in the given toggle group. Disable otherwise.\n setActionElementStates(root, toggleGroupName, !checkedSlaves.length);\n\n // Get all toggle group levels and toggle accordingly all parent master checkboxes and action elements from each\n // level. Exclude the given toggle group (toggleGroupName) as the master checkboxes and action elements from this\n // level have been already toggled.\n var toggleGroupLevels = getToggleGroupLevels(toggleGroupName)\n .filter(toggleGroupLevel => toggleGroupLevel !== toggleGroupName);\n\n toggleGroupLevels.forEach(function(toggleGroupLevel) {\n // Toggle the master checkboxes action elements in the given toggle group level.\n toggleMastersAndActionElements(root, toggleGroupLevel);\n });\n\n PubSub.publish(events.checkboxToggled, {\n root: root,\n toggleGroupName: toggleGroupName,\n slaves: slaves,\n checkedSlaves: checkedSlaves,\n anyChecked: targetState,\n });\n };\n\n /**\n * Set the state for an entire group of checkboxes.\n *\n * @param {HTMLElement} root\n * @param {String} toggleGroupName\n * @param {Bool} targetState\n */\n var setGroupState = function(root, toggleGroupName, targetState) {\n // Normalise to jQuery Object.\n root = $(root);\n\n // Set the master and slaves.\n setMasterStates(root, toggleGroupName, targetState, true);\n toggleSlavesToState(root, toggleGroupName, targetState);\n };\n\n /**\n * Toggles the master checkboxes in a given toggle group when all or none of the slave checkboxes in the same toggle group\n * have been selected.\n *\n * @private\n * @param {Object} e The event object.\n */\n var toggleMastersFromSlaves = function(e) {\n var root = e.data.root;\n var target = $(e.target);\n var toggleGroupName = target.data('togglegroup');\n var slaves = getAllSlaveCheckboxes(root, toggleGroupName);\n var checkedSlaves = slaves.filter(':checked');\n\n // Get all toggle group levels for the given toggle group and toggle accordingly all master checkboxes\n // and action elements from each level.\n var toggleGroupLevels = getToggleGroupLevels(toggleGroupName);\n toggleGroupLevels.forEach(function(toggleGroupLevel) {\n // Toggle the master checkboxes action elements in the given toggle group level.\n toggleMastersAndActionElements(root, toggleGroupLevel);\n });\n\n PubSub.publish(events.checkboxToggled, {\n root: root,\n toggleGroupName: toggleGroupName,\n slaves: slaves,\n checkedSlaves: checkedSlaves,\n anyChecked: !!checkedSlaves.length,\n });\n };\n\n /**\n * Enables or disables the action elements.\n *\n * @private\n * @param {jQuery} root The root jQuery element.\n * @param {string} toggleGroupName The toggle group name of the action element(s).\n * @param {boolean} disableActionElements Whether to disable or to enable the action elements.\n */\n var setActionElementStates = function(root, toggleGroupName, disableActionElements) {\n getActionElements(root, toggleGroupName).prop('disabled', disableActionElements);\n };\n\n /**\n * Selects or deselects the master elements.\n *\n * @private\n * @param {jQuery} root The root jQuery element.\n * @param {string} toggleGroupName The toggle group name of the master element(s).\n * @param {boolean} targetState Whether to select (true) or deselect (false).\n * @param {boolean} exactMatch Whether to do an exact match for the toggle group name or not.\n */\n var setMasterStates = function(root, toggleGroupName, targetState, exactMatch) {\n // Set the master checkboxes value and ARIA labels..\n var masters = getControlCheckboxes(root, toggleGroupName, exactMatch);\n masters.prop('checked', targetState);\n masters.each(function(i, masterElement) {\n masterElement = $(masterElement);\n\n var targetString;\n if (targetState) {\n targetString = masterElement.data('toggle-deselectall');\n } else {\n targetString = masterElement.data('toggle-selectall');\n }\n\n if (masterElement.is(':checkbox')) {\n var masterLabel = root.find('[for=\"' + masterElement.attr('id') + '\"]');\n if (masterLabel.length) {\n if (masterLabel.html() !== targetString) {\n masterLabel.html(targetString);\n }\n }\n } else {\n masterElement.text(targetString);\n // Set the checkall data attribute.\n masterElement.data('checkall', targetState ? 0 : 1);\n }\n });\n };\n\n /**\n * Registers the event listeners.\n *\n * @private\n */\n var registerListeners = function() {\n if (!registered) {\n registered = true;\n\n var root = $(document.body);\n root.on('click', '[data-action=\"toggle\"][data-toggle=\"master\"]', {root: root}, toggleSlavesFromMasters);\n root.on('click', '[data-action=\"toggle\"][data-toggle=\"slave\"]', {root: root}, toggleMastersFromSlaves);\n }\n };\n\n return {\n init: function() {\n registerListeners();\n },\n events: events,\n setGroupState: setGroupState,\n updateSlavesFromMasterState: updateSlavesFromMasterState,\n };\n});\n"],"names":["define","$","PubSub","registered","events","checkboxToggled","getToggleGroupElements","root","toggleGroup","exactMatch","find","getAllSlaveCheckboxes","filter","getControlCheckboxes","toggleSlavesFromMasters","e","targetState","data","target","toggleGroupName","is","toggleSlavesToState","toggleMastersAndActionElements","toggleGroupSlaves","length","toggleGroupCheckedSlaves","setMasterStates","setActionElementStates","getToggleGroupLevels","toggleGroups","split","toggleGroupLevels","toggleGroupLevel","forEach","push","trim","slaves","prop","trigger","checkedSlaves","publish","anyChecked","toggleMastersFromSlaves","disableActionElements","getActionElements","masters","each","i","masterElement","targetString","masterLabel","attr","html","text","init","document","body","on","registerListeners","setGroupState","updateSlavesFromMasterState"],"mappings":";;;;;;;AAsBAA,iCAAO,CAAC,SAAU,gBAAgB,SAASC,EAAGC,YAQtCC,YAAa,EAQbC,OAAS,CACTC,gBAAiB,2CAajBC,uBAAyB,SAASC,KAAMC,YAAaC,mBACjDA,WACOF,KAAKG,KAAK,4CAA8CF,YAAc,MAEtED,KAAKG,KAAK,6CAA+CF,YAAc,OAYlFG,sBAAwB,SAASJ,KAAMC,oBAChCF,uBAAuBC,KAAMC,aAAa,GAAOI,OAAO,0BAY/DC,qBAAuB,SAASN,KAAMC,YAAaC,mBAC5CH,uBAAuBC,KAAMC,YAAaC,YAAYG,OAAO,2BAqBpEE,wBAA0B,SAASC,OAK/BC,YAJAT,KAAOQ,EAAEE,KAAKV,KACdW,OAASjB,EAAEc,EAAEG,QAEbC,gBAAkBD,OAAOD,KAAK,eAG9BD,YADAE,OAAOE,GAAG,aACIF,OAAOE,GAAG,YAEkB,IAA5BF,OAAOD,KAAK,YAG9BI,oBAAoBd,KAAMY,gBAAiBH,cA8B3CM,+BAAiC,SAASf,KAAMY,qBAC5CI,kBAAoBZ,sBAAsBJ,KAAMY,oBAChDI,kBAAkBC,OAAS,EAAG,KAC1BC,yBAA2BF,kBAAkBX,OAAO,YACpDI,YAAcO,kBAAkBC,SAAWC,yBAAyBD,OAGxEE,gBAAgBnB,KAAMY,gBAAiBH,aAAa,GAGpDW,uBAAuBpB,KAAMY,iBAAkBM,yBAAyBD,UAU5EI,qBAAuB,SAAST,qBAC5BU,aAAeV,gBAAgBW,MAAM,KACrCC,kBAAoB,GACpBC,iBAAmB,UAEvBH,aAAaI,SAAQ,SAASd,iBAC1Ba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