lib/amd/src/userfeedback.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. * Handle clicking on action links of the feedback alert.
  17. *
  18. * @module core/userfeedback
  19. * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Ajax from 'core/ajax';
  23. import Notification from 'core/notification';
  24. const Selectors = {
  25. regions: {
  26. root: '[data-region="core/userfeedback"]',
  27. },
  28. actions: {},
  29. };
  30. Selectors.actions.give = `${Selectors.regions.root} [data-action="give"]`;
  31. Selectors.actions.remind = `${Selectors.regions.root} [data-action="remind"]`;
  32. /**
  33. * Attach the necessary event handlers to the action links
  34. */
  35. export const registerEventListeners = () => {
  36. document.addEventListener('click', e => {
  37. const giveAction = e.target.closest(Selectors.actions.give);
  38. if (giveAction) {
  39. e.preventDefault();
  40. if (!window.open(giveAction.href)) {
  41. throw new Error('Unable to open popup');
  42. }
  43. Promise.resolve(giveAction)
  44. .then(hideRoot)
  45. .then(recordAction)
  46. .catch(Notification.exception);
  47. }
  48. const remindAction = e.target.closest(Selectors.actions.remind);
  49. if (remindAction) {
  50. e.preventDefault();
  51. Promise.resolve(remindAction)
  52. .then(hideRoot)
  53. .then(recordAction)
  54. .catch(Notification.exception);
  55. }
  56. });
  57. };
  58. /**
  59. * Record the action that the user took.
  60. *
  61. * @param {HTMLElement} clickedItem The action element that the user chose.
  62. * @returns {Promise}
  63. */
  64. const recordAction = clickedItem => {
  65. if (clickedItem.dataset.record) {
  66. return Ajax.call([{
  67. methodname: 'core_create_userfeedback_action_record',
  68. args: {
  69. action: clickedItem.dataset.action,
  70. contextid: M.cfg.contextid,
  71. }
  72. }])[0];
  73. }
  74. return Promise.resolve();
  75. };
  76. /**
  77. * Hide the root node of the CTA notification.
  78. *
  79. * @param {HTMLElement} clickedItem The action element that the user chose.
  80. * @returns {HTMLElement}
  81. */
  82. const hideRoot = clickedItem => {
  83. if (clickedItem.dataset.hide) {
  84. clickedItem.closest(Selectors.regions.root).remove();
  85. }
  86. return clickedItem;
  87. };