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