user/amd/src/private_files.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. * Module to handle AJAX interactions with user private files
  17. *
  18. * @module core_user/private_files
  19. * @copyright 2020 Marina Glancy
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import DynamicForm from 'core_form/dynamicform';
  23. import ModalForm from 'core_form/modalform';
  24. import {getString} from 'core/str';
  25. import {add as addToast} from 'core/toast';
  26. /**
  27. * Initialize private files form as AJAX form
  28. *
  29. * @param {String} containerSelector
  30. * @param {String} formClass
  31. */
  32. export const initDynamicForm = (containerSelector, formClass) => {
  33. const form = new DynamicForm(document.querySelector(containerSelector), formClass);
  34. // When form is saved, refresh it to remove validation errors, if any:
  35. form.addEventListener(form.events.FORM_SUBMITTED, () => {
  36. form.load();
  37. getString('changessaved')
  38. .then(addToast)
  39. .catch(null);
  40. });
  41. // Reload the page on cancel.
  42. form.addEventListener(form.events.CANCEL_BUTTON_PRESSED, () => window.location.reload());
  43. };
  44. /**
  45. * Initialize private files form as Modal form
  46. *
  47. * @param {String} elementSelector
  48. * @param {String} formClass
  49. */
  50. export const initModal = (elementSelector, formClass) => {
  51. document.querySelector(elementSelector).addEventListener('click', function(e) {
  52. e.preventDefault();
  53. const form = new ModalForm({
  54. formClass,
  55. args: {nosubmit: true},
  56. modalConfig: {title: getString('privatefilesmanage')},
  57. returnFocus: e.target,
  58. });
  59. form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());
  60. form.show();
  61. });
  62. };