a147eb8a63641fbce5bd37c85da221a9108a37c4
[yaffs-website] / web / core / modules / tracker / js / tracker-history.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, window) {
9   Drupal.behaviors.trackerHistory = {
10     attach: function attach(context) {
11       var nodeIDs = [];
12       var $nodeNewPlaceholders = $(context).find('[data-history-node-timestamp]').once('history').filter(function () {
13         var nodeTimestamp = parseInt(this.getAttribute('data-history-node-timestamp'), 10);
14         var nodeID = this.getAttribute('data-history-node-id');
15         if (Drupal.history.needsServerCheck(nodeID, nodeTimestamp)) {
16           nodeIDs.push(nodeID);
17           return true;
18         }
19
20         return false;
21       });
22
23       var $newRepliesPlaceholders = $(context).find('[data-history-node-last-comment-timestamp]').once('history').filter(function () {
24         var lastCommentTimestamp = parseInt(this.getAttribute('data-history-node-last-comment-timestamp'), 10);
25         var nodeTimestamp = parseInt(this.previousSibling.previousSibling.getAttribute('data-history-node-timestamp'), 10);
26
27         if (lastCommentTimestamp === nodeTimestamp) {
28           return false;
29         }
30         var nodeID = this.previousSibling.previousSibling.getAttribute('data-history-node-id');
31         if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
32           if (nodeIDs.indexOf(nodeID) === -1) {
33             nodeIDs.push(nodeID);
34           }
35           return true;
36         }
37
38         return false;
39       });
40
41       if ($nodeNewPlaceholders.length === 0 && $newRepliesPlaceholders.length === 0) {
42         return;
43       }
44
45       Drupal.history.fetchTimestamps(nodeIDs, function () {
46         processNodeNewIndicators($nodeNewPlaceholders);
47         processNewRepliesIndicators($newRepliesPlaceholders);
48       });
49     }
50   };
51
52   function processNodeNewIndicators($placeholders) {
53     var newNodeString = Drupal.t('new');
54     var updatedNodeString = Drupal.t('updated');
55
56     $placeholders.each(function (index, placeholder) {
57       var timestamp = parseInt(placeholder.getAttribute('data-history-node-timestamp'), 10);
58       var nodeID = placeholder.getAttribute('data-history-node-id');
59       var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
60
61       if (timestamp > lastViewTimestamp) {
62         var message = lastViewTimestamp === 0 ? newNodeString : updatedNodeString;
63         $(placeholder).append('<span class="marker">' + message + '</span>');
64       }
65     });
66   }
67
68   function processNewRepliesIndicators($placeholders) {
69     var placeholdersToUpdate = {};
70     $placeholders.each(function (index, placeholder) {
71       var timestamp = parseInt(placeholder.getAttribute('data-history-node-last-comment-timestamp'), 10);
72       var nodeID = placeholder.previousSibling.previousSibling.getAttribute('data-history-node-id');
73       var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
74
75       if (timestamp > lastViewTimestamp) {
76         placeholdersToUpdate[nodeID] = placeholder;
77       }
78     });
79
80     var nodeIDs = Object.keys(placeholdersToUpdate);
81     if (nodeIDs.length === 0) {
82       return;
83     }
84     $.ajax({
85       url: Drupal.url('comments/render_new_comments_node_links'),
86       type: 'POST',
87       data: { 'node_ids[]': nodeIDs },
88       dataType: 'json',
89       success: function success(results) {
90         Object.keys(results || {}).forEach(function (nodeID) {
91           if (placeholdersToUpdate.hasOwnProperty(nodeID)) {
92             var url = results[nodeID].first_new_comment_link;
93             var text = Drupal.formatPlural(results[nodeID].new_comment_count, '1 new', '@count new');
94             $(placeholdersToUpdate[nodeID]).append('<br /><a href="' + url + '">' + text + '</a>');
95           }
96         });
97       }
98     });
99   }
100 })(jQuery, Drupal, window);