mod/forum/amd/src/lock_toggle.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 the manual locking of individual discussions
  17. *
  18. * @module mod_forum/lock_toggle
  19. * @copyright 2019 Peter Dias <peter@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define([
  23. 'jquery',
  24. 'core/templates',
  25. 'core/notification',
  26. 'mod_forum/repository',
  27. 'mod_forum/selectors',
  28. ], function(
  29. $,
  30. Templates,
  31. Notification,
  32. Repository,
  33. Selectors
  34. ) {
  35. /**
  36. * Register event listeners for the subscription toggle.
  37. *
  38. * @param {object} root The discussion list root element
  39. * @param {boolean} preventDefault Should the default action of the event be prevented
  40. */
  41. var registerEventListeners = function(root, preventDefault) {
  42. root.on('click', Selectors.lock.toggle, function(e) {
  43. var toggleElement = $(this);
  44. var forumId = toggleElement.data('forumid');
  45. var discussionId = toggleElement.data('discussionid');
  46. var state = toggleElement.data('state');
  47. Repository.setDiscussionLockState(forumId, discussionId, state)
  48. .then(function() {
  49. return location.reload();
  50. })
  51. .catch(Notification.exception);
  52. if (preventDefault) {
  53. e.preventDefault();
  54. }
  55. });
  56. };
  57. return {
  58. init: registerEventListeners
  59. };
  60. });