877a9caec71b95ddbd342d0a4733383004cabe5a
[yaffs-website] / web / core / modules / tracker / js / tracker-history.js
1 /**
2  * Attaches behaviors for the Tracker module's History module integration.
3  *
4  * May only be loaded for authenticated users, with the History module enabled.
5  */
6 (function ($, Drupal, window) {
7
8   'use strict';
9
10   /**
11    * Render "new" and "updated" node indicators, as well as "X new" replies links.
12    */
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.
17       var nodeIDs = [];
18       var $nodeNewPlaceholders = $(context)
19         .find('[data-history-node-timestamp]')
20         .once('history')
21         .filter(function () {
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)) {
25             nodeIDs.push(nodeID);
26             return true;
27           }
28           else {
29             return false;
30           }
31         });
32
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]')
37         .once('history')
38         .filter(function () {
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) {
43             return false;
44           }
45           var nodeID = this.previousSibling.previousSibling.getAttribute('data-history-node-id');
46           if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
47             if (nodeIDs.indexOf(nodeID) === -1) {
48               nodeIDs.push(nodeID);
49             }
50             return true;
51           }
52           else {
53             return false;
54           }
55         });
56
57       if ($nodeNewPlaceholders.length === 0 && $newRepliesPlaceholders.length === 0) {
58         return;
59       }
60
61       // Fetch the node read timestamps from the server.
62       Drupal.history.fetchTimestamps(nodeIDs, function () {
63         processNodeNewIndicators($nodeNewPlaceholders);
64         processNewRepliesIndicators($newRepliesPlaceholders);
65       });
66     }
67   };
68
69   function processNodeNewIndicators($placeholders) {
70     var newNodeString = Drupal.t('new');
71     var updatedNodeString = Drupal.t('updated');
72
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);
77
78       if (timestamp > lastViewTimestamp) {
79         var message = (lastViewTimestamp === 0) ? newNodeString : updatedNodeString;
80         $(placeholder).append('<span class="marker">' + message + '</span>');
81       }
82     });
83   }
84
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);
92
93       // Queue this placeholder's "X new" replies link to be downloaded from the
94       // server.
95       if (timestamp > lastViewTimestamp) {
96         placeholdersToUpdate[nodeID] = placeholder;
97       }
98     });
99
100     // Perform an AJAX request to retrieve node view timestamps.
101     var nodeIDs = Object.keys(placeholdersToUpdate);
102     if (nodeIDs.length === 0) {
103       return;
104     }
105     $.ajax({
106       url: Drupal.url('comments/render_new_comments_node_links'),
107       type: 'POST',
108       data: {'node_ids[]': nodeIDs},
109       dataType: 'json',
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>');
116           }
117         }
118       }
119     });
120   }
121
122 })(jQuery, Drupal, window);