mod/data/amd/src/deletepreset.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 deleting a database as a preset.
  17. *
  18. * @module mod_data/deletepreset
  19. * @copyright 2022 Amaia Anabitarte <amaia@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 {getString} from 'core/str';
  25. import Ajax from 'core/ajax';
  26. import Url from 'core/url';
  27. const selectors = {
  28. deletePresetButton: '[data-action="deletepreset"]',
  29. };
  30. /**
  31. * Initialize module
  32. */
  33. export const init = () => {
  34. prefetchStrings('mod_data', [
  35. 'deleteconfirm',
  36. 'deletewarning',
  37. ]);
  38. prefetchStrings('core', [
  39. 'delete',
  40. ]);
  41. registerEventListeners();
  42. };
  43. /**
  44. * Register events for delete preset option in action menu.
  45. */
  46. const registerEventListeners = () => {
  47. document.addEventListener('click', (event) => {
  48. const deleteOption = event.target.closest(selectors.deletePresetButton);
  49. if (deleteOption) {
  50. event.preventDefault();
  51. deletePresetConfirm(deleteOption);
  52. }
  53. });
  54. };
  55. /**
  56. * Show the confirmation modal to delete the preset.
  57. *
  58. * @param {HTMLElement} deleteOption the element to delete.
  59. */
  60. const deletePresetConfirm = (deleteOption) => {
  61. const presetName = deleteOption.getAttribute('data-presetname');
  62. const dataId = deleteOption.getAttribute('data-dataid');
  63. Notification.deleteCancelPromise(
  64. getString('deleteconfirm', 'mod_data', presetName),
  65. getString('deletewarning', 'mod_data'),
  66. ).then(() => {
  67. return deletePreset(dataId, presetName);
  68. }).catch(() => {
  69. return;
  70. });
  71. };
  72. /**
  73. * Delete site user preset.
  74. *
  75. * @param {int} dataId The id of the current database activity.
  76. * @param {string} presetName The preset name to delete.
  77. * @return {promise} Resolved with the result and warnings of deleting a preset.
  78. */
  79. async function deletePreset(dataId, presetName) {
  80. var request = {
  81. methodname: 'mod_data_delete_saved_preset',
  82. args: {
  83. dataid: dataId,
  84. presetnames: {presetname: presetName},
  85. }
  86. };
  87. try {
  88. await Ajax.call([request])[0];
  89. window.location.href = Url.relativeUrl(
  90. 'mod/data/preset.php',
  91. {
  92. d: dataId,
  93. },
  94. false
  95. );
  96. } catch (error) {
  97. Notification.exception(error);
  98. }
  99. }