mod/forum/amd/src/subscription_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 discussion subscription toggling on a discussion list in
  17. * the forum view.
  18. *
  19. * @module mod_forum/subscription_toggle
  20. * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. define([
  24. 'jquery',
  25. 'core/templates',
  26. 'core/notification',
  27. 'mod_forum/repository',
  28. 'mod_forum/selectors',
  29. 'core/pubsub',
  30. 'mod_forum/forum_events',
  31. ], function(
  32. $,
  33. Templates,
  34. Notification,
  35. Repository,
  36. Selectors,
  37. PubSub,
  38. ForumEvents
  39. ) {
  40. /**
  41. * Register event listeners for the subscription toggle.
  42. *
  43. * @param {object} root The discussion list root element
  44. * @param {boolean} preventDefault Should the default action of the event be prevented
  45. * @param {function} callback Success callback
  46. */
  47. var registerEventListeners = function(root, preventDefault, callback) {
  48. root.on('click', Selectors.subscription.toggle, function(e) {
  49. var toggleElement = $(this);
  50. var forumId = toggleElement.data('forumid');
  51. var discussionId = toggleElement.data('discussionid');
  52. var subscriptionState = toggleElement.data('targetstate');
  53. Repository.setDiscussionSubscriptionState(forumId, discussionId, subscriptionState)
  54. .then(function(context) {
  55. PubSub.publish(ForumEvents.SUBSCRIPTION_TOGGLED, {
  56. discussionId: discussionId,
  57. subscriptionState: subscriptionState
  58. });
  59. return callback(toggleElement, context);
  60. })
  61. .catch(Notification.exception);
  62. if (preventDefault) {
  63. e.preventDefault();
  64. }
  65. });
  66. };
  67. return {
  68. init: registerEventListeners
  69. };
  70. });