AutorÃa | Ultima modificación | Ver Log |
{"version":3,"file":"schema_backup_form.min.js","sources":["../src/schema_backup_form.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 * Schema selector javascript controls.\n *\n * This module controls:\n * - The select all feature.\n * - Disabling activities checkboxes when the section is not selected.\n *\n * @module core_backup/schem
a_backup_form\n * @copyright 2024 Ferran Recio <ferran@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Notification from 'core/notification';\nimport * as Templates from 'core/templates';\n\nconst Selectors = {\n action: '[data-mdl-action]',\n checkboxes: '#id_coursesettings input[type=\"checkbox\"]',\n firstSection: 'fieldset#id_coursesettings .fcontainer .grouped_settings.section_level',\n modCheckboxes: (modName) => `setting_activity_${modName}_`,\n};\n\nconst Suffixes = {\n userData: '_userdata',\n userInfo: '_userinfo',\n included: '_included',\n};\n\n/**\n * Adds select all/none links to the top of the backup/restore/import schema page.\n */\nexport default class BackupFormController {\n\n /**\n * Static module init method.\n * @param {Array<string>} modNames - The names of the modules.\n * @returns {BackupFormController}\n */\n static init(modNames) {\n return new BackupFormController(modNames);\n
}\n\n /**\n * Creates a new instance of the SchemaBackupForm class.\n * @param {Array<string>} modNames - The names of the modules.\n */\n constructor(modNames) {\n this.modNames = modNames;\n this.scanFormUserData();\n this.addSelectorsToPage();\n }\n\n /**\n * Detect the user data attribute from the form.\n *\n * @private\n */\n scanFormUserData() {\n this.withuserdata = false;\n this.userDataSuffix = Suffixes.userData;\n\n const checkboxes = document.querySelectorAll(Selectors.checkboxes);\n if (!checkboxes) {\n return;\n }\n // Depending on the form, user data inclusion is called userinfo or userdata.\n for (const checkbox of checkboxes) {\n const name = checkbox.name;\n if (name.endsWith(Suffixes.userData)) {\n this.withuserdata = true;\n break;\n } else if (name.endsWith(Suffixes.userInfo)) {\n this.wit
huserdata = true;\n this.userDataSuffix = Suffixes.userInfo;\n break;\n }\n }\n }\n\n /**\n * Initializes all related events.\n *\n * @private\n * @param {HTMLElement} element - The element to attach the events to.\n */\n initEvents(element) {\n element.addEventListener('click', (event) => {\n const action = event.target.closest(Selectors.action);\n if (!action) {\n return;\n }\n event.preventDefault();\n\n const suffix = (action.dataset?.mdlType == 'userdata') ? this.userDataSuffix : Suffixes.included;\n\n this.changeSelection(\n action.dataset.mdlAction == 'selectall',\n suffix,\n action.dataset?.mdlMod ?? null\n );\n });\n }\n\n /**\n * Changes the selection according to the params.\n *\n * @private\n * @param {boolean} checked - The checked state for the chec
kboxes.\n * @param {string} suffix - The checkboxes suffix\n * @param {string} [modName] - The module name.\n */\n changeSelection(checked, suffix, modName) {\n const prefix = modName ? Selectors.modCheckboxes(modName) : null;\n\n let formId;\n\n const checkboxes = document.querySelectorAll(Selectors.checkboxes);\n for (const checkbox of checkboxes) {\n formId = formId ?? checkbox.closest('form').getAttribute('id');\n\n if (prefix && !checkbox.name.startsWith(prefix)) {\n continue;\n }\n if (checkbox.name.endsWith(suffix)) {\n checkbox.checked = checked;\n }\n }\n\n // At this point, we really need to persuade the form we are part of to\n // update all of its disabledIf rules. However, as far as I can see,\n // given the way that lib/form/form.js is written, that is impossible.\n if (formId && M.form) {\n M.form.updateFormState(formId);\
n }\n }\n\n /**\n * Generates the full selectors element to add to the page.\n *\n * @private\n * @returns {HTMLElement} The selectors element.\n */\n generateSelectorsElement() {\n const links = document.createElement('div');\n links.id = 'backup_selectors';\n this.initEvents(links);\n this.renderSelectorsTemplate(links);\n return links;\n }\n\n /**\n * Load the select all template.\n *\n * @private\n * @param {HTMLElement} element the container\n */\n renderSelectorsTemplate(element) {\n const data = {\n modules: this.getModulesTemplateData(),\n withuserdata: (this.withuserdata) ? true : undefined,\n };\n Templates.renderForPromise(\n 'core_backup/formselectall',\n data\n ).then(({html, js}) => {\n return Templates.replaceNodeContents(element, html, js);\n }).catch(Notification.exception);\n }\n\n /**\n * Gene
rate the modules template data.\n *\n * @private\n * @returns {Array} of modules data.\n */\n getModulesTemplateData() {\n const modules = [];\n for (const modName in this.modNames) {\n if (!this.modNames.hasOwnProperty(modName)) {\n continue;\n }\n modules.push({\n modname: modName,\n heading: this.modNames[modName],\n });\n }\n return modules;\n }\n\n /**\n * Adds select all/none functionality to the backup form.\n *\n * @private\n */\n addSelectorsToPage() {\n const firstSection = document.querySelector(Selectors.firstSection);\n if (!firstSection) {\n // This is not a relevant page.\n return;\n }\n if (!firstSection.querySelector(Selectors.checkboxes)) {\n // No checkboxes.\n return;\n }\n\n // Add global select all/none options.\n const selector = this.g
enerateSelectorsElement();\n firstSection.parentNode.insertBefore(selector, firstSection);\n }\n}\n"],"names":["Selectors","modName","Suffixes","BackupFormController","modNames","constructor","scanFormUserData","addSelectorsToPage","withuserdata","userDataSuffix","checkboxes","document","querySelectorAll","checkbox","name","endsWith","initEvents","element","addEventListener","event","action","target","closest","preventDefault","suffix","dataset","mdlType","this","changeSelection","mdlAction","_action$dataset2","mdlMod","checked","prefix","formId","getAttribute","startsWith","M","form","updateFormState","generateSelectorsElement","links","createElement","id","renderSelectorsTemplate","data","modules","getModulesTemplateData","undefined","Templates","renderForPromise","then","_ref","html","js","replaceNodeContents","catch","Notification","exception","hasOwnProperty","push","modname","heading","firstSection","querySelector","selector","parentNode","insertBefore"],"mappings":";;;;;;;;;;;olCA8BMA,iBACM,
oBADNA,qBAEU,4CAFVA,uBAGY,yEAHZA,wBAIcC,oCAAgCA,aAG9CC,kBACQ,YADRA,kBAEQ,YAFRA,kBAGQ,kBAMOC,iCAOLC,iBACD,IAAID,qBAAqBC,UAOpCC,YA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