mod/bigbluebuttonbn/amd/src/guest_access_modal.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 importing presets.
  17. *
  18. * @module mod_bigbluebuttonbn/guest_access_modal
  19. * @copyright 2022 Blindside Networks Inc
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {getString} from 'core/str';
  23. import ModalForm from 'core_form/modalform';
  24. import {add as toastAdd, addToastRegion} from 'core/toast';
  25. import {
  26. exception as displayException,
  27. } from 'core/notification';
  28. const selectors = {
  29. showGuestAccessButton: '[data-action="show-guest-access"]',
  30. };
  31. /**
  32. * Intialise the object and click event to show the popup form
  33. *
  34. * @param {object} guestInfo
  35. * @param {string} guestInfo.id
  36. * @param {string} guestInfo.groupid
  37. * @param {string} guestInfo.guestjoinurl
  38. * @param {string} guestInfo.guestpassword
  39. */
  40. export const init = (guestInfo) => {
  41. const showGuestAccessButton = document.querySelector(selectors.showGuestAccessButton);
  42. if (showGuestAccessButton === null) {
  43. return;
  44. }
  45. const modalForm = new ModalForm({
  46. modalConfig: {
  47. title: getString('guestaccess_title', 'mod_bigbluebuttonbn'),
  48. large: true,
  49. },
  50. args: guestInfo,
  51. saveButtonText: getString('ok', 'core_moodle'),
  52. formClass: 'mod_bigbluebuttonbn\\form\\guest_add',
  53. });
  54. showGuestAccessButton.addEventListener('click', event => {
  55. modalForm.show().then(() => {
  56. addToastRegion(modalForm.modal.getRoot()[0]);
  57. return true;
  58. }).catch(displayException);
  59. modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, (e) => {
  60. // Remove toast region as if not it will be displayed on the closed modal.
  61. const modalElement = modalForm.modal.getRoot()[0];
  62. const regions = modalElement.querySelectorAll('.toast-wrapper');
  63. regions.forEach((reg) => reg.remove());
  64. if (e.detail.result) {
  65. if (e.detail.emailcount > 0) {
  66. toastAdd(getString('guestaccess_invite_success', 'mod_bigbluebuttonbn', e.detail),
  67. {
  68. type: 'success',
  69. }
  70. );
  71. }
  72. } else {
  73. toastAdd(getString('guestaccess_invite_failure', 'mod_bigbluebuttonbn', e.detail),
  74. {
  75. type: 'warning',
  76. }
  77. );
  78. }
  79. }, {once: true});
  80. event.stopPropagation();
  81. });
  82. };