Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / comment / js / comment-new-indicator.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.commentNewIndicator = {
10     attach: function attach(context) {
11       var nodeIDs = [];
12       var $placeholders = $(context).find('[data-comment-timestamp]').once('history').filter(function () {
13         var $placeholder = $(this);
14         var commentTimestamp = parseInt($placeholder.attr('data-comment-timestamp'), 10);
15         var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
16         if (Drupal.history.needsServerCheck(nodeID, commentTimestamp)) {
17           nodeIDs.push(nodeID);
18           return true;
19         }
20
21         return false;
22       });
23
24       if ($placeholders.length === 0) {
25         return;
26       }
27
28       Drupal.history.fetchTimestamps(nodeIDs, function () {
29         processCommentNewIndicators($placeholders);
30       });
31     }
32   };
33
34   function processCommentNewIndicators($placeholders) {
35     var isFirstNewComment = true;
36     var newCommentString = Drupal.t('new');
37     var $placeholder = void 0;
38
39     $placeholders.each(function (index, placeholder) {
40       $placeholder = $(placeholder);
41       var timestamp = parseInt($placeholder.attr('data-comment-timestamp'), 10);
42       var $node = $placeholder.closest('[data-history-node-id]');
43       var nodeID = $node.attr('data-history-node-id');
44       var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
45
46       if (timestamp > lastViewTimestamp) {
47         var $comment = $(placeholder).removeClass('hidden').text(newCommentString).closest('.js-comment').addClass('new');
48
49         if (isFirstNewComment) {
50           isFirstNewComment = false;
51           $comment.prev().before('<a id="new" />');
52
53           if (window.location.hash === '#new') {
54             window.scrollTo(0, $comment.offset().top - Drupal.displace.offsets.top);
55           }
56         }
57       }
58     });
59   }
60 })(jQuery, Drupal, window);