2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function ($, Drupal, drupalSettings) {
9 Drupal.behaviors.nodeNewCommentsLink = {
10 attach: function attach(context) {
12 var $placeholders = $(context).find('[data-history-node-last-comment-timestamp]').once('history').filter(function () {
13 var $placeholder = $(this);
14 var lastCommentTimestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
15 var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
16 if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
27 if ($placeholders.length === 0) {
31 Drupal.history.fetchTimestamps(nodeIDs, function () {
32 processNodeNewCommentLinks($placeholders);
37 function hide($placeholder) {
38 return $placeholder.closest('.comment-new-comments').prev().addClass('last').end().hide();
41 function remove($placeholder) {
42 hide($placeholder).remove();
45 function show($placeholder) {
46 return $placeholder.closest('.comment-new-comments').prev().removeClass('last').end().show();
49 function processNodeNewCommentLinks($placeholders) {
50 var $placeholdersToUpdate = {};
51 var fieldName = 'comment';
52 var $placeholder = void 0;
53 $placeholders.each(function (index, placeholder) {
54 $placeholder = $(placeholder);
55 var timestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
56 fieldName = $placeholder.attr('data-history-node-field-name');
57 var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
58 var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
60 if (timestamp > lastViewTimestamp) {
61 $placeholdersToUpdate[nodeID] = $placeholder;
67 var nodeIDs = Object.keys($placeholdersToUpdate);
68 if (nodeIDs.length === 0) {
72 function render(results) {
73 for (var nodeID in results) {
74 if (results.hasOwnProperty(nodeID) && $placeholdersToUpdate.hasOwnProperty(nodeID)) {
75 $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');
76 show($placeholdersToUpdate[nodeID]);
81 if (drupalSettings.comment && drupalSettings.comment.newCommentsLinks) {
82 render(drupalSettings.comment.newCommentsLinks.node[fieldName]);
85 url: Drupal.url('comments/render_new_comments_node_links'),
87 data: { 'node_ids[]': nodeIDs, field_name: fieldName },
93 })(jQuery, Drupal, drupalSettings);