Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"actions.min.js","sources":["../src/actions.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 * Various actions on badges - enabling, disabling, etc.\n *\n * @module      core_badges/actions\n * @copyright   2024 Sara Arjona <sara@moodle.com>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport selectors from 'core_badges/selectors';\nimport Notification from 'core/notification';\nimport {prefetchStrings} from 'core/prefetch';\nimport {getString} from 'core/str';\nimport Ajax from 'core/ajax';\nimport Pending from 'core/pending';\nimport {dispatchEvent} from 'core/event_dispatcher';\nimport {add as addToast} from 'core/toast';\nimport * as reportEvents from 'core_reportbuilder/local/events';\nimport * as reportSelectors from 'core_reportbuilder/local/selectors';\n\n/**\n * Initialize module.\n */\nexport const init = () => {\n    prefetchStrings('core_badges', [\n        'reviewconfirm',\n        'activatesuccess',\n        'deactivatesuccess',\n        'awardoncron',\n        'numawardstat',\n    ]);\n    prefetchStrings('core', [\n        'confirm',\n        'enable',\n    ]);\n\n    registerEventListeners();\n};\n\n/**\n * Register events for delete preset option in action menu.\n */\nconst registerEventListeners = () => {\n    document.addEventListener('click', (event) => {\n        const enableOption = event.target.closest(selectors.actions.enablebadge);\n\n        if (enableOption) {\n            event.preventDefault();\n\n            // Use triggerElement to return focus to the action menu toggle.\n            const reportElement = event.target.closest(reportSelectors.regions.report);\n            const triggerElement = reportElement ? enableOption.closest('.dropdown').querySelector('.dropdown-toggle') : null;\n            const badgeId = enableOption.dataset.badgeid;\n            const badgeName = enableOption.dataset.badgename;\n\n            Notification.saveCancelPromise(\n                getString('confirm', 'core'),\n                getString('reviewconfirm', 'core_badges', badgeName),\n                getString('enable', 'core'),\n                {triggerElement}\n            ).then(() => {\n                return enableBadge(badgeId, badgeName, reportElement);\n            }).catch(() => {\n                return;\n            });\n        }\n\n        const disableOption = event.target.closest(selectors.actions.disablebadge);\n        if (disableOption) {\n            event.preventDefault();\n            const badgeId = disableOption.dataset.badgeid;\n            const badgeName = disableOption.dataset.badgename;\n            const reportElement = event.target.closest(reportSelectors.regions.report);\n            disableBadge(badgeId, badgeName, reportElement);\n        }\n    });\n};\n\n/**\n * Enable the badge.\n *\n * @param {Number} badgeId The id of the badge to enable.\n * @param {String} badgeName The name of the badge to enable.\n * @param {HTMLElement} reportElement the report element.\n */\nasync function enableBadge(badgeId, badgeName, reportElement) {\n    var request = {\n        methodname: 'core_badges_enable_badges',\n        args: {\n            badgeids: [badgeId],\n        }\n    };\n\n    const pendingPromise = new Pending('core_badges/enable');\n    try {\n        const result = await Ajax.call([request])[0];\n        if (reportElement) {\n            showEnableResultToast(badgeName, result);\n            // Report element is present, reload the table.\n            dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);\n        } else {\n            // Report element is not present, add the parameters to the current page to display the message.\n            const awards = result.result?.pop().awards;\n            document.location = document.location.pathname + `?id=${badgeId}&awards=${awards}`;\n        }\n    } catch (error) {\n        Notification.exception(error);\n    }\n    pendingPromise.resolve();\n}\n\n/**\n * Show the result of enabling a badge.\n *\n * @param {String} badgeName The name of the badge to enable.\n * @param {Object} result The result of enabling a badge.\n */\nfunction showEnableResultToast(badgeName, result) {\n    if (result.result?.length > 0) {\n        addToast(getString('activatesuccess', 'core_badges', badgeName), {type: 'success'});\n        const awards = result.result?.pop().awards;\n        if (awards == 'cron') {\n            addToast(getString('awardoncron', 'core_badges', {badgename: badgeName}));\n        } else if (awards > 0) {\n            addToast(getString('numawardstat', 'core_badges', {badgename: badgeName, awards: awards}));\n        }\n    } else if (result.warnings.length > 0) {\n        addToast(result.warnings[0].message, {type: 'danger'});\n    }\n}\n\n/**\n * Disable the badge.\n *\n * @param {Number} badgeId The id of the badge to disable.\n * @param {String} badgeName The name of the badge to enable.\n * @param {HTMLElement} reportElement the report element.\n */\nasync function disableBadge(badgeId, badgeName, reportElement) {\n    var request = {\n        methodname: 'core_badges_disable_badges',\n        args: {\n            badgeids: [badgeId],\n        }\n    };\n\n    try {\n        const result = await Ajax.call([request])[0];\n        if (reportElement) {\n            // Report element is present, show the message in a toast and reload the table.\n            showDisableResultToast(badgeName, result);\n            dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);\n        } else {\n            // Report element is not present, the page should be reloaded.\n            document.location = document.location.pathname + `?id=${badgeId}`;\n        }\n    } catch (error) {\n        Notification.exception(error);\n    }\n}\n\n/**\n * Show the result of disabling a badge.\n *\n * @param {String} badgeName The name of the badge to disable.\n * @param {Object} result The result of disabling a badge.\n */\nfunction showDisableResultToast(badgeName, result) {\n    if (result.result) {\n        addToast(\n            getString('deactivatesuccess', 'core_badges', badgeName),\n            {type: 'success'}\n        );\n    } else if (result.warnings.length > 0) {\n        addToast(\n            result.warnings[0].message,\n            {type: 'danger'}\n        );\n    }\n}\n"],"names":["registerEventListeners","document","addEventListener","event","enableOption","target","closest","selectors","actions","enablebadge","preventDefault","reportElement","reportSelectors","regions","report","triggerElement","querySelector","badgeId","dataset","badgeid","badgeName","badgename","saveCancelPromise","then","request","methodname","args","badgeids","pendingPromise","Pending","result","Ajax","call","length","type","awards","_result$result3","pop","warnings","message","showEnableResultToast","reportEvents","tableReload","preservePagination","_result$result","location","pathname","error","exception","resolve","enableBadge","catch","disableOption","disablebadge","showDisableResultToast","disableBadge"],"mappings":";;;;;;;4XAqCoB,mCACA,cAAe,CAC3B,gBACA,kBACA,oBACA,cACA,+CAEY,OAAQ,CACpB,UACA,WAGJA,gCAMEA,uBAAyB,KAC3B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