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) {
11 * Render "new" and "updated" node indicators, as well as "X new" replies links.
13 Drupal.behaviors.trackerHistory = {
14 attach: function (context) {
15 // Find all "new" comment indicator placeholders newer than 30 days ago that
16 // have not already been read after their last comment timestamp.
18 var $nodeNewPlaceholders = $(context)
19 .find('[data-history-node-timestamp]')
22 var nodeTimestamp = parseInt(this.getAttribute('data-history-node-timestamp'), 10);
23 var nodeID = this.getAttribute('data-history-node-id');
24 if (Drupal.history.needsServerCheck(nodeID, nodeTimestamp)) {
33 // Find all "new" comment indicator placeholders newer than 30 days ago that
34 // have not already been read after their last comment timestamp.
35 var $newRepliesPlaceholders = $(context)
36 .find('[data-history-node-last-comment-timestamp]')
39 var lastCommentTimestamp = parseInt(this.getAttribute('data-history-node-last-comment-timestamp'), 10);
40 var nodeTimestamp = parseInt(this.previousSibling.previousSibling.getAttribute('data-history-node-timestamp'), 10);
41 // Discard placeholders that have zero comments.
42 if (lastCommentTimestamp === nodeTimestamp) {
45 var nodeID = this.previousSibling.previousSibling.getAttribute('data-history-node-id');
46 if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
47 if (nodeIDs.indexOf(nodeID) === -1) {
57 if ($nodeNewPlaceholders.length === 0 && $newRepliesPlaceholders.length === 0) {
61 // Fetch the node read timestamps from the server.
62 Drupal.history.fetchTimestamps(nodeIDs, function () {
63 processNodeNewIndicators($nodeNewPlaceholders);
64 processNewRepliesIndicators($newRepliesPlaceholders);
69 function processNodeNewIndicators($placeholders) {
70 var newNodeString = Drupal.t('new');
71 var updatedNodeString = Drupal.t('updated');
73 $placeholders.each(function (index, placeholder) {
74 var timestamp = parseInt(placeholder.getAttribute('data-history-node-timestamp'), 10);
75 var nodeID = placeholder.getAttribute('data-history-node-id');
76 var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
78 if (timestamp > lastViewTimestamp) {
79 var message = (lastViewTimestamp === 0) ? newNodeString : updatedNodeString;
80 $(placeholder).append('<span class="marker">' + message + '</span>');
85 function processNewRepliesIndicators($placeholders) {
86 // Figure out which placeholders need the "x new" replies links.
87 var placeholdersToUpdate = {};
88 $placeholders.each(function (index, placeholder) {
89 var timestamp = parseInt(placeholder.getAttribute('data-history-node-last-comment-timestamp'), 10);
90 var nodeID = placeholder.previousSibling.previousSibling.getAttribute('data-history-node-id');
91 var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
93 // Queue this placeholder's "X new" replies link to be downloaded from the
95 if (timestamp > lastViewTimestamp) {
96 placeholdersToUpdate[nodeID] = placeholder;
100 // Perform an AJAX request to retrieve node view timestamps.
101 var nodeIDs = Object.keys(placeholdersToUpdate);
102 if (nodeIDs.length === 0) {
106 url: Drupal.url('comments/render_new_comments_node_links'),
108 data: {'node_ids[]': nodeIDs},
110 success: function (results) {
111 for (var nodeID in results) {
112 if (results.hasOwnProperty(nodeID) && placeholdersToUpdate.hasOwnProperty(nodeID)) {
113 var url = results[nodeID].first_new_comment_link;
114 var text = Drupal.formatPlural(results[nodeID].new_comment_count, '1 new', '@count new');
115 $(placeholdersToUpdate[nodeID]).append('<br /><a href="' + url + '">' + text + '</a>');
122 })(jQuery, Drupal, window);