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