mod/forum/amd/src/favourite_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/favourite_toggle
  20. * @copyright 2019 Peter Dias <peter@moodle.com>
  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/str',
  30. ], function(
  31. $,
  32. Templates,
  33. Notification,
  34. Repository,
  35. Selectors,
  36. String
  37. ) {
  38. /**
  39. * Register event listeners for the subscription toggle.
  40. *
  41. * @param {object} root The discussion list root element
  42. * @param {boolean} preventDefault Should the default action of the event be prevented
  43. * @param {function} callback Success callback
  44. */
  45. var registerEventListeners = function(root, preventDefault, callback) {
  46. root.on('click', Selectors.favourite.toggle, function(e) {
  47. var toggleElement = $(this);
  48. var forumId = toggleElement.data('forumid');
  49. var discussionId = toggleElement.data('discussionid');
  50. var subscriptionState = toggleElement.data('targetstate');
  51. Repository.setFavouriteDiscussionState(forumId, discussionId, subscriptionState)
  52. .then(function(context) {
  53. return callback(toggleElement, context);
  54. })
  55. .then(function() {
  56. return String.get_string("favouriteupdated", "forum")
  57. .done(function(s) {
  58. return Notification.addNotification({
  59. message: s,
  60. type: "info"
  61. });
  62. });
  63. })
  64. .catch(Notification.exception);
  65. if (preventDefault) {
  66. e.preventDefault();
  67. }
  68. });
  69. };
  70. return {
  71. init: registerEventListeners
  72. };
  73. });