Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / comment / js / comment-new-indicator.es6.js
1 /**
2  * @file
3  * Attaches behaviors for the Comment module's "new" indicator.
4  *
5  * May only be loaded for authenticated users, with the History module
6  * installed.
7  */
8
9 (function($, Drupal, window) {
10   /**
11    * Processes the markup for "new comment" indicators.
12    *
13    * @param {jQuery} $placeholders
14    *   The elements that should be processed.
15    */
16   function processCommentNewIndicators($placeholders) {
17     let isFirstNewComment = true;
18     const newCommentString = Drupal.t('new');
19     let $placeholder;
20
21     $placeholders.each((index, placeholder) => {
22       $placeholder = $(placeholder);
23       const timestamp = parseInt(
24         $placeholder.attr('data-comment-timestamp'),
25         10,
26       );
27       const $node = $placeholder.closest('[data-history-node-id]');
28       const nodeID = $node.attr('data-history-node-id');
29       const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
30
31       if (timestamp > lastViewTimestamp) {
32         // Turn the placeholder into an actual "new" indicator.
33         const $comment = $(placeholder)
34           .removeClass('hidden')
35           .text(newCommentString)
36           .closest('.js-comment')
37           // Add 'new' class to the comment, so it can be styled.
38           .addClass('new');
39
40         // Insert "new" anchor just before the "comment-<cid>" anchor if
41         // this is the first new comment in the DOM.
42         if (isFirstNewComment) {
43           isFirstNewComment = false;
44           $comment.prev().before('<a id="new" />');
45           // If the URL points to the first new comment, then scroll to that
46           // comment.
47           if (window.location.hash === '#new') {
48             window.scrollTo(
49               0,
50               $comment.offset().top - Drupal.displace.offsets.top,
51             );
52           }
53         }
54       }
55     });
56   }
57
58   /**
59    * Renders "new" comment indicators wherever necessary.
60    *
61    * @type {Drupal~behavior}
62    *
63    * @prop {Drupal~behaviorAttach} attach
64    *   Attaches "new" comment indicators behavior.
65    */
66   Drupal.behaviors.commentNewIndicator = {
67     attach(context) {
68       // Collect all "new" comment indicator placeholders (and their
69       // corresponding node IDs) newer than 30 days ago that have not already
70       // been read after their last comment timestamp.
71       const nodeIDs = [];
72       const $placeholders = $(context)
73         .find('[data-comment-timestamp]')
74         .once('history')
75         .filter(function() {
76           const $placeholder = $(this);
77           const commentTimestamp = parseInt(
78             $placeholder.attr('data-comment-timestamp'),
79             10,
80           );
81           const nodeID = $placeholder
82             .closest('[data-history-node-id]')
83             .attr('data-history-node-id');
84           if (Drupal.history.needsServerCheck(nodeID, commentTimestamp)) {
85             nodeIDs.push(nodeID);
86             return true;
87           }
88
89           return false;
90         });
91
92       if ($placeholders.length === 0) {
93         return;
94       }
95
96       // Fetch the node read timestamps from the server.
97       Drupal.history.fetchTimestamps(nodeIDs, () => {
98         processCommentNewIndicators($placeholders);
99       });
100     },
101   };
102 })(jQuery, Drupal, window);