PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/amd/src/discussion_list.js

https://github.com/mackensen/moodle
JavaScript | 142 lines | 113 code | 8 blank | 21 comment | 4 complexity | 0daadf645c65ddc9f2966a6b8ba1b75a MD5 | raw file
  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 for the list of discussions on when viewing a forum.
  17. *
  18. * @module mod_forum/discussion_list
  19. * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define([
  23. 'jquery',
  24. 'core/templates',
  25. 'core/str',
  26. 'core/notification',
  27. 'mod_forum/subscription_toggle',
  28. 'mod_forum/selectors',
  29. 'mod_forum/repository',
  30. 'core/pubsub',
  31. 'mod_forum/forum_events',
  32. ], function(
  33. $,
  34. Templates,
  35. Str,
  36. Notification,
  37. SubscriptionToggle,
  38. Selectors,
  39. Repository,
  40. PubSub,
  41. ForumEvents
  42. ) {
  43. var registerEventListeners = function(root) {
  44. PubSub.subscribe(ForumEvents.SUBSCRIPTION_TOGGLED, function(data) {
  45. var discussionId = data.discussionId;
  46. var subscribed = data.subscriptionState;
  47. var discussionListItem = root.find(Selectors.discussion.item + '[data-discussionid= ' + discussionId + ']');
  48. var subscribedLabel = discussionListItem.find(Selectors.discussion.subscribedLabel);
  49. if (subscribed) {
  50. discussionListItem.addClass('subscribed');
  51. subscribedLabel.removeAttr('hidden');
  52. } else {
  53. discussionListItem.removeClass('subscribed');
  54. subscribedLabel.attr('hidden', true);
  55. }
  56. });
  57. root.on('click', Selectors.favourite.toggle, function() {
  58. var toggleElement = $(this);
  59. var forumId = toggleElement.data('forumid');
  60. var discussionId = toggleElement.data('discussionid');
  61. var subscriptionState = toggleElement.data('targetstate');
  62. Repository.setFavouriteDiscussionState(forumId, discussionId, subscriptionState)
  63. .then(function() {
  64. return location.reload();
  65. })
  66. .catch(Notification.exception);
  67. });
  68. root.on('click', Selectors.pin.toggle, function(e) {
  69. e.preventDefault();
  70. var toggleElement = $(this);
  71. var forumId = toggleElement.data('forumid');
  72. var discussionId = toggleElement.data('discussionid');
  73. var state = toggleElement.data('targetstate');
  74. Repository.setPinDiscussionState(forumId, discussionId, state)
  75. .then(function() {
  76. return location.reload();
  77. })
  78. .catch(Notification.exception);
  79. });
  80. root.on('click', Selectors.lock.toggle, function(e) {
  81. var toggleElement = $(this);
  82. var forumId = toggleElement.data('forumid');
  83. var discussionId = toggleElement.data('discussionid');
  84. var state = toggleElement.data('state');
  85. Repository.setDiscussionLockState(forumId, discussionId, state)
  86. .then(function(context) {
  87. var icon = toggleElement.parents(Selectors.summary.actions).find(Selectors.lock.icon);
  88. var lockedLabel = toggleElement.parents(Selectors.discussion.item).find(Selectors.discussion.lockedLabel);
  89. if (context.locked) {
  90. icon.removeClass('hidden');
  91. lockedLabel.removeAttr('hidden');
  92. } else {
  93. icon.addClass('hidden');
  94. lockedLabel.attr('hidden', true);
  95. }
  96. return context;
  97. })
  98. .then(function(context) {
  99. context.forumid = forumId;
  100. return Templates.render('mod_forum/discussion_lock_toggle', context);
  101. })
  102. .then(function(html, js) {
  103. return Templates.replaceNode(toggleElement, html, js);
  104. })
  105. .then(function() {
  106. return Str.get_string('lockupdated', 'forum')
  107. .done(function(s) {
  108. return Notification.addNotification({
  109. message: s,
  110. type: "info"
  111. });
  112. });
  113. })
  114. .catch(Notification.exception);
  115. e.preventDefault();
  116. });
  117. };
  118. return {
  119. init: function(root) {
  120. SubscriptionToggle.init(root, false, function(toggleElement, context) {
  121. var toggleId = toggleElement.attr('id');
  122. var newTargetState = context.userstate.subscribed ? 0 : 1;
  123. toggleElement.data('targetstate', newTargetState);
  124. var stringKey = context.userstate.subscribed ? 'unsubscribediscussion' : 'subscribediscussion';
  125. return Str.get_string(stringKey, 'mod_forum')
  126. .then(function(string) {
  127. toggleElement.closest('td').find('label[for="' + toggleId + '"]').find('span').text(string);
  128. return string;
  129. });
  130. });
  131. registerEventListeners(root);
  132. }
  133. };
  134. });