annotate core/modules/comment/js/node-new-comments-link.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 /**
Chris@0 2 * DO NOT EDIT THIS FILE.
Chris@0 3 * See the following change record for more information,
Chris@0 4 * https://www.drupal.org/node/2815083
Chris@0 5 * @preserve
Chris@0 6 **/
Chris@0 7
Chris@0 8 (function ($, Drupal, drupalSettings) {
Chris@0 9 function hide($placeholder) {
Chris@0 10 return $placeholder.closest('.comment-new-comments').prev().addClass('last').end().hide();
Chris@0 11 }
Chris@0 12
Chris@0 13 function remove($placeholder) {
Chris@0 14 hide($placeholder).remove();
Chris@0 15 }
Chris@0 16
Chris@0 17 function show($placeholder) {
Chris@0 18 return $placeholder.closest('.comment-new-comments').prev().removeClass('last').end().show();
Chris@0 19 }
Chris@0 20
Chris@0 21 function processNodeNewCommentLinks($placeholders) {
Chris@0 22 var $placeholdersToUpdate = {};
Chris@0 23 var fieldName = 'comment';
Chris@0 24 var $placeholder = void 0;
Chris@0 25 $placeholders.each(function (index, placeholder) {
Chris@0 26 $placeholder = $(placeholder);
Chris@0 27 var timestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
Chris@0 28 fieldName = $placeholder.attr('data-history-node-field-name');
Chris@0 29 var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
Chris@0 30 var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
Chris@0 31
Chris@0 32 if (timestamp > lastViewTimestamp) {
Chris@0 33 $placeholdersToUpdate[nodeID] = $placeholder;
Chris@0 34 } else {
Chris@0 35 remove($placeholder);
Chris@0 36 }
Chris@0 37 });
Chris@0 38
Chris@0 39 var nodeIDs = Object.keys($placeholdersToUpdate);
Chris@0 40 if (nodeIDs.length === 0) {
Chris@0 41 return;
Chris@0 42 }
Chris@0 43
Chris@0 44 function render(results) {
Chris@14 45 Object.keys(results || {}).forEach(function (nodeID) {
Chris@14 46 if ($placeholdersToUpdate.hasOwnProperty(nodeID)) {
Chris@0 47 $placeholdersToUpdate[nodeID].attr('href', results[nodeID].first_new_comment_link).text(Drupal.formatPlural(results[nodeID].new_comment_count, '1 new comment', '@count new comments')).removeClass('hidden');
Chris@0 48 show($placeholdersToUpdate[nodeID]);
Chris@0 49 }
Chris@14 50 });
Chris@0 51 }
Chris@0 52
Chris@0 53 if (drupalSettings.comment && drupalSettings.comment.newCommentsLinks) {
Chris@0 54 render(drupalSettings.comment.newCommentsLinks.node[fieldName]);
Chris@0 55 } else {
Chris@0 56 $.ajax({
Chris@0 57 url: Drupal.url('comments/render_new_comments_node_links'),
Chris@0 58 type: 'POST',
Chris@0 59 data: { 'node_ids[]': nodeIDs, field_name: fieldName },
Chris@0 60 dataType: 'json',
Chris@0 61 success: render
Chris@0 62 });
Chris@0 63 }
Chris@0 64 }
Chris@17 65
Chris@17 66 Drupal.behaviors.nodeNewCommentsLink = {
Chris@17 67 attach: function attach(context) {
Chris@17 68 var nodeIDs = [];
Chris@17 69 var $placeholders = $(context).find('[data-history-node-last-comment-timestamp]').once('history').filter(function () {
Chris@17 70 var $placeholder = $(this);
Chris@17 71 var lastCommentTimestamp = parseInt($placeholder.attr('data-history-node-last-comment-timestamp'), 10);
Chris@17 72 var nodeID = $placeholder.closest('[data-history-node-id]').attr('data-history-node-id');
Chris@17 73 if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
Chris@17 74 nodeIDs.push(nodeID);
Chris@17 75
Chris@17 76 hide($placeholder);
Chris@17 77 return true;
Chris@17 78 }
Chris@17 79
Chris@17 80 remove($placeholder);
Chris@17 81 return false;
Chris@17 82 });
Chris@17 83
Chris@17 84 if ($placeholders.length === 0) {
Chris@17 85 return;
Chris@17 86 }
Chris@17 87
Chris@17 88 Drupal.history.fetchTimestamps(nodeIDs, function () {
Chris@17 89 processNodeNewCommentLinks($placeholders);
Chris@17 90 });
Chris@17 91 }
Chris@17 92 };
Chris@0 93 })(jQuery, Drupal, drupalSettings);