node-new-comments-link.es6.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file
  3. * Attaches behaviors for the Comment module's "X new comments" link.
  4. *
  5. * May only be loaded for authenticated users, with the History module
  6. * installed.
  7. */
  8. (function ($, Drupal, drupalSettings) {
  9. /**
  10. * Render "X new comments" links wherever necessary.
  11. *
  12. * @type {Drupal~behavior}
  13. *
  14. * @prop {Drupal~behaviorAttach} attach
  15. * Attaches new comment links behavior.
  16. */
  17. Drupal.behaviors.nodeNewCommentsLink = {
  18. attach(context) {
  19. // Collect all "X new comments" node link placeholders (and their
  20. // corresponding node IDs) newer than 30 days ago that have not already
  21. // been read after their last comment timestamp.
  22. const nodeIDs = [];
  23. const $placeholders = $(context)
  24. .find('[data-history-node-last-comment-timestamp]')
  25. .once('history')
  26. .filter(function () {
  27. const $placeholder = $(this);
  28. const lastCommentTimestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
  29. const nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
  30. if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
  31. nodeIDs.push(nodeID);
  32. // Hide this placeholder link until it is certain we'll need it.
  33. hide($placeholder);
  34. return true;
  35. }
  36. // Remove this placeholder link from the DOM because we won't need
  37. // it.
  38. remove($placeholder);
  39. return false;
  40. });
  41. if ($placeholders.length === 0) {
  42. return;
  43. }
  44. // Perform an AJAX request to retrieve node read timestamps.
  45. Drupal.history.fetchTimestamps(nodeIDs, () => {
  46. processNodeNewCommentLinks($placeholders);
  47. });
  48. },
  49. };
  50. /**
  51. * Hides a "new comment" element.
  52. *
  53. * @param {jQuery} $placeholder
  54. * The placeholder element of the new comment link.
  55. *
  56. * @return {jQuery}
  57. * The placeholder element passed in as a parameter.
  58. */
  59. function hide($placeholder) {
  60. return $placeholder
  61. // Find the parent <li>.
  62. .closest('.comment-new-comments')
  63. // Find the preceding <li>, if any, and give it the 'last' class.
  64. .prev()
  65. .addClass('last')
  66. // Go back to the parent <li> and hide it.
  67. .end()
  68. .hide();
  69. }
  70. /**
  71. * Removes a "new comment" element.
  72. *
  73. * @param {jQuery} $placeholder
  74. * The placeholder element of the new comment link.
  75. */
  76. function remove($placeholder) {
  77. hide($placeholder).remove();
  78. }
  79. /**
  80. * Shows a "new comment" element.
  81. *
  82. * @param {jQuery} $placeholder
  83. * The placeholder element of the new comment link.
  84. *
  85. * @return {jQuery}
  86. * The placeholder element passed in as a parameter.
  87. */
  88. function show($placeholder) {
  89. return $placeholder
  90. // Find the parent <li>.
  91. .closest('.comment-new-comments')
  92. // Find the preceding <li>, if any, and remove its 'last' class, if any.
  93. .prev()
  94. .removeClass('last')
  95. // Go back to the parent <li> and show it.
  96. .end()
  97. .show();
  98. }
  99. /**
  100. * Processes new comment links and adds appropriate text in relevant cases.
  101. *
  102. * @param {jQuery} $placeholders
  103. * The placeholder elements of the current page.
  104. */
  105. function processNodeNewCommentLinks($placeholders) {
  106. // Figure out which placeholders need the "x new comments" links.
  107. const $placeholdersToUpdate = {};
  108. let fieldName = 'comment';
  109. let $placeholder;
  110. $placeholders.each((index, placeholder) => {
  111. $placeholder = $(placeholder);
  112. const timestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
  113. fieldName = $placeholder.attr('data-history-node-field-name');
  114. const nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
  115. const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
  116. // Queue this placeholder's "X new comments" link to be downloaded from
  117. // the server.
  118. if (timestamp > lastViewTimestamp) {
  119. $placeholdersToUpdate[nodeID] = $placeholder;
  120. }
  121. // No "X new comments" link necessary; remove it from the DOM.
  122. else {
  123. remove($placeholder);
  124. }
  125. });
  126. // Perform an AJAX request to retrieve node view timestamps.
  127. const nodeIDs = Object.keys($placeholdersToUpdate);
  128. if (nodeIDs.length === 0) {
  129. return;
  130. }
  131. /**
  132. * Renders the "X new comments" links.
  133. *
  134. * Either use the data embedded in the page or perform an AJAX request to
  135. * retrieve the same data.
  136. *
  137. * @param {object} results
  138. * Data about new comment links indexed by nodeID.
  139. */
  140. function render(results) {
  141. Object.keys(results || {}).forEach((nodeID) => {
  142. if ($placeholdersToUpdate.hasOwnProperty(nodeID)) {
  143. $placeholdersToUpdate[nodeID]
  144. .attr('href', results[nodeID].first_new_comment_link)
  145. .text(Drupal.formatPlural(results[nodeID].new_comment_count, '1 new comment', '@count new comments'))
  146. .removeClass('hidden');
  147. show($placeholdersToUpdate[nodeID]);
  148. }
  149. });
  150. }
  151. if (drupalSettings.comment && drupalSettings.comment.newCommentsLinks) {
  152. render(drupalSettings.comment.newCommentsLinks.node[fieldName]);
  153. }
  154. else {
  155. $.ajax({
  156. url: Drupal.url('comments/render_new_comments_node_links'),
  157. type: 'POST',
  158. data: { 'node_ids[]': nodeIDs, field_name: fieldName },
  159. dataType: 'json',
  160. success: render,
  161. });
  162. }
  163. }
  164. }(jQuery, Drupal, drupalSettings));