admin/tool/dataprivacy/amd/src/categoriesactions.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. * AMD module for categories actions.
  17. *
  18. * @module tool_dataprivacy/categoriesactions
  19. * @copyright 2018 David Monllao
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import * as Ajax from 'core/ajax';
  23. import * as Notification from 'core/notification';
  24. import * as Str from 'core/str';
  25. import ModalEvents from 'core/modal_events';
  26. import ModalSaveCancel from 'core/modal_save_cancel';
  27. /**
  28. * List of action selectors.
  29. *
  30. * @type {{DELETE: string}}
  31. */
  32. const ACTIONS = {
  33. DELETE: '[data-action="deletecategory"]',
  34. };
  35. export default class CategoriesActions {
  36. static init() {
  37. return new this();
  38. }
  39. /**
  40. * CategoriesActions class.
  41. */
  42. constructor() {
  43. this.registerEvents();
  44. }
  45. deleteCategory(id) {
  46. return Ajax.call([{
  47. methodname: 'tool_dataprivacy_delete_category',
  48. args: {id}
  49. }])[0];
  50. }
  51. handleCategoryRemoval(id) {
  52. this.deleteCategory(id)
  53. .then((data) => {
  54. if (data.result) {
  55. document.querySelector(`tr[data-categoryid="${id}"]`)?.remove();
  56. } else {
  57. Notification.addNotification({
  58. message: data.warnings[0].message,
  59. type: 'error'
  60. });
  61. }
  62. return;
  63. })
  64. .catch(Notification.exception);
  65. }
  66. /**
  67. * Register event listeners.
  68. */
  69. registerEvents() {
  70. document.addEventListener('click', (e) => {
  71. const target = e.target.closest(ACTIONS.DELETE);
  72. if (!target) {
  73. return;
  74. }
  75. e.preventDefault();
  76. this.confirmCategoryRemoval(target);
  77. });
  78. }
  79. confirmCategoryRemoval(target) {
  80. const id = target.dataset.id;
  81. var categoryname = target.dataset.name;
  82. var stringkeys = [
  83. {
  84. key: 'deletecategory',
  85. component: 'tool_dataprivacy'
  86. },
  87. {
  88. key: 'deletecategorytext',
  89. component: 'tool_dataprivacy',
  90. param: categoryname
  91. },
  92. {
  93. key: 'delete'
  94. }
  95. ];
  96. Str.get_strings(stringkeys).then(([
  97. title,
  98. body,
  99. save,
  100. ]) => ModalSaveCancel.create({
  101. title,
  102. body,
  103. buttons: {
  104. save,
  105. },
  106. show: true,
  107. removeOnClose: true,
  108. }))
  109. .then((modal) => {
  110. // Handle save event.
  111. modal.getRoot().on(ModalEvents.save, () => this.handleCategoryRemoval(id));
  112. return modal;
  113. })
  114. .catch(Notification.exception);
  115. }
  116. }