+(function($, Drupal, window) {
+ function processNodeNewIndicators($placeholders) {
+ const newNodeString = Drupal.t('new');
+ const updatedNodeString = Drupal.t('updated');
+
+ $placeholders.each((index, placeholder) => {
+ const timestamp = parseInt(
+ placeholder.getAttribute('data-history-node-timestamp'),
+ 10,
+ );
+ const nodeID = placeholder.getAttribute('data-history-node-id');
+ const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
+
+ if (timestamp > lastViewTimestamp) {
+ const message =
+ lastViewTimestamp === 0 ? newNodeString : updatedNodeString;
+ $(placeholder).append(`<span class="marker">${message}</span>`);
+ }
+ });
+ }
+
+ function processNewRepliesIndicators($placeholders) {
+ // Figure out which placeholders need the "x new" replies links.
+ const placeholdersToUpdate = {};
+ $placeholders.each((index, placeholder) => {
+ const timestamp = parseInt(
+ placeholder.getAttribute('data-history-node-last-comment-timestamp'),
+ 10,
+ );
+ const nodeID = placeholder.previousSibling.previousSibling.getAttribute(
+ 'data-history-node-id',
+ );
+ const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
+
+ // Queue this placeholder's "X new" replies link to be downloaded from the
+ // server.
+ if (timestamp > lastViewTimestamp) {
+ placeholdersToUpdate[nodeID] = placeholder;
+ }
+ });
+
+ // Perform an AJAX request to retrieve node view timestamps.
+ const nodeIDs = Object.keys(placeholdersToUpdate);
+ if (nodeIDs.length === 0) {
+ return;
+ }
+ $.ajax({
+ url: Drupal.url('comments/render_new_comments_node_links'),
+ type: 'POST',
+ data: { 'node_ids[]': nodeIDs },
+ dataType: 'json',
+ success(results) {
+ Object.keys(results || {}).forEach(nodeID => {
+ if (placeholdersToUpdate.hasOwnProperty(nodeID)) {
+ const url = results[nodeID].first_new_comment_link;
+ const text = Drupal.formatPlural(
+ results[nodeID].new_comment_count,
+ '1 new',
+ '@count new',
+ );
+ $(placeholdersToUpdate[nodeID]).append(
+ `<br /><a href="${url}">${text}</a>`,
+ );
+ }
+ });
+ },
+ });
+ }
+