mod/forum/amd/src/posts_list.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/posts_list
  23. * @copyright 2019 Peter Dias
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. define([
  27. 'jquery',
  28. 'core/templates',
  29. 'core/notification',
  30. 'core/pending',
  31. 'mod_forum/selectors',
  32. 'mod_forum/inpage_reply',
  33. 'core_form/changechecker',
  34. ], function(
  35. $,
  36. Templates,
  37. Notification,
  38. Pending,
  39. Selectors,
  40. InPageReply,
  41. FormChangeChecker
  42. ) {
  43. var registerEventListeners = function(root, throttlingwarningmsg) {
  44. root.on('click', Selectors.post.inpageReplyLink, function(e) {
  45. e.preventDefault();
  46. // After adding a reply a url hash is being generated that scrolls (points) to the newly added reply.
  47. // The hash being present causes this scrolling behavior to the particular reply to persists even when
  48. // another, non-related in-page replay link is being clicked which ultimately causes a bad user experience.
  49. // A particular solution for this problem would be changing the browser's history state when a url hash is
  50. // present.
  51. if (window.location.hash) {
  52. // Remove the fragment identifier from the url.
  53. var url = window.location.href.split('#')[0];
  54. history.pushState({}, document.title, url);
  55. }
  56. var pending = new Pending('inpage-reply');
  57. var currentTarget = $(e.currentTarget).parents(Selectors.post.forumCoreContent);
  58. var currentSubject = currentTarget.find(Selectors.post.forumSubject);
  59. var currentRoot = $(e.currentTarget).parents(Selectors.post.forumContent);
  60. var context = {
  61. postid: $(currentRoot).data('post-id'),
  62. "reply_url": $(e.currentTarget).attr('href'),
  63. sesskey: M.cfg.sesskey,
  64. parentsubject: currentSubject.data('replySubject'),
  65. canreplyprivately: $(e.currentTarget).data('can-reply-privately'),
  66. postformat: InPageReply.CONTENT_FORMATS.MOODLE,
  67. throttlingwarningmsg: throttlingwarningmsg
  68. };
  69. if (!currentRoot.find(Selectors.post.inpageReplyContent).length) {
  70. Templates.render('mod_forum/inpage_reply', context)
  71. .then(function(html, js) {
  72. return Templates.appendNodeContents(currentTarget, html, js);
  73. })
  74. .then(function() {
  75. return currentRoot.find(Selectors.post.inpageReplyContent)
  76. .slideToggle(300, pending.resolve).find('textarea').focus();
  77. })
  78. .then(function() {
  79. FormChangeChecker.watchFormById(`inpage-reply-${context.postid}`);
  80. return;
  81. })
  82. .catch(Notification.exception);
  83. } else {
  84. var form = currentRoot.find(Selectors.post.inpageReplyContent);
  85. form.slideToggle(300, pending.resolve);
  86. if (form.is(':visible')) {
  87. form.find('textarea').focus();
  88. }
  89. }
  90. });
  91. };
  92. return {
  93. init: function(root, throttlingwarningmsg) {
  94. registerEventListeners(root, throttlingwarningmsg);
  95. InPageReply.init(root);
  96. }
  97. };
  98. });