annotate core/modules/comment/js/comment-new-indicator.es6.js @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
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@4 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@4 23 const timestamp = parseInt(
Chris@4 24 $placeholder.attr('data-comment-timestamp'),
Chris@4 25 10,
Chris@4 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@4 48 window.scrollTo(
Chris@4 49 0,
Chris@4 50 $comment.offset().top - Drupal.displace.offsets.top,
Chris@4 51 );
Chris@0 52 }
Chris@0 53 }
Chris@0 54 }
Chris@0 55 });
Chris@0 56 }
Chris@4 57
Chris@4 58 /**
Chris@4 59 * Renders "new" comment indicators wherever necessary.
Chris@4 60 *
Chris@4 61 * @type {Drupal~behavior}
Chris@4 62 *
Chris@4 63 * @prop {Drupal~behaviorAttach} attach
Chris@4 64 * Attaches "new" comment indicators behavior.
Chris@4 65 */
Chris@4 66 Drupal.behaviors.commentNewIndicator = {
Chris@4 67 attach(context) {
Chris@4 68 // Collect all "new" comment indicator placeholders (and their
Chris@4 69 // corresponding node IDs) newer than 30 days ago that have not already
Chris@4 70 // been read after their last comment timestamp.
Chris@4 71 const nodeIDs = [];
Chris@4 72 const $placeholders = $(context)
Chris@4 73 .find('[data-comment-timestamp]')
Chris@4 74 .once('history')
Chris@4 75 .filter(function() {
Chris@4 76 const $placeholder = $(this);
Chris@4 77 const commentTimestamp = parseInt(
Chris@4 78 $placeholder.attr('data-comment-timestamp'),
Chris@4 79 10,
Chris@4 80 );
Chris@4 81 const nodeID = $placeholder
Chris@4 82 .closest('[data-history-node-id]')
Chris@4 83 .attr('data-history-node-id');
Chris@4 84 if (Drupal.history.needsServerCheck(nodeID, commentTimestamp)) {
Chris@4 85 nodeIDs.push(nodeID);
Chris@4 86 return true;
Chris@4 87 }
Chris@4 88
Chris@4 89 return false;
Chris@4 90 });
Chris@4 91
Chris@4 92 if ($placeholders.length === 0) {
Chris@4 93 return;
Chris@4 94 }
Chris@4 95
Chris@4 96 // Fetch the node read timestamps from the server.
Chris@4 97 Drupal.history.fetchTimestamps(nodeIDs, () => {
Chris@4 98 processCommentNewIndicators($placeholders);
Chris@4 99 });
Chris@4 100 },
Chris@4 101 };
Chris@4 102 })(jQuery, Drupal, window);