comment-new-indicator.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, window) {
  8. function processCommentNewIndicators($placeholders) {
  9. var isFirstNewComment = true;
  10. var newCommentString = Drupal.t('new');
  11. var $placeholder = void 0;
  12. $placeholders.each(function (index, placeholder) {
  13. $placeholder = $(placeholder);
  14. var timestamp = parseInt($placeholder.attr('data-comment-timestamp'), 10);
  15. var $node = $placeholder.closest('[data-history-node-id]');
  16. var nodeID = $node.attr('data-history-node-id');
  17. var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
  18. if (timestamp > lastViewTimestamp) {
  19. var $comment = $(placeholder).removeClass('hidden').text(newCommentString).closest('.js-comment').addClass('new');
  20. if (isFirstNewComment) {
  21. isFirstNewComment = false;
  22. $comment.prev().before('<a id="new"></a>');
  23. if (window.location.hash === '#new') {
  24. window.scrollTo(0, $comment.offset().top - Drupal.displace.offsets.top);
  25. }
  26. }
  27. }
  28. });
  29. }
  30. Drupal.behaviors.commentNewIndicator = {
  31. attach: function attach(context) {
  32. var nodeIDs = [];
  33. var $placeholders = $(context).find('[data-comment-timestamp]').once('history').filter(function () {
  34. var $placeholder = $(this);
  35. var commentTimestamp = parseInt($placeholder.attr('data-comment-timestamp'), 10);
  36. var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
  37. if (Drupal.history.needsServerCheck(nodeID, commentTimestamp)) {
  38. nodeIDs.push(nodeID);
  39. return true;
  40. }
  41. return false;
  42. });
  43. if ($placeholders.length === 0) {
  44. return;
  45. }
  46. Drupal.history.fetchTimestamps(nodeIDs, function () {
  47. processCommentNewIndicators($placeholders);
  48. });
  49. }
  50. };
  51. })(jQuery, Drupal, window);