2 * Attaches behaviors for the Tracker module's History module integration.
4 * May only be loaded for authenticated users, with the History module enabled.
6 (function($, Drupal, window) {
7 function processNodeNewIndicators($placeholders) {
8 const newNodeString = Drupal.t('new');
9 const updatedNodeString = Drupal.t('updated');
11 $placeholders.each((index, placeholder) => {
12 const timestamp = parseInt(
13 placeholder.getAttribute('data-history-node-timestamp'),
16 const nodeID = placeholder.getAttribute('data-history-node-id');
17 const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
19 if (timestamp > lastViewTimestamp) {
21 lastViewTimestamp === 0 ? newNodeString : updatedNodeString;
22 $(placeholder).append(`<span class="marker">${message}</span>`);
27 function processNewRepliesIndicators($placeholders) {
28 // Figure out which placeholders need the "x new" replies links.
29 const placeholdersToUpdate = {};
30 $placeholders.each((index, placeholder) => {
31 const timestamp = parseInt(
32 placeholder.getAttribute('data-history-node-last-comment-timestamp'),
35 const nodeID = placeholder.previousSibling.previousSibling.getAttribute(
36 'data-history-node-id',
38 const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
40 // Queue this placeholder's "X new" replies link to be downloaded from the
42 if (timestamp > lastViewTimestamp) {
43 placeholdersToUpdate[nodeID] = placeholder;
47 // Perform an AJAX request to retrieve node view timestamps.
48 const nodeIDs = Object.keys(placeholdersToUpdate);
49 if (nodeIDs.length === 0) {
53 url: Drupal.url('comments/render_new_comments_node_links'),
55 data: { 'node_ids[]': nodeIDs },
58 Object.keys(results || {}).forEach(nodeID => {
59 if (placeholdersToUpdate.hasOwnProperty(nodeID)) {
60 const url = results[nodeID].first_new_comment_link;
61 const text = Drupal.formatPlural(
62 results[nodeID].new_comment_count,
66 $(placeholdersToUpdate[nodeID]).append(
67 `<br /><a href="${url}">${text}</a>`,
76 * Render "new" and "updated" node indicators, as well as "X new" replies links.
78 Drupal.behaviors.trackerHistory = {
80 // Find all "new" comment indicator placeholders newer than 30 days ago that
81 // have not already been read after their last comment timestamp.
83 const $nodeNewPlaceholders = $(context)
84 .find('[data-history-node-timestamp]')
87 const nodeTimestamp = parseInt(
88 this.getAttribute('data-history-node-timestamp'),
91 const nodeID = this.getAttribute('data-history-node-id');
92 if (Drupal.history.needsServerCheck(nodeID, nodeTimestamp)) {
100 // Find all "new" comment indicator placeholders newer than 30 days ago that
101 // have not already been read after their last comment timestamp.
102 const $newRepliesPlaceholders = $(context)
103 .find('[data-history-node-last-comment-timestamp]')
106 const lastCommentTimestamp = parseInt(
107 this.getAttribute('data-history-node-last-comment-timestamp'),
110 const nodeTimestamp = parseInt(
111 this.previousSibling.previousSibling.getAttribute(
112 'data-history-node-timestamp',
116 // Discard placeholders that have zero comments.
117 if (lastCommentTimestamp === nodeTimestamp) {
120 const nodeID = this.previousSibling.previousSibling.getAttribute(
121 'data-history-node-id',
123 if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
124 if (nodeIDs.indexOf(nodeID) === -1) {
125 nodeIDs.push(nodeID);
134 $nodeNewPlaceholders.length === 0 &&
135 $newRepliesPlaceholders.length === 0
140 // Fetch the node read timestamps from the server.
141 Drupal.history.fetchTimestamps(nodeIDs, () => {
142 processNodeNewIndicators($nodeNewPlaceholders);
143 processNewRepliesIndicators($newRepliesPlaceholders);
147 })(jQuery, Drupal, window);