mod/forum/amd/src/pin_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. * This module is the highest level module for the calendar. It is
  17. * responsible for initialising all of the components required for
  18. * the calendar to run. It also coordinates the interaction between
  19. * components by listening for and responding to different events
  20. * triggered within the calendar UI.
  21. *
  22. * @module mod_forum/pin_toggle
  23. * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. define([
  27. 'jquery',
  28. 'core/ajax',
  29. 'core/str',
  30. 'core/templates',
  31. 'core/notification',
  32. 'mod_forum/repository',
  33. 'mod_forum/selectors',
  34. 'core/str',
  35. ], function(
  36. $,
  37. Ajax,
  38. Str,
  39. Templates,
  40. Notification,
  41. Repository,
  42. Selectors,
  43. String
  44. ) {
  45. /**
  46. * Registery event listeners for the pin toggle.
  47. *
  48. * @param {object} root The calendar root element
  49. * @param {boolean} preventDefault Should the default action of the event be prevented
  50. * @param {function} callback Success callback
  51. */
  52. var registerEventListeners = function(root, preventDefault, callback) {
  53. root.on('click', Selectors.pin.toggle, function(e) {
  54. var toggleElement = $(this);
  55. var forumid = toggleElement.data('forumid');
  56. var discussionid = toggleElement.data('discussionid');
  57. var pinstate = toggleElement.data('targetstate');
  58. Repository.setPinDiscussionState(forumid, discussionid, pinstate)
  59. .then(function(context) {
  60. return callback(toggleElement, context);
  61. })
  62. .then(function() {
  63. return String.get_string("pinupdated", "forum")
  64. .done(function(s) {
  65. return Notification.addNotification({
  66. message: s,
  67. type: "info"
  68. });
  69. });
  70. })
  71. .fail(Notification.exception);
  72. if (preventDefault) {
  73. e.preventDefault();
  74. }
  75. });
  76. };
  77. return {
  78. init: registerEventListeners
  79. };
  80. });