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