node-new-comments-link.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, drupalSettings) {
  8. Drupal.behaviors.nodeNewCommentsLink = {
  9. attach: function attach(context) {
  10. var nodeIDs = [];
  11. var $placeholders = $(context).find('[data-history-node-last-comment-timestamp]').once('history').filter(function () {
  12. var $placeholder = $(this);
  13. var lastCommentTimestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
  14. var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
  15. if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
  16. nodeIDs.push(nodeID);
  17. hide($placeholder);
  18. return true;
  19. }
  20. remove($placeholder);
  21. return false;
  22. });
  23. if ($placeholders.length === 0) {
  24. return;
  25. }
  26. Drupal.history.fetchTimestamps(nodeIDs, function () {
  27. processNodeNewCommentLinks($placeholders);
  28. });
  29. }
  30. };
  31. function hide($placeholder) {
  32. return $placeholder.closest('.comment-new-comments').prev().addClass('last').end().hide();
  33. }
  34. function remove($placeholder) {
  35. hide($placeholder).remove();
  36. }
  37. function show($placeholder) {
  38. return $placeholder.closest('.comment-new-comments').prev().removeClass('last').end().show();
  39. }
  40. function processNodeNewCommentLinks($placeholders) {
  41. var $placeholdersToUpdate = {};
  42. var fieldName = 'comment';
  43. var $placeholder = void 0;
  44. $placeholders.each(function (index, placeholder) {
  45. $placeholder = $(placeholder);
  46. var timestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
  47. fieldName = $placeholder.attr('data-history-node-field-name');
  48. var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
  49. var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
  50. if (timestamp > lastViewTimestamp) {
  51. $placeholdersToUpdate[nodeID] = $placeholder;
  52. } else {
  53. remove($placeholder);
  54. }
  55. });
  56. var nodeIDs = Object.keys($placeholdersToUpdate);
  57. if (nodeIDs.length === 0) {
  58. return;
  59. }
  60. function render(results) {
  61. Object.keys(results || {}).forEach(function (nodeID) {
  62. if ($placeholdersToUpdate.hasOwnProperty(nodeID)) {
  63. $placeholdersToUpdate[nodeID].attr('href', results[nodeID].first_new_comment_link).text(Drupal.formatPlural(results[nodeID].new_comment_count, '1 new comment', '@count new comments')).removeClass('hidden');
  64. show($placeholdersToUpdate[nodeID]);
  65. }
  66. });
  67. }
  68. if (drupalSettings.comment && drupalSettings.comment.newCommentsLinks) {
  69. render(drupalSettings.comment.newCommentsLinks.node[fieldName]);
  70. } else {
  71. $.ajax({
  72. url: Drupal.url('comments/render_new_comments_node_links'),
  73. type: 'POST',
  74. data: { 'node_ids[]': nodeIDs, field_name: fieldName },
  75. dataType: 'json',
  76. success: render
  77. });
  78. }
  79. }
  80. })(jQuery, Drupal, drupalSettings);