admin/tool/dataprivacy/amd/src/purposesactions.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 purposes actions.
  17. *
  18. * @module tool_dataprivacy/purposesactions
  19. * @copyright 2018 David Monllao
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. // This file is part of Moodle - http://moodle.org/
  23. //
  24. // Moodle is free software: you can redistribute it and/or modify
  25. // it under the terms of the GNU General Public License as published by
  26. // the Free Software Foundation, either version 3 of the License, or
  27. // (at your option) any later version.
  28. //
  29. // Moodle is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. // GNU General Public License for more details.
  33. //
  34. // You should have received a copy of the GNU General Public License
  35. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  36. /**
  37. * Module for purpose actions.
  38. *
  39. * @module tool_dataprivacy/purposeactions
  40. * @copyright 2018 David Monllao
  41. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42. */
  43. import * as Ajax from 'core/ajax';
  44. import * as Notification from 'core/notification';
  45. import * as Str from 'core/str';
  46. import ModalEvents from 'core/modal_events';
  47. import ModalSaveCancel from 'core/modal_save_cancel';
  48. /**
  49. * List of action selectors.
  50. *
  51. * @type {{DELETE: string}}
  52. */
  53. const ACTIONS = {
  54. DELETE: '[data-action="deletepurpose"]',
  55. };
  56. export default class PurposeActions {
  57. static init() {
  58. return new this();
  59. }
  60. constructor() {
  61. this.registerEvents();
  62. }
  63. deletePurpose(id) {
  64. return Ajax.call([{
  65. methodname: 'tool_dataprivacy_delete_purpose',
  66. args: {id}
  67. }])[0];
  68. }
  69. handleRemoval(id) {
  70. this.deletePurpose(id)
  71. .then((data) => {
  72. if (data.result) {
  73. document.querySelector(`tr[data-purposeid="${id}"]`)?.remove();
  74. } else {
  75. Notification.addNotification({
  76. message: data.warnings[0].message,
  77. type: 'error'
  78. });
  79. }
  80. return;
  81. })
  82. .catch(Notification.exception);
  83. }
  84. /**
  85. * Register event listeners.
  86. */
  87. registerEvents() {
  88. document.addEventListener('click', (e) => {
  89. const target = e.target.closest(ACTIONS.DELETE);
  90. if (!target) {
  91. return;
  92. }
  93. e.preventDefault();
  94. this.confirmRemoval(target);
  95. });
  96. }
  97. confirmRemoval(target) {
  98. const id = target.dataset.id;
  99. var purposename = target.dataset.name;
  100. var stringkeys = [
  101. {
  102. key: 'deletepurpose',
  103. component: 'tool_dataprivacy'
  104. },
  105. {
  106. key: 'deletepurposetext',
  107. component: 'tool_dataprivacy',
  108. param: purposename
  109. },
  110. {
  111. key: 'delete'
  112. }
  113. ];
  114. Str.get_strings(stringkeys).then(([
  115. title,
  116. body,
  117. save,
  118. ]) => ModalSaveCancel.create({
  119. title,
  120. body,
  121. buttons: {
  122. save,
  123. },
  124. show: true,
  125. removeOnClose: true,
  126. }))
  127. .then((modal) => {
  128. // Handle save event.
  129. modal.getRoot().on(ModalEvents.save, () => this.handleRemoval(id));
  130. return modal;
  131. })
  132. .catch(Notification.exception);
  133. }
  134. }