mod/data/amd/src/resetalltemplates.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Javascript module for reseting all templates.
  17. *
  18. * @module mod_data/resetalltemplates
  19. * @copyright 2022 Ferran Recio <ferran@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Notification from 'core/notification';
  23. import {prefetchStrings} from 'core/prefetch';
  24. import {get_string as getString} from 'core/str';
  25. const selectors = {
  26. resetAllTemplatesAction: '[data-action="resetalltemplates"]',
  27. };
  28. /**
  29. * Initialize module
  30. */
  31. export const init = () => {
  32. prefetchStrings('mod_data', [
  33. 'resetalltemplatesconfirmtitle',
  34. 'resetalltemplatesconfirm',
  35. ]);
  36. prefetchStrings('core', [
  37. 'reset',
  38. ]);
  39. registerEventListeners();
  40. };
  41. /**
  42. * Register events for option in action menu.
  43. */
  44. const registerEventListeners = () => {
  45. document.addEventListener('click', (event) => {
  46. const actionLink = event.target.closest(selectors.resetAllTemplatesAction);
  47. if (actionLink) {
  48. event.preventDefault();
  49. resetAllTemplatesConfirm(actionLink);
  50. }
  51. });
  52. };
  53. /**
  54. * Show the confirmation modal to reset all the templates.
  55. *
  56. * @param {HTMLElement} actionLink the element that triggers the action.
  57. */
  58. const resetAllTemplatesConfirm = async(actionLink) => {
  59. try {
  60. await Notification.saveCancelPromise(
  61. getString('resetalltemplatesconfirmtitle', 'mod_data'),
  62. getString('resetalltemplatesconfirm', 'mod_data'),
  63. getString('reset', 'core'),
  64. );
  65. window.location = actionLink.href;
  66. } catch (error) {
  67. return;
  68. }
  69. };